Browse Source

Not working, first hack

Braden Shepherdson 6 years ago
parent
commit
d36086a5a2
4 changed files with 142 additions and 25 deletions
  1. 52 0
      src/SUnit-Tests.js
  2. 12 0
      src/SUnit-Tests.st
  3. 68 20
      src/SUnit.js
  4. 10 5
      src/SUnit.st

+ 52 - 0
src/SUnit-Tests.js

@@ -5,6 +5,58 @@ $core.addPackage("SUnit-Tests");
 ($core.packageDescriptors||$core.packages)["SUnit-Tests"].innerEval = function (expr) { return eval(expr); };
 ($core.packageDescriptors||$core.packages)["SUnit-Tests"].innerEval = function (expr) { return eval(expr); };
 ($core.packageDescriptors||$core.packages)["SUnit-Tests"].transport = {"type":"amd","amdNamespace":"amber_core"};
 ($core.packageDescriptors||$core.packages)["SUnit-Tests"].transport = {"type":"amd","amdNamespace":"amber_core"};
 
 
+$core.addClass("BasicTest", $globals.TestCase, [], "SUnit-Tests");
+$core.addMethod(
+$core.method({
+selector: "testPromise",
+protocol: "as yet unclassified",
+fn: function(){
+var self=this,$self=this;
+var p;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+p=$recv($globals.Promise)._new_((function(model){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+return $recv((function(){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx3) {
+//>>excludeEnd("ctx");
+return $recv(model)._value_((7));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)});
+//>>excludeEnd("ctx");
+}))._valueWithTimeout_((300));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({model:model},$ctx1,1)});
+//>>excludeEnd("ctx");
+}));
+return $recv(p)._then_((function(x){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+return $self._assert_equals_(x,(5));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1,3)});
+//>>excludeEnd("ctx");
+}));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"testPromise",{p:p},$globals.BasicTest)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "testPromise\x0a\x09| p |\x0a\x09p := Promise new: [ :model | [ model value: 7 ] valueWithTimeout: 300 ].\x0a\x09^ p then: [ :x | self assert: x equals: 5 ]",
+referencedClasses: ["Promise"],
+//>>excludeEnd("ide");
+messageSends: ["new:", "valueWithTimeout:", "value:", "then:", "assert:equals:"]
+}),
+$globals.BasicTest);
+
+
+
 $core.addClass("ExampleSetTest", $globals.TestCase, ["empty", "full"], "SUnit-Tests");
 $core.addClass("ExampleSetTest", $globals.TestCase, ["empty", "full"], "SUnit-Tests");
 //>>excludeStart("ide", pragmas.excludeIdeData);
 //>>excludeStart("ide", pragmas.excludeIdeData);
 $globals.ExampleSetTest.comment="ExampleSetTest is taken from Pharo 1.4.\x0a\x0aTHe purpose of this class is to demonstrate a simple use case of the test framework.";
 $globals.ExampleSetTest.comment="ExampleSetTest is taken from Pharo 1.4.\x0a\x0aTHe purpose of this class is to demonstrate a simple use case of the test framework.";

+ 12 - 0
src/SUnit-Tests.st

@@ -1,4 +1,16 @@
 Smalltalk createPackage: 'SUnit-Tests'!
 Smalltalk createPackage: 'SUnit-Tests'!
+TestCase subclass: #BasicTest
+	instanceVariableNames: ''
+	package: 'SUnit-Tests'!
+
+!BasicTest methodsFor: 'as yet unclassified'!
+
+testPromise
+	| p |
+	p := Promise new: [ :model | [ model value: 7 ] valueWithTimeout: 300 ].
+	^ p then: [ :x | self assert: x equals: 5 ]
+! !
+
 TestCase subclass: #ExampleSetTest
 TestCase subclass: #ExampleSetTest
 	instanceVariableNames: 'empty full'
 	instanceVariableNames: 'empty full'
 	package: 'SUnit-Tests'!
 	package: 'SUnit-Tests'!

+ 68 - 20
src/SUnit.js

