Browse Source

ifPresent:{,ifAbsent:} tests at base, removed subtests

Eigth errors stemming from two bugs.
Herbert Vojčík 11 years ago
parent
commit
c4eb9e6e69
2 changed files with 139 additions and 102 deletions
  1. 113 74
      js/Kernel-Tests.js
  2. 26 28
      st/Kernel-Tests.st

+ 113 - 74
js/Kernel-Tests.js

@@ -1935,6 +1935,119 @@ referencedClasses: []
 }),
 smalltalk.IndexableCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testAtIfPresent",
+protocol: 'tests',
+fn: function (){
+var self=this;
+var visited,sentinel;
+function $Object(){return smalltalk.Object||(typeof Object=="undefined"?nil:Object)}
+return smalltalk.withContext(function($ctx1) { 
+var $2,$1,$4,$3;
+sentinel=_st($Object())._new();
+self._nonIndexesDo_((function(each){
+return smalltalk.withContext(function($ctx2) {
+visited=nil;
+visited;
+$2=self._collection();
+$ctx2.sendIdx["collection"]=1;
+$1=_st($2)._at_ifPresent_(each,(function(value1){
+return smalltalk.withContext(function($ctx3) {
+visited=value1;
+visited;
+return sentinel;
+}, function($ctx3) {$ctx3.fillBlock({value1:value1},$ctx2,2)})}));
+$ctx2.sendIdx["at:ifPresent:"]=1;
+self._assert_equals_($1,nil);
+$ctx2.sendIdx["assert:equals:"]=1;
+return self._assert_(_st(visited)._isNil());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,1)})}));
+self._samplesDo_((function(index,value){
+return smalltalk.withContext(function($ctx2) {
+visited=nil;
+visited;
+$4=self._collection();
+$ctx2.sendIdx["collection"]=2;
+$3=_st($4)._at_ifPresent_(index,(function(value2){
+return smalltalk.withContext(function($ctx3) {
+visited=value2;
+visited;
+return sentinel;
+}, function($ctx3) {$ctx3.fillBlock({value2:value2},$ctx2,4)})}));
+self._assert_equals_($3,sentinel);
+$ctx2.sendIdx["assert:equals:"]=2;
+return self._assert_equals_(visited,_st(self._collection())._at_(index));
+}, function($ctx2) {$ctx2.fillBlock({index:index,value:value},$ctx1,3)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfPresent",{visited:visited,sentinel:sentinel},smalltalk.IndexableCollectionTest)})},
+args: [],
+source: "testAtIfPresent\x0a\x09| visited sentinel |\x0a\x09sentinel := Object new.\x0a\x09self nonIndexesDo: [ :each |\x0a\x09\x09visited := nil.\x0a\x09\x09self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ]) equals: nil.\x0a\x09\x09self assert: visited isNil ].\x0a\x09self samplesDo: [ :index :value |\x0a\x09\x09visited := nil.\x0a\x09\x09self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ]) equals: sentinel.\x0a\x09\x09self assert: visited equals: (self collection at: index) ]",
+messageSends: ["new", "nonIndexesDo:", "assert:equals:", "at:ifPresent:", "collection", "assert:", "isNil", "samplesDo:", "at:"],
+referencedClasses: ["Object"]
+}),
+smalltalk.IndexableCollectionTest);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testAtIfPresentIfAbsent",
+protocol: 'tests',
+fn: function (){
+var self=this;
+var visited,sentinel;
+function $Object(){return smalltalk.Object||(typeof Object=="undefined"?nil:Object)}
+return smalltalk.withContext(function($ctx1) { 
+var $2,$1,$3,$5,$4;
+sentinel=_st($Object())._new();
+self._nonIndexesDo_((function(each){
+return smalltalk.withContext(function($ctx2) {
+visited=nil;
+visited;
+$2=self._collection();
+$ctx2.sendIdx["collection"]=1;
+$1=_st($2)._at_ifPresent_ifAbsent_(each,(function(value1){
+return smalltalk.withContext(function($ctx3) {
+visited=value1;
+visited;
+return sentinel;
+}, function($ctx3) {$ctx3.fillBlock({value1:value1},$ctx2,2)})}),(function(){
+return smalltalk.withContext(function($ctx3) {
+return self._sampleNonValue();
+$ctx3.sendIdx["sampleNonValue"]=1;
+}, function($ctx3) {$ctx3.fillBlock({},$ctx2,3)})}));
+$ctx2.sendIdx["at:ifPresent:ifAbsent:"]=1;
+$3=self._sampleNonValue();
+$ctx2.sendIdx["sampleNonValue"]=2;
+self._assert_equals_($1,$3);
+$ctx2.sendIdx["assert:equals:"]=1;
+return self._assert_(_st(visited)._isNil());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,1)})}));
+self._samplesDo_((function(index,value){
+return smalltalk.withContext(function($ctx2) {
+visited=nil;
+visited;
+$5=self._collection();
+$ctx2.sendIdx["collection"]=2;
+$4=_st($5)._at_ifPresent_ifAbsent_(index,(function(value2){
+return smalltalk.withContext(function($ctx3) {
+visited=value2;
+visited;
+return sentinel;
+}, function($ctx3) {$ctx3.fillBlock({value2:value2},$ctx2,5)})}),(function(){
+return smalltalk.withContext(function($ctx3) {
+return self._sampleNonValue();
+}, function($ctx3) {$ctx3.fillBlock({},$ctx2,6)})}));
+self._assert_equals_($4,sentinel);
+$ctx2.sendIdx["assert:equals:"]=2;
+return self._assert_equals_(visited,_st(self._collection())._at_(index));
+}, function($ctx2) {$ctx2.fillBlock({index:index,value:value},$ctx1,4)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"testAtIfPresentIfAbsent",{visited:visited,sentinel:sentinel},smalltalk.IndexableCollectionTest)})},
+args: [],
+source: "testAtIfPresentIfAbsent\x0a\x09| visited sentinel |\x0a\x09sentinel := Object new.\x0a\x09self nonIndexesDo: [ :each |\x0a\x09\x09visited := nil.\x0a\x09\x09self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ] ifAbsent: [ self sampleNonValue ] ) equals: self sampleNonValue.\x0a\x09\x09self assert: visited isNil ].\x0a\x09self samplesDo: [ :index :value |\x0a\x09\x09visited := nil.\x0a\x09\x09self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ] ifAbsent: [ self sampleNonValue ]) equals: sentinel.\x0a\x09\x09self assert: visited equals: (self collection at: index) ]",
+messageSends: ["new", "nonIndexesDo:", "assert:equals:", "at:ifPresent:ifAbsent:", "collection", "sampleNonValue", "assert:", "isNil", "samplesDo:", "at:"],
+referencedClasses: ["Object"]
+}),
+smalltalk.IndexableCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testIndexOf",
@@ -2403,80 +2516,6 @@ referencedClasses: ["Dictionary"]
 }),
 smalltalk.DictionaryTest);
 
-smalltalk.addMethod(
-smalltalk.method({
-selector: "testIfPresent",
-protocol: 'tests',
-fn: function (){
-var self=this;
-var d,visited,absent;
-function $Dictionary(){return smalltalk.Dictionary||(typeof Dictionary=="undefined"?nil:Dictionary)}
-return smalltalk.withContext(function($ctx1) { 
-visited=false;
-d=_st($Dictionary())._new();
-_st(d)._at_put_("hello","world");
-_st(d)._at_ifPresent_("hello",(function(value){
-return smalltalk.withContext(function($ctx2) {
-visited=value;
-return visited;
-}, function($ctx2) {$ctx2.fillBlock({value:value},$ctx1,1)})}));
-$ctx1.sendIdx["at:ifPresent:"]=1;
-self._assert_equals_(visited,"world");
-absent=_st(d)._at_ifPresent_("bye",(function(value){
-return smalltalk.withContext(function($ctx2) {
-visited=value;
-return visited;
-}, function($ctx2) {$ctx2.fillBlock({value:value},$ctx1,2)})}));
-self._assert_(_st(absent)._isNil());
-return self}, function($ctx1) {$ctx1.fill(self,"testIfPresent",{d:d,visited:visited,absent:absent},smalltalk.DictionaryTest)})},
-args: [],
-source: "testIfPresent\x0a\x0a\x09| d visited absent |\x0a\x09visited := false.\x0a\x09d := Dictionary new.\x0a\x09d at: 'hello' put: 'world'.\x0a\x0a\x09d at: 'hello' ifPresent: [ :value | visited := value ].\x0a\x09self assert: visited equals: 'world'.\x0a\x0a\x09absent := d at: 'bye' ifPresent: [ :value | visited := value ].\x0a\x09self assert: absent isNil.",
-messageSends: ["new", "at:put:", "at:ifPresent:", "assert:equals:", "assert:", "isNil"],
-referencedClasses: ["Dictionary"]
-}),
-smalltalk.DictionaryTest);
-
-smalltalk.addMethod(
-smalltalk.method({
-selector: "testIfPresentIfAbsent",
-protocol: 'tests',
-fn: function (){
-var self=this;
-var d,visited;
-function $Dictionary(){return smalltalk.Dictionary||(typeof Dictionary=="undefined"?nil:Dictionary)}
-return smalltalk.withContext(function($ctx1) { 
-visited=false;
-d=_st($Dictionary())._new();
-_st(d)._at_put_("hello","world");
-_st(d)._at_ifPresent_ifAbsent_("hello",(function(value){
-return smalltalk.withContext(function($ctx2) {
-visited=value;
-return visited;
-}, function($ctx2) {$ctx2.fillBlock({value:value},$ctx1,1)})}),(function(){
-return smalltalk.withContext(function($ctx2) {
-visited=true;
-return visited;
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)})}));
-$ctx1.sendIdx["at:ifPresent:ifAbsent:"]=1;
-self._assert_equals_(visited,"world");
-_st(d)._at_ifPresent_ifAbsent_("buy",(function(value){
-return smalltalk.withContext(function($ctx2) {
-visited=value;
-return visited;
-}, function($ctx2) {$ctx2.fillBlock({value:value},$ctx1,3)})}),(function(){
-return smalltalk.withContext(function($ctx2) {
-visited=true;
-return visited;
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1,4)})}));
-self._assert_(visited);
-return self}, function($ctx1) {$ctx1.fill(self,"testIfPresentIfAbsent",{d:d,visited:visited},smalltalk.DictionaryTest)})},
-args: [],
-source: "testIfPresentIfAbsent\x0a\x0a\x09| d visited |\x0a\x09visited := false.\x0a\x09d := Dictionary new.\x0a\x09d at: 'hello' put: 'world'.\x0a\x0a\x09d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].\x0a\x09self assert: visited equals: 'world'.\x0a\x0a\x09d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].\x0a\x09self assert: visited.",
-messageSends: ["new", "at:put:", "at:ifPresent:ifAbsent:", "assert:equals:", "assert:"],
-referencedClasses: ["Dictionary"]
-}),
-smalltalk.DictionaryTest);
-
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testKeys",

