Skip to content

Commit

Permalink
fixup! test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Jan 13, 2025
1 parent 7677afb commit a8bdf43
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/immutable-arraybuffer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ImmutableArrayBufferInternal {

sliceToImmutable(start = undefined, end = undefined) {
// eslint-disable-next-line no-use-before-define
return bufferSliceToImmutable(this.#buffer, start, end);
return sliceBufferToImmutable(this.#buffer, start, end);
}

resize(_newByteLength = undefined) {
Expand Down Expand Up @@ -175,7 +175,8 @@ export const isBufferImmutable = buffer => {
throw err;
}
};
export const bufferSliceToImmutable = (

export const sliceBufferToImmutable = (
buffer,
start = undefined,
end = undefined,
Expand Down
4 changes: 2 additions & 2 deletions packages/immutable-arraybuffer/shim.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
transferBufferToImmutable,
isBufferImmutable,
bufferSliceToImmutable,
sliceBufferToImmutable,
} from './index.js';

const { getOwnPropertyDescriptors, defineProperties } = Object;
Expand All @@ -12,7 +12,7 @@ const arrayBufferMethods = {
return transferBufferToImmutable(this, newLength);
},
sliceToImmutable(start = undefined, end = undefined) {
return bufferSliceToImmutable(this, start, end);
return sliceBufferToImmutable(this, start, end);
},
get immutable() {
return isBufferImmutable(this);
Expand Down
87 changes: 86 additions & 1 deletion packages/immutable-arraybuffer/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import test from 'ava';
import {
transferBufferToImmutable,
// isBufferImmutable,
isBufferImmutable,
sliceBufferToImmutable,
} from '../index.js';

const { isFrozen, getPrototypeOf } = Object;
Expand Down Expand Up @@ -124,3 +125,87 @@ test('TypedArray on Immutable ArrayBuffer ponyfill limitations', t => {
const ta3 = new Uint8Array(iab);
t.is(ta3.byteLength, 0);
});

test('Standard buf.transfer(newLength) behavior baseline', t => {
if (!('transfer' in ArrayBuffer.prototype)) {
// Will happen on Node <= 20
const msg =
'skip when platform does not yet implement ArrayBuffer.prototype.transfer';
t.pass(msg);
t.log(msg);
return;
}
// Will happen on Node >= 22
t.log('ArrayBuffer.prototype.transfer exists');
const ta12 = new Uint8Array([3, 4, 5]);
const ab12 = ta12.buffer;
t.is(ab12.byteLength, 3);
t.deepEqual([...ta12], [3, 4, 5]);

const ab2 = ab12.transfer(5);
t.false(isBufferImmutable(ab2));
t.is(ab2.byteLength, 5);
t.is(ab12.byteLength, 0);
const ta2 = new Uint8Array(ab2);
t.deepEqual([...ta2], [3, 4, 5, 0, 0]);

const ta13 = new Uint8Array([3, 4, 5]);
const ab13 = ta13.buffer;

const ab3 = ab13.transfer(2);
t.false(isBufferImmutable(ab3));
t.is(ab3.byteLength, 2);
t.is(ab13.byteLength, 0);
const ta3 = new Uint8Array(ab3);
t.deepEqual([...ta3], [3, 4]);
});

test('Analogous transferBufferToImmutable(buf, newLength) behavior', t => {
const ta12 = new Uint8Array([3, 4, 5]);
const ab12 = ta12.buffer;
t.is(ab12.byteLength, 3);
t.deepEqual([...ta12], [3, 4, 5]);

const ab2 = transferBufferToImmutable(ab12, 5);
t.true(isBufferImmutable(ab2));
t.is(ab2.byteLength, 5);
t.is(ab12.byteLength, 0);
// slice needed due to ponyfill limitations.
const ta2 = new Uint8Array(ab2.slice());
t.deepEqual([...ta2], [3, 4, 5, 0, 0]);

const ta13 = new Uint8Array([3, 4, 5]);
const ab13 = ta13.buffer;

const ab3 = transferBufferToImmutable(ab13, 2);
t.true(isBufferImmutable(ab3));
t.is(ab3.byteLength, 2);
t.is(ab13.byteLength, 0);
// slice needed due to ponyfill limitations.
const ta3 = new Uint8Array(ab3.slice());
t.deepEqual([...ta3], [3, 4]);
});

// TODO does not yet work
test.skip('sliceBufferToImmutable', t => {
const ta12 = new Uint8Array([3, 4, 5]);
const ab12 = ta12.buffer;
t.is(ab12.byteLength, 3);
t.deepEqual([...ta12], [3, 4, 5]);

const ab2 = sliceBufferToImmutable(ab12, 1, 5);
t.true(isBufferImmutable(ab2));
t.is(ab2.byteLength, 2);
t.is(ab12.byteLength, 3);
// slice needed due to ponyfill limitations.
const ta2 = new Uint8Array(ab2.slice());
t.deepEqual([...ta2], [4, 5]);

const ab3 = sliceBufferToImmutable(ab2, 1, 1);
t.true(isBufferImmutable(ab3));
t.is(ab3.byteLength, 2);
t.is(ab2.byteLength, 0);
// slice needed due to ponyfill limitations.
const ta3 = new Uint8Array(ab3.slice());
t.deepEqual([...ta3], [3, 4]);
});

0 comments on commit a8bdf43

Please sign in to comment.