-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
284 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,6 @@ | |
] | ||
} | ||
}, | ||
"include": ["src", "index.ts"], | ||
"include": ["src", "tests","index.ts"], | ||
"exclude": ["node_modules"] | ||
} |