Skip to content

Commit 1efe000

Browse files
committed
Only run typed array value check tests against supported types
1 parent c64868e commit 1efe000

File tree

1 file changed

+95
-55
lines changed

1 file changed

+95
-55
lines changed

test/node/types.js

Lines changed: 95 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -134,71 +134,111 @@ console.log('Testing', 'isBoxedPrimitive');
134134
});
135135

136136
{
137-
const primitive = true;
138-
const arrayBuffer = new ArrayBuffer();
139-
const buffer = Buffer.from(arrayBuffer);
140-
const dataView = new DataView(arrayBuffer);
141-
const uint8Array = new Uint8Array(arrayBuffer);
142-
const uint8ClampedArray = new Uint8ClampedArray(arrayBuffer);
143-
const uint16Array = new Uint16Array(arrayBuffer);
144-
const uint32Array = new Uint32Array(arrayBuffer);
145-
const int8Array = new Int8Array(arrayBuffer);
146-
const int16Array = new Int16Array(arrayBuffer);
147-
const int32Array = new Int32Array(arrayBuffer);
148-
const float32Array = new Float32Array(arrayBuffer);
149-
const float64Array = new Float64Array(arrayBuffer);
150-
const bigInt64Array = new BigInt64Array(arrayBuffer);
151-
const bigUint64Array = new BigUint64Array(arrayBuffer);
137+
let primitive = () => true;
138+
let arrayBuffer = () => new ArrayBuffer();
152139

153-
const fakeBuffer = Object.create(Buffer.prototype);
154-
const fakeDataView = Object.create(DataView.prototype);
155-
const fakeUint8Array = Object.create(Uint8Array.prototype);
156-
const fakeUint8ClampedArray = Object.create(Uint8ClampedArray.prototype);
157-
const fakeUint16Array = Object.create(Uint16Array.prototype);
158-
const fakeUint32Array = Object.create(Uint32Array.prototype);
159-
const fakeInt8Array = Object.create(Int8Array.prototype);
160-
const fakeInt16Array = Object.create(Int16Array.prototype);
161-
const fakeInt32Array = Object.create(Int32Array.prototype);
162-
const fakeFloat32Array = Object.create(Float32Array.prototype);
163-
const fakeFloat64Array = Object.create(Float64Array.prototype);
164-
const fakeBigInt64Array = Object.create(BigInt64Array.prototype);
165-
const fakeBigUint64Array = Object.create(BigUint64Array.prototype);
140+
let buffer = () => Buffer.from(arrayBuffer());
141+
let dataView = () => new DataView(arrayBuffer());
142+
let uint8Array = () => new Uint8Array(arrayBuffer());
143+
let uint8ClampedArray = () => new Uint8ClampedArray(arrayBuffer());
144+
let uint16Array = () => new Uint16Array(arrayBuffer());
145+
let uint32Array = () => new Uint32Array(arrayBuffer());
146+
let int8Array = () => new Int8Array(arrayBuffer());
147+
let int16Array = () => new Int16Array(arrayBuffer());
148+
let int32Array = () => new Int32Array(arrayBuffer());
149+
let float32Array = () => new Float32Array(arrayBuffer());
150+
let float64Array = () => new Float64Array(arrayBuffer());
151+
let bigInt64Array = () => new BigInt64Array(arrayBuffer());
152+
let bigUint64Array = () => new BigUint64Array(arrayBuffer());
166153

167-
const stealthyDataView =
168-
Object.setPrototypeOf(new DataView(arrayBuffer), Uint8Array.prototype);
169-
const stealthyUint8Array =
170-
Object.setPrototypeOf(new Uint8Array(arrayBuffer), ArrayBuffer.prototype);
171-
const stealthyUint8ClampedArray =
154+
let fakeBuffer = () => Object.create(Buffer.prototype);
155+
let fakeDataView = () => Object.create(DataView.prototype);
156+
let fakeUint8Array = () => Object.create(Uint8Array.prototype);
157+
let fakeUint8ClampedArray = () => Object.create(Uint8ClampedArray.prototype);
158+
let fakeUint16Array = () => Object.create(Uint16Array.prototype);
159+
let fakeUint32Array = () => Object.create(Uint32Array.prototype);
160+
let fakeInt8Array = () => Object.create(Int8Array.prototype);
161+
let fakeInt16Array = () => Object.create(Int16Array.prototype);
162+
let fakeInt32Array = () => Object.create(Int32Array.prototype);
163+
let fakeFloat32Array = () => Object.create(Float32Array.prototype);
164+
let fakeFloat64Array = () => Object.create(Float64Array.prototype);
165+
let fakeBigInt64Array = () => Object.create(BigInt64Array.prototype);
166+
let fakeBigUint64Array = () => Object.create(BigUint64Array.prototype);
167+
168+
let stealthyDataView = () =>
169+
Object.setPrototypeOf(new DataView(arrayBuffer()), Uint8Array.prototype);
170+
let stealthyUint8Array = () =>
171+
Object.setPrototypeOf(new Uint8Array(arrayBuffer()), ArrayBuffer.prototype);
172+
let stealthyUint8ClampedArray = () =>
172173
Object.setPrototypeOf(
173-
new Uint8ClampedArray(arrayBuffer), ArrayBuffer.prototype
174+
new Uint8ClampedArray(arrayBuffer()), ArrayBuffer.prototype
174175
);
175-
const stealthyUint16Array =
176-
Object.setPrototypeOf(new Uint16Array(arrayBuffer), Uint16Array.prototype);
177-
const stealthyUint32Array =
178-
Object.setPrototypeOf(new Uint32Array(arrayBuffer), Uint32Array.prototype);
179-
const stealthyInt8Array =
180-
Object.setPrototypeOf(new Int8Array(arrayBuffer), Int8Array.prototype);
181-
const stealthyInt16Array =
182-
Object.setPrototypeOf(new Int16Array(arrayBuffer), Int16Array.prototype);
183-
const stealthyInt32Array =
184-
Object.setPrototypeOf(new Int32Array(arrayBuffer), Int32Array.prototype);
185-
const stealthyFloat32Array =
176+
let stealthyUint16Array = () =>
177+
Object.setPrototypeOf(new Uint16Array(arrayBuffer()), Uint16Array.prototype);
178+
let stealthyUint32Array = () =>
179+
Object.setPrototypeOf(new Uint32Array(arrayBuffer()), Uint32Array.prototype);
180+
let stealthyInt8Array = () =>
181+
Object.setPrototypeOf(new Int8Array(arrayBuffer()), Int8Array.prototype);
182+
let stealthyInt16Array = () =>
183+
Object.setPrototypeOf(new Int16Array(arrayBuffer()), Int16Array.prototype);
184+
let stealthyInt32Array = () =>
185+
Object.setPrototypeOf(new Int32Array(arrayBuffer()), Int32Array.prototype);
186+
let stealthyFloat32Array = () =>
186187
Object.setPrototypeOf(
187-
new Float32Array(arrayBuffer), Float32Array.prototype
188+
new Float32Array(arrayBuffer()), Float32Array.prototype
188189
);
189-
const stealthyFloat64Array =
190+
let stealthyFloat64Array = () =>
190191
Object.setPrototypeOf(
191-
new Float64Array(arrayBuffer), Float64Array.prototype
192+
new Float64Array(arrayBuffer()), Float64Array.prototype
192193
);
193-
const stealthyBigInt64Array =
194+
let stealthyBigInt64Array = () =>
194195
Object.setPrototypeOf(
195-
new BigInt64Array(arrayBuffer), BigInt64Array.prototype
196+
new BigInt64Array(arrayBuffer()), BigInt64Array.prototype
196197
);
197-
const stealthyBigUint64Array =
198+
let stealthyBigUint64Array = () =>
198199
Object.setPrototypeOf(
199-
new BigUint64Array(arrayBuffer), BigUint64Array.prototype
200+
new BigUint64Array(arrayBuffer()), BigUint64Array.prototype
200201
);
201202

203+
const typedArrays = Object.entries({
204+
primitive, arrayBuffer, buffer, fakeBuffer,
205+
dataView, fakeDataView, stealthyDataView,
206+
uint8Array, fakeUint8Array, stealthyUint8Array,
207+
uint8ClampedArray, fakeUint8ClampedArray, stealthyUint8ClampedArray,
208+
uint16Array, fakeUint16Array, stealthyUint16Array,
209+
uint32Array, fakeUint32Array, stealthyUint32Array,
210+
int8Array, fakeInt8Array, stealthyInt8Array,
211+
int16Array, fakeInt16Array, stealthyInt16Array,
212+
int32Array, fakeInt32Array, stealthyInt32Array,
213+
float32Array, fakeFloat32Array, stealthyFloat32Array,
214+
float64Array, fakeFloat64Array, stealthyFloat64Array,
215+
bigInt64Array, fakeBigInt64Array, stealthyBigInt64Array,
216+
bigUint64Array, fakeBigUint64Array, stealthyBigUint64Array
217+
});
218+
typedArrays.forEach(([key, createTypedArray]) => {
219+
try {
220+
typedArrays[key] = createTypedArray();
221+
} catch (e) {
222+
typedArrays[key] = undefined;
223+
}
224+
});
225+
226+
({
227+
primitive, arrayBuffer, buffer, fakeBuffer,
228+
dataView, fakeDataView, stealthyDataView,
229+
uint8Array, fakeUint8Array, stealthyUint8Array,
230+
uint8ClampedArray, fakeUint8ClampedArray, stealthyUint8ClampedArray,
231+
uint16Array, fakeUint16Array, stealthyUint16Array,
232+
uint32Array, fakeUint32Array, stealthyUint32Array,
233+
int8Array, fakeInt8Array, stealthyInt8Array,
234+
int16Array, fakeInt16Array, stealthyInt16Array,
235+
int32Array, fakeInt32Array, stealthyInt32Array,
236+
float32Array, fakeFloat32Array, stealthyFloat32Array,
237+
float64Array, fakeFloat64Array, stealthyFloat64Array,
238+
bigInt64Array, fakeBigInt64Array, stealthyBigInt64Array,
239+
bigUint64Array, fakeBigUint64Array, stealthyBigUint64Array
240+
} = typedArrays);
241+
202242
const all = [
203243
primitive, arrayBuffer, buffer, fakeBuffer,
204244
dataView, fakeDataView, stealthyDataView,
@@ -281,15 +321,15 @@ console.log('Testing', 'isBoxedPrimitive');
281321
};
282322

283323
for (const testedFunc of Object.keys(expected)) {
324+
console.log('Testing', testedFunc);
284325
const func = types[testedFunc];
285326
const yup = [];
286-
for (const value of all) {
327+
for (const value of all.filter(a => typeof a !== 'undefined')) {
287328
if (func(value)) {
288329
yup.push(value);
289330
}
290331
}
291-
console.log('Testing', testedFunc);
292-
assert.deepStrictEqual(yup, expected[testedFunc]);
332+
assert.deepStrictEqual(yup, expected[testedFunc].filter(a => typeof a !== 'undefined'));
293333
}
294334
}
295335

0 commit comments

Comments
 (0)