|
1 // String.prototype[@@iterator] and StringIterator.prototype surface tests |
|
2 |
|
3 load(libdir + "array-compare.js"); |
|
4 load(libdir + "asserts.js"); |
|
5 load(libdir + "iteration.js"); |
|
6 |
|
7 function assertDataDescriptor(actual, expected) { |
|
8 assertEq(actual.value, expected.value); |
|
9 assertEq(actual.writable, expected.writable); |
|
10 assertEq(actual.enumerable, expected.enumerable); |
|
11 assertEq(actual.configurable, expected.configurable); |
|
12 } |
|
13 |
|
14 function isConstructor(o) { |
|
15 try { |
|
16 new (new Proxy(o, {construct: () => ({})})); |
|
17 return true; |
|
18 } catch(e) { |
|
19 return false; |
|
20 } |
|
21 } |
|
22 |
|
23 function assertBuiltinFunction(o, name, arity) { |
|
24 var fn = o[name]; |
|
25 assertDataDescriptor(Object.getOwnPropertyDescriptor(o, name), { |
|
26 value: fn, |
|
27 writable: true, |
|
28 enumerable: false, |
|
29 configurable: true, |
|
30 }); |
|
31 |
|
32 assertEq(typeof fn, "function"); |
|
33 assertEq(Object.getPrototypeOf(fn), Function.prototype); |
|
34 // FIXME: Proxy should only have [[Construct]] if target has [[Construct]] (bug 929467) |
|
35 // assertEq(isConstructor(fn), false); |
|
36 |
|
37 arraysEqual(Object.getOwnPropertyNames(fn).sort(), ["length", "name", "arguments", "caller"].sort()); |
|
38 |
|
39 // Also test "name", "arguments" and "caller" in addition to "length"? |
|
40 assertDataDescriptor(Object.getOwnPropertyDescriptor(fn, "length"), { |
|
41 value: arity, |
|
42 writable: false, |
|
43 enumerable: false, |
|
44 configurable: false, |
|
45 }); |
|
46 } |
|
47 |
|
48 |
|
49 // String.prototype[@@iterator] is a built-in function |
|
50 assertBuiltinFunction(String.prototype, std_iterator, 0); |
|
51 |
|
52 // Test StringIterator.prototype surface |
|
53 var iter = ""[std_iterator](); |
|
54 var iterProto = Object.getPrototypeOf(iter); |
|
55 |
|
56 // StringIterator.prototype inherits from Object.prototype |
|
57 assertEq(Object.getPrototypeOf(iterProto), Object.prototype); |
|
58 |
|
59 // Own properties for StringIterator.prototype: "next" and @@iterator |
|
60 arraysEqual(Object.getOwnPropertyNames(iterProto).sort(), ["next", std_iterator].sort()); |
|
61 |
|
62 // StringIterator.prototype[@@iterator] is a built-in function |
|
63 assertBuiltinFunction(iterProto, std_iterator, 0); |
|
64 |
|
65 // StringIterator.prototype.next is a built-in function |
|
66 assertBuiltinFunction(iterProto, "next", 0); |
|
67 |
|
68 // StringIterator.prototype[@@iterator] is generic and returns |this| |
|
69 for (var v of [void 0, null, true, false, "", 0, 1, {}, [], iter, iterProto]) { |
|
70 assertEq(iterProto[std_iterator].call(v), v); |
|
71 } |
|
72 |
|
73 // StringIterator.prototype.next is not generic |
|
74 for (var v of [void 0, null, true, false, "", 0, 1, {}, [], iterProto]) { |
|
75 assertThrowsInstanceOf(() => iterProto.next.call(v), TypeError); |
|
76 } |