MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1const assert = require("node:assert");
2
3function assertArrayIncludesRegressionSurface() {
4 assert.strictEqual([1, 2, 3].includes(2), true);
5 assert.strictEqual([1, 2, 3].includes(4), false);
6 assert.strictEqual([1, 2, 3].includes(1, 1), false);
7 assert.strictEqual([NaN].includes(NaN), true);
8 assert.strictEqual([1, 2, 3].includes(3, -1), true);
9 assert.strictEqual([1, 2, 3].includes(1, -1), false);
10
11 assert.strictEqual([,].includes(undefined), true);
12 assert.strictEqual(Array(4).includes(undefined), true);
13 assert.strictEqual(Array(4).includes(1), false);
14
15 const holey = [];
16 holey[3] = "tail";
17 assert.strictEqual(holey.includes(undefined), true);
18 assert.strictEqual(holey.includes("tail"), true);
19 assert.strictEqual(holey.includes("tail", 4), false);
20
21 const grown = [1];
22 grown.length = 3;
23 assert.strictEqual(grown.includes(undefined), true);
24
25 try {
26 Array.prototype[1] = "from-array-proto";
27 assert.strictEqual(Array(3).includes("from-array-proto"), true);
28 assert.strictEqual(Array(3).includes("missing"), false);
29 } finally {
30 delete Array.prototype[1];
31 }
32
33 const customIncludes = {
34 includes(value) {
35 return value === "custom";
36 }
37 };
38 assert.strictEqual(customIncludes.includes("custom"), true);
39
40 const originalIncludes = Array.prototype.includes;
41 try {
42 Array.prototype.includes = function(value) {
43 return value === "patched";
44 };
45 assert.strictEqual([1, 2, 3].includes("patched"), true);
46 } finally {
47 Array.prototype.includes = originalIncludes;
48 }
49
50 let steps = 0;
51 const generic = {
52 get length() {
53 steps += 1;
54 return 6;
55 },
56 get 2() {
57 steps += 10;
58 return "match";
59 },
60 get 4() {
61 steps += 100;
62 return "late";
63 }
64 };
65 assert.strictEqual([].includes.call(generic, "match", 1), true);
66 assert.strictEqual(steps, 11);
67
68 const proto = { 3: "from-proto" };
69 const inherited = Object.create(proto);
70 Object.defineProperty(inherited, "length", {
71 configurable: true,
72 enumerable: true,
73 get() {
74 return 5;
75 }
76 });
77 assert.strictEqual([].includes.call(inherited, "from-proto"), true);
78 assert.strictEqual([].includes.call(inherited, undefined), true);
79
80 const proxyLog = [];
81 const proxy = new Proxy(
82 { length: 4, 0: NaN, 1: "", 2: NaN, 3: "" },
83 {
84 get(target, key) {
85 proxyLog.push(String(key));
86 return target[key];
87 }
88 }
89 );
90 assert.strictEqual(Array.prototype.includes.call(proxy, NaN, 1), true);
91 assert.deepStrictEqual(proxyLog, ["length", "1", "2"]);
92
93 [
94 Int8Array,
95 Uint8Array,
96 Uint8ClampedArray,
97 Int16Array,
98 Uint16Array,
99 Int32Array,
100 Uint32Array,
101 Float32Array,
102 Float64Array
103 ].forEach((TypedArray) => {
104 const view = new TypedArray([1, 2, 3]);
105 assert.strictEqual(view.includes(1), true);
106 assert.strictEqual(view.includes(4), false);
107 assert.strictEqual(view.includes(1, 1), false);
108 });
109
110 const floatView = new Float64Array([1, NaN, 3]);
111 assert.strictEqual(floatView.includes(NaN), true);
112}
113
114assertArrayIncludesRegressionSurface();
115console.log("array includes regression surface passes");