Skip to content

Commit

Permalink
test: More type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 20, 2024
1 parent d1254e4 commit 93de82e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
4 changes: 3 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,9 @@ for (const name in CURVES) {
throws(() => C.getPublicKey(undefined), 'undefined');
throws(() => C.getPublicKey(''), "''");
// NOTE: passes because of disabled hex padding checks for starknet, maybe enable?
// throws(() => C.getPublicKey('1'), "'1'");
if (name !== 'starknet') {
// throws(() => C.getPublicKey('1'), "'1'");
}
throws(() => C.getPublicKey('key'), "'key'");
throws(() => C.getPublicKey({}));
throws(() => C.getPublicKey(new Uint8Array([])));
Expand Down
29 changes: 26 additions & 3 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function json(path) {
}
}

// Everything except undefined, string, Uint8Array
const TYPE_TEST_BASE = [
null,
[1, 2, 3],
Expand All @@ -33,24 +34,46 @@ const TYPE_TEST_BASE = [
new Uint32Array([1, 2, 3]),
100n,
new Set([1, 2, 3]),
new Map([['aa', 'bb']]),
new Uint8ClampedArray([1, 2, 3]),
new Int16Array([1, 2, 3]),
new Float32Array([1]),
new BigInt64Array([1n, 2n, 3n]),
new ArrayBuffer(100),
new DataView(new ArrayBuffer(100)),
{ constructor: { name: 'Uint8Array' }, length: '1e30' },
() => {},
async () => {},
class Test {},
Symbol.for('a'),
new Proxy(new Uint8Array(), {}),
];
const TYPE_TEST_NOT_STR = [

const TYPE_TEST_OPT = [
'',
new Uint8Array(),
new (class Test {})(),
class Test {},
() => {},
0,
0.1234,
NaN,
null,
];

const TYPE_TEST_NOT_BOOL = [false, true];
const TYPE_TEST_NOT_BYTES = ['', 'test', '1', new Uint8Array([]), new Uint8Array([1, 2, 3])];
const TYPE_TEST_NOT_HEX = [
'0xbe',
' 1 2 3 4 5',
'010203040x',
'abcdefgh',
'1 2 3 4 5 ',
'bee',
new String('1234'),
];
const TYPE_TEST_NOT_INT = [-0.0, 0, 1];
export const TYPE_TEST = {
hex: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_STR),
bytes: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_STR),
bytes: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BOOL),
hex: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_INT, TYPE_TEST_NOT_BOOL, TYPE_TEST_NOT_HEX),
};
21 changes: 12 additions & 9 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { throws } from 'node:assert';
import { deepStrictEqual, throws } from 'node:assert';
import { describe, should } from 'micro-should';
import { bytesToHex, hexToBytes } from '../esm/abstract/utils.js';
import { TYPE_TEST } from './utils.js';

// Here goes test for tests...
describe('Tests', () => {
describe('utils', () => {
const staticHexVectors = [
{ bytes: Uint8Array.from([]), hex: '' },
{ bytes: Uint8Array.from([0xbe]), hex: 'be' },
{ bytes: Uint8Array.from([0xca, 0xfe]), hex: 'cafe' },
{ bytes: Uint8Array.from(new Array(1024).fill(0x69)), hex: '69'.repeat(1024) },
];
should('hexToBytes', () => {
for (let v of staticHexVectors) deepStrictEqual(hexToBytes(v.hex), v.bytes);
for (let v of TYPE_TEST.hex) {
throws(() => {
hexToBytes(v);
});
throws(() => hexToBytes(v));
}
});
should('bytesToHex', () => {
for (let v of staticHexVectors) deepStrictEqual(bytesToHex(v.bytes), v.hex);
for (let v of TYPE_TEST.bytes) {
throws(() => {
bytesToHex(v);
});
throws(() => bytesToHex(v));
}
});
});
Expand Down

0 comments on commit 93de82e

Please sign in to comment.