Browse Source

Fixed issue #181 + unit tests

Nicolas Petton 12 years ago
parent
commit
af2e579d59
6 changed files with 404 additions and 364 deletions
  1. 2 6
      js/Kernel-Collections.deploy.js
  2. 3 7
      js/Kernel-Collections.js
  3. 187 169
      js/Kernel-Tests.deploy.js
  4. 199 176
      js/Kernel-Tests.js
  5. 2 6
      st/Kernel-Collections.st
  6. 11 0
      st/Kernel-Tests.st

+ 2 - 6
js/Kernel-Collections.deploy.js

@@ -1389,12 +1389,8 @@ selector: "at:ifAbsent:",
 fn: function (anIndex, aBlock) {
 var self=this;
 
-	    var value = self[anIndex - 1];
-	    if(value === undefined) {
-		return aBlock();
-	    } else {
-		return value;
-	    }
+		if((anIndex < 1) || (self.length < anIndex)) {return aBlock()};
+		return self[anIndex - 1];
 	;
 return self;}
 }),

+ 3 - 7
js/Kernel-Collections.js

@@ -1956,16 +1956,12 @@ category: 'accessing',
 fn: function (anIndex, aBlock) {
 var self=this;
 
-	    var value = self[anIndex - 1];
-	    if(value === undefined) {
-		return aBlock();
-	    } else {
-		return value;
-	    }
+		if((anIndex < 1) || (self.length < anIndex)) {return aBlock()};
+		return self[anIndex - 1];
 	;
 return self;},
 args: ["anIndex", "aBlock"],
-source: "at: anIndex ifAbsent: aBlock\x0a\x09<\x0a\x09    var value = self[anIndex - 1];\x0a\x09    if(value === undefined) {\x0a\x09\x09return aBlock();\x0a\x09    } else {\x0a\x09\x09return value;\x0a\x09    }\x0a\x09>",
+source: "at: anIndex ifAbsent: aBlock\x0a\x09<\x0a\x09\x09if((anIndex < 1) || (self.length < anIndex)) {return aBlock()};\x0a\x09\x09return self[anIndex - 1];\x0a\x09>",
 messageSends: [],
 referencedClasses: []
 }),

File diff suppressed because it is too large
+ 187 - 169
js/Kernel-Tests.deploy.js


File diff suppressed because it is too large
+ 199 - 176
js/Kernel-Tests.js


+ 2 - 6
st/Kernel-Collections.st

@@ -739,12 +739,8 @@ SequenceableCollection subclass: #Array
 
 at: anIndex ifAbsent: aBlock
 	<
-	    var value = self[anIndex - 1];
-	    if(value === undefined) {
-		return aBlock();
-	    } else {
-		return value;
-	    }
+		if((anIndex < 1) || (self.length < anIndex)) {return aBlock()};
+		return self[anIndex - 1];
 	>
 !
 

+ 11 - 0
st/Kernel-Tests.st

@@ -5,6 +5,17 @@ TestCase subclass: #ArrayTest
 
 !ArrayTest methodsFor: 'testing'!
 
+testAtIfAbsent
+	| array |
+	array := #('hello' 'world').
+	self assert: (array at: 1) equals: 'hello'.
+	self assert: (array at: 2) equals: 'world'.
+	self assert: (array at: 2 ifAbsent: ['not found']) equals: 'world'.
+	self assert: (array at: 0 ifAbsent: ['not found']) equals: 'not found'.
+	self assert: (array at: -10 ifAbsent: ['not found']) equals: 'not found'.
+	self assert: (array at: 3 ifAbsent: ['not found']) equals: 'not found'.
+!
+
 testFirstN
 	self assert: {1. 2. 3} equals: ({1. 2. 3. 4. 5} first: 3).
 !

Some files were not shown because too many files changed in this diff