Browse Source

RunningTestContext governs run of single test case.

Herby Vojčík 11 years ago
parent
commit
6cae2d96ef
3 changed files with 274 additions and 117 deletions
  1. 97 43
      js/SUnit.deploy.js
  2. 131 57
      js/SUnit.js
  3. 46 17
      st/SUnit.st

+ 97 - 43
js/SUnit.deploy.js

@@ -24,6 +24,98 @@ smalltalk.ResultAnnouncement);
 
 
 
+smalltalk.addClass('RunningTestContext', smalltalk.Object, ['finished', 'testCase', 'result'], 'SUnit');
+smalltalk.addMethod(
+"_execute_",
+smalltalk.method({
+selector: "execute:",
+fn: function (aBlock){
+var self=this;
+smalltalk.send((function(){
+return smalltalk.send((function(){
+return smalltalk.send((function(){
+return smalltalk.send(aBlock,"_ensure_",[(function(){
+smalltalk.send(self["@result"],"_increaseRuns",[]);
+return smalltalk.send(self["@testCase"],"_tearDown",[]);
+})]);
+}),"_on_do_",[(smalltalk.TestFailure || TestFailure),(function(ex){
+return smalltalk.send(self["@result"],"_addFailure_",[self["@testCase"]]);
+})]);
+}),"_on_do_",[(smalltalk.Error || Error),(function(ex){
+return smalltalk.send(self["@result"],"_addError_",[self["@testCase"]]);
+})]);
+}),"_ensure_",[self["@finished"]]);
+return self}
+}),
+smalltalk.RunningTestContext);
+
+smalltalk.addMethod(
+"_finished_",
+smalltalk.method({
+selector: "finished:",
+fn: function (aBlock){
+var self=this;
+self["@finished"]=aBlock;
+return self}
+}),
+smalltalk.RunningTestContext);
+
+smalltalk.addMethod(
+"_result_",
+smalltalk.method({
+selector: "result:",
+fn: function (aTestResult){
+var self=this;
+self["@result"]=aTestResult;
+return self}
+}),
+smalltalk.RunningTestContext);
+
+smalltalk.addMethod(
+"_start",
+smalltalk.method({
+selector: "start",
+fn: function (){
+var self=this;
+smalltalk.send(self,"_execute_",[(function(){
+smalltalk.send(self["@testCase"],"_setUp",[]);
+return smalltalk.send(self["@testCase"],"_performTest",[]);
+})]);
+return self}
+}),
+smalltalk.RunningTestContext);
+
+smalltalk.addMethod(
+"_testCase_",
+smalltalk.method({
+selector: "testCase:",
+fn: function (aTestCase){
+var self=this;
+self["@testCase"]=aTestCase;
+return self}
+}),
+smalltalk.RunningTestContext);
+
+
+smalltalk.addMethod(
+"_testCase_result_finished_",
+smalltalk.method({
+selector: "testCase:result:finished:",
+fn: function (aTestCase,aTestResult,aBlock){
+var self=this;
+var $2,$3,$1;
+$2=smalltalk.send(self,"_new",[]);
+smalltalk.send($2,"_testCase_",[aTestCase]);
+smalltalk.send($2,"_result_",[aTestResult]);
+smalltalk.send($2,"_finished_",[aBlock]);
+$3=smalltalk.send($2,"_yourself",[]);
+$1=$3;
+return $1;
+}
+}),
+smalltalk.RunningTestContext.klass);
+
+
 smalltalk.addClass('TestCase', smalltalk.Object, ['testSelector'], 'SUnit');
 smalltalk.addMethod(
 "_assert_",
@@ -188,18 +280,6 @@ fn: function (aString) {
 }),
 smalltalk.TestCase);
 
-smalltalk.addMethod(
-"_startCase",
-smalltalk.method({
-selector: "startCase",
-fn: function (){
-var self=this;
-smalltalk.send(self,"_setUp",[]);
-smalltalk.send(self,"_performTest",[]);
-return self}
-}),
-smalltalk.TestCase);
-
 smalltalk.addMethod(
 "_tearDown",
 smalltalk.method({
@@ -405,30 +485,6 @@ return $1;
 }),
 smalltalk.TestResult);
 
