Skip to content

Commit

Permalink
test: add utils.test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 18, 2024
1 parent 308df93 commit f18ee49
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,34 @@ export function json(path) {
return require('./' + file + '.json'); // in this form so that bundler can glob this
}
}


const TYPE_TEST_BASE = [
null,
[1, 2, 3],
{ a: 1, b: 2, c: 3 },
NaN,
0.1234,
1.0000000000001,
10e9999,
new Uint32Array([1, 2, 3]),
100n,
new Set([1, 2, 3]),
new Uint8ClampedArray([1, 2, 3]),
new Int16Array([1, 2, 3]),
new ArrayBuffer(100),
new DataView(new ArrayBuffer(100)),
{ constructor: { name: 'Uint8Array' }, length: '1e30' },
() => {},
async () => {},
class Test {},
];
const TYPE_TEST_NOT_STR = [
' 1 2 3 4 5',
'010203040x',
'abcdefgh',
'1 2 3 4 5 ',
'bee',
new String('1234'),
];
export const TYPE_TEST = { hex: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_STR), bytes: TYPE_TEST_BASE.concat(TYPE_TEST_NOT_STR) };
28 changes: 28 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { 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', () => {
should('hexToBytes', () => {
for (let v of TYPE_TEST.hex) {
throws(() => {
hexToBytes(v);
});
}
});
should('bytesToHex', () => {
for (let v of TYPE_TEST.bytes) {
throws(() => {
bytesToHex(v);
});
}
});
});

// ESM is broken.
import url from 'node:url';
if (import.meta.url === url.pathToFileURL(process.argv[1]).href) {
should.run();
}

0 comments on commit f18ee49

Please sign in to comment.