Skip to content

Commit f3bd396

Browse files
committed
Use 'ava' framework for unit testing
1 parent e349e4e commit f3bd396

File tree

5 files changed

+101
-81
lines changed

5 files changed

+101
-81
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# bytebuf
2+
![npm](https://img.shields.io/npm/v/bytebuf) ![David](https://img.shields.io/david/electroid/bytebuf)
23

34
A byte buffer for encoding and decoding binary data in JavaScript. Supports the major web browsers, NodeJS, Deno, and Cloudflare Workers.
45

bytebuf_test.mjs renamed to bytebuf.test.ts

+69-72
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
import { ByteBuf } from "./dist/bytebuf.mjs"
1+
import test, { ExecutionContext } from "ava"
2+
import { ByteBuf } from "./bytebuf.js"
23

3-
const tests = [
4+
interface TestValue<V> {
5+
readonly value: V
6+
readonly bytes: Uint8Array
7+
readonly littleEndian?: boolean
8+
readonly byteLength?: number
9+
readonly byteEncoding?: string
10+
}
11+
12+
interface TestEntry<V> {
13+
readonly type: string
14+
readonly values: TestValue<V>[]
15+
getValue(buffer: ByteBuf, entry: TestValue<V>): V
16+
readValue(buffer: ByteBuf, entry: TestValue<V>): V
17+
setValue(buffer: ByteBuf, entry: TestValue<V>): any
18+
writeValue(buffer: ByteBuf, entry: TestValue<V>): any
19+
}
20+
21+
const entries: TestEntry<any>[] = [
422
{
523
type: "Bool",
624
getValue: function(buffer) {
@@ -10,10 +28,10 @@ const tests = [
1028
return buffer.readBool()
1129
},
1230
setValue: function(buffer, entry) {
13-
return buffer.setBool(0, entry.value)
31+
buffer.setBool(0, entry.value)
1432
},
1533
writeValue: function(buffer, entry) {
16-
return buffer.writeBool(entry.value)
34+
buffer.writeBool(entry.value)
1735
},
1836
values: [
1937
{
@@ -373,9 +391,9 @@ const tests = [
373391
getValue: function(buffer, entry) {
374392
const { value, byteLength } = buffer.getVarInt(0, entry.byteLength)
375393

376-
console.assert(byteLength === entry.byteLength,
377-
{ type: "VarInt", expected: entry.byteLength,
378-
actual: byteLength, value })
394+
if (entry.byteLength !== byteLength) {
395+
throw new RangeError("Expected byteLength to be " + entry.byteLength + ", but received " + byteLength)
396+
}
379397

380398
return value
381399
},
@@ -431,9 +449,9 @@ const tests = [
431449
getValue: function(buffer, entry) {
432450
const { value, byteLength } = buffer.getVarUint(0, entry.byteLength)
433451

434-
console.assert(byteLength === entry.byteLength,
435-
{ type: "VarUint", expected: entry.byteLength,
436-
actual: byteLength, value })
452+
if (entry.byteLength !== byteLength) {
453+
throw new RangeError("Expected byteLength to be " + entry.byteLength + ", but received " + byteLength)
454+
}
437455

438456
return value
439457
},
@@ -474,9 +492,9 @@ const tests = [
474492
getValue: function(buffer, entry) {
475493
const { value, byteLength } = buffer.getVarZint(0, entry.byteLength)
476494

477-
console.assert(byteLength === entry.byteLength,
478-
{ type: "VarZint", expected: entry.byteLength,
479-
actual: byteLength, value })
495+
if (entry.byteLength !== byteLength) {
496+
throw new RangeError("Expected byteLength to be " + entry.byteLength + ", but received " + byteLength)
497+
}
480498

481499
return value
482500
},
@@ -523,8 +541,8 @@ const tests = [
523541
},
524542
values: [
525543
{
526-
value: new Uint8Array([]),
527-
bytes: new Uint8Array([])
544+
value: new Uint8Array(0),
545+
bytes: new Uint8Array(0)
528546
},
529547
{
530548
value: new Uint8Array([0xff]),
@@ -552,8 +570,8 @@ const tests = [
552570
},
553571
values: [
554572
{
555-
value: new Uint16Array([]),
556-
bytes: new Uint8Array([])
573+
value: new Uint16Array(0),
574+
bytes: new Uint8Array(0)
557575
},
558576
{
559577
value: new Uint16Array([0xff]),
@@ -621,9 +639,9 @@ const tests = [
621639
getValue: function(buffer, entry) {
622640
const { value, byteLength } = buffer.getVarString(0, entry.byteLength)
623641

624-
console.assert(byteLength === entry.byteLength,
625-
{ type: "VarString", expected: entry.byteLength,
626-
actual: byteLength, value })
642+
if (entry.byteLength !== byteLength) {
643+
throw new RangeError("Expected byteLength to be " + entry.byteLength + ", but received " + byteLength)
644+
}
627645

628646
return value
629647
},
@@ -656,60 +674,39 @@ const tests = [
656674
}
657675
]
658676

659-
function equals(a, b) {
660-
if (a === b) return true
661-
if (!ArrayBuffer.isView(a) || !ArrayBuffer.isView(b)) return false
662-
if (a.byteLength !== b.byteLength) return false
663-
return a.every((val, i) => val === b[i])
664-
}
677+
test("ByteBuf", (ctx: ExecutionContext) => {
678+
for (const entry of entries) {
679+
const { type, values, getValue, readValue, setValue, writeValue } = entry
665680

666-
function testType(test) {
667-
const { type, values, getValue, readValue, setValue, writeValue } = test
681+
for (const entry of values) {
682+
const { value, bytes, byteLength } = entry
683+
const buffer = ByteBuf.from(bytes)
684+
685+
const valueGet = getValue(buffer, entry)
686+
ctx.deepEqual(value, valueGet,
687+
`expected get${type}() to return ${value}, but got ${valueGet} instead.`)
668688

669-
for (const entry of values) {
670-
const { value, bytes, byteLength } = entry
671-
const buffer = ByteBuf.from(bytes.slice())
672-
673-
const decoded = getValue(buffer, entry)
674-
console.assert(equals(value, decoded),
675-
{ method: `get${type}`, expected: value, actual: decoded, bytes })
676-
677-
if (!equals(decoded, readValue(buffer, entry))) {
678-
console.warn(`read${type} return value does not match get${type}`)
679-
}
689+
const valueRead = readValue(buffer, entry)
690+
ctx.deepEqual(valueGet, valueRead,
691+
`expected read${type}() to return ${value}, but got ${valueRead} instead.`)
692+
ctx.is(buffer.byteOffset, buffer.byteLength,
693+
`expected read${type}() to offset ${buffer.byteLength} bytes, but got ${buffer.byteOffset} instead.`)
694+
695+
buffer.reset()
696+
buffer.clear()
680697

681-
if (buffer.byteOffset !== buffer.byteLength) {
682-
console.warn(`read${type} byte offset does not match ${buffer.byteLength}`)
698+
const lengthSet = setValue(buffer, entry)
699+
ctx.is(lengthSet, byteLength,
700+
`expected set${type}(${value}) to return a byte length of ${byteLength}, but got ${lengthSet} instead.`)
701+
const bytesSet = new Uint8Array(buffer.buffer)
702+
ctx.deepEqual(bytesSet, bytes,
703+
`expected set${type}(${value}) to encode as ${bytes}, but got ${bytesSet} instead.`)
704+
705+
writeValue(buffer, entry)
706+
ctx.deepEqual(bytesSet, bytes,
707+
`expected write${type}(${value}) to encode as ${bytes}, but got ${bytesSet} instead.`)
708+
ctx.is(buffer.byteOffset, buffer.byteLength,
709+
`expected write${type}(${value}) to offset ${buffer.byteLength} bytes, but got ${buffer.byteOffset} instead.`)
683710
}
684-
685-
buffer.reset()
686-
new Uint8Array(buffer.buffer).set(new Uint8Array(buffer.byteLength))
687-
688-
const encodedLength = setValue(buffer, entry)
689-
console.assert(byteLength === encodedLength,
690-
{ method: `set${type} (length)`,
691-
expected: byteLength, actual: encodedLength, bytes })
692-
693-
const encoded = new Uint8Array(buffer.buffer)
694-
console.assert(equals(bytes, encoded),
695-
{ method: `set${type}`, expected: bytes, actual: encoded, value })
696-
697-
writeValue(buffer, entry)
698-
699-
if (!equals(bytes, encoded)) {
700-
console.warn(`write${type} encoding not match set${type}`)
701-
}
702-
703-
if (buffer.byteOffset !== buffer.byteLength) {
704-
console.warn(`write${type} byte offset does not match ${buffer.byteLength}`)
705-
}
706-
}
707-
}
708-
709-
function testAll() {
710-
for (const test of tests) {
711-
testType(test)
712711
}
713-
}
714-
715-
testAll()
712+
})

bytebuf.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ByteBuf extends DataView {
5050
}
5151

5252
/**
53-
* Gets the number of remaining bytes.
53+
* The number of remaining bytes.
5454
* @returns The number of bytes remaining.
5555
*/
5656
get bytesRemaining(): number {
@@ -971,11 +971,11 @@ interface IntResult {
971971
/**
972972
* The byte length.
973973
*/
974-
byteLength: number
974+
readonly byteLength: number
975975
/**
976976
* The value.
977977
*/
978-
value: number
978+
readonly value: number
979979
}
980980

981981
/**
@@ -985,9 +985,9 @@ interface StringResult {
985985
/**
986986
* The byte length.
987987
*/
988-
byteLength: number
988+
readonly byteLength: number
989989
/**
990990
* The value.
991991
*/
992-
value: string
992+
readonly value: string
993993
}

package.json

+21-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,26 @@
2929
"scripts": {
3030
"build": "tsc && mv dist/bytebuf.js dist/bytebuf.mjs",
3131
"prepublish": "npm run build",
32-
"test:deno": "npm run build && deno run bytebuf_test.mjs",
33-
"test:node": "npm run build && node bytebuf_test.mjs"
32+
"test": "ava"
33+
},
34+
"devDependencies": {
35+
"ava": "^3.15.0",
36+
"ts-node": "^10.2.1",
37+
"typescript": "^4.4.3"
38+
},
39+
"ava": {
40+
"files": [
41+
"*.test.ts"
42+
],
43+
"extensions": {
44+
"ts": "module"
45+
},
46+
"nonSemVerExperiments": {
47+
"configurableModuleFormat": true
48+
},
49+
"nodeArguments": [
50+
"--no-warnings",
51+
"--loader=ts-node/esm"
52+
]
3453
}
3554
}

tsconfig.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compileOnSave": true,
33
"compilerOptions": {
4-
"module": "es2020",
4+
"module": "esnext",
55
"target": "es2020",
66
"moduleResolution": "node",
77
"checkJs": true,
@@ -10,6 +10,9 @@
1010
"outDir": "dist/"
1111
},
1212
"include": [
13-
"**/*.ts"
13+
"*.ts"
14+
],
15+
"exclude": [
16+
"*.test.ts"
1417
]
1518
}

0 commit comments

Comments
 (0)