@@ -301,21 +301,20 @@ $core.addMethod(
 $core.method({
 $core.method({
 selector: "performTest",
 selector: "performTest",
 protocol: "running",
 protocol: "running",
-fn: function (){
+fn: function(){
 var self=this,$self=this;
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
 $self["@asyncTimeout"]=nil;
 $self["@asyncTimeout"]=nil;
-$self._perform_($self._selector());
-return self;
+return $self._perform_($self._selector());
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"performTest",{},$globals.TestCase)});
 }, function($ctx1) {$ctx1.fill(self,"performTest",{},$globals.TestCase)});
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
 },
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
 args: [],
-source: "performTest\x0a\x09asyncTimeout := nil.\x0a\x09self perform: self selector",
+source: "performTest\x0a\x09asyncTimeout := nil.\x0a\x09^ self perform: self selector",
 referencedClasses: [],
 referencedClasses: [],
 //>>excludeEnd("ide");
 //>>excludeEnd("ide");
 messageSends: ["perform:", "selector"]
 messageSends: ["perform:", "selector"]
@@ -815,13 +814,13 @@ $core.addMethod(
 $core.method({
 $core.method({
 selector: "execute:",
 selector: "execute:",
 protocol: "running",
 protocol: "running",
-fn: function (aBlock){
+fn: function(aBlock){
 var self=this,$self=this;
 var self=this,$self=this;
-var failed;
+var failed,returned;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
-var $1,$2;
+var $1,$2,$3,$4,$5;
 $recv($self["@testCase"])._context_(self);
 $recv($self["@testCase"])._context_(self);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.sendIdx["context:"]=1;
 $ctx1.sendIdx["context:"]=1;
@@ -832,9 +831,58 @@ return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
 failed=true;
 failed=true;
 failed;
 failed;
-$recv(aBlock)._value();
+returned=$recv(aBlock)._value();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx2.sendIdx["value"]=1;
+//>>excludeEnd("ctx");
+returned;
 failed=false;
 failed=false;
-return failed;
+failed;
+$1=$recv(returned)._isKindOf_($globals.Promise);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx2.sendIdx["isKindOf:"]=1;
+//>>excludeEnd("ctx");
+$recv($globals.Transcript)._show_($1);
+$recv($globals.Transcript)._cr();
+$2=$recv(returned)._isKindOf_($globals.Promise);
+if($core.assert($2)){
+$3=$recv($self["@testCase"])._isAsync();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx2.sendIdx["isAsync"]=1;
+//>>excludeEnd("ctx");
+if(!$core.assert($3)){
+$recv($self["@testCase"])._timeout_((2000));
+}
+return $recv(returned)._then_catch_((function(){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx3) {
+//>>excludeEnd("ctx");
+return $recv($self["@testCase"])._finished();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx3.sendIdx["finished"]=1;
+//>>excludeEnd("ctx");
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx3) {$ctx3.fillBlock({},$ctx2,4)});
+//>>excludeEnd("ctx");
+}),(function(e){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx3) {
+//>>excludeEnd("ctx");
+$recv(e)._inspect();
+return $recv($recv($self["@testCase"])._async_((function(){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx4) {
+//>>excludeEnd("ctx");
+return $recv(e)._resignal();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx4) {$ctx4.fillBlock({},$ctx3,6)});
+//>>excludeEnd("ctx");
+})))._value();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx3) {$ctx3.fillBlock({e:e},$ctx2,5)});
+//>>excludeEnd("ctx");
+}));
+}
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)});
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)});
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
@@ -843,40 +891,40 @@ return failed;
 return $core.withContext(function($ctx2) {
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
 $recv($self["@testCase"])._context_(nil);
 $recv($self["@testCase"])._context_(nil);
-$1=$recv(failed)._and_((function(){
+$4=$recv(failed)._and_((function(){
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx3) {
 return $core.withContext(function($ctx3) {
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
 return $recv($self["@testCase"])._isAsync();
 return $recv($self["@testCase"])._isAsync();
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-$ctx3.sendIdx["isAsync"]=1;
+$ctx3.sendIdx["isAsync"]=2;
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx3) {$ctx3.fillBlock({},$ctx2,3)});
+}, function($ctx3) {$ctx3.fillBlock({},$ctx2,8)});
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
 }));
 }));
-if($core.assert($1)){
+if($core.assert($4)){
 $recv($self["@testCase"])._finished();
 $recv($self["@testCase"])._finished();
 }
 }
-$2=$recv($self["@testCase"])._isAsync();
-if(!$core.assert($2)){
+$5=$recv($self["@testCase"])._isAsync();
+if(!$core.assert($5)){
 return $recv($self["@testCase"])._tearDown();
 return $recv($self["@testCase"])._tearDown();
 }
 }
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,7)});
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
 }));
 }));
 return self;
 return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"execute:",{aBlock:aBlock,failed:failed},$globals.TestContext)});
