Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed May 2, 2024
1 parent 2a7b151 commit 870f232
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 3 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
"scripts": {
"start": "bun index.ts",
"check": "bunx @biomejs/biome check --apply ./",
"build": "tsup"
"build": "tsup",
"test": "vitest"
},
"devDependencies": {
"@biomejs/biome": "1.7.0",
"@fast-check/vitest": "0.1.1",
"@types/bun": "latest",
"tsup": "8.0.2"
"tsup": "8.0.2",
"vitest": "1.5.3"
},
"peerDependencies": {
"typescript": "5.4.5"
Expand Down
32 changes: 32 additions & 0 deletions tests/array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {fc, test} from "@fast-check/vitest";
import {describe, expect} from "vitest";
import {decode, encode, Protocol} from "../src";
import {UINT16, UINT8} from "./shared";

describe("array", () => {
test.prop([fc.array(fc.integer(UINT8), { maxLength: UINT8.max })])("8", (value) => {
const type = {
type: "array",
size: 8,
item: {type: "int", size: 8}
} satisfies Protocol.Array;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toEqual(value);
});

test.prop([fc.array(fc.integer(UINT8), { maxLength: UINT16.max })])("8", (value) => {
const type = {
type: "array",
size: 16,
item: {type: "int", size: 8}
} satisfies Protocol.Array;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toEqual(value);
});
});
34 changes: 34 additions & 0 deletions tests/assert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {fc, test} from "@fast-check/vitest";
import {describe, expect} from "vitest";
import {decode, encode, Protocol} from "../src";
import {UINT8} from "./shared";

describe("assert", () => {
const assert = (value: unknown) => {
if (value === 0) {
throw new Error("zero");
}

return value;
};

test.prop([fc.integer({ min: 1, max: UINT8.max})])("passes", (value) => {
const type = { type: "int", size: 8, assert } satisfies Protocol.Int;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});

test.prop([fc.constant(0)])("throws", (value) => {
const throwType = { type: "int", size: 8, assert } satisfies Protocol.Int;
const passType = { type: "int", size: 8 } satisfies Protocol.Int;

expect(() => encode(throwType, value)).toThrow("zero");

const encoded = encode(passType, value);

expect(() => decode(throwType, encoded)).toThrow("zero");
});
});
14 changes: 14 additions & 0 deletions tests/boolean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {fc, test} from "@fast-check/vitest";
import {expect} from "vitest";
import {decode, encode, Protocol} from "../src";

test.prop([fc.boolean()])("boolean", (value) => {
const type = {
type: "boolean",
} satisfies Protocol.Boolean;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toEqual(value);
});
15 changes: 15 additions & 0 deletions tests/enum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {fc, test} from "@fast-check/vitest";
import {expect} from "vitest";
import {decode, encode, Protocol} from "../src";

test.prop([fc.oneof(fc.constant("ADMIN"), fc.constant("USER"))])("enum", (value) => {
const type = {
type: "enum",
options: ["ADMIN", "USER"]
} satisfies Protocol.Enum;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toEqual(value);
});
23 changes: 23 additions & 0 deletions tests/float.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {fc, test} from "@fast-check/vitest";
import {describe, expect} from "vitest";
import {decode, encode, Protocol} from "../src";

describe("float", () => {
test.prop([fc.float()])("float32", (value) => {
const type = { type: "float", size: 32 } satisfies Protocol.Float;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});

test.prop([fc.integer()])("float64", (value) => {
const type = { type: "float", size: 64 } satisfies Protocol.Float;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});
});
60 changes: 60 additions & 0 deletions tests/int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {fc, test} from "@fast-check/vitest";
import {describe, expect} from "vitest";
import {decode, encode, Protocol} from "../src";
import {INT16, INT32, INT8, UINT16, UINT32, UINT8} from "./shared";

describe("int", () => {
test.prop([fc.integer(UINT8)])("uint8", (value) => {
const type = { type: "int", size: 8, signed: false } satisfies Protocol.Int;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});

test.prop([fc.integer(UINT16)])("uint16", (value) => {
const type = { type: "int", size: 16, signed: false } satisfies Protocol.Int;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});

test.prop([fc.integer(UINT32)])("uint32", (value) => {
const type = { type: "int", size: 32, signed: false } satisfies Protocol.Int;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});

test.prop([fc.integer(INT8)])("int8", (value) => {
const type = { type: "int", size: 8, signed: true } satisfies Protocol.Int;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});

test.prop([fc.integer(INT16)])("int16", (value) => {
const type = { type: "int", size: 16, signed: true } satisfies Protocol.Int;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});

test.prop([fc.integer(INT32)])("int32", (value) => {
const type = { type: "int", size: 32, signed: true } satisfies Protocol.Int;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});
});
24 changes: 24 additions & 0 deletions tests/nullable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {fc, test} from "@fast-check/vitest";
import {describe, expect} from "vitest";
import {decode, encode, Protocol} from "../src";
import {UINT8} from "./shared";

describe("nullable", () => {
test.prop([fc.oneof(fc.integer(UINT8), fc.constant(null))])("true", (value) => {
const type = { type: "int", size: 8, nullable: true } satisfies Protocol.Int;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});

test.prop([fc.integer(UINT8)])("false", (value) => {
const type = { type: "int", size: 8, nullable: false } satisfies Protocol.Int;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});
});
22 changes: 22 additions & 0 deletions tests/object.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {fc, test} from "@fast-check/vitest";
import {expect} from "vitest";
import {decode, encode, Protocol} from "../src";
import {UINT8} from "./shared";

test.prop([fc.record({
name: fc.string(),
age: fc.integer({min: 0, max: 150}),
})])("object", (value) => {
const type = {
type: "object",
properties: [
{key: "name", type: "string", kind: "ascii", size: 8},
{key: "age", type: "int", size: 8},
]
} satisfies Protocol.Object;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toEqual(value);
});
7 changes: 7 additions & 0 deletions tests/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const UINT8 = { min: 0, max: 255 };
export const UINT16 = { min: 0, max: 65_535 };
export const UINT32 = { min: 0, max: 4_294_967_295 };

export const INT8 = {min: -128, max: 127};
export const INT16 = {min: -32_768, max: 32_767};
export const INT32 = {min: -2_147_483_648, max: 2_147_483_647};
28 changes: 28 additions & 0 deletions tests/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {fc, test} from "@fast-check/vitest";
import {describe, expect} from "vitest";
import {decode, encode, Protocol} from "../src";
import {UINT16, UINT8} from "./shared";

describe("string", () => {
describe("ascii", () => {
test.prop([fc.string({ maxLength: UINT8.max })])("8", (value) => {
const type = { type: "string", kind: "ascii", size: 8 } satisfies Protocol.String;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});

test.prop([fc.string({ maxLength: UINT16.max })])("16", (value) => {
const type = { type: "string", kind: "ascii", size: 16 } satisfies Protocol.String;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toBe(value);
});
});

// TODO: once utf8/16 are implemented, add tests for them
});
19 changes: 19 additions & 0 deletions tests/tuple.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {fc, test} from "@fast-check/vitest";
import {expect} from "vitest";
import {decode, encode, Protocol} from "../src";
import {UINT8} from "./shared";

test.prop([fc.tuple(fc.integer(UINT8), fc.string())])("tuple", (value) => {
const type = {
type: "tuple",
items: [
{type: "int", size: 8},
{type: "string", kind: "ascii", size: 8},
]
} satisfies Protocol.Tuple;

const encoded = encode(type, value);
const decoded = decode(type, encoded);

expect(decoded).toEqual(value);
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
]
}
},
"include": ["src", "index.ts"],
"include": ["src", "tests","index.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 870f232

Please sign in to comment.