| 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 |
assertEq(isConstructor(fn), false); |
| 35 |
|
| 36 |
assertEq(arraysEqual(Object.getOwnPropertyNames(fn).sort(), ["length", "name"].sort()), true); |
| 37 |
|
| 38 |
assertDataDescriptor(Object.getOwnPropertyDescriptor(fn, "length"), { |
| 39 |
value: arity, |
| 40 |
writable: false, |
| 41 |
enumerable: false, |
| 42 |
configurable: true |
| 43 |
}); |
| 44 |
|
| 45 |
var functionName = typeof name === "symbol" |
| 46 |
? String(name).replace(/^Symbol\((.+)\)$/, "[$1]") |
| 47 |
: name; |
| 48 |
assertDataDescriptor(Object.getOwnPropertyDescriptor(fn, "name"), { |
| 49 |
value: functionName, |
| 50 |
writable: false, |
| 51 |
enumerable: false, |
| 52 |
configurable: true |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
// String.prototype[@@iterator] is a built-in function |
| 58 |
assertBuiltinFunction(String.prototype, Symbol.iterator, 0); |
| 59 |
|
| 60 |
// Test StringIterator.prototype surface |
| 61 |
var iter = ""[Symbol.iterator](); |
| 62 |
var iterProto = Object.getPrototypeOf(iter); |
| 63 |
|
| 64 |
// StringIterator.prototype inherits from %IteratorPrototype%. Check it's the |
| 65 |
// same object as %ArrayIteratorPrototype%'s proto. |
| 66 |
assertEq(Object.getPrototypeOf(iterProto), |
| 67 |
Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()))); |
| 68 |
|
| 69 |
// Own properties for StringIterator.prototype: "next" |
| 70 |
assertEq(arraysEqual(Object.getOwnPropertyNames(iterProto).sort(), ["next"]), true); |
| 71 |
|
| 72 |
// StringIterator.prototype.next is a built-in function |
| 73 |
assertBuiltinFunction(iterProto, "next", 0); |
| 74 |
|
| 75 |
// StringIterator.prototype[@@iterator] is generic and returns |this| |
| 76 |
for (var v of [void 0, null, true, false, "", 0, 1, {}, [], iter, iterProto]) { |
| 77 |
assertEq(iterProto[Symbol.iterator].call(v), v); |
| 78 |
} |
| 79 |
|
| 80 |
// StringIterator.prototype.next is not generic |
| 81 |
for (var v of [void 0, null, true, false, "", 0, 1, {}, [], iterProto]) { |
| 82 |
assertThrowsInstanceOf(() => iterProto.next.call(v), TypeError); |
| 83 |
} |