Skip to content

Commit

Permalink
✅ Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerrylum committed Jun 23, 2022
1 parent 35b9eb8 commit baf9458
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/VexCRC.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import crc16ccitt from 'crc/calculators/crc16ccitt';
import { CrcGenerator } from '../src';


function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}

function getRandomUInt8Array(size: number) {
let rtn: number[] = [];
for (let i = 0; i < size; i++) {
rtn.push(getRandomInt(256));
}
return new Uint8Array(rtn);
}

const crcgen = new CrcGenerator();

test("CRC 16 ccitt", async () => {
for (let i = 10; i < 100; i++) {
let test = getRandomUInt8Array(i);
if (crc16ccitt(test, 0) !== crcgen.crc16(test, 0)) {
fail();
}
}
});
44 changes: 44 additions & 0 deletions test/VexEvent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { VexEventTarget } from "../src";

function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}

test("VexEventEmitter and VexEventTarget", async () => {
let target = new VexEventTarget();

let fn1 = jest.fn();
let fn2 = jest.fn();

target.remove("test1", fn1);

target.emit("test1", undefined);


target.on("test", fn1);

let r1 = getRandomInt(10) + 10;
for (let i = 0; i < r1; i++) target.emit("test", undefined);

target.on("test", fn2);

let r2 = getRandomInt(10) + 10;
for (let i = 0; i < r2; i++) target.emit("test", undefined);

expect(fn1).toBeCalledTimes(r1 + r2);
expect(fn2).toBeCalledTimes(r2);

target.remove("test", fn1);

target.emit("test", undefined);

expect(fn1).toBeCalledTimes(r1 + r2);
expect(fn2).toBeCalledTimes(r2 + 1);

target.clearListeners();

target.emit("test", undefined);

expect(fn1).toBeCalledTimes(r1 + r2);
expect(fn2).toBeCalledTimes(r2 + 1);
});
40 changes: 40 additions & 0 deletions test/VexFirmwareVersion.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { VexFirmwareVersion } from "../src";

function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}

test("Constructor, fromString, fromCatalogString and Uint8 related methods", async () => {
for (let i = 0; i < 1000; i++) {
let a = getRandomInt(256), b = getRandomInt(256), c = getRandomInt(256), d = getRandomInt(256);

let subject = new VexFirmwareVersion(a, b, c, d);
let str = `${a}.${b}.${c}.b${d}`;

expect(subject.toInternalString()).toBe(str);
expect(VexFirmwareVersion.fromString(str)).toEqual(subject);
expect(VexFirmwareVersion.fromCatalogString(`${a}_${b}_${c}_b${d}`)).toEqual(subject);
expect(VexFirmwareVersion.fromUint8Array(subject.toUint8Array(false)).toInternalString()).toBe(str);
expect(VexFirmwareVersion.fromUint8Array(subject.toUint8Array(true), 0, true).toInternalString()).toBe(str);
subject.beta = 0;
expect(VexFirmwareVersion.fromString(`${a}.${b}.${c}`)).toEqual(subject);
}
});

test("Compare", async () => {
let base = new VexFirmwareVersion(1, 2, 3, 4);
expect(base.compare(base)).toBe(0);
expect(new VexFirmwareVersion(1, 2, 3, 5).compare(base)).toBe(1);
expect(new VexFirmwareVersion(1, 2, 4, 5).compare(base)).toBe(1);
expect(new VexFirmwareVersion(1, 3, 4, 5).compare(base)).toBe(1);
expect(new VexFirmwareVersion(2, 3, 4, 5).compare(base)).toBe(1);
});

test("Is beta", async () => {
expect(VexFirmwareVersion.allZero().isBeta()).toBeFalsy();
expect(new VexFirmwareVersion(0, 0, 0, 1).isBeta()).toBeTruthy();
});

test("All zero", async () => {
expect(VexFirmwareVersion.allZero().toInternalString()).toBe("0.0.0.b0");
});

0 comments on commit baf9458

Please sign in to comment.