123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- const assert = require('assert');
- exports.test_has_instance = function(sym) {
- class Array1 {
- static [sym](instance) {
- return Array.isArray(instance);
- }
- }
- assert.equal(typeof sym, "symbol");
- assert.ok([] instanceof Array1);
- };
- exports.test_is_concat_spreadable = function(sym) {
- const alpha = ['a', 'b', 'c'];
- const numeric = [1, 2, 3];
- let alphaNumeric = alpha.concat(numeric);
- assert.deepEqual(alphaNumeric, ["a", "b", "c", 1, 2, 3]);
- numeric[sym] = false;
- alphaNumeric = alpha.concat(numeric);
- assert.deepEqual(alphaNumeric, ["a", "b", "c", numeric]);
- };
- exports.test_iterator = function(sym) {
- const iterable1 = new Object();
- iterable1[sym] = function* () {
- yield 1;
- yield 2;
- yield 3;
- };
- assert.deepEqual([...iterable1], [1, 2, 3]);
- };
- exports.test_async_iterator = async function(sym) {
- const iterable1 = new Object();
- iterable1[sym] = function () {
- let done = false;
- return {
- next() {
- if (done) {
- return Promise.resolve({
- done: true,
- value: 1
- });
- } else {
- done = true;
- return Promise.resolve({
- done: false,
- value: 0
- });
- }
- }
- };
- };
- const values = [];
- for await (let value of iterable1) {
- values.push(value);
- }
- assert.deepEqual(values, [0]);
- };
- exports.test_match = function(sym) {
- const regexp1 = /foo/;
- assert.throws(() => '/foo/'.startsWith(regexp1));
- regexp1[sym] = false;
- assert.ok('/foo/'.startsWith(regexp1));
- assert.equal('/baz/'.endsWith(regexp1), false);
- };
- exports.test_replace = function(sym) {
- class Replace1 {
- constructor(value) {
- this.value = value;
- }
- [sym](string) {
- return `s/${string}/${this.value}/g`;
- }
- }
- assert.equal('foo'.replace(new Replace1('bar')), 's/foo/bar/g');
- };
- exports.test_search = function(sym) {
- class Search1 {
- constructor(value) {
- this.value = value;
- }
- [sym](string) {
- return string.indexOf(this.value);
- }
- }
- assert.equal('foobar'.search(new Search1('bar')), 3);
- };
- exports.test_species = function(sym) {
- class Array1 extends Array {
- static get [sym]() { return Array; }
- }
- const a = new Array1(1, 2, 3);
- const mapped = a.map(x => x * x);
- assert.equal(mapped instanceof Array1, false);
- assert.ok(mapped instanceof Array);
- };
- exports.test_split = function(sym) {
- class Split1 {
- constructor(value) {
- this.value = value;
- }
- [sym](string) {
- var index = string.indexOf(this.value);
- return this.value + string.substr(0, index) + "/"
- + string.substr(index + this.value.length);
- }
- }
- assert.equal('foobar'.split(new Split1('foo')), 'foo/bar');
- };
- exports.test_to_primitive = function(sym) {
- const object1 = {
- [sym](hint) {
- if (hint == 'number') {
- return 42;
- }
- return null;
- }
- };
- assert.equal(+object1, 42);
- };
- exports.test_to_string_tag = function(sym) {
- class ValidatorClass {
- get [sym]() {
- return 'Validator';
- }
- }
- assert.equal(Object.prototype.toString.call(new ValidatorClass()), '[object Validator]');
- };
|