Skip to content

Commit

Permalink
fix for Map/Set working with ConsString as key also; closes #583
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri authored and gbrail committed Jul 24, 2019
1 parent 300d6df commit a27fac1
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/org/mozilla/javascript/Hashtable.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public static final class Entry
if ((k instanceof Number) && ( ! ( k instanceof Double))) {
// Hash comparison won't work if we don't do this
this.key = ((Number)k).doubleValue();
} else if (k instanceof ConsString) {
this.key = k.toString();
} else {
this.key = k;
}
Expand Down
42 changes: 42 additions & 0 deletions testsrc/jstests/es6/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,46 @@ function logElement(value, key, m) {
assertEquals("c) map[key1] = '' map[key2] = 'undefined' map[key3] = 'undefined' map[key4] = 'null' ", res);
})();

(function TestGetConcatenatedStrings() {
var myMap = new Map([['key1', 'value1'], ['key2', 17]]);
for(let i = 1; i <= 1; i++) {
let key = 'key' + i;

assertEquals("value1", myMap.get(key));
assertEquals("value1", myMap.get('key1'));
}
})();

(function TestSetConcatenatedStrings() {
var myMap = new Map([['key1', 'value1'], ['key2', 17]]);
for(let i = 1; i <= 1; i++) {
let key = 'key' + i;

myMap.set(key, 'value2')
assertEquals("value2", myMap.get(key));
assertEquals("value2", myMap.get('key1'));
}
})();

(function TestHasConcatenatedStrings() {
var myMap = new Map([['key1', 'value1'], ['key2', 17]]);
for(let i = 1; i <= 1; i++) {
let key = 'key' + i;

assertTrue(myMap.has(key));
assertTrue(myMap.has('key1'));
}
})();

(function TestDeleteConcatenatedStrings() {
var myMap = new Map([['key1', 'value1'], ['key2', 17]]);
for(let i = 1; i <= 1; i++) {
let key = 'key' + i;

assertEquals(2, myMap.size);
myMap.delete(key)
assertEquals(1, myMap.size);
}
})();

"success";
66 changes: 66 additions & 0 deletions testsrc/jstests/es6/set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

load("testsrc/assert.js");

res = "";

function logElement(value, key) {
res += "set[" + key + "] ";
}

(function TestForEach() {
res = "a) ";
var mySet = new Set(['key1', 17]);
mySet.forEach(logElement);

assertEquals("a) set[key1] set[17] ", res);
})();

(function TestForEachNoKey() {
res = "b) ";
var mySet = new Set(['', undefined, null, 19]);
mySet.forEach(logElement);

assertEquals("b) set[] set[undefined] set[null] set[19] ", res);
})();

(function TestAddConcatenatedStrings() {
var mySet = new Set(['key1']);
var mySet2 = new Set(['key2']);
for(let i = 1; i <= 1; i++) {
let key = 'key' + i;

assertEquals(1, mySet.size);
mySet.add(key)
assertEquals(1, mySet.size);

assertEquals(1, mySet2.size);
mySet2.add(key)
assertEquals(2, mySet2.size);
}
})();

(function TestHasConcatenatedStrings() {
var mySet = new Set(['key1', 'key2']);
for(let i = 1; i <= 1; i++) {
let key = 'key' + i;

assertTrue(mySet.has(key));
assertTrue(mySet.has('key1'));
}
})();

(function TestDeleteConcatenatedStrings() {
var mySet = new Set(['key1', 'key2']);
for(let i = 1; i <= 1; i++) {
let key = 'key' + i;

assertEquals(2, mySet.size);
mySet.delete(key)
assertEquals(1, mySet.size);
}
})();

"success";
16 changes: 16 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/NativeSetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.javascript.tests.es6;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.drivers.LanguageVersion;
import org.mozilla.javascript.drivers.RhinoTest;
import org.mozilla.javascript.drivers.ScriptTestsBase;

@RhinoTest("testsrc/jstests/es6/set.js")
@LanguageVersion(Context.VERSION_ES6)
public class NativeSetTest extends ScriptTestsBase
{
}

0 comments on commit a27fac1

Please sign in to comment.