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 --harmony-tostring 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 assertEquals("[object Set Iterator]", 24 Object.prototype.toString.call(iter)); 25 assertEquals("Set Iterator", SetIteratorPrototype[Symbol.toStringTag]); 26 var desc = Object.getOwnPropertyDescriptor( 27 SetIteratorPrototype, Symbol.toStringTag); 28 assertTrue(desc.configurable); 29 assertFalse(desc.writable); 30 assertEquals("Set Iterator", desc.value); 31 })(); 32 33 34 (function TestSetIteratorValues() { 35 var s = new Set; 36 s.add(1); 37 s.add(2); 38 s.add(3); 39 var iter = s.values(); 40 41 assertEquals({value: 1, done: false}, iter.next()); 42 assertEquals({value: 2, done: false}, iter.next()); 43 assertEquals({value: 3, done: false}, iter.next()); 44 assertEquals({value: undefined, done: true}, iter.next()); 45 assertEquals({value: undefined, done: true}, iter.next()); 46 })(); 47 48 49 (function TestSetIteratorKeys() { 50 assertEquals(Set.prototype.keys, Set.prototype.values); 51 })(); 52 53 54 (function TestSetIteratorEntries() { 55 var s = new Set; 56 s.add(1); 57 s.add(2); 58 s.add(3); 59 var iter = s.entries(); 60 61 assertEquals({value: [1, 1], done: false}, iter.next()); 62 assertEquals({value: [2, 2], done: false}, iter.next()); 63 assertEquals({value: [3, 3], done: false}, iter.next()); 64 assertEquals({value: undefined, done: true}, iter.next()); 65 assertEquals({value: undefined, done: true}, iter.next()); 66 })(); 67 68 69 (function TestSetIteratorMutations() { 70 var s = new Set; 71 s.add(1); 72 var iter = s.values(); 73 assertEquals({value: 1, done: false}, iter.next()); 74 s.add(2); 75 s.add(3); 76 s.add(4); 77 s.add(5); 78 assertEquals({value: 2, done: false}, iter.next()); 79 s.delete(3); 80 assertEquals({value: 4, done: false}, iter.next()); 81 s.delete(5); 82 assertEquals({value: undefined, done: true}, iter.next()); 83 s.add(4); 84 assertEquals({value: undefined, done: true}, iter.next()); 85 })(); 86 87 88 (function TestSetInvalidReceiver() { 89 assertThrows(function() { 90 Set.prototype.values.call({}); 91 }, TypeError); 92 assertThrows(function() { 93 Set.prototype.entries.call({}); 94 }, TypeError); 95 })(); 96 97 98 (function TestSetIteratorInvalidReceiver() { 99 var iter = new Set().values(); 100 assertThrows(function() { 101 iter.next.call({}); 102 }); 103 })(); 104 105 106 (function TestSetIteratorSymbol() { 107 assertEquals(Set.prototype[Symbol.iterator], Set.prototype.values); 108 assertTrue(Set.prototype.hasOwnProperty(Symbol.iterator)); 109 assertFalse(Set.prototype.propertyIsEnumerable(Symbol.iterator)); 110 111 var iter = new Set().values(); 112 assertEquals(iter, iter[Symbol.iterator]()); 113 assertEquals(iter[Symbol.iterator].name, '[Symbol.iterator]'); 114 })(); 115 116 117 (function TestMapIterator() { 118 var m = new Map; 119 var iter = m.values(); 120 assertEquals('Map Iterator', %_ClassOf(iter)); 121 122 var MapIteratorPrototype = iter.__proto__; 123 assertFalse(MapIteratorPrototype.hasOwnProperty('constructor')); 124 assertEquals(MapIteratorPrototype.__proto__, Object.prototype); 125 126 var propertyNames = Object.getOwnPropertyNames(MapIteratorPrototype); 127 assertArrayEquals(['next'], propertyNames); 128 129 assertEquals(new Map().values().__proto__, MapIteratorPrototype); 130 assertEquals(new Map().keys().__proto__, MapIteratorPrototype); 131 assertEquals(new Map().entries().__proto__, MapIteratorPrototype); 132 133 assertEquals("[object Map Iterator]", 134 Object.prototype.toString.call(iter)); 135 assertEquals("Map Iterator", MapIteratorPrototype[Symbol.toStringTag]); 136 var desc = Object.getOwnPropertyDescriptor( 137 MapIteratorPrototype, Symbol.toStringTag); 138 assertTrue(desc.configurable); 139 assertFalse(desc.writable); 140 assertEquals("Map Iterator", desc.value); 141 })(); 142 143 144 (function TestMapIteratorValues() { 145 var m = new Map; 146 m.set(1, 11); 147 m.set(2, 22); 148 m.set(3, 33); 149 var iter = m.values(); 150 151 assertEquals({value: 11, done: false}, iter.next()); 152 assertEquals({value: 22, done: false}, iter.next()); 153 assertEquals({value: 33, done: false}, iter.next()); 154 assertEquals({value: undefined, done: true}, iter.next()); 155 assertEquals({value: undefined, done: true}, iter.next()); 156 })(); 157 158 159 (function TestMapIteratorKeys() { 160 var m = new Map; 161 m.set(1, 11); 162 m.set(2, 22); 163 m.set(3, 33); 164 var iter = m.keys(); 165 166 assertEquals({value: 1, done: false}, iter.next()); 167 assertEquals({value: 2, done: false}, iter.next()); 168 assertEquals({value: 3, done: false}, iter.next()); 169 assertEquals({value: undefined, done: true}, iter.next()); 170 assertEquals({value: undefined, done: true}, iter.next()); 171 })(); 172 173 174 (function TestMapIteratorEntries() { 175 var m = new Map; 176 m.set(1, 11); 177 m.set(2, 22); 178 m.set(3, 33); 179 var iter = m.entries(); 180 181 assertEquals({value: [1, 11], done: false}, iter.next()); 182 assertEquals({value: [2, 22], done: false}, iter.next()); 183 assertEquals({value: [3, 33], done: false}, iter.next()); 184 assertEquals({value: undefined, done: true}, iter.next()); 185 assertEquals({value: undefined, done: true}, iter.next()); 186 })(); 187 188 189 (function TestMapInvalidReceiver() { 190 assertThrows(function() { 191 Map.prototype.values.call({}); 192 }, TypeError); 193 assertThrows(function() { 194 Map.prototype.keys.call({}); 195 }, TypeError); 196 assertThrows(function() { 197 Map.prototype.entries.call({}); 198 }, TypeError); 199 })(); 200 201 202 (function TestMapIteratorInvalidReceiver() { 203 var iter = new Map().values(); 204 assertThrows(function() { 205 iter.next.call({}); 206 }, TypeError); 207 })(); 208 209 210 (function TestMapIteratorSymbol() { 211 assertEquals(Map.prototype[Symbol.iterator], Map.prototype.entries); 212 assertTrue(Map.prototype.hasOwnProperty(Symbol.iterator)); 213 assertFalse(Map.prototype.propertyIsEnumerable(Symbol.iterator)); 214 215 var iter = new Map().values(); 216 assertEquals(iter, iter[Symbol.iterator]()); 217 assertEquals(iter[Symbol.iterator].name, '[Symbol.iterator]'); 218 })(); 219