Chromium Code Reviews| Index: test/mjsunit/harmony/collections.js |
| diff --git a/test/mjsunit/harmony/collections.js b/test/mjsunit/harmony/collections.js |
| index edbdd41340c3249f178be6f19ae0c41a38600579..d2e471951c666f4c8805a8f73c160589c814ed13 100644 |
| --- a/test/mjsunit/harmony/collections.js |
| +++ b/test/mjsunit/harmony/collections.js |
| @@ -29,6 +29,10 @@ |
| // Flags: --expose-gc --allow-natives-syntax |
| +var originalSetPrototypeAdd = Set.prototype.add; |
|
rossberg
2014/06/23 14:16:00
Nit: I'd prefer to localise these declarations int
arv (Not doing code reviews)
2014/06/23 15:43:56
Done.
|
| +var originalMapPrototypeSet = Map.prototype.set; |
| + |
| + |
| // Test valid getter and setter calls on Sets and WeakSets |
| function TestValidSetCalls(m) { |
| assertDoesNotThrow(function () { m.add(new Object) }); |
| @@ -987,3 +991,284 @@ for (var i = 9; i >= 0; i--) { |
| assertArrayEquals([0, 1, 2, 3, 4], buffer); |
| })(); |
| + |
| + |
| +(function TestSetConstructor() { |
| + var s = new Set(null); |
| + assertEquals(s.size, 0); |
| + |
| + s = new Set(undefined); |
| + assertEquals(s.size, 0); |
| + |
| + // No @@iterator |
| + assertThrows(function() { |
| + new Set({}); |
| + }, TypeError); |
| + |
| + // @@iterator not callable |
| + assertThrows(function() { |
| + var object = {}; |
| + object[Symbol.iterator] = 42; |
| + new Set(object); |
| + }, TypeError); |
| + |
| + // @@iterator result not object |
| + assertThrows(function() { |
| + var object = {}; |
| + object[Symbol.iterator] = function() { |
| + return 42; |
| + }; |
| + new Set(object); |
| + }, TypeError); |
| + |
| + var s2 = new Set(); |
| + s2.add('a'); |
| + s2.add('b'); |
| + s2.add('c'); |
| + s = new Set(s2.values()); |
| + assertEquals(s.size, 3); |
| + assertTrue(s.has('a')); |
| + assertTrue(s.has('b')); |
| + assertTrue(s.has('c')); |
| +})(); |
| + |
| + |
| +(function TestSetConstructorAddNotCallable() { |
| + var twoSet = new Set(); |
|
rossberg
2014/06/23 14:16:00
Nit: can't you simply use the array [1, 2] here? (
arv (Not doing code reviews)
2014/06/23 15:43:56
OK. I didn't do that because I didn't want a depen
|
| + twoSet.add(1); |
| + twoSet.add(2); |
| + |
| + assertThrows(function() { |
| + Set.prototype.add = 42; |
| + new Set(twoSet); |
| + }, TypeError); |
| + Set.prototype.add = originalSetPrototypeAdd; |
| +})(); |
| + |
| + |
| +(function TestSetConstructorGetAddOnce() { |
| + var twoSet = new Set(); |
| + twoSet.add(1); |
| + twoSet.add(2); |
| + |
| + var getAddCount = 0; |
| + Object.defineProperty(Set.prototype, 'add', { |
| + get: function() { |
| + getAddCount++; |
| + return function() {}; |
| + } |
| + }); |
| + var s = new Set(twoSet); |
| + assertEquals(getAddCount, 1); |
| + assertEquals(s.size, 0); |
| + Object.defineProperty(Set.prototype, 'add', { |
| + value: originalSetPrototypeAdd, |
| + writable: true |
| + }); |
| +})(); |
| + |
| + |
| +(function TestSetConstructorAddReplaced() { |
| + var twoSet = new Set(); |
| + twoSet.add(1); |
| + twoSet.add(2); |
| + |
| + var addCount = 0; |
| + Set.prototype.add = function(value) { |
| + addCount++; |
| + originalSetPrototypeAdd.call(this, value); |
| + Set.prototype.add = null; |
| + }; |
| + |
| + var s = new Set(twoSet); |
| + assertEquals(addCount, 2); |
| + assertEquals(s.size, 2); |
| + Set.prototype.add = originalSetPrototypeAdd; |
| +})(); |
| + |
| + |
| +(function TestSetConstructorOrderOfDoneValue() { |
| + var valueCount = 0, doneCount = 0; |
| + var iterator = { |
| + next: function() { |
| + return { |
| + get value() { |
| + valueCount++; |
| + }, |
| + get done() { |
| + doneCount++; |
| + throw new Error(); |
| + } |
| + }; |
| + } |
| + }; |
| + iterator[Symbol.iterator] = function() { |
| + return this; |
| + }; |
| + assertThrows(function() { |
| + new Set(iterator); |
| + }); |
| + assertEquals(doneCount, 1); |
| + assertEquals(valueCount, 0); |
| +})(); |
| + |
| + |
| +(function TestSetConstructorNextNotAnObject() { |
| + var iterator = { |
| + next: function() { |
| + return 'abc'; |
| + } |
| + }; |
| + iterator[Symbol.iterator] = function() { |
| + return this; |
| + }; |
| + assertThrows(function() { |
| + new Set(iterator); |
| + }); |
|
rossberg
2014/06/23 14:16:00
Assert TypeError, specifically.
arv (Not doing code reviews)
2014/06/23 15:43:57
Done.
|
| +})(); |
| + |
| + |
| +(function TestMapConstructor() { |
| + var m = new Map(null); |
| + assertEquals(m.size, 0); |
| + |
| + m = new Map(undefined); |
| + assertEquals(m.size, 0); |
| + |
| + // No @@iterator |
| + assertThrows(function() { |
| + new Map({}); |
| + }, TypeError); |
| + |
| + // @@iterator not callable |
| + assertThrows(function() { |
| + var object = {}; |
| + object[Symbol.iterator] = 42; |
| + new Map(object); |
| + }, TypeError); |
| + |
| + // @@iterator result not object |
| + assertThrows(function() { |
| + var object = {}; |
| + object[Symbol.iterator] = function() { |
| + return 42; |
| + }; |
| + new Map(object); |
| + }, TypeError); |
| + |
| + var m2 = new Map(); |
| + m2.set(0, 'a'); |
| + m2.set(1, 'b'); |
| + m2.set(2, 'c'); |
| + m = new Map(m2.entries()); |
| + assertEquals(m.size, 3); |
| + assertEquals(m.get(0), 'a'); |
| + assertEquals(m.get(1), 'b'); |
| + assertEquals(m.get(2), 'c'); |
| +})(); |
| + |
| + |
| +(function TestMapConstructorSetNotCallable() { |
| + var twoMap = new Map(); |
| + twoMap.set(1, 1); |
| + twoMap.set(2, 2); |
| + |
| + assertThrows(function() { |
| + Map.prototype.set = 42; |
| + new Map(twoMap); |
| + }, TypeError); |
| + Map.prototype.set = originalMapPrototypeSet; |
| +})(); |
| + |
| + |
| +(function TestMapConstructorGetAddOnce() { |
| + var twoMap = new Map(); |
| + twoMap.set(1, 1); |
| + twoMap.set(2, 1); |
| + |
| + var getSetCount = 0; |
| + Object.defineProperty(Map.prototype, 'set', { |
| + get: function() { |
| + getSetCount++; |
| + return function() {}; |
| + } |
| + }); |
| + var m = new Map(twoMap); |
| + assertEquals(getSetCount, 1); |
| + assertEquals(m.size, 0); |
| + Object.defineProperty(Map.prototype, 'set', { |
| + value: originalMapPrototypeSet, |
| + writable: true |
| + }); |
| +})(); |
| + |
| + |
| +(function TestMapConstructorSetReplaced() { |
| + var twoMap = new Map(); |
| + twoMap.set(1, 1); |
| + twoMap.set(2, 2); |
| + |
| + var setCount = 0; |
| + Map.prototype.set = function(key, value) { |
| + setCount++; |
| + originalMapPrototypeSet.call(this, key, value); |
| + Map.prototype.set = null; |
| + }; |
| + |
| + var m = new Map(twoMap); |
| + assertEquals(setCount, 2); |
| + assertEquals(m.size, 2); |
| + Map.prototype.set = originalMapPrototypeSet; |
| +})(); |
| + |
| + |
| +(function TestMapConstructorOrderOfDoneValue() { |
| + var valueCount = 0, doneCount = 0; |
| + var iterator = { |
| + next: function() { |
| + return { |
| + get value() { |
| + valueCount++; |
| + }, |
| + get done() { |
| + doneCount++; |
| + throw new Error(); |
| + } |
| + }; |
| + } |
| + }; |
| + iterator[Symbol.iterator] = function() { |
| + return this; |
| + }; |
| + assertThrows(function() { |
| + new Map(iterator); |
| + }); |
| + assertEquals(doneCount, 1); |
| + assertEquals(valueCount, 0); |
| +})(); |
| + |
| + |
| +(function TestMapConstructorNextNotAnObject() { |
| + var iterator = { |
| + next: function() { |
| + return 'abc'; |
| + } |
| + }; |
| + iterator[Symbol.iterator] = function() { |
| + return this; |
| + }; |
| + assertThrows(function() { |
| + new Map(iterator); |
| + }); |
| +})(); |
| + |
| + |
| +(function TestMapConstructorIteratorNotObjectValues() { |
| + var twoMap = new Map(); |
| + twoMap.set(1, 1); |
| + twoMap.set(2, 1); |
| + |
| + assertThrows(function() { |
| + new Map(twoMap.values()); |
| + }); |
| +})(); |