+ 26 - 28
st/Kernel-Tests.st

@@ -590,6 +590,32 @@ testAtIfAbsent
 		self assert: (self collection at: index ifAbsent: [ self sampleNonValue ]) equals: value ].
 !
 
+testAtIfPresent
+	| visited sentinel |
+	sentinel := Object new.
+	self nonIndexesDo: [ :each |
+		visited := nil.
+		self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ]) equals: nil.
+		self assert: visited isNil ].
+	self samplesDo: [ :index :value |
+		visited := nil.
+		self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ]) equals: sentinel.
+		self assert: visited equals: (self collection at: index) ]
+!
+
+testAtIfPresentIfAbsent
+	| visited sentinel |
+	sentinel := Object new.
+	self nonIndexesDo: [ :each |
+		visited := nil.
+		self assert: (self collection at: each ifPresent: [ :value1 | visited := value1. sentinel ] ifAbsent: [ self sampleNonValue ] ) equals: self sampleNonValue.
+		self assert: visited isNil ].
+	self samplesDo: [ :index :value |
+		visited := nil.
+		self assert: (self collection at: index ifPresent: [ :value2 | visited := value2. sentinel ] ifAbsent: [ self sampleNonValue ]) equals: sentinel.
+		self assert: visited equals: (self collection at: index) ]
+!
+
 testIndexOf
 	self should: [ self collection indexOf: self sampleNonValue ] raise: Error.
 	self samplesDo: [ :index :value |
@@ -732,34 +758,6 @@ testEquality
 	self deny: d1 = d2.
 !
 
-testIfPresent
-
-	| d visited absent |
-	visited := false.
-	d := Dictionary new.
-	d at: 'hello' put: 'world'.
-
-	d at: 'hello' ifPresent: [ :value | visited := value ].
-	self assert: visited equals: 'world'.
-
-	absent := d at: 'bye' ifPresent: [ :value | visited := value ].
-	self assert: absent isNil.
-!
-
-testIfPresentIfAbsent
-
-	| d visited |
-	visited := false.
-	d := Dictionary new.
-	d at: 'hello' put: 'world'.
-
-	d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
-	self assert: visited equals: 'world'.
-
-	d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
-	self assert: visited.
-!
-
 testKeys
 	| d |