Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Feb 21, 2023
1 parent adf9681 commit 26f6de3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
34 changes: 33 additions & 1 deletion tests/unit-global/web.structured-clone.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Originally from: https://github.com/web-platform-tests/wpt/blob/4b35e758e2fc4225368304b02bcec9133965fd1a/IndexedDB/structured-clone.any.js
// Copyright © web-platform-tests contributors. Available under the 3-Clause BSD License.
import { GLOBAL, NODE } from '../helpers/constants';
import { fromSource } from '../helpers/helpers';
import { bufferToArray, fromSource } from '../helpers/helpers';

const { from } = Array;
const { assign, getPrototypeOf, keys } = Object;
Expand Down Expand Up @@ -178,6 +178,38 @@ QUnit.module('structuredClone', () => {
});
});
}

if ('resizable' in ArrayBuffer.prototype) {
QUnit.test('Resizable ArrayBuffer', assert => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];

let buffer = new ArrayBuffer(8, { maxByteLength: 16 });
new Int8Array(buffer).set(array);
let copy = structuredClone(buffer);
assert.arrayEqual(bufferToArray(copy), array, 'resizable-ab-1');
assert.true(copy.resizable, 'resizable-ab-1');

buffer = new ArrayBuffer(8);
new Int8Array(buffer).set(array);
copy = structuredClone(buffer);
assert.arrayEqual(bufferToArray(copy), array, 'non-resizable-ab-1');
assert.false(copy.resizable, 'non-resizable-ab-1');

buffer = new ArrayBuffer(8, { maxByteLength: 16 });
let tarray = new Int8Array(buffer);
tarray.set(array);
copy = structuredClone(tarray).buffer;
assert.arrayEqual(bufferToArray(copy), array, 'resizable-ab-2');
assert.true(copy.resizable, 'resizable-ab-2');

buffer = new ArrayBuffer(8);
tarray = new Int8Array(buffer);
tarray.set(array);
copy = structuredClone(tarray).buffer;
assert.arrayEqual(bufferToArray(copy), array, 'non-resizable-ab-2');
assert.false(copy.resizable, 'non-resizable-ab-2');
});
}
}

// Map
Expand Down
34 changes: 33 additions & 1 deletion tests/unit-pure/web.structured-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright © web-platform-tests contributors. Available under the 3-Clause BSD License.
/* eslint-disable es/no-error-cause, es/no-typed-arrays -- safe */
import { GLOBAL, NODE } from '../helpers/constants';
import { fromSource } from '../helpers/helpers';
import { bufferToArray, fromSource } from '../helpers/helpers';

import structuredClone from 'core-js-pure/stable/structured-clone';
import from from 'core-js-pure/es/array/from';
Expand Down Expand Up @@ -186,6 +186,38 @@ QUnit.module('structuredClone', () => {
});
});
}

if ('resizable' in ArrayBuffer.prototype) {
QUnit.test('Resizable ArrayBuffer', assert => {
const array = [1, 2, 3, 4, 5, 6, 7, 8];

let buffer = new ArrayBuffer(8, { maxByteLength: 16 });
new Int8Array(buffer).set(array);
let copy = structuredClone(buffer);
assert.arrayEqual(bufferToArray(copy), array, 'resizable-ab-1');
assert.true(copy.resizable, 'resizable-ab-1');

buffer = new ArrayBuffer(8);
new Int8Array(buffer).set(array);
copy = structuredClone(buffer);
assert.arrayEqual(bufferToArray(copy), array, 'non-resizable-ab-1');
assert.false(copy.resizable, 'non-resizable-ab-1');

buffer = new ArrayBuffer(8, { maxByteLength: 16 });
let tarray = new Int8Array(buffer);
tarray.set(array);
copy = structuredClone(tarray).buffer;
assert.arrayEqual(bufferToArray(copy), array, 'resizable-ab-2');
assert.true(copy.resizable, 'resizable-ab-2');

buffer = new ArrayBuffer(8);
tarray = new Int8Array(buffer);
tarray.set(array);
copy = structuredClone(tarray).buffer;
assert.arrayEqual(bufferToArray(copy), array, 'non-resizable-ab-2');
assert.false(copy.resizable, 'non-resizable-ab-2');
});
}
}

// Map
Expand Down

0 comments on commit 26f6de3

Please sign in to comment.