1
0
ソースを参照

direct fix of Issue #147 in my master, because Issue147 branch is using the non-canonical fileout methods and merging is a nightmare

Dale Henrichs 12 年 前
コミット
1bbfe074eb

+ 15 - 14
js/Kernel-Collections.deploy.js

@@ -818,9 +818,9 @@ smalltalk.addMethod(
 unescape('_removeKey_'),
 smalltalk.method({
 selector: unescape('removeKey%3A'),
-fn: function (aKey) {
+fn: function (aKey){
 var self=this;
-smalltalk.send(self, "_remove_", [aKey]);
+return smalltalk.send(self, "_remove_", [aKey]);
 return self;}
 }),
 smalltalk.HashedCollection);
@@ -1019,18 +1019,19 @@ smalltalk.addMethod(
 unescape('_removeKey_ifAbsent_'),
 smalltalk.method({
 selector: unescape('removeKey%3AifAbsent%3A'),
-fn: function (aKey, aBlock) {
-var self=this;
-
-		var index = self['@keys'].indexOf(aKey);
-		if(index === -1) {
-			return aBlock()
-		} else {
-			self['@keys'].splice(i, 1);
-			self['@values'].splice(i, 1);
-			return aKey
-		};
-	;
+fn: function (aKey, aBlock){
+var self=this;
+
+            var index = self['@keys'].indexOf(aKey);
+            if(index === -1) {
+                return aBlock()
+            } else {
+                var value;
+                self['@keys'].splice(index, 1);
+                value = self['@values'].splice(index, 1);
+                return value[0];
+            };
+    ;
 return self;}
 }),
 smalltalk.Dictionary);

+ 15 - 14
js/Kernel-Collections.js

@@ -1165,12 +1165,12 @@ unescape('_removeKey_'),
 smalltalk.method({
 selector: unescape('removeKey%3A'),
 category: 'adding/removing',
-fn: function (aKey) {
+fn: function (aKey){
 var self=this;
-smalltalk.send(self, "_remove_", [aKey]);
+return smalltalk.send(self, "_remove_", [aKey]);
 return self;},
 args: ["aKey"],
-source: unescape('removeKey%3A%20aKey%0A%20%20%20%20self%20remove%3A%20aKey'),
+source: unescape('removeKey%3A%20aKey%0A%20%20%20%20%5Eself%20remove%3A%20aKey'),
 messageSends: ["remove:"],
 referencedClasses: []
 }),
@@ -1441,21 +1441,22 @@ unescape('_removeKey_ifAbsent_'),
 smalltalk.method({
 selector: unescape('removeKey%3AifAbsent%3A'),
 category: 'adding/removing',
-fn: function (aKey, aBlock) {
+fn: function (aKey, aBlock){
 var self=this;
 
-		var index = self['@keys'].indexOf(aKey);
-		if(index === -1) {
-			return aBlock()
-		} else {
-			self['@keys'].splice(i, 1);
-			self['@values'].splice(i, 1);
-			return aKey
-		};
-	;
+            var index = self['@keys'].indexOf(aKey);
+            if(index === -1) {
+                return aBlock()
+            } else {
+                var value;
+                self['@keys'].splice(index, 1);
+                value = self['@values'].splice(index, 1);
+                return value[0];
+            };
+    ;
 return self;},
 args: ["aKey", "aBlock"],
-source: unescape('removeKey%3A%20aKey%20ifAbsent%3A%20aBlock%0A%09%3C%0A%09%09var%20index%20%3D%20self%5B%27@keys%27%5D.indexOf%28aKey%29%3B%0A%09%09if%28index%20%3D%3D%3D%20-1%29%20%7B%0A%09%09%09return%20aBlock%28%29%0A%09%09%7D%20else%20%7B%0A%09%09%09self%5B%27@keys%27%5D.splice%28i%2C%201%29%3B%0A%09%09%09self%5B%27@values%27%5D.splice%28i%2C%201%29%3B%0A%09%09%09return%20aKey%0A%09%09%7D%3B%0A%09%3E'),
+source: unescape('removeKey%3A%20aKey%20ifAbsent%3A%20aBlock%0A%20%20%20%20%3C%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20index%20%3D%20self%5B%27@keys%27%5D.indexOf%28aKey%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%28index%20%3D%3D%3D%20-1%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20aBlock%28%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20value%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self%5B%27@keys%27%5D.splice%28index%2C%201%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%20self%5B%27@values%27%5D.splice%28index%2C%201%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20value%5B0%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%20%20%20%3E'),
 messageSends: [],
 referencedClasses: []
 }),

+ 44 - 0
js/Kernel-Tests.deploy.js

@@ -335,6 +335,50 @@ return self;}
 }),
 smalltalk.DictionaryTest);
 
