| 1 |
const assert = require('assert'); |
| 2 |
|
| 3 |
exports.test_has_instance = function(sym) { |
| 4 |
class Array1 { |
| 5 |
static [sym](instance) { |
| 6 |
return Array.isArray(instance); |
| 7 |
} |
| 8 |
} |
| 9 |
|
| 10 |
assert.equal(typeof sym, "symbol"); |
| 11 |
assert.ok([] instanceof Array1); |
| 12 |
}; |
| 13 |
|
| 14 |
exports.test_is_concat_spreadable = function(sym) { |
| 15 |
const alpha = ['a', 'b', 'c']; |
| 16 |
const numeric = [1, 2, 3]; |
| 17 |
let alphaNumeric = alpha.concat(numeric); |
| 18 |
|
| 19 |
assert.deepEqual(alphaNumeric, ["a", "b", "c", 1, 2, 3]); |
| 20 |
|
| 21 |
numeric[sym] = false; |
| 22 |
alphaNumeric = alpha.concat(numeric); |
| 23 |
|
| 24 |
assert.deepEqual(alphaNumeric, ["a", "b", "c", numeric]); |
| 25 |
}; |
| 26 |
|
| 27 |
exports.test_iterator = function(sym) { |
| 28 |
const iterable1 = new Object(); |
| 29 |
|
| 30 |
iterable1[sym] = function* () { |
| 31 |
yield 1; |
| 32 |
yield 2; |
| 33 |
yield 3; |
| 34 |
}; |
| 35 |
|
| 36 |
assert.deepEqual([...iterable1], [1, 2, 3]); |
| 37 |
}; |
| 38 |
|
| 39 |
exports.test_async_iterator = async function(sym) { |
| 40 |
const iterable1 = new Object(); |
| 41 |
|
| 42 |
iterable1[sym] = function () { |
| 43 |
let done = false; |
| 44 |
|
| 45 |
return { |
| 46 |
next() { |
| 47 |
if (done) { |
| 48 |
return Promise.resolve({ |
| 49 |
done: true, |
| 50 |
value: 1 |
| 51 |
}); |
| 52 |
|
| 53 |
} else { |
| 54 |
done = true; |
| 55 |
|
| 56 |
return Promise.resolve({ |
| 57 |
done: false, |
| 58 |
value: 0 |
| 59 |
}); |
| 60 |
} |
| 61 |
} |
| 62 |
}; |
| 63 |
}; |
| 64 |
|
| 65 |
const values = []; |
| 66 |
|
| 67 |
for await (let value of iterable1) { |
| 68 |
values.push(value); |
| 69 |
} |
| 70 |
|
| 71 |
assert.deepEqual(values, [0]); |
| 72 |
}; |
| 73 |
|
| 74 |
exports.test_match = function(sym) { |
| 75 |
const regexp1 = /foo/; |
| 76 |
assert.throws(() => '/foo/'.startsWith(regexp1)); |
| 77 |
|
| 78 |
regexp1[sym] = false; |
| 79 |
|
| 80 |
assert.ok('/foo/'.startsWith(regexp1)); |
| 81 |
|
| 82 |
assert.equal('/baz/'.endsWith(regexp1), false); |
| 83 |
}; |
| 84 |
|
| 85 |
exports.test_replace = function(sym) { |
| 86 |
class Replace1 { |
| 87 |
constructor(value) { |
| 88 |
this.value = value; |
| 89 |
} |
| 90 |
[sym](string) { |
| 91 |
return `s/${string}/${this.value}/g`; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
assert.equal('foo'.replace(new Replace1('bar')), 's/foo/bar/g'); |
| 96 |
}; |
| 97 |
|
| 98 |
exports.test_search = function(sym) { |
| 99 |
class Search1 { |
| 100 |
constructor(value) { |
| 101 |
this.value = value; |
| 102 |
} |
| 103 |
|
| 104 |
[sym](string) { |
| 105 |
return string.indexOf(this.value); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
assert.equal('foobar'.search(new Search1('bar')), 3); |
| 110 |
}; |
| 111 |
|
| 112 |
exports.test_species = function(sym) { |
| 113 |
class Array1 extends Array { |
| 114 |
static get [sym]() { return Array; } |
| 115 |
} |
| 116 |
|
| 117 |
const a = new Array1(1, 2, 3); |
| 118 |
const mapped = a.map(x => x * x); |
| 119 |
|
| 120 |
assert.equal(mapped instanceof Array1, false); |
| 121 |
|
| 122 |
assert.ok(mapped instanceof Array); |
| 123 |
}; |
| 124 |
|
| 125 |
exports.test_split = function(sym) { |
| 126 |
class Split1 { |
| 127 |
constructor(value) { |
| 128 |
this.value = value; |
| 129 |
} |
| 130 |
|
| 131 |
[sym](string) { |
| 132 |
var index = string.indexOf(this.value); |
| 133 |
return this.value + string.substr(0, index) + "/" |
| 134 |
+ string.substr(index + this.value.length); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
assert.equal('foobar'.split(new Split1('foo')), 'foo/bar'); |
| 139 |
}; |
| 140 |
|
| 141 |
exports.test_to_primitive = function(sym) { |
| 142 |
const object1 = { |
| 143 |
[sym](hint) { |
| 144 |
if (hint == 'number') { |
| 145 |
return 42; |
| 146 |
} |
| 147 |
return null; |
| 148 |
} |
| 149 |
}; |
| 150 |
|
| 151 |
assert.equal(+object1, 42); |
| 152 |
}; |
| 153 |
|
| 154 |
exports.test_to_string_tag = function(sym) { |
| 155 |
class ValidatorClass { |
| 156 |
get [sym]() { |
| 157 |
return 'Validator'; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
assert.equal(Object.prototype.toString.call(new ValidatorClass()), '[object Validator]'); |
| 162 |
}; |