Initial commit

This commit is contained in:
Endeavorance 2025-03-25 16:15:04 -04:00
commit 1baf20f6c0
9 changed files with 825 additions and 0 deletions

228
src/test/parse.test.ts Normal file
View file

@ -0,0 +1,228 @@
import { describe, expect, test } from "bun:test";
import { Parse, ParseError } from "../index.ts";
describe(Parse.unknown, () => {
test("returns the value as provided", () => {
const val = Parse.unknown("hello");
expect(val).toBe("hello");
});
});
describe(Parse.string, () => {
test("Parses strings", () => {
expect(Parse.string("hello")).toBe("hello");
});
test("Throws an error when parsing a non-string", () => {
const INVALID_VALUES = [null, undefined, 123, true, false, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.string(val)).toThrow(ParseError);
}
});
});
describe(Parse.number, () => {
test("Parses numbers", () => {
expect(Parse.number(123)).toBe(123);
});
test("Throws an error when parsing a non-number", () => {
const INVALID_VALUES = [null, undefined, "hello", true, false, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.number(val)).toThrow(ParseError);
}
});
});
describe(Parse.boolean, () => {
test("Parses booleans", () => {
expect(Parse.boolean(true)).toBe(true);
expect(Parse.boolean(false)).toBe(false);
});
test("Throws an error when parsing a non-boolean", () => {
const INVALID_VALUES = [null, undefined, "hello", 123, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.boolean(val)).toThrow(ParseError);
}
});
});
describe(Parse.bigint, () => {
test("Parses bigints", () => {
expect(Parse.bigint(BigInt(123))).toBe(BigInt(123));
});
test("Throws an error when parsing a non-bigint", () => {
const INVALID_VALUES = [null, undefined, "hello", 123, true, false, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.bigint(val)).toThrow(ParseError);
}
});
});
describe(Parse.symbol, () => {
test("Parses symbols", () => {
const symbol = Symbol("hello");
expect(Parse.symbol(symbol)).toBe(symbol);
});
test("Throws an error when parsing a non-symbol", () => {
const INVALID_VALUES = [null, undefined, "hello", 123, true, false, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.symbol(val)).toThrow(ParseError);
}
});
});
describe(Parse.null, () => {
test("Parses null", () => {
expect(Parse.null(null)).toBe(null);
});
test("Throws an error when parsing a non-null", () => {
const INVALID_VALUES = [undefined, "hello", 123, true, false, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.null(val)).toThrow(ParseError);
}
});
});
describe(Parse.undefined, () => {
test("Parses undefined", () => {
expect(Parse.undefined(undefined)).toBe(undefined);
});
test("Throws an error when parsing a non-undefined", () => {
const INVALID_VALUES = [null, "hello", 123, true, false, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.undefined(val)).toThrow(ParseError);
}
});
});
describe(Parse.arrayOf, () => {
test("Parses arrays of a specific type", () => {
const arr = [1, 2, 3];
expect(Parse.arrayOf(Parse.number)(arr)).toEqual(arr);
});
test("Throws an error when parsing a non-array", () => {
expect(() => Parse.arrayOf(Parse.number)("hello")).toThrow(ParseError);
});
test("Throws an error when parsing an array with invalid values", () => {
const INVALID_VALUES = [null, undefined, "hello", true, false, {}];
for (const val of INVALID_VALUES) {
const arr = [1, val, 3];
expect(() => Parse.arrayOf(Parse.number)(arr)).toThrow(ParseError);
}
});
});
describe(Parse.shape, () => {
test("Parses objects", () => {
const obj = { name: "hello", age: 123 };
const shape = { name: Parse.string, age: Parse.number };
expect(Parse.shape(shape)(obj)).toEqual(obj);
});
test("Throws an error when parsing a non-object", () => {
expect(() => Parse.shape({})("hello")).toThrow(ParseError);
});
test("Throws an error when parsing an object with invalid values", () => {
const obj = { name: "hello", age: "world" };
const shape = { name: Parse.string, age: Parse.number };
expect(() => Parse.shape(shape)(obj)).toThrow(ParseError);
});
});
describe(Parse.enum, () => {
test("Parses valid enum values", () => {
const enumValues = ["hello", "world"] as const;
expect(Parse.enum(enumValues)("hello")).toBe("hello");
});
test("Throws an error when parsing an invalid enum value", () => {
const enumValues = ["hello", "world"] as const;
expect(() => Parse.enum(enumValues)("invalid")).toThrow(ParseError);
});
});
describe(Parse.regex, () => {
test("Parses valid regex values", () => {
expect(Parse.regex(/hello/)("hello")).toBe("hello");
});
test("Throws an error when parsing an invalid regex value", () => {
expect(() => Parse.regex(/hello/)("world")).toThrow(ParseError);
});
});
describe(Parse.optional, () => {
test("Parses undefined values", () => {
expect(Parse.optional(Parse.string)(undefined)).toBeUndefined();
});
});
describe(Parse.nullable, () => {
test("Parses null values", () => {
expect(Parse.nullable(Parse.string)(null)).toBeNull();
});
});
describe(Parse.nan, () => {
test("Parses NaN", () => {
expect(Parse.nan(Number.NaN)).toBe(Number.NaN);
});
test("Throws an error when parsing a non-NaN", () => {
const INVALID_VALUES = [null, undefined, "hello", 123, true, false, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.nan(val)).toThrow(ParseError);
}
});
});
class TestClass { }
describe(Parse.instanceOf, () => {
test("Parses instances of a class", () => {
const testClass = new TestClass();
expect(Parse.instanceOf(TestClass)(testClass)).toBe(testClass);
});
test("Throws an error when parsing a non-instance", () => {
const INVALID_VALUES = [null, undefined, "hello", 123, true, false, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.instanceOf(TestClass)(val)).toThrow(ParseError);
}
});
test("Throws an error when parsing an instance of a different class", () => {
const testClass = new TestClass();
expect(() => Parse.instanceOf(class AnotherClass { })(testClass)).toThrow(
ParseError,
);
});
});
describe(Parse.oneOf, () => {
test("Parses values that match one of the parsers", () => {
const parsers = [Parse.string, Parse.number];
expect(Parse.oneOf(parsers)("hello")).toEqual("hello");
});
test("Throws an error when parsing a value that doesn't match any parser", () => {
const parsers = [Parse.string, Parse.number];
const INVALID_VALUES = [null, undefined, true, false, {}];
for (const val of INVALID_VALUES) {
expect(() => Parse.oneOf(parsers)(val)).toThrow(ParseError);
}
});
});