+smalltalk.addMethod(
+unescape('_testRemoveKey'),
+smalltalk.method({
+selector: unescape('testRemoveKey'),
+fn: function (){
+var self=this;
+var d=nil;
+var key=nil;
+(d=smalltalk.send((smalltalk.Dictionary || Dictionary), "_new", []));
+smalltalk.send(d, "_at_put_", [(1), (2)]);
+smalltalk.send(d, "_at_put_", [(2), (3)]);
+smalltalk.send(d, "_at_put_", [(3), (4)]);
+(key=(2));
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_keys", []), "__eq", [[(1), (2), (3)]])]);
+smalltalk.send(d, "_removeKey_", [key]);
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_keys", []), "__eq", [[(1), (3)]])]);
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_values", []), "__eq", [[(2), (4)]])]);
+smalltalk.send(self, "_deny_", [smalltalk.send(d, "_includesKey_", [(2)])]);
+return self;}
+}),
+smalltalk.DictionaryTest);
+
+smalltalk.addMethod(
+unescape('_testRemoveKeyIfAbsent'),
+smalltalk.method({
+selector: unescape('testRemoveKeyIfAbsent'),
+fn: function (){
+var self=this;
+var d=nil;
+var key=nil;
+(d=smalltalk.send((smalltalk.Dictionary || Dictionary), "_new", []));
+smalltalk.send(d, "_at_put_", [(1), (2)]);
+smalltalk.send(d, "_at_put_", [(2), (3)]);
+smalltalk.send(d, "_at_put_", [(3), (4)]);
+(key=(2));
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_removeKey_", [key]), "__eq", [(3)])]);
+(key=(3));
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_removeKey_ifAbsent_", [key, (function(){return (42);})]), "__eq", [(4)])]);
+(key="why");
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_removeKey_ifAbsent_", [key, (function(){return (42);})]), "__eq", [(42)])]);
+return self;}
+}),
+smalltalk.DictionaryTest);
+
 smalltalk.addMethod(
 unescape('_testSize'),
 smalltalk.method({

+ 54 - 0
js/Kernel-Tests.js

@@ -450,6 +450,60 @@ referencedClasses: ["Dictionary"]
 }),
 smalltalk.DictionaryTest);
 
