Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 4, 2023
1 parent 9d67637 commit 86f6f87
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/unit-global/esnext.uint8-array.from-base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ if (DESCRIPTORS) QUnit.test('Uint8Array.fromBase64', assert => {

assert.deepEqual(fromBase64('SGVsbG8gV29ybGQ='), array, 'proper result');

assert.throws(() => fromBase64('1234', null), TypeError, 'incorrect options argument #1');
assert.throws(() => fromBase64('1234', 1), TypeError, 'incorrect options argument #2');

assert.throws(() => fromBase64('1234', { alphabet: 'base32' }), TypeError, 'incorrect encoding');

assert.deepEqual(fromBase64('12/3'), new Uint8Array([215, 111, 247]), 'encoding #1');
Expand Down
32 changes: 32 additions & 0 deletions tests/unit-global/esnext.uint8-array.to-base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DESCRIPTORS } from '../helpers/constants.js';

if (DESCRIPTORS) QUnit.test('Uint8Array.prototype.toBase64', assert => {
const { toBase64 } = Uint8Array.prototype;
assert.isFunction(toBase64);
assert.arity(toBase64, 0);
assert.name(toBase64, 'toBase64');
assert.looksNative(toBase64);

const array = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]);

assert.same(array.toBase64(), 'SGVsbG8gV29ybGQ=', 'proper result');
assert.same(array.toBase64({ alphabet: 'base64' }), 'SGVsbG8gV29ybGQ=', 'proper result, base64');
assert.same(array.toBase64({ alphabet: 'base64url' }), 'SGVsbG8gV29ybGQ=', 'proper result, base64url');

assert.throws(() => array.toBase64(null), TypeError, 'incorrect options argument #1');
assert.throws(() => array.toBase64(1), TypeError, 'incorrect options argument #2');

assert.throws(() => array.toBase64({ alphabet: 'base32' }), TypeError, 'incorrect encoding');

assert.same(new Uint8Array([215, 111, 247]).toBase64(), '12/3', 'encoding #1');
assert.same(new Uint8Array([215, 111, 247]).toBase64({ alphabet: 'base64' }), '12/3', 'encoding #2');
assert.same(new Uint8Array([215, 111, 247]).toBase64({ alphabet: 'base64url' }), '12_3', 'encoding #3');
assert.same(new Uint8Array([215, 111, 183]).toBase64(), '12+3', 'encoding #4');
assert.same(new Uint8Array([215, 111, 183]).toBase64({ alphabet: 'base64' }), '12+3', 'encoding #5');
assert.same(new Uint8Array([215, 111, 183]).toBase64({ alphabet: 'base64url' }), '12-3', 'encoding #6');

assert.throws(() => toBase64.call(null), TypeError, "isn't generic #1");
assert.throws(() => toBase64.call(undefined), TypeError, "isn't generic #2");
assert.throws(() => toBase64.call(new Int16Array([1])), TypeError, "isn't generic #3");
assert.throws(() => toBase64.call([1]), TypeError, "isn't generic #4");
});
16 changes: 16 additions & 0 deletions tests/unit-global/esnext.uint8-array.to-hex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DESCRIPTORS } from '../helpers/constants.js';

if (DESCRIPTORS) QUnit.test('Uint8Array.prototype.toHex', assert => {
const { toHex } = Uint8Array.prototype;
assert.isFunction(toHex);
assert.arity(toHex, 0);
assert.name(toHex, 'toHex');
assert.looksNative(toHex);

assert.same(new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]).toHex(), '48656c6c6f20576f726c64', 'proper result');

assert.throws(() => toHex.call(null), TypeError, "isn't generic #1");
assert.throws(() => toHex.call(undefined), TypeError, "isn't generic #2");
assert.throws(() => toHex.call(new Int16Array([1])), TypeError, "isn't generic #3");
assert.throws(() => toHex.call([1]), TypeError, "isn't generic #4");
});

0 comments on commit 86f6f87

Please sign in to comment.