Browse Source

Merge pull request #320 from herby/async-test-refactor-readable

`RunningTestContext >> execute:` refactored for readability.
Nicolas Petton 11 năm trước cách đây
mục cha
commit
c43b4fa360
3 tập tin đã thay đổi với 127 bổ sung94 xóa
  1. 49 37
      js/SUnit.deploy.js
  2. 60 43
      js/SUnit.js
  3. 18 14
      st/SUnit.st

+ 49 - 37
js/SUnit.deploy.js

@@ -24,24 +24,14 @@ smalltalk.ResultAnnouncement);
 
 
 
-smalltalk.addClass('RunningTestContext', smalltalk.Object, ['finished', 'testCase', 'result', 'step'], 'SUnit');
+smalltalk.addClass('RunningTestContext', smalltalk.Object, ['finished', 'testCase', 'result'], 'SUnit');
 smalltalk.addMethod(
-"_exception_ifNotAsync_",
+"_exception_do_",
 smalltalk.method({
-selector: "exception:ifNotAsync:",
+selector: "exception:do:",
 fn: function (anException,aBlock){
 var self=this;
-var $1;
-$1=smalltalk.send(self["@testCase"],"_isAsync",[]);
-if(smalltalk.assert($1)){
-self["@step"]=(function(){
-smalltalk.send(self["@testCase"],"_finished",[]);
-return smalltalk.send(anException,"_signal",[]);
-});
-self["@step"];
-} else {
 smalltalk.send(aBlock,"_value",[]);
-};
 return self}
 }),
 smalltalk.RunningTestContext);
