1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Flags: --allow-natives-syntax 6 7 8 (function TestSetIterator() { 9 var s = new Set; 10 var iter = s.values(); 11 assertEquals('Set Iterator', %_ClassOf(iter)); 12 13 var SetIteratorPrototype = iter.__proto__; 14 assertFalse(SetIteratorPrototype.hasOwnProperty('constructor')); 15 assertEquals(SetIteratorPrototype.__proto__, Object.prototype); 16 17 var propertyNames = Object.getOwnPropertyNames(SetIteratorPrototype); 18 assertArrayEquals(['next'], propertyNames); 19 20 assertEquals(new Set().values().__proto__, SetIteratorPrototype); 21 assertEquals(new Set().entries().__proto__, SetIteratorPrototype); 22 })(); 23 24 25 (function TestSetIteratorValues() { 26 var s = new Set; 27 s.add(1); 28 s.add(2); 29 s.add(3); 30 var iter = s.values(); 31 32 assertEquals({value: 1, done: false}, iter.next()); 33 assertEquals({value: 2, done: false}, iter.next()); 34 assertEquals({value: 3, done: false}, iter.next()); 35 assertEquals({value: undefined, done: true}, iter.next()); 36 assertEquals({value: undefined, done: true}, iter.next()); 37 })(); 38 39 40 (function TestSetIteratorKeys() { 41 assertEquals(Set.prototype.keys, Set.prototype.values); 42 })(); 43 44 45 (function TestSetIteratorEntries() { 46 var s = new Set; 47 s.add(1); 48 s.add(2); 49 s.add(3); 50 var iter = s.entries(); 51 52 assertEquals({value: [1, 1], done: false}, iter.next()); 53 assertEquals({value: [2, 2], done: false}, iter.next()); 54 assertEquals({value: [3, 3], done: false}, iter.next()); 55 assertEquals({value: undefined, done: true}, iter.next()); 56 assertEquals({value: undefined, done: true}, iter.next()); 57 })(); 58 59 60 (function TestSetIteratorMutations() { 61 var s = new Set; 62 s.add(1); 63 var iter = s.values(); 64 assertEquals({value: 1, done: false}, iter.next()); 65 s.add(2); 66 s.add(3); 67 s.add(4); 68 s.add(5); 69 assertEquals({value: 2, done: false}, iter.next()); 70 s.delete(3); 71 assertEquals({value: 4, done: false}, iter.next()); 72 s.delete(5); 73 assertEquals({value: undefined, done: true}, iter.next()); 74 s.add(4); 75 assertEquals({value: undefined, done: true}, iter.next()); 76 })(); 77 78 79 (function TestSetInvalidReceiver() { 80 assertThrows(function() { 81 Set.prototype.values.call({}); 82 }, TypeError); 83 assertThrows(function() { 84 Set.prototype.entries.call({}); 85 }, TypeError); 86 })(); 87 88 89 (function TestSetIteratorInvalidReceiver() { 90 var iter = new Set().values(); 91 assertThrows(function() { 92 iter.next.call({}); 93 }); 94 })(); 95 96 97 (function TestSetIteratorSymbol() { 98 assertEquals(Set.prototype[Symbol.iterator], Set.prototype.values); 99 assertTrue(Set.prototype.hasOwnProperty(Symbol.iterator)); 100 assertFalse(Set.prototype.propertyIsEnumerable(Symbol.iterator)); 101 102 var iter = new Set().values(); 103 assertEquals(iter, iter[Symbol.iterator]()); 104 assertEquals(iter[Symbol.iterator].name, '[Symbol.iterator]'); 105 })(); 106 107 108 (function TestMapIterator() { 109 var m = new Map; 110 var iter = m.values(); 111 assertEquals('Map Iterator', %_ClassOf(iter)); 112 113 var MapIteratorPrototype = iter.__proto__; 114 assertFalse(MapIteratorPrototype.hasOwnProperty('constructor')); 115 assertEquals(MapIteratorPrototype.__proto__, Object.prototype); 116 117 var propertyNames = Object.getOwnPropertyNames(MapIteratorPrototype); 118 assertArrayEquals(['next'], propertyNames); 119 120 assertEquals(new Map().values().__proto__, MapIteratorPrototype); 121 assertEquals(new Map().keys().__proto__, MapIteratorPrototype); 122 assertEquals(new Map().entries().__proto__, MapIteratorPrototype); 123 })(); 124 125 126 (function TestMapIteratorValues() { 127 var m = new Map; 128 m.set(1, 11); 129 m.set(2, 22); 130 m.set(3, 33); 131 var iter = m.values(); 132 133 assertEquals({value: 11, done: false}, iter.next()); 134 assertEquals({value: 22, done: false}, iter.next()); 135 assertEquals({value: 33, done: false}, iter.next()); 136 assertEquals({value: undefined, done: true}, iter.next()); 137 assertEquals({value: undefined, done: true}, iter.next()); 138 })(); 139 140 141 (function TestMapIteratorKeys() { 142 var m = new Map; 143 m.set(1, 11); 144 m.set(2, 22); 145 m.set(3, 33); 146 var iter = m.keys(); 147 148 assertEquals({value: 1, done: false}, iter.next()); 149 assertEquals({value: 2, done: false}, iter.next()); 150 assertEquals({value: 3, done: false}, iter.next()); 151 assertEquals({value: undefined, done: true}, iter.next()); 152 assertEquals({value: undefined, done: true}, iter.next()); 153 })(); 154 155 156 (function TestMapIteratorEntries() { 157 var m = new Map; 158 m.set(1, 11); 159 m.set(2, 22); 160 m.set(3, 33); 161 var iter = m.entries(); 162 163 assertEquals({value: [1, 11], done: false}, iter.next()); 164 assertEquals({value: [2, 22], done: false}, iter.next()); 165 assertEquals({value: [3, 33], done: false}, iter.next()); 166 assertEquals({value: undefined, done: true}, iter.next()); 167 assertEquals({value: undefined, done: true}, iter.next()); 168 })(); 169 170 171 (function TestMapInvalidReceiver() { 172 assertThrows(function() { 173 Map.prototype.values.call({}); 174 }, TypeError); 175 assertThrows(function() { 176 Map.prototype.keys.call({}); 177 }, TypeError); 178 assertThrows(function() { 179 Map.prototype.entries.call({}); 180 }, TypeError); 181 })(); 182 183 184 (function TestMapIteratorInvalidReceiver() { 185 var iter = new Map().values(); 186 assertThrows(function() { 187 iter.next.call({}); 188 }, TypeError); 189 })(); 190 191 192 (function TestMapIteratorSymbol() { 193 assertEquals(Map.prototype[Symbol.iterator], Map.prototype.entries); 194 assertTrue(Map.prototype.hasOwnProperty(Symbol.iterator)); 195 assertFalse(Map.prototype.propertyIsEnumerable(Symbol.iterator)); 196 197 var iter = new Map().values(); 198 assertEquals(iter, iter[Symbol.iterator]()); 199 assertEquals(iter[Symbol.iterator].name, '[Symbol.iterator]'); 200 })(); 201