Skip to content

Commit

Permalink
- add running tests to the CI pipeline (#12)
Browse files Browse the repository at this point in the history
- small refactor of shared type
  • Loading branch information
mieszkosabo committed Apr 15, 2024
1 parent b5e4d8d commit 864c6ce
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ jobs:

- name: Run check:types
run: bun check:types

- name: Run tests
run: bun test
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"scripts": {
"build": "tsup",
"clean": "rm -rf dist",
"prettier:write": "prettier --write .",
"check:lint": "eslint .",
"check:format": "prettier --check .",
Expand Down
6 changes: 1 addition & 5 deletions src/arrays.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fn } from "hotscript";
import { Validator } from "./shared.types";
import { Identity, Validator } from "./shared.types";

interface ArrayType extends Fn {
return: this["args"] extends [...any, infer last]
Expand Down Expand Up @@ -54,10 +54,6 @@ export const nonEmpty = () => ({
},
});

interface Identity extends Fn {
return: this["arg0"];
}

export const min = (min: number) => ({
name: "min" as const,
$inputType: "array" as unknown as [any, ...any[]],
Expand Down
6 changes: 1 addition & 5 deletions src/object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fn } from "hotscript";
import { Validator } from "./shared.types";
import { Identity, Validator } from "./shared.types";

interface ObjectSchema extends Fn {
return: MakeFieldsWithUndefinedOptional<{
Expand Down Expand Up @@ -52,10 +52,6 @@ type MakeFieldsWithUndefinedOptional<T> = Expand<
} & { [K in FieldsWithUndefined<T>]?: T[K] }
>;

interface Identity extends Fn {
return: this["arg0"];
}

export const passthrough = () => {
const ctx = {
chain: null as Validator<any, any> | null,
Expand Down
6 changes: 1 addition & 5 deletions src/sets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fn } from "hotscript";
import { Validator } from "./shared.types";
import { Identity, Validator } from "./shared.types";

interface SetType extends Fn {
return: Set<this["arg1"]["$outputType"]>;
Expand Down Expand Up @@ -27,10 +27,6 @@ export const set = <const Schema extends Validator<any, any>>(
},
});

interface Identity extends Fn {
return: this["arg0"];
}

export const nonEmpty = () => ({
name: "nonEmpty" as const,
$inputType: "set" as unknown as Set<any>,
Expand Down
6 changes: 6 additions & 0 deletions src/shared.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Fn } from "hotscript";

export type Validator<Input, Output> = {
name: string;
$inputType: Input;
Expand All @@ -17,3 +19,7 @@ export type Primitive =
| symbol
| null
| undefined;

export interface Identity extends Fn {
return: this["arg0"];
}
79 changes: 79 additions & 0 deletions tests/arrays.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { describe, expect, test } from "bun:test";
import { initCorrettore, min, string, arrays, Infer } from "../src";
import { Equal, Expect } from "./helpers.types";

describe("arrays", () => {
const c = initCorrettore([
arrays.array,
string,
min,
arrays.nonEmpty,
arrays.min,
arrays.max,
arrays.length,
]);

test("arrays", () => {
expect(() => c.array(c.string().min(2)).parse(["ab", "ba"])).not.toThrow();
expect(() => c.array(c.string()).parse([])).not.toThrow();
expect(() => c.array(c.string()).parse([42])).toThrow();
expect(() =>
c.array(c.string()).nonEmpty().parse(["a", "b"]),
).not.toThrow();
expect(() => c.array(c.string()).nonEmpty().parse([])).toThrow();
expect(() =>
c
.array(c.string())
.nonEmpty()
.min(3)
.max(5)
.length(4)
.parse(["a", "b", "c", "d"]),
).not.toThrow();
expect(() =>
c
.array(c.string())
.nonEmpty()
.min(3)
.max(5)
.length(4)
.parse(["a", "b", "d"]),
).toThrow();

const schema = c.array(c.string());
type SchemaType = Infer<typeof schema>;
type _test = Expect<Equal<SchemaType, string[]>>;
});

test("arrays alternative syntax", () => {
expect(() => c.string().min(2).array().parse(["ab", "ba"])).not.toThrow();
expect(() => c.string().array().parse([])).not.toThrow();
expect(() => c.string().array().parse([42])).toThrow();
expect(() => c.string().array().nonEmpty().parse(["a", "b"])).not.toThrow();
expect(() => c.string().array().nonEmpty().parse([])).toThrow();
expect(() =>
c
.string()
.array()
.nonEmpty()
.min(3)
.max(5)
.length(4)
.parse(["a", "b", "c", "d"]),
).not.toThrow();
expect(() =>
c
.string()
.array()
.nonEmpty()
.min(3)
.max(5)
.length(4)
.parse(["a", "b", "d"]),
).toThrow();

const schema = c.string().array();
type SchemaType = Infer<typeof schema>;
type _test = Expect<Equal<SchemaType, string[]>>;
});
});
66 changes: 2 additions & 64 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ describe("basic tests", () => {
coerce.bigintCoerce,
]);

// TODO: split into *.test.ts files and add more tests

test("smoke tests", () => {
expect(() => c.number().parse(42)).not.toThrow();
expect(() => c.number().parse("hello")).toThrow();
Expand Down Expand Up @@ -115,70 +117,6 @@ describe("basic tests", () => {
type _typeTest3 = Expect<Equal<InferredSchema3, [string, ...string[]]>>;
});

test("arrays", () => {
expect(() => c.array(c.string().min(2)).parse(["ab", "ba"])).not.toThrow();
expect(() => c.array(c.string()).parse([])).not.toThrow();
expect(() => c.array(c.string()).parse([42])).toThrow();
expect(() =>
c.array(c.string()).nonEmpty().parse(["a", "b"]),
).not.toThrow();
expect(() => c.array(c.string()).nonEmpty().parse([])).toThrow();
expect(() =>
c
.array(c.string())
.nonEmpty()
.min(3)
.max(5)
.length(4)
.parse(["a", "b", "c", "d"]),
).not.toThrow();
expect(() =>
c
.array(c.string())
.nonEmpty()
.min(3)
.max(5)
.length(4)
.parse(["a", "b", "d"]),
).toThrow();

const schema = c.array(c.string());
type SchemaType = Infer<typeof schema>;
type _test = Expect<Equal<SchemaType, string[]>>;
});

test("arrays alternative syntax", () => {
expect(() => c.string().min(2).array().parse(["ab", "ba"])).not.toThrow();
expect(() => c.string().array().parse([])).not.toThrow();
expect(() => c.string().array().parse([42])).toThrow();
expect(() => c.string().array().nonEmpty().parse(["a", "b"])).not.toThrow();
expect(() => c.string().array().nonEmpty().parse([])).toThrow();
expect(() =>
c
.string()
.array()
.nonEmpty()
.min(3)
.max(5)
.length(4)
.parse(["a", "b", "c", "d"]),
).not.toThrow();
expect(() =>
c
.string()
.array()
.nonEmpty()
.min(3)
.max(5)
.length(4)
.parse(["a", "b", "d"]),
).toThrow();

const schema = c.string().array();
type SchemaType = Infer<typeof schema>;
type _test = Expect<Equal<SchemaType, string[]>>;
});

test("sets", () => {
expect(() =>
c.set(c.string().min(2)).parse(new Set(["ab", "ba"])),
Expand Down

0 comments on commit 864c6ce

Please sign in to comment.