|
1 // for-of works on strings and String objects. |
|
2 |
|
3 load(libdir + "string.js"); |
|
4 |
|
5 function test(s, expectedCodePoints) { |
|
6 var copy = ''; |
|
7 var codepoints = 0; |
|
8 var singleHighSurrogate = false; |
|
9 for (var v of s) { |
|
10 assertEq(typeof v, 'string'); |
|
11 assertEq(v.length, isSurrogatePair(v) ? 2 : 1); |
|
12 assertEq(false, singleHighSurrogate && isLowSurrogate(v)); |
|
13 copy += v; |
|
14 codepoints += 1; |
|
15 singleHighSurrogate = !isSurrogatePair(v) && isHighSurrogate(v); |
|
16 } |
|
17 assertEq(copy, String(s)); |
|
18 assertEq(codepoints, expectedCodePoints); |
|
19 } |
|
20 |
|
21 test('', 0); |
|
22 test('abc', 3); |
|
23 test('a \0 \ufffe \ufeff', 7); |
|
24 |
|
25 // Non-BMP characters are generally passed to JS in UTF-16, as surrogate pairs. |
|
26 // ES6 requires that such pairs be treated as a single code point in for-of. |
|
27 test('\ud808\udf45', 1); |
|
28 |
|
29 // Also test invalid surrogate pairs: |
|
30 // (1) High surrogate not followed by low surrogate |
|
31 test('\ud808', 1); |
|
32 test('\ud808\u0000', 2); |
|
33 // (2) Low surrogate not preceded by high surrogate |
|
34 test('\udf45', 1); |
|
35 test('\u0000\udf45', 2); |
|
36 // (3) Low surrogate followed by high surrogate |
|
37 test('\udf45\ud808', 2); |
|
38 |
|
39 test(new String(''), 0); |
|
40 test(new String('abc'), 3); |
|
41 test(new String('a \0 \ufffe \ufeff'), 7); |
|
42 test(new String('\ud808\udf45'), 1); |
|
43 test(new String('\ud808'), 1); |
|
44 test(new String('\ud808\u0000'), 2); |
|
45 test(new String('\udf45'), 1); |
|
46 test(new String('\u0000\udf45'), 2); |
|
47 test(new String('\udf45\ud808'), 2); |