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