+smalltalk.addMethod(
+unescape('_testRemoveKey'),
+smalltalk.method({
+selector: unescape('testRemoveKey'),
+category: 'tests',
+fn: function (){
+var self=this;
+var d=nil;
+var key=nil;
+(d=smalltalk.send((smalltalk.Dictionary || Dictionary), "_new", []));
+smalltalk.send(d, "_at_put_", [(1), (2)]);
+smalltalk.send(d, "_at_put_", [(2), (3)]);
+smalltalk.send(d, "_at_put_", [(3), (4)]);
+(key=(2));
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_keys", []), "__eq", [[(1), (2), (3)]])]);
+smalltalk.send(d, "_removeKey_", [key]);
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_keys", []), "__eq", [[(1), (3)]])]);
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_values", []), "__eq", [[(2), (4)]])]);
+smalltalk.send(self, "_deny_", [smalltalk.send(d, "_includesKey_", [(2)])]);
+return self;},
+args: [],
+source: unescape('testRemoveKey%0A%20%20%20%20%7C%20d%20key%20%7C%0A%0A%20%20%20%20d%20%3A%3D%20Dictionary%20new.%0A%20%20%20%20d%20at%3A%201%20put%3A%202.%0A%20%20%20%20d%20at%3A%202%20put%3A%203.%0A%20%20%20%20d%20at%3A%203%20put%3A%204.%0A%0A%20%20%20%20key%20%3A%3D%202.%0A%0A%20%20%20%20self%20assert%3A%20d%20keys%20%3D%20%23%281%202%203%29.%0A%0A%20%20%20%20d%20removeKey%3A%20key.%0A%20%20%20%20self%20assert%3A%20d%20keys%20%3D%20%23%281%203%29.%0A%20%20%20%20self%20assert%3A%20d%20values%20%3D%20%23%282%204%29.%0A%20%20%20%20self%20deny%3A%20%28d%20includesKey%3A%202%29'),
+messageSends: ["new", "at:put:", "assert:", unescape("%3D"), "keys", "removeKey:", "values", "deny:", "includesKey:"],
+referencedClasses: ["Dictionary"]
+}),
+smalltalk.DictionaryTest);
+
+smalltalk.addMethod(
+unescape('_testRemoveKeyIfAbsent'),
+smalltalk.method({
+selector: unescape('testRemoveKeyIfAbsent'),
+category: 'tests',
+fn: function (){
+var self=this;
+var d=nil;
+var key=nil;
+(d=smalltalk.send((smalltalk.Dictionary || Dictionary), "_new", []));
+smalltalk.send(d, "_at_put_", [(1), (2)]);
+smalltalk.send(d, "_at_put_", [(2), (3)]);
+smalltalk.send(d, "_at_put_", [(3), (4)]);
+(key=(2));
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_removeKey_", [key]), "__eq", [(3)])]);
+(key=(3));
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_removeKey_ifAbsent_", [key, (function(){return (42);})]), "__eq", [(4)])]);
+(key="why");
+smalltalk.send(self, "_assert_", [smalltalk.send(smalltalk.send(d, "_removeKey_ifAbsent_", [key, (function(){return (42);})]), "__eq", [(42)])]);
+return self;},
+args: [],
+source: unescape('testRemoveKeyIfAbsent%0A%20%20%20%20%7C%20d%20key%20%7C%0A%0A%20%20%20%20d%20%3A%3D%20Dictionary%20new.%0A%20%20%20%20d%20at%3A%201%20put%3A%202.%0A%20%20%20%20d%20at%3A%202%20put%3A%203.%0A%20%20%20%20d%20at%3A%203%20put%3A%204.%0A%0A%20%20%20%20key%20%3A%3D%202.%0A%20%20%20%20self%20assert%3A%20%28d%20removeKey%3A%20key%29%20%3D%203.%0A%0A%20%20%20%20key%20%3A%3D%203.%0A%20%20%20%20self%20assert%3A%20%28d%20removeKey%3A%20key%20ifAbsent%3A%20%5B42%5D%29%20%3D%204.%0A%0A%20%20%20%20key%20%3A%3D%20%27why%27.%0A%20%20%20%20self%20assert%3A%20%28d%20removeKey%3A%20key%20ifAbsent%3A%20%5B42%5D%20%29%20%3D%2042.'),
+messageSends: ["new", "at:put:", "assert:", unescape("%3D"), "removeKey:", "removeKey:ifAbsent:"],
+referencedClasses: ["Dictionary"]
+}),
+smalltalk.DictionaryTest);
+
 smalltalk.addMethod(
 unescape('_testSize'),
 smalltalk.method({

+ 12 - 11
st/Kernel-Collections.st

@@ -353,7 +353,7 @@ remove: aKey ifAbsent: aBlock
 !
 
 removeKey: aKey
-    self remove: aKey
+    ^self remove: aKey
 !
 
 removeKey: aKey ifAbsent: aBlock
@@ -534,16 +534,17 @@ values
 !Dictionary methodsFor: 'adding/removing'!
 
 removeKey: aKey ifAbsent: aBlock
-	<
-		var index = self['@keys'].indexOf(aKey);
-		if(index === -1) {
-			return aBlock()
-		} else {
-			self['@keys'].splice(i, 1);
-			self['@values'].splice(i, 1);
-			return aKey
-		};
-	>
+    <
+            var index = self['@keys'].indexOf(aKey);
+            if(index === -1) {
+                return aBlock()
+            } else {
+                var value;
+                self['@keys'].splice(index, 1);
+                value = self['@values'].splice(index, 1);
+                return value[0];
+            };
+    >
 ! !
 
 !Dictionary methodsFor: 'converting'!

+ 36 - 0
st/Kernel-Tests.st

@@ -241,6 +241,42 @@ testPrintString
                         	printString)
 !
 
+testRemoveKey
+    | d key |
+
+    d := Dictionary new.
+    d at: 1 put: 2.
+    d at: 2 put: 3.
+    d at: 3 put: 4.
+
+    key := 2.
+
+    self assert: d keys = #(1 2 3).
+
+    d removeKey: key.
+    self assert: d keys = #(1 3).
+    self assert: d values = #(2 4).
+    self deny: (d includesKey: 2)
+!
+
+testRemoveKeyIfAbsent
+    | d key |
+
+    d := Dictionary new.
+    d at: 1 put: 2.
+    d at: 2 put: 3.
+    d at: 3 put: 4.
+
+    key := 2.
+    self assert: (d removeKey: key) = 3.
+
+    key := 3.
+    self assert: (d removeKey: key ifAbsent: [42]) = 4.
+
+    key := 'why'.
+    self assert: (d removeKey: key ifAbsent: [42] ) = 42.
+!
+
 testSize
 	| d |