1 // This test checks the behavior of the String iterator
3 var testString
= "Cocoa,Cappuccino";
4 var stringIterator
= testString
[Symbol
.iterator
]();
5 var stringIteratorPrototype
= stringIterator
.__proto__
;
6 var stringIteratorPrototypeNext
= stringIteratorPrototype
.next
;
8 if (stringIterator
.hasOwnProperty('next'))
9 throw "next method should exists on %StringIteratorPrototype%";
10 if (!stringIteratorPrototype
.hasOwnProperty('next'))
11 throw "next method should exists on %StringIteratorPrototype%";
13 var iterator
= testString
[Symbol
.iterator
]();
16 var {done
, value
} = iterator
.next();
19 if (value
!== testString
[i
])
20 throw "Error: bad value: " + value
;
24 if (testString
.length
!== i
)
25 throw "Error: bad value: " + i
;
27 function testSurrogatePair(testString
, expected
, numberOfElements
) {
28 if (testString
.length
!== numberOfElements
)
29 throw "Error: bad value: " + testString
.length
;
31 var iterator
= testString
[Symbol
.iterator
]();
34 var {done
, value
} = iterator
.next();
37 if (value
!== expected
[i
])
38 throw "Error: bad value: " + value
;
42 if (i
!== expected
.length
)
43 throw "Error: bad value: " + i
;
45 for (var codePoint
of testString
) {
46 if (value
!== expected
[i
])
47 throw "Error: bad value: " + value
;
51 // "\uD842\uDFB7\u91ce\u5bb6"
52 var testString
= "𠮷野家";
54 String
.fromCharCode(0xD842, 0xDFB7),
55 String
.fromCharCode(0x91CE),
56 String
.fromCharCode(0x5BB6),
58 testSurrogatePair(testString
, expected
, 4);
60 var testString
= "A\uD842";
62 String
.fromCharCode(0x0041),
63 String
.fromCharCode(0xD842),
65 testSurrogatePair(testString
, expected
, 2);
67 var testString
= "A\uD842A";
69 String
.fromCharCode(0x0041),
70 String
.fromCharCode(0xD842),
71 String
.fromCharCode(0x0041),
73 testSurrogatePair(testString
, expected
, 3);
75 var testString
= "A\uD842\uDFB7";
77 String
.fromCharCode(0x0041),
78 String
.fromCharCode(0xD842, 0xDFB7),
80 testSurrogatePair(testString
, expected
, 3);
82 var testString
= "\uD842A\uDFB7";
84 String
.fromCharCode(0xD842),
85 String
.fromCharCode(0x0041),
86 String
.fromCharCode(0xDFB7),
88 testSurrogatePair(testString
, expected
, 3);
90 var testString
= "\uDFB7\uD842A";
92 String
.fromCharCode(0xDFB7),
93 String
.fromCharCode(0xD842),
94 String
.fromCharCode(0x0041),
96 testSurrogatePair(testString
, expected
, 3);
98 var string1
= "Cocoa";
99 var string1Iterator
= string1
[Symbol
.iterator
]();
102 var result
= stringIteratorPrototypeNext
.call(string1Iterator
);
103 var value
= result
.value
;
107 if (value
!== string1
[index
++])
108 throw "Error: bad value: " + value
;
111 throw "Error: bad index: " + index
;
113 function increment(iter
) {
114 return stringIteratorPrototypeNext
.call(iter
);
116 var string1
= "Cocoa";
117 var string2
= "Cocoa";
118 var string1Iterator
= string1
[Symbol
.iterator
]();
119 var string2Iterator
= string2
[Symbol
.iterator
]();
120 for (var i
= 0; i
< 3; ++i
) {
121 var value1
= increment(string1Iterator
).value
;
122 var value2
= increment(string2Iterator
).value
;
123 if (value1
!== value2
)
124 throw "Error: bad value: " + value1
+ " " + value2
;
127 var string1
= "Cappuccino";
128 var string1Iterator
= string1
[Symbol
.iterator
]();
130 var value
= string1Iterator
.next().value
;
132 throw "Error: bad value: " + value
;
133 var value
= string1Iterator
.next().value
;
135 throw "Error: bad value: " + value
;
136 var value
= string1Iterator
.next().value
;
138 throw "Error: bad value: " + value
;
139 var value
= stringIteratorPrototypeNext
.call(string1Iterator
).value
;
141 throw "Error: bad value: " + value
;
142 var value
= stringIteratorPrototypeNext
.call(string1Iterator
).value
;
144 throw "Error: bad value: " + value
;
145 var value
= stringIteratorPrototypeNext
.call(string1Iterator
).value
;
147 throw "Error: bad value: " + value
;
148 var value
= stringIteratorPrototypeNext
.call(string1Iterator
).value
;
150 throw "Error: bad value: " + value
;
151 var value
= stringIteratorPrototypeNext
.call(string1Iterator
).value
;
153 throw "Error: bad value: " + value
;
154 var value
= stringIteratorPrototypeNext
.call(string1Iterator
).value
;
156 throw "Error: bad value: " + value
;
157 var value
= stringIteratorPrototypeNext
.call(string1Iterator
).value
;
159 throw "Error: bad value: " + value
;
160 var value
= stringIteratorPrototypeNext
.call(string1Iterator
).value
;
161 if (value
!== undefined)
162 throw "Error: bad value: " + value
;
174 for (var primitive
of primitives
) {
177 stringIteratorPrototypeNext
.call(primitive
);
182 throw "Error: no error thrown";
183 var message
= 'TypeError: %StringIteratorPrototype%.next requires that |this| be a String Iterator instance';
184 if (primitive
== null)
185 message
= 'TypeError: %StringIteratorPrototype%.next requires that |this| not be null or undefined'
186 if (String(didThrow
) !== message
)
187 throw "Error: bad error thrown: " + didThrow
;
190 var nonRelatedObjects
= [
196 new String("Cappuccino"),
201 for (var object
of nonRelatedObjects
) {
204 stringIteratorPrototypeNext
.call(object
);
209 throw "Error: no error thrown";
210 if (String(didThrow
) !== 'TypeError: %StringIteratorPrototype%.next requires that |this| be a String Iterator instance')
211 throw "Error: bad error thrown: " + didThrow
;