summaryrefslogtreecommitdiffstats
path: root/vendor/js-sys/tests/wasm/Symbol.js
blob: a66691fd9ba892b5f23750d8b6d72fbf4e2f188f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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]');
};