+}, function($ctx1) {$ctx1.fill(self,"execute:",{aBlock:aBlock,failed:failed,returned:returned},$globals.TestContext)});
 //>>excludeEnd("ctx");
 //>>excludeEnd("ctx");
 },
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aBlock"],
 args: ["aBlock"],
-source: "execute: aBlock\x0a\x09| failed |\x0a\x09\x0a\x09testCase context: self.\x0a\x09[\x0a\x09\x09failed := true.\x0a\x09\x09aBlock value.\x0a\x09\x09failed := false\x0a\x09]\x0a\x09\x09ensure: [\x0a\x09\x09\x09testCase context: nil.\x0a\x09\x09\x09\x0a\x09\x09\x09(failed and: [ testCase isAsync ]) ifTrue: [\x0a\x09\x09\x09\x09testCase finished ].\x0a\x09\x09\x09testCase isAsync ifFalse: [\x0a\x09\x09\x09\x09testCase tearDown ] ]",
-referencedClasses: [],
+source: "execute: aBlock\x0a\x09| failed returned |\x0a\x09\x0a\x09testCase context: self.\x0a\x09[\x0a\x09\x09failed := true.\x0a\x09\x09returned := aBlock value.\x0a\x09\x09failed := false.\x0a\x09\x09Transcript show: (returned isKindOf: Promise); cr.\x0a\x09\x09(returned isKindOf: Promise) ifTrue: [\x0a\x09\x09\x09testCase isAsync ifFalse: [ testCase timeout: 2000 ]. \x22Set a default timeout if there wasn't one already.\x22\x0a\x09\x09\x09returned then: [ testCase finished ] catch: [ :e | e inspect. (testCase async: [ e resignal ]) value ] ].\x0a\x09\x09\x09\x0a\x09]\x0a\x09\x09ensure: [\x0a\x09\x09\x09testCase context: nil.\x0a\x0a\x09\x09\x09(failed and: [ testCase isAsync ]) ifTrue: [\x0a\x09\x09\x09\x09testCase finished ].\x0a\x09\x09\x09testCase isAsync ifFalse: [\x0a\x09\x09\x09\x09testCase tearDown ] ]",
+referencedClasses: ["Transcript", "Promise"],
 //>>excludeEnd("ide");
 //>>excludeEnd("ide");
-messageSends: ["context:", "ensure:", "value", "ifTrue:", "and:", "isAsync", "finished", "ifFalse:", "tearDown"]
+messageSends: ["context:", "ensure:", "value", "show:", "isKindOf:", "cr", "ifTrue:", "ifFalse:", "isAsync", "timeout:", "then:catch:", "finished", "inspect", "async:", "resignal", "and:", "tearDown"]
 }),
 }),
 $globals.TestContext);
 $globals.TestContext);
 
 

+ 10 - 5
src/SUnit.st

@@ -93,7 +93,7 @@ signalFailure: aString
 
 
 performTest
 performTest
 	asyncTimeout := nil.
 	asyncTimeout := nil.
-	self perform: self selector
+	^ self perform: self selector
 !
 !
 
 
 runCase
 runCase
@@ -213,17 +213,22 @@ testCase: aTestCase
 !TestContext methodsFor: 'running'!
 !TestContext methodsFor: 'running'!
 
 
 execute: aBlock
 execute: aBlock
-	| failed |
+	| failed returned |
 	
 	
 	testCase context: self.
 	testCase context: self.
 	[
 	[
 		failed := true.
 		failed := true.
-		aBlock value.
-		failed := false
+		returned := aBlock value.
+		failed := false.
+		Transcript show: (returned isKindOf: Promise); cr.
+		(returned isKindOf: Promise) ifTrue: [
+			testCase isAsync ifFalse: [ testCase timeout: 2000 ]. "Set a default timeout if there wasn't one already."
+			returned then: [ testCase finished ] catch: [ :e | e inspect. (testCase async: [ e resignal ]) value ] ].
+			
 	]
 	]
 		ensure: [
 		ensure: [
 			testCase context: nil.
 			testCase context: nil.
-			
+
 			(failed and: [ testCase isAsync ]) ifTrue: [
 			(failed and: [ testCase isAsync ]) ifTrue: [
 				testCase finished ].
 				testCase finished ].
 			testCase isAsync ifFalse: [
 			testCase isAsync ifFalse: [