♻️ Label arrange-act-assert blocks in specs
Rework every test body into // Arrange / // Act / // Assert blocks separated by blank lines: the factory is arranged once, the matcher is built from it in a single act, and all type/runtime checks sink to the end. index.test.ts adopts the same shape. Also migrate off expect-type's deprecated toMatchTypeOf: the checks are assignability tests, so toExtend is the faithful replacement.
This commit is contained in:
1 parent
a25ac6d1e2
commit
34567856a5
2 files changed
+74
-15
No files matched your search
+17
-2
@@ -7,44 +7,59 @@ import { expectTypeOf } from "expect-type";
|
|||||||
import { type Matcher, P, match } from "./index.ts";
|
import { type Matcher, P, match } from "./index.ts";
|
||||||
|
|
||||||
test("match returns a builder", () => {
|
test("match returns a builder", () => {
|
||||||
|
// Act
|
||||||
const builder = match("x");
|
const builder = match("x");
|
||||||
|
|
||||||
|
// Assert
|
||||||
expectTypeOf(builder).toHaveProperty("with");
|
expectTypeOf(builder).toHaveProperty("with");
|
||||||
expectTypeOf(builder).toHaveProperty("exhaustive");
|
expectTypeOf(builder).toHaveProperty("exhaustive");
|
||||||
expectTypeOf(builder).toHaveProperty("otherwise");
|
expectTypeOf(builder).toHaveProperty("otherwise");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("P.literal narrows to its literal type", () => {
|
test("P.literal narrows to its literal type", () => {
|
||||||
|
// Act
|
||||||
const matcher = P.literal("yes");
|
const matcher = P.literal("yes");
|
||||||
expectTypeOf(matcher).toMatchTypeOf<Matcher<"yes">>();
|
|
||||||
|
// Assert
|
||||||
|
expectTypeOf(matcher).toExtend<Matcher<"yes">>();
|
||||||
assert.equal(matcher.matches("yes"), true);
|
assert.equal(matcher.matches("yes"), true);
|
||||||
assert.equal(matcher.matches("no"), false);
|
assert.equal(matcher.matches("no"), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("P.type narrows to the typeof target", () => {
|
test("P.type narrows to the typeof target", () => {
|
||||||
|
// Act
|
||||||
const matcher = P.type<string>("string");
|
const matcher = P.type<string>("string");
|
||||||
expectTypeOf(matcher).toMatchTypeOf<Matcher<string>>();
|
|
||||||
|
// Assert
|
||||||
|
expectTypeOf(matcher).toExtend<Matcher<string>>();
|
||||||
assert.equal(matcher.matches("hi"), true);
|
assert.equal(matcher.matches("hi"), true);
|
||||||
assert.equal(matcher.matches(42), false);
|
assert.equal(matcher.matches(42), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("exhaustive() returns the union of handler return types", () => {
|
test("exhaustive() returns the union of handler return types", () => {
|
||||||
|
// Act
|
||||||
const result = match<"a" | "b">("a")
|
const result = match<"a" | "b">("a")
|
||||||
.with(P.literal("a"), () => 1 as const)
|
.with(P.literal("a"), () => 1 as const)
|
||||||
.with(P.literal("b"), () => "two" as const)
|
.with(P.literal("b"), () => "two" as const)
|
||||||
.exhaustive();
|
.exhaustive();
|
||||||
|
|
||||||
|
// Assert
|
||||||
expectTypeOf(result).toEqualTypeOf<1 | "two">();
|
expectTypeOf(result).toEqualTypeOf<1 | "two">();
|
||||||
assert.equal(result, 1);
|
assert.equal(result, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("otherwise() falls back when no case matches", () => {
|
test("otherwise() falls back when no case matches", () => {
|
||||||
|
// Act
|
||||||
const result = match<"x" | "y" | "z">("z")
|
const result = match<"x" | "y" | "z">("z")
|
||||||
.with(P.literal("x"), (v): string => `got ${v}`)
|
.with(P.literal("x"), (v): string => `got ${v}`)
|
||||||
.otherwise((v): string => `fallback ${v}`);
|
.otherwise((v): string => `fallback ${v}`);
|
||||||
|
|
||||||
|
// Assert
|
||||||
assert.equal(result, "fallback z");
|
assert.equal(result, "fallback z");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("exhaustive throws when no case matches", () => {
|
test("exhaustive throws when no case matches", () => {
|
||||||
|
// Assert
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() =>
|
() =>
|
||||||
match<"a" | "b" | "c">("c")
|
match<"a" | "b" | "c">("c")
|
||||||
|
|||||||
+57
-13
@@ -16,26 +16,37 @@ import {
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
test("getPrimitiveUnionMatcherW requires every literal key", () => {
|
test("getPrimitiveUnionMatcherW requires every literal key", () => {
|
||||||
const matcher = getPrimitiveUnionMatcherW<"a" | "b">()({
|
// Arrange
|
||||||
|
const factory = getPrimitiveUnionMatcherW<"a" | "b">();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const matcher = factory({
|
||||||
a: () => 1 as const,
|
a: () => 1 as const,
|
||||||
b: () => "two" as const,
|
b: () => "two" as const,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
// ❌ ReturnsStrict: mixed handler returns widen to their union.
|
// ❌ ReturnsStrict: mixed handler returns widen to their union.
|
||||||
expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b") => 1 | "two">();
|
expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b") => 1 | "two">();
|
||||||
// A pattern missing a key must not satisfy the parameter type.
|
// A pattern missing a key must not satisfy the parameter type.
|
||||||
const factory = getPrimitiveUnionMatcherW<"a" | "b">();
|
|
||||||
expectTypeOf<{
|
expectTypeOf<{
|
||||||
a: () => number;
|
a: () => number;
|
||||||
}>().not.toMatchTypeOf<Parameters<typeof factory>[0]>();
|
}>().not.toExtend<Parameters<typeof factory>[0]>();
|
||||||
assert.equal(matcher("a"), 1);
|
assert.equal(matcher("a"), 1);
|
||||||
assert.equal(matcher("b"), "two");
|
assert.equal(matcher("b"), "two");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("getPrimitiveUnionMatcherW dispatches on numeric literal keys", () => {
|
test("getPrimitiveUnionMatcherW dispatches on numeric literal keys", () => {
|
||||||
const matcher = getPrimitiveUnionMatcherW<1 | 2>()({
|
// Arrange
|
||||||
|
const factory = getPrimitiveUnionMatcherW<1 | 2>();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const matcher = factory({
|
||||||
1: (n) => n + 1,
|
1: (n) => n + 1,
|
||||||
2: (n) => n * 10,
|
2: (n) => n * 10,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
expectTypeOf(matcher).toEqualTypeOf<(shape: 1 | 2) => number>();
|
expectTypeOf(matcher).toEqualTypeOf<(shape: 1 | 2) => number>();
|
||||||
assert.equal(matcher(1), 2);
|
assert.equal(matcher(1), 2);
|
||||||
assert.equal(matcher(2), 20);
|
assert.equal(matcher(2), 20);
|
||||||
@@ -46,10 +57,16 @@ test("getPrimitiveUnionMatcherW dispatches on numeric literal keys", () => {
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
test("getPrimitiveUnionMatcher infers a single return type shared by all handlers", () => {
|
test("getPrimitiveUnionMatcher infers a single return type shared by all handlers", () => {
|
||||||
const matcher = getPrimitiveUnionMatcher<"a" | "b">()({
|
// Arrange
|
||||||
|
const factory = getPrimitiveUnionMatcher<"a" | "b">();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const matcher = factory({
|
||||||
a: (): number => 1,
|
a: (): number => 1,
|
||||||
b: (): 1 | 2 => 2,
|
b: (): 1 | 2 => 2,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
// ✔️ ReturnsStrict: R is the best common return type, not a widening union.
|
// ✔️ ReturnsStrict: R is the best common return type, not a widening union.
|
||||||
expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b") => number>();
|
expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b") => number>();
|
||||||
assert.equal(matcher("a"), 1);
|
assert.equal(matcher("a"), 1);
|
||||||
@@ -57,7 +74,11 @@ test("getPrimitiveUnionMatcher infers a single return type shared by all handler
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("getPrimitiveUnionMatcher handlers receive the matched literal", () => {
|
test("getPrimitiveUnionMatcher handlers receive the matched literal", () => {
|
||||||
const matcher = getPrimitiveUnionMatcher<"on" | "off">()({
|
// Arrange
|
||||||
|
const factory = getPrimitiveUnionMatcher<"on" | "off">();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const matcher = factory({
|
||||||
on: (s) => {
|
on: (s) => {
|
||||||
expectTypeOf(s).toEqualTypeOf<"on">();
|
expectTypeOf(s).toEqualTypeOf<"on">();
|
||||||
return `handler ${s}`;
|
return `handler ${s}`;
|
||||||
@@ -67,6 +88,8 @@ test("getPrimitiveUnionMatcher handlers receive the matched literal", () => {
|
|||||||
return `handler ${s}`;
|
return `handler ${s}`;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
expectTypeOf(matcher).toEqualTypeOf<(shape: "on" | "off") => string>();
|
expectTypeOf(matcher).toEqualTypeOf<(shape: "on" | "off") => string>();
|
||||||
assert.equal(matcher("on"), "handler on");
|
assert.equal(matcher("on"), "handler on");
|
||||||
assert.equal(matcher("off"), "handler off");
|
assert.equal(matcher("off"), "handler off");
|
||||||
@@ -77,7 +100,13 @@ test("getPrimitiveUnionMatcher handlers receive the matched literal", () => {
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
test("getPrimitiveUnionMatcherPartial routes shapes without a handler to _", () => {
|
test("getPrimitiveUnionMatcherPartial routes shapes without a handler to _", () => {
|
||||||
const matcher = getPrimitiveUnionMatcherPartial<"a" | "b" | "c">()({
|
// Arrange
|
||||||
|
const factory = getPrimitiveUnionMatcherPartial<"a" | "b" | "c">();
|
||||||
|
// Two keys, so `{ a }` lacks only the `_` fallback, nothing else.
|
||||||
|
const sparseFactory = getPrimitiveUnionMatcherPartial<"a" | "b">();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const matcher = factory({
|
||||||
a: (): 1 | 2 => 1,
|
a: (): 1 | 2 => 1,
|
||||||
_: (s): 1 | 2 => {
|
_: (s): 1 | 2 => {
|
||||||
// The fallback sees the whole union, not a single literal.
|
// The fallback sees the whole union, not a single literal.
|
||||||
@@ -85,22 +114,29 @@ test("getPrimitiveUnionMatcherPartial routes shapes without a handler to _", ()
|
|||||||
return 2;
|
return 2;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b" | "c") => 1 | 2>();
|
expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b" | "c") => 1 | 2>();
|
||||||
// ❌ Exhaustive: gaps are allowed, but only with a `_` fallback.
|
// ❌ Exhaustive: gaps are allowed, but only with a `_` fallback.
|
||||||
const factory = getPrimitiveUnionMatcherPartial<"a" | "b">();
|
|
||||||
expectTypeOf<{
|
expectTypeOf<{
|
||||||
a: () => number;
|
a: () => number;
|
||||||
}>().not.toMatchTypeOf<Parameters<typeof factory>[0]>();
|
}>().not.toExtend<Parameters<typeof sparseFactory>[0]>();
|
||||||
assert.equal(matcher("a"), 1);
|
assert.equal(matcher("a"), 1);
|
||||||
assert.equal(matcher("b"), 2);
|
assert.equal(matcher("b"), 2);
|
||||||
assert.equal(matcher("c"), 2);
|
assert.equal(matcher("c"), 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("getPrimitiveUnionMatcherPartial also accepts an exhaustive pattern", () => {
|
test("getPrimitiveUnionMatcherPartial also accepts an exhaustive pattern", () => {
|
||||||
const matcher = getPrimitiveUnionMatcherPartial<"a" | "b">()({
|
// Arrange
|
||||||
|
const factory = getPrimitiveUnionMatcherPartial<"a" | "b">();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const matcher = factory({
|
||||||
a: () => "A",
|
a: () => "A",
|
||||||
b: () => "B",
|
b: () => "B",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b") => string>();
|
expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b") => string>();
|
||||||
assert.equal(matcher("a"), "A");
|
assert.equal(matcher("a"), "A");
|
||||||
assert.equal(matcher("b"), "B");
|
assert.equal(matcher("b"), "B");
|
||||||
@@ -111,17 +147,25 @@ test("getPrimitiveUnionMatcherPartial also accepts an exhaustive pattern", () =>
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
test("getPrimitiveUnionMatcherPartialW allows gaps and widens to the union of handler returns", () => {
|
test("getPrimitiveUnionMatcherPartialW allows gaps and widens to the union of handler returns", () => {
|
||||||
const matcher = getPrimitiveUnionMatcherPartialW<"x" | "y" | "z">()({
|
// Arrange
|
||||||
|
const factory = getPrimitiveUnionMatcherPartialW<"x" | "y" | "z">();
|
||||||
|
// Two keys, so `{ x }` lacks only the `_` fallback, nothing else.
|
||||||
|
const sparseFactory = getPrimitiveUnionMatcherPartialW<"x" | "y">();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
const matcher = factory({
|
||||||
x: () => 1 as const,
|
x: () => 1 as const,
|
||||||
_: () => "fallback" as const,
|
_: () => "fallback" as const,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
expectTypeOf(matcher).toEqualTypeOf<
|
expectTypeOf(matcher).toEqualTypeOf<
|
||||||
(shape: "x" | "y" | "z") => 1 | "fallback"
|
(shape: "x" | "y" | "z") => 1 | "fallback"
|
||||||
>();
|
>();
|
||||||
const factory = getPrimitiveUnionMatcherPartialW<"x" | "y">();
|
// ❌ Exhaustive: gaps are allowed, but only with a `_` fallback.
|
||||||
expectTypeOf<{
|
expectTypeOf<{
|
||||||
x: () => number;
|
x: () => number;
|
||||||
}>().not.toMatchTypeOf<Parameters<typeof factory>[0]>();
|
}>().not.toExtend<Parameters<typeof sparseFactory>[0]>();
|
||||||
assert.equal(matcher("x"), 1);
|
assert.equal(matcher("x"), 1);
|
||||||
assert.equal(matcher("y"), "fallback");
|
assert.equal(matcher("y"), "fallback");
|
||||||
assert.equal(matcher("z"), "fallback");
|
assert.equal(matcher("z"), "fallback");
|
||||||
|
|||||||
Reference in new issue
Block a user