@@ -52,42 +42,64 @@ smalltalk.method({
 selector: "execute:",
 fn: function (aBlock){
 var self=this;
-var $1,$2;
-self["@step"]=aBlock;
-smalltalk.send((function(){
-return smalltalk.send(self["@step"],"_isNil",[]);
-}),"_whileFalse_",[(function(){
-smalltalk.send(self["@testCase"],"_context_",[self]);
-return smalltalk.send((function(){
+var $1,$3,$2;
+$1=(function(){
 return smalltalk.send((function(){
 return smalltalk.send((function(){
-return smalltalk.send(self["@step"],"_ensure_",[(function(){
-smalltalk.send(self["@testCase"],"_context_",[nil]);
-self["@step"]=nil;
-self["@step"];
-$1=smalltalk.send(self["@testCase"],"_isAsync",[]);
-if(! smalltalk.assert($1)){
-return smalltalk.send(self["@testCase"],"_tearDown",[]);
-};
-})]);
+return smalltalk.send(self,"_executeWithCleanup_",[aBlock]);
 }),"_on_do_",[(smalltalk.TestFailure || TestFailure),(function(ex){
-return smalltalk.send(self,"_exception_ifNotAsync_",[ex,(function(){
+return smalltalk.send(self,"_exception_do_",[ex,(function(){
 return smalltalk.send(self["@result"],"_addFailure_",[self["@testCase"]]);
 })]);
 })]);
 }),"_on_do_",[(smalltalk.Error || Error),(function(ex){
-return smalltalk.send(self,"_exception_ifNotAsync_",[ex,(function(){
+return smalltalk.send(self,"_exception_do_",[ex,(function(){
 return smalltalk.send(self["@result"],"_addError_",[self["@testCase"]]);
 })]);
 })]);
-}),"_ensure_",[(function(){
-$2=smalltalk.send(self["@testCase"],"_isAsync",[]);
-if(! smalltalk.assert($2)){
+});
+$2=(function(){
+$3=smalltalk.send(self["@testCase"],"_isAsync",[]);
+if(! smalltalk.assert($3)){
 smalltalk.send(self["@result"],"_increaseRuns",[]);
 return smalltalk.send(self["@finished"],"_value",[]);
 };
+});
+smalltalk.send($1,"_ensure_",[$2]);
+return self}
+}),
+smalltalk.RunningTestContext);
+
+smalltalk.addMethod(
+"_executeWithCleanup_",
+smalltalk.method({
+selector: "executeWithCleanup:",
+fn: function (aBlock){
+var self=this;
+var $1,$3,$4,$2;
+var failed;
+smalltalk.send(self["@testCase"],"_context_",[self]);
+$1=(function(){
+failed=true;
+failed;
+smalltalk.send(aBlock,"_value",[]);
+failed=false;
+return failed;
+});
+$2=(function(){
+smalltalk.send(self["@testCase"],"_context_",[nil]);
+$3=smalltalk.send(failed,"_and_",[(function(){
+return smalltalk.send(self["@testCase"],"_isAsync",[]);
 })]);
-})]);
+if(smalltalk.assert($3)){
+smalltalk.send(self["@testCase"],"_finished",[]);
+};
+$4=smalltalk.send(self["@testCase"],"_isAsync",[]);
+if(! smalltalk.assert($4)){
+return smalltalk.send(self["@testCase"],"_tearDown",[]);
+};
+});
+smalltalk.send($1,"_ensure_",[$2]);
 return self}
 }),
 smalltalk.RunningTestContext);
@@ -161,9 +173,9 @@ smalltalk.RunningTestContext.klass);
 
 smalltalk.addClass('ErroringTestContext', smalltalk.RunningTestContext, [], 'SUnit');
 smalltalk.addMethod(
-"_exception_ifNotAsync_",
+"_exception_do_",
 smalltalk.method({
-selector: "exception:ifNotAsync:",
+selector: "exception:do:",
 fn: function (anException,aBlock){
 var self=this;
 smalltalk.send(anException,"_signal",[]);

+ 60 - 43
js/SUnit.js

@@ -34,29 +34,19 @@ smalltalk.ResultAnnouncement);
 
 
 
-smalltalk.addClass('RunningTestContext', smalltalk.Object, ['finished', 'testCase', 'result', 'step'], 'SUnit');
+smalltalk.addClass('RunningTestContext', smalltalk.Object, ['finished', 'testCase', 'result'], 'SUnit');
 smalltalk.addMethod(
-"_exception_ifNotAsync_",
+"_exception_do_",
 smalltalk.method({
-selector: "exception:ifNotAsync:",
+selector: "exception:do:",
 category: 'private',
 fn: function (anException,aBlock){
 var self=this;
-var $1;
-$1=smalltalk.send(self["@testCase"],"_isAsync",[]);
-if(smalltalk.assert($1)){
-self["@step"]=(function(){
-smalltalk.send(self["@testCase"],"_finished",[]);
-return smalltalk.send(anException,"_signal",[]);
-});
-self["@step"];
-} else {
 smalltalk.send(aBlock,"_value",[]);
-};
 return self},
 args: ["anException", "aBlock"],
-source: "exception: anException ifNotAsync: aBlock\x0a\x09testCase isAsync\x0a\x09\x09ifTrue: [ step := [ testCase finished. anException signal ]]\x0a\x09\x09ifFalse: [ aBlock value ]\x0a",
-messageSends: ["ifTrue:ifFalse:", "finished", "signal", "value", "isAsync"],
+source: "exception: anException do: aBlock\x0a\x09aBlock value\x0a",
+messageSends: ["value"],
 referencedClasses: []
 }),
 smalltalk.RunningTestContext);
@@ -68,50 +58,77 @@ selector: "execute:",
 category: 'running',
 fn: function (aBlock){
 var self=this;
-var $1,$2;
-self["@step"]=aBlock;
-smalltalk.send((function(){
-return smalltalk.send(self["@step"],"_isNil",[]);
-}),"_whileFalse_",[(function(){
-smalltalk.send(self["@testCase"],"_context_",[self]);
-return smalltalk.send((function(){
+var $1,$3,$2;
+$1=(function(){
 return smalltalk.send((function(){
 return smalltalk.send((function(){
-return smalltalk.send(self["@step"],"_ensure_",[(function(){
-smalltalk.send(self["@testCase"],"_context_",[nil]);
-self["@step"]=nil;
-self["@step"];
-$1=smalltalk.send(self["@testCase"],"_isAsync",[]);
-if(! smalltalk.assert($1)){
-return smalltalk.send(self["@testCase"],"_tearDown",[]);
-};
-})]);
+return smalltalk.send(self,"_executeWithCleanup_",[aBlock]);
 }),"_on_do_",[(smalltalk.TestFailure || TestFailure),(function(ex){
-return smalltalk.send(self,"_exception_ifNotAsync_",[ex,(function(){
+return smalltalk.send(self,"_exception_do_",[ex,(function(){
 return smalltalk.send(self["@result"],"_addFailure_",[self["@testCase"]]);
 })]);
 })]);
 }),"_on_do_",[(smalltalk.Error || Error),(function(ex){
-return smalltalk.send(self,"_exception_ifNotAsync_",[ex,(function(){
+return smalltalk.send(self,"_exception_do_",[ex,(function(){
 return smalltalk.send(self["@result"],"_addError_",[self["@testCase"]]);
 })]);
 })]);
-}),"_ensure_",[(function(){
-$2=smalltalk.send(self["@testCase"],"_isAsync",[]);
-if(! smalltalk.assert($2)){
+});
+$2=(function(){
+$3=smalltalk.send(self["@testCase"],"_isAsync",[]);
+if(! smalltalk.assert($3)){
 smalltalk.send(self["@result"],"_increaseRuns",[]);
 return smalltalk.send(self["@finished"],"_value",[]);
 };
-})]);
-})]);
+});
+smalltalk.send($1,"_ensure_",[$2]);
 return self},
 args: ["aBlock"],
-source: "execute: aBlock\x0a    step := aBlock.\x0a\x09[ step isNil ] whileFalse: [\x0a\x09    testCase context: self.\x0a\x09\x09[[[\x09step\x0a        \x09\x09ensure: [ testCase context: nil. step := nil. testCase isAsync ifFalse: [ testCase tearDown ]]]\x0a\x09\x09\x09\x09on: TestFailure do: [:ex | self exception: ex ifNotAsync: [ result addFailure: testCase]]]\x0a\x09\x09\x09\x09on: Error do: [:ex | self exception: ex ifNotAsync: [ result addError: testCase]]]\x0a\x09\x09\x09\x09ensure: [ testCase isAsync ifFalse: [ result increaseRuns. finished value ]]]",
-messageSends: ["whileFalse:", "context:", "ensure:", "ifFalse:", "increaseRuns", "value", "isAsync", "on:do:", "exception:ifNotAsync:", "addError:", "addFailure:", "tearDown", "isNil"],
+source: "execute: aBlock\x0a    [[[ self executeWithCleanup: aBlock ]\x0a    on: TestFailure do: [:ex | self exception: ex do: [ result addFailure: testCase]]]\x0a    on: Error do: [:ex | self exception: ex do: [ result addError: testCase]]]\x0a    ensure: [ testCase isAsync ifFalse: [ result increaseRuns. finished value ]]",
+messageSends: ["ensure:", "ifFalse:", "increaseRuns", "value", "isAsync", "on:do:", "exception:do:", "addError:", "addFailure:", "executeWithCleanup:"],
 referencedClasses: ["Error", "TestFailure"]
 }),
 smalltalk.RunningTestContext);
 
+smalltalk.addMethod(
+"_executeWithCleanup_",
+smalltalk.method({
+selector: "executeWithCleanup:",
+category: 'private',
+fn: function (aBlock){
+var self=this;
+var $1,$3,$4,$2;
+var failed;
+smalltalk.send(self["@testCase"],"_context_",[self]);
+$1=(function(){
+failed=true;
+failed;
+smalltalk.send(aBlock,"_value",[]);
+failed=false;
+return failed;
+});
+$2=(function(){
+smalltalk.send(self["@testCase"],"_context_",[nil]);
+$3=smalltalk.send(failed,"_and_",[(function(){
+return smalltalk.send(self["@testCase"],"_isAsync",[]);
+})]);
+if(smalltalk.assert($3)){
+smalltalk.send(self["@testCase"],"_finished",[]);
+};
+$4=smalltalk.send(self["@testCase"],"_isAsync",[]);
+if(! smalltalk.assert($4)){
+return smalltalk.send(self["@testCase"],"_tearDown",[]);
+};
+});
+smalltalk.send($1,"_ensure_",[$2]);
+return self},
+args: ["aBlock"],
+source: "executeWithCleanup: aBlock\x0a\x09| failed |\x0a    testCase context: self.\x0a    [ failed := true. aBlock value. failed := false ] ensure: [\x0a        testCase context: nil.\x0a        (failed and: [testCase isAsync]) ifTrue: [ testCase finished ].\x0a        testCase isAsync ifFalse: [ testCase tearDown ]\x0a    ]\x0a",
+messageSends: ["context:", "ensure:", "ifTrue:", "finished", "and:", "isAsync", "ifFalse:", "tearDown", "value"],
+referencedClasses: []
+}),
+smalltalk.RunningTestContext);
+
 smalltalk.addMethod(
 "_finished_",
 smalltalk.method({
@@ -206,16 +223,16 @@ smalltalk.RunningTestContext.klass);
 
 smalltalk.addClass('ErroringTestContext', smalltalk.RunningTestContext, [], 'SUnit');
 smalltalk.addMethod(
-"_exception_ifNotAsync_",
+"_exception_do_",
 smalltalk.method({
-selector: "exception:ifNotAsync:",
+selector: "exception:do:",
 category: 'private',
 fn: function (anException,aBlock){
 var self=this;
 smalltalk.send(anException,"_signal",[]);
 return self},
 args: ["anException", "aBlock"],
-source: "exception: anException ifNotAsync: aBlock\x0a\x09anException signal",
+source: "exception: anException do: aBlock\x0a\x09anException signal",
 messageSends: ["signal"],
 referencedClasses: []
 }),

+ 18 - 14
st/SUnit.st

@@ -14,7 +14,7 @@ result: aTestResult
 ! !
 
 Object subclass: #RunningTestContext
-	instanceVariableNames: 'finished testCase result step'
+	instanceVariableNames: 'finished testCase result'
 	package: 'SUnit'!
 
 !RunningTestContext methodsFor: 'accessing'!
@@ -33,23 +33,27 @@ testCase: aTestCase
 
 !RunningTestContext methodsFor: 'private'!
 
-exception: anException ifNotAsync: aBlock
-	testCase isAsync
-		ifTrue: [ step := [ testCase finished. anException signal ]]
-		ifFalse: [ aBlock value ]
+exception: anException do: aBlock
+	aBlock value
+!
+
+executeWithCleanup: aBlock
+	| failed |
+    testCase context: self.
+    [ failed := true. aBlock value. failed := false ] ensure: [
+        testCase context: nil.
+        (failed and: [testCase isAsync]) ifTrue: [ testCase finished ].
+        testCase isAsync ifFalse: [ testCase tearDown ]
+    ]
 ! !
 
 !RunningTestContext methodsFor: 'running'!
 
 execute: aBlock
-    step := aBlock.
-	[ step isNil ] whileFalse: [
-	    testCase context: self.
-		[[[	step
-        		ensure: [ testCase context: nil. step := nil. testCase isAsync ifFalse: [ testCase tearDown ]]]
-				on: TestFailure do: [:ex | self exception: ex ifNotAsync: [ result addFailure: testCase]]]
-				on: Error do: [:ex | self exception: ex ifNotAsync: [ result addError: testCase]]]
-				ensure: [ testCase isAsync ifFalse: [ result increaseRuns. finished value ]]]
+    [[[ self executeWithCleanup: aBlock ]
+    on: TestFailure do: [:ex | self exception: ex do: [ result addFailure: testCase]]]
+    on: Error do: [:ex | self exception: ex do: [ result addError: testCase]]]
+    ensure: [ testCase isAsync ifFalse: [ result increaseRuns. finished value ]]
 !
 
 start
@@ -72,7 +76,7 @@ RunningTestContext subclass: #ErroringTestContext
 
 !ErroringTestContext methodsFor: 'private'!
 
-exception: anException ifNotAsync: aBlock
+exception: anException do: aBlock
 	anException signal
 ! !