-smalltalk.addMethod(
-"_runCase_",
-smalltalk.method({
-selector: "runCase:",
-fn: function (aTestCase){
-var self=this;
-smalltalk.send((function(){
-return smalltalk.send((function(){
-return smalltalk.send((function(){
-smalltalk.send(self,"_increaseRuns",[]);
-return smalltalk.send(aTestCase,"_startCase",[]);
-}),"_ensure_",[(function(){
-return smalltalk.send(aTestCase,"_tearDown",[]);
-})]);
-}),"_on_do_",[(smalltalk.TestFailure || TestFailure),(function(ex){
-return smalltalk.send(self,"_addFailure_",[aTestCase]);
-})]);
-}),"_on_do_",[(smalltalk.Error || Error),(function(ex){
-return smalltalk.send(self,"_addError_",[aTestCase]);
-})]);
-return self}
-}),
-smalltalk.TestResult);
-
 smalltalk.addMethod(
 "_runs",
 smalltalk.method({
@@ -499,7 +555,7 @@ smalltalk.TestResult);
 
 
 
-smalltalk.addClass('TestSuiteRunner', smalltalk.Object, ['suite', 'result', 'announcer', 'worker'], 'SUnit');
+smalltalk.addClass('TestSuiteRunner', smalltalk.Object, ['suite', 'result', 'announcer', 'runNextTest'], 'SUnit');
 smalltalk.addMethod(
 "_announcer",
 smalltalk.method({
@@ -520,13 +576,11 @@ var self=this;
 smalltalk.send(self,"_initialize",[],smalltalk.Object);
 self["@announcer"]=smalltalk.send((smalltalk.Announcer || Announcer),"_new",[]);
 self["@result"]=smalltalk.send((smalltalk.TestResult || TestResult),"_new",[]);
-self["@worker"]=(function(){
+self["@runNextTest"]=(function(){
 return smalltalk.send(self["@result"],"_nextRunDo_",[(function(index){
-return smalltalk.send((function(){
-return smalltalk.send(self["@result"],"_runCase_",[smalltalk.send(self["@suite"],"_at_",[index])]);
-}),"_ensure_",[(function(){
+return smalltalk.send(smalltalk.send((smalltalk.RunningTestContext || RunningTestContext),"_testCase_result_finished_",[smalltalk.send(self["@suite"],"_at_",[index]),self["@result"],(function(){
 return smalltalk.send(self,"_resume",[]);
-})]);
+})]),"_start",[]);
 })]);
 });
 return self}
@@ -550,7 +604,7 @@ smalltalk.method({
 selector: "resume",
 fn: function (){
 var self=this;
-smalltalk.send(self["@worker"],"_fork",[]);
+smalltalk.send(self["@runNextTest"],"_fork",[]);
 smalltalk.send(self["@announcer"],"_announce_",[smalltalk.send(smalltalk.send((smalltalk.ResultAnnouncement || ResultAnnouncement),"_new",[]),"_result_",[self["@result"]])]);
 return self}
 }),

+ 131 - 57
js/SUnit.js

@@ -34,6 +34,128 @@ smalltalk.ResultAnnouncement);
 
 
 
+smalltalk.addClass('RunningTestContext', smalltalk.Object, ['finished', 'testCase', 'result'], 'SUnit');
+smalltalk.addMethod(
+"_execute_",
+smalltalk.method({
+selector: "execute:",
+category: 'running',
+fn: function (aBlock){
+var self=this;
+smalltalk.send((function(){
+return smalltalk.send((function(){
+return smalltalk.send((function(){
+return smalltalk.send(aBlock,"_ensure_",[(function(){
+smalltalk.send(self["@result"],"_increaseRuns",[]);
+return smalltalk.send(self["@testCase"],"_tearDown",[]);
+})]);
+}),"_on_do_",[(smalltalk.TestFailure || TestFailure),(function(ex){
+return smalltalk.send(self["@result"],"_addFailure_",[self["@testCase"]]);
+})]);
+}),"_on_do_",[(smalltalk.Error || Error),(function(ex){
+return smalltalk.send(self["@result"],"_addError_",[self["@testCase"]]);
+})]);
+}),"_ensure_",[self["@finished"]]);
+return self},
+args: ["aBlock"],
+source: "execute: aBlock\x0a\x09[[[\x09aBlock\x0a        \x09ensure: [ result increaseRuns. testCase tearDown ]]\x0a\x09\x09\x09on: TestFailure do: [:ex | result addFailure: testCase]]\x0a\x09\x09\x09on: Error do: [:ex | result addError: testCase]]\x0a\x09\x09\x09ensure: finished",
+messageSends: ["ensure:", "on:do:", "addError:", "addFailure:", "increaseRuns", "tearDown"],
+referencedClasses: ["Error", "TestFailure"]
+}),
+smalltalk.RunningTestContext);
+
+smalltalk.addMethod(
+"_finished_",
+smalltalk.method({
+selector: "finished:",
+category: 'accessing',
+fn: function (aBlock){
+var self=this;
+self["@finished"]=aBlock;
+return self},
+args: ["aBlock"],
+source: "finished: aBlock\x0a\x09finished := aBlock",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.RunningTestContext);
+
+smalltalk.addMethod(
+"_result_",
+smalltalk.method({
+selector: "result:",
+category: 'accessing',
+fn: function (aTestResult){
+var self=this;
+self["@result"]=aTestResult;
+return self},
+args: ["aTestResult"],
+source: "result: aTestResult\x0a\x09result := aTestResult",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.RunningTestContext);
+
+smalltalk.addMethod(
+"_start",
+smalltalk.method({
+selector: "start",
+category: 'running',
+fn: function (){
+var self=this;
+smalltalk.send(self,"_execute_",[(function(){
+smalltalk.send(self["@testCase"],"_setUp",[]);
+return smalltalk.send(self["@testCase"],"_performTest",[]);
+})]);
+return self},
+args: [],
+source: "start\x0a\x09self execute: [ testCase setUp. testCase performTest ]",
+messageSends: ["execute:", "setUp", "performTest"],
+referencedClasses: []
+}),
+smalltalk.RunningTestContext);
+
+smalltalk.addMethod(
+"_testCase_",
+smalltalk.method({
+selector: "testCase:",
+category: 'accessing',
+fn: function (aTestCase){
+var self=this;
+self["@testCase"]=aTestCase;
+return self},
+args: ["aTestCase"],
+source: "testCase: aTestCase\x0a\x09testCase := aTestCase",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.RunningTestContext);
+
+
+smalltalk.addMethod(
+"_testCase_result_finished_",
+smalltalk.method({
+selector: "testCase:result:finished:",
+category: 'instance creation',
+fn: function (aTestCase,aTestResult,aBlock){
+var self=this;
+var $2,$3,$1;
+$2=smalltalk.send(self,"_new",[]);
+smalltalk.send($2,"_testCase_",[aTestCase]);
+smalltalk.send($2,"_result_",[aTestResult]);
+smalltalk.send($2,"_finished_",[aBlock]);
+$3=smalltalk.send($2,"_yourself",[]);
+$1=$3;
+return $1;
+},
+args: ["aTestCase", "aTestResult", "aBlock"],
+source: "testCase: aTestCase result: aTestResult finished: aBlock\x0a\x09^self new\x0a        testCase: aTestCase;\x0a        result: aTestResult;\x0a        finished: aBlock;\x0a        yourself",
+messageSends: ["testCase:", "new", "result:", "finished:", "yourself"],
+referencedClasses: []
+}),
+smalltalk.RunningTestContext.klass);
+
+
 smalltalk.addClass('TestCase', smalltalk.Object, ['testSelector'], 'SUnit');
 smalltalk.addMethod(
 "_assert_",
@@ -263,23 +385,6 @@ referencedClasses: ["TestFailure"]
 }),
 smalltalk.TestCase);
 
-smalltalk.addMethod(
-"_startCase",
-smalltalk.method({
-selector: "startCase",
-category: 'running',
-fn: function (){
-var self=this;
-smalltalk.send(self,"_setUp",[]);
-smalltalk.send(self,"_performTest",[]);
-return self},
-args: [],
-source: "startCase\x0a\x09self setUp.\x0a\x09self performTest\x0a",
-messageSends: ["setUp", "performTest"],
-referencedClasses: []
-}),
-smalltalk.TestCase);
-
 smalltalk.addMethod(
 "_tearDown",
 smalltalk.method({
@@ -560,35 +665,6 @@ referencedClasses: []
 }),
 smalltalk.TestResult);
 
-smalltalk.addMethod(
-"_runCase_",
-smalltalk.method({
-selector: "runCase:",
-category: 'running',
-fn: function (aTestCase){
-var self=this;
-smalltalk.send((function(){
-return smalltalk.send((function(){
-return smalltalk.send((function(){
-smalltalk.send(self,"_increaseRuns",[]);
-return smalltalk.send(aTestCase,"_startCase",[]);
-}),"_ensure_",[(function(){
-return smalltalk.send(aTestCase,"_tearDown",[]);
-})]);
-}),"_on_do_",[(smalltalk.TestFailure || TestFailure),(function(ex){
-return smalltalk.send(self,"_addFailure_",[aTestCase]);
-})]);
-}),"_on_do_",[(smalltalk.Error || Error),(function(ex){
-return smalltalk.send(self,"_addError_",[aTestCase]);
-})]);
-return self},
-args: ["aTestCase"],
-source: "runCase: aTestCase\x0a\x09[[[\x09self increaseRuns.\x0a    \x09aTestCase startCase ] ensure: [ aTestCase tearDown ]]\x0a\x09on: TestFailure do: [:ex | self addFailure: aTestCase]]\x0a\x09on: Error do: [:ex | self addError: aTestCase]\x0a",
-messageSends: ["on:do:", "addError:", "addFailure:", "ensure:", "tearDown", "increaseRuns", "startCase"],
-referencedClasses: ["Error", "TestFailure"]
-}),
-smalltalk.TestResult);
-
 smalltalk.addMethod(
 "_runs",
 smalltalk.method({
@@ -684,7 +760,7 @@ smalltalk.TestResult);
 
 
 
-smalltalk.addClass('TestSuiteRunner', smalltalk.Object, ['suite', 'result', 'announcer', 'worker'], 'SUnit');
+smalltalk.addClass('TestSuiteRunner', smalltalk.Object, ['suite', 'result', 'announcer', 'runNextTest'], 'SUnit');
 smalltalk.addMethod(
 "_announcer",
 smalltalk.method({
@@ -711,20 +787,18 @@ var self=this;
 smalltalk.send(self,"_initialize",[],smalltalk.Object);
 self["@announcer"]=smalltalk.send((smalltalk.Announcer || Announcer),"_new",[]);
 self["@result"]=smalltalk.send((smalltalk.TestResult || TestResult),"_new",[]);
-self["@worker"]=(function(){
+self["@runNextTest"]=(function(){
 return smalltalk.send(self["@result"],"_nextRunDo_",[(function(index){
-return smalltalk.send((function(){
-return smalltalk.send(self["@result"],"_runCase_",[smalltalk.send(self["@suite"],"_at_",[index])]);
-}),"_ensure_",[(function(){
+return smalltalk.send(smalltalk.send((smalltalk.RunningTestContext || RunningTestContext),"_testCase_result_finished_",[smalltalk.send(self["@suite"],"_at_",[index]),self["@result"],(function(){
 return smalltalk.send(self,"_resume",[]);
-})]);
+})]),"_start",[]);
 })]);
 });
 return self},
 args: [],
-source: "initialize\x0a\x09super initialize.\x0a\x09announcer := Announcer new.\x0a    result := TestResult new.\x0a    worker := [ result nextRunDo: [ :index |\x0a\x09\x09[ result runCase: (suite at: index) ]\x0a\x09\x09ensure: [ self resume ]]].\x0a",
-messageSends: ["initialize", "new", "nextRunDo:", "ensure:", "resume", "runCase:", "at:"],
-referencedClasses: ["Announcer", "TestResult"]
+source: "initialize\x0a\x09super initialize.\x0a\x09announcer := Announcer new.\x0a    result := TestResult new.\x0a    runNextTest := [ result nextRunDo: [ :index |\x0a    \x09(RunningTestContext testCase: (suite at: index) result: result finished: [ self resume ]) start ]].\x0a",
+messageSends: ["initialize", "new", "nextRunDo:", "start", "testCase:result:finished:", "at:", "resume"],
+referencedClasses: ["Announcer", "TestResult", "RunningTestContext"]
 }),
 smalltalk.TestSuiteRunner);
 
@@ -751,11 +825,11 @@ selector: "resume",
 category: 'actions',
 fn: function (){
 var self=this;
-smalltalk.send(self["@worker"],"_fork",[]);
+smalltalk.send(self["@runNextTest"],"_fork",[]);
 smalltalk.send(self["@announcer"],"_announce_",[smalltalk.send(smalltalk.send((smalltalk.ResultAnnouncement || ResultAnnouncement),"_new",[]),"_result_",[self["@result"]])]);
 return self},
 args: [],
-source: "resume\x0a\x09worker fork.\x0a    announcer announce: (ResultAnnouncement new result: result)\x0a",
+source: "resume\x0a\x09runNextTest fork.\x0a    announcer announce: (ResultAnnouncement new result: result)\x0a",
 messageSends: ["fork", "announce:", "result:", "new"],
 referencedClasses: ["ResultAnnouncement"]
 }),

+ 46 - 17
st/SUnit.st

@@ -13,6 +13,48 @@ result: aTestResult
 	result := aTestResult
 ! !
 
+Object subclass: #RunningTestContext
+	instanceVariableNames: 'finished testCase result'
+	package: 'SUnit'!
+
+!RunningTestContext methodsFor: 'accessing'!
+
+finished: aBlock
+	finished := aBlock
+!
+
+result: aTestResult
+	result := aTestResult
+!
+
+testCase: aTestCase
+	testCase := aTestCase
+! !
+
+!RunningTestContext methodsFor: 'running'!
+
+execute: aBlock
+	[[[	aBlock
+        	ensure: [ result increaseRuns. testCase tearDown ]]
+			on: TestFailure do: [:ex | result addFailure: testCase]]
+			on: Error do: [:ex | result addError: testCase]]
+			ensure: finished
+!
+
+start
+	self execute: [ testCase setUp. testCase performTest ]
+! !
+
+!RunningTestContext class methodsFor: 'instance creation'!
+
+testCase: aTestCase result: aTestResult finished: aBlock
+	^self new
+        testCase: aTestCase;
+        result: aTestResult;
+        finished: aBlock;
+        yourself
+! !
+
 Object subclass: #TestCase
 	instanceVariableNames: 'testSelector'
 	package: 'SUnit'!
@@ -50,11 +92,6 @@ runCase
 setUp
 !
 
-startCase
-	self setUp.
-	self performTest
-!
-
 tearDown
 ! !
 
@@ -203,17 +240,10 @@ nextRunDo: aBlock
 or does nothing if no more runs"
 ^self runs == self total
 	ifFalse: [ aBlock value: self runs + 1 ]
-!
-
-runCase: aTestCase
-	[[[	self increaseRuns.
-    	aTestCase startCase ] ensure: [ aTestCase tearDown ]]
-	on: TestFailure do: [:ex | self addFailure: aTestCase]]
-	on: Error do: [:ex | self addError: aTestCase]
 ! !
 
 Object subclass: #TestSuiteRunner
-	instanceVariableNames: 'suite result announcer worker'
+	instanceVariableNames: 'suite result announcer runNextTest'
 	package: 'SUnit'!
 
 !TestSuiteRunner methodsFor: 'accessing'!
@@ -233,7 +263,7 @@ suite: aCollection
 !TestSuiteRunner methodsFor: 'actions'!
 
 resume
-	worker fork.
+	runNextTest fork.
     announcer announce: (ResultAnnouncement new result: result)
 !
 
@@ -248,9 +278,8 @@ initialize
 	super initialize.
 	announcer := Announcer new.
     result := TestResult new.
-    worker := [ result nextRunDo: [ :index |
-		[ result runCase: (suite at: index) ]
-		ensure: [ self resume ]]].
+    runNextTest := [ result nextRunDo: [ :index |
+    	(RunningTestContext testCase: (suite at: index) result: result finished: [ self resume ]) start ]].
 ! !
 
 !TestSuiteRunner class methodsFor: 'instance creation'!