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 6 function TestStringPrototypeIterator() { 7 assertTrue(String.prototype.hasOwnProperty(Symbol.iterator)); 8 assertFalse("".hasOwnProperty(Symbol.iterator)); 9 assertFalse("".propertyIsEnumerable(Symbol.iterator)); 10 } 11 TestStringPrototypeIterator(); 12 13 14 function assertIteratorResult(value, done, result) { 15 assertEquals({value: value, done: done}, result); 16 } 17 18 19 function TestManualIteration() { 20 var string = "abc"; 21 var iterator = string[Symbol.iterator](); 22 assertIteratorResult('a', false, iterator.next()); 23 assertIteratorResult('b', false, iterator.next()); 24 assertIteratorResult('c', false, iterator.next()); 25 assertIteratorResult(void 0, true, iterator.next()); 26 assertIteratorResult(void 0, true, iterator.next()); 27 } 28 TestManualIteration(); 29 30 31 function TestSurrogatePairs() { 32 var lo = "\uD834"; 33 var hi = "\uDF06"; 34 var pair = lo + hi; 35 var string = "abc" + pair + "def" + lo + pair + hi + lo; 36 var iterator = string[Symbol.iterator](); 37 assertIteratorResult('a', false, iterator.next()); 38 assertIteratorResult('b', false, iterator.next()); 39 assertIteratorResult('c', false, iterator.next()); 40 assertIteratorResult(pair, false, iterator.next()); 41 assertIteratorResult('d', false, iterator.next()); 42 assertIteratorResult('e', false, iterator.next()); 43 assertIteratorResult('f', false, iterator.next()); 44 assertIteratorResult(lo, false, iterator.next()); 45 assertIteratorResult(pair, false, iterator.next()); 46 assertIteratorResult(hi, false, iterator.next()); 47 assertIteratorResult(lo, false, iterator.next()); 48 assertIteratorResult(void 0, true, iterator.next()); 49 assertIteratorResult(void 0, true, iterator.next()); 50 } 51 TestSurrogatePairs(); 52 53 54 function TestStringIteratorPrototype() { 55 var iterator = ""[Symbol.iterator](); 56 var StringIteratorPrototype = iterator.__proto__; 57 assertFalse(StringIteratorPrototype.hasOwnProperty('constructor')); 58 assertEquals(StringIteratorPrototype.__proto__, Object.prototype); 59 assertArrayEquals(['next'], 60 Object.getOwnPropertyNames(StringIteratorPrototype)); 61 assertEquals('[object String Iterator]', "" + iterator); 62 } 63 TestStringIteratorPrototype(); 64 65 66 function TestForOf() { 67 var lo = "\uD834"; 68 var hi = "\uDF06"; 69 var pair = lo + hi; 70 var string = "abc" + pair + "def" + lo + pair + hi + lo; 71 var expected = ['a', 'b', 'c', pair, 'd', 'e', 'f', lo, pair, hi, lo]; 72 73 var i = 0; 74 for (var char of string) { 75 assertEquals(expected[i++], char); 76 } 77 78 assertEquals(expected.length, i); 79 } 80 TestForOf(); 81 82 83 function TestNonOwnSlots() { 84 var iterator = ""[Symbol.iterator](); 85 var object = {__proto__: iterator}; 86 87 assertThrows(function() { object.next(); }, TypeError); 88 } 89 TestNonOwnSlots(); 90