Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use IsExact of @std/testing #133

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions _testutil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assertEquals } from "@std/assert";
import type { IsExact } from "@std/testing/types";
import type { Predicate } from "./type.ts";

const examples = {
Expand Down Expand Up @@ -50,16 +49,6 @@ export async function testWithExamples<T>(
}
}

// It seems 'IsExact' in deno_std is false positive so use `Equal` in type-challenges
// https://github.com/type-challenges/type-challenges/blob/e77262dba62e9254451f661cb4fe5517ffd1d933/utils/index.d.ts#L7-L9
/** @deprecated use {@linkcode Equal} */
export type TypeChallengesEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false;

// `Equal` in type-challenges is false positive so combine `IsExact` + `Equal`.
export type Equal<X, Y> = TypeChallengesEqual<X, Y> extends true ? IsExact<X, Y>
: false;

export function stringify(x: unknown): string {
if (x instanceof Date) return `Date(${x.valueOf()})`;
if (x instanceof Promise) return "Promise";
Expand Down
43 changes: 0 additions & 43 deletions _testutil_test.ts

This file was deleted.

18 changes: 9 additions & 9 deletions as/optional_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from "@std/assert";
import { assertType } from "@std/testing/types";
import { type Equal, testWithExamples } from "../_testutil.ts";
import { assertType, type IsExact } from "@std/testing/types";
import { testWithExamples } from "../_testutil.ts";
import { is } from "../is/mod.ts";
import type { AsOptional } from "../_annotation.ts";
import { asOptional, asUnoptional, hasOptional } from "./optional.ts";
Expand Down Expand Up @@ -35,7 +35,7 @@ Deno.test("asOptional<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<
IsExact<
typeof v,
{ a: number; b?: number | undefined; c?: number | undefined }
>
Expand All @@ -56,7 +56,7 @@ Deno.test("asOptional<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<typeof v, [number, number | undefined, number | undefined]>
IsExact<typeof v, [number, number | undefined, number | undefined]>
>(
true,
);
Expand All @@ -76,7 +76,7 @@ Deno.test("asOptional<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<
IsExact<
typeof v,
[number, (number | undefined)?, (number | undefined)?]
>
Expand Down Expand Up @@ -117,7 +117,7 @@ Deno.test("asUnoptional<T>", async (t) => {
await t.step("predicated type is correct", () => {
const v: unknown = undefined;
if (pred(v)) {
assertType<Equal<typeof v, { a: number; b: number; c: number }>>(
assertType<IsExact<typeof v, { a: number; b: number; c: number }>>(
true,
);
}
Expand All @@ -134,7 +134,7 @@ Deno.test("asUnoptional<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<typeof v, [number, number, number]>
IsExact<typeof v, [number, number, number]>
>(
true,
);
Expand All @@ -154,7 +154,7 @@ Deno.test("asUnoptional<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<typeof v, [number, number, number]>
IsExact<typeof v, [number, number, number]>
>(
true,
);
Expand All @@ -178,7 +178,7 @@ Deno.test("hasOptional<P>", async (t) => {
const pred = asOptional(is.Number);
type P = typeof pred;
if (hasOptional(pred)) {
assertType<Equal<typeof pred, P & AsOptional<number>>>(true);
assertType<IsExact<typeof pred, P & AsOptional<number>>>(true);
}
});
});
21 changes: 12 additions & 9 deletions as/readonly_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from "@std/assert";
import { assertType } from "@std/testing/types";
import { type Equal, testWithExamples } from "../_testutil.ts";
import { assertType, type IsExact } from "@std/testing/types";
import { testWithExamples } from "../_testutil.ts";
import { is } from "../is/mod.ts";
import type { AsReadonly } from "../_annotation.ts";
import { asReadonly, asUnreadonly, hasReadonly } from "./readonly.ts";
Expand Down Expand Up @@ -35,7 +35,10 @@ Deno.test("asReadonly<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<typeof v, { a: number; readonly b: number; readonly c: number }>
IsExact<
typeof v,
{ a: number; readonly b: number; readonly c: number }
>
>(
true,
);
Expand All @@ -53,7 +56,7 @@ Deno.test("asReadonly<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<typeof v, [number, number, number]>
IsExact<typeof v, [number, number, number]>
>(
true,
);
Expand All @@ -73,7 +76,7 @@ Deno.test("asReadonly<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<typeof v, [number, number, number]>
IsExact<typeof v, [number, number, number]>
>(
true,
);
Expand Down Expand Up @@ -111,7 +114,7 @@ Deno.test("asUnreadonly<T>", async (t) => {
await t.step("predicated type is correct", () => {
const v: unknown = undefined;
if (pred(v)) {
assertType<Equal<typeof v, { a: number; b: number; c: number }>>(
assertType<IsExact<typeof v, { a: number; b: number; c: number }>>(
true,
);
}
Expand All @@ -128,7 +131,7 @@ Deno.test("asUnreadonly<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<typeof v, [number, number, number]>
IsExact<typeof v, [number, number, number]>
>(
true,
);
Expand All @@ -148,7 +151,7 @@ Deno.test("asUnreadonly<T>", async (t) => {
const v: unknown = undefined;
if (pred(v)) {
assertType<
Equal<typeof v, [number, number, number]>
IsExact<typeof v, [number, number, number]>
>(
true,
);
Expand All @@ -172,7 +175,7 @@ Deno.test("hasReadonly<P>", async (t) => {
const pred = asReadonly(is.Number);
type P = typeof pred;
if (hasReadonly(pred)) {
assertType<Equal<typeof pred, P & AsReadonly>>(true);
assertType<IsExact<typeof pred, P & AsReadonly>>(true);
}
});
});
4 changes: 2 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@
"@core/iterutil": "jsr:@core/iterutil@^0.3.0",
"@core/unknownutil": "./mod.ts",
"@deno/dnt": "jsr:@deno/dnt@^0.41.1",
"@std/assert": "jsr:@std/assert@^0.221.0",
"@std/assert": "jsr:@std/assert@^1.0.4",
"@std/jsonc": "jsr:@std/jsonc@^1.0.0",
"@std/path": "jsr:@std/path@^1.0.2",
"@std/testing": "jsr:@std/testing@^0.221.0"
"@std/testing": "jsr:@std/testing@^1.0.2"
},
"tasks": {
"check": "deno check **/*.ts",
Expand Down
5 changes: 2 additions & 3 deletions is/array_of_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assertEquals } from "@std/assert";
import { assertType } from "@std/testing/types";
import type { Equal } from "../_testutil.ts";
import { assertType, type IsExact } from "@std/testing/types";
import { is } from "./mod.ts";
import { isArrayOf } from "./array_of.ts";

Expand Down Expand Up @@ -30,7 +29,7 @@ Deno.test("isArrayOf<T>", async (t) => {
const a: unknown = undefined;

if (isArrayOf(is.Number)(a)) {
assertType<Equal<typeof a, number[]>>(true);
assertType<IsExact<typeof a, number[]>>(true);
}
});
});
9 changes: 4 additions & 5 deletions is/instance_of_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assertEquals } from "@std/assert";
import { assertType } from "@std/testing/types";
import type { Equal } from "../_testutil.ts";
import { assertType, type IsExact } from "@std/testing/types";
import { isInstanceOf } from "./instance_of.ts";

Deno.test("isInstanceOf<T>", async (t) => {
Expand Down Expand Up @@ -29,15 +28,15 @@ Deno.test("isInstanceOf<T>", async (t) => {
const a: unknown = undefined;

if (isInstanceOf(Cls)(a)) {
assertType<Equal<typeof a, Cls>>(true);
assertType<IsExact<typeof a, Cls>>(true);
}

if (isInstanceOf(Date)(a)) {
assertType<Equal<typeof a, Date>>(true);
assertType<IsExact<typeof a, Date>>(true);
}

if (isInstanceOf(Promise)(a)) {
assertType<Equal<typeof a, Promise<unknown>>>(true);
assertType<IsExact<typeof a, Promise<unknown>>>(true);
}
});
});
19 changes: 9 additions & 10 deletions is/intersection_of_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assertEquals } from "@std/assert";
import { assertSnapshot } from "@std/testing/snapshot";
import { assertType } from "@std/testing/types";
import type { Equal } from "../_testutil.ts";
import { assertType, type IsExact } from "@std/testing/types";
import { is } from "./mod.ts";
import { isIntersectionOf } from "./intersection_of.ts";

Expand Down Expand Up @@ -40,16 +39,16 @@ Deno.test("isIntersectionOf<T>", async (t) => {
const a: unknown = undefined;

if (isIntersectionOf([is.String])(a)) {
assertType<Equal<typeof a, string>>(true);
assertType<IsExact<typeof a, string>>(true);
}

if (isIntersectionOf(objPreds)(a)) {
assertType<Equal<typeof a, { a: number } & { b: string }>>(true);
assertType<IsExact<typeof a, { a: number } & { b: string }>>(true);
}

if (isIntersectionOf(mixPreds)(a)) {
assertType<
Equal<
IsExact<
typeof a,
& ((...args: unknown[]) => unknown)
& { b: string }
Expand All @@ -70,7 +69,7 @@ Deno.test("isIntersectionOf<T>", async (t) => {

if (pred(a)) {
assertType<
Equal<
IsExact<
typeof a,
{ id: string } & ({ result: string } | { error: string })
>
Expand Down Expand Up @@ -114,16 +113,16 @@ Deno.test("isIntersectionOf<T>", async (t) => {
const a: unknown = undefined;

if (isIntersectionOf([is.String])(a)) {
assertType<Equal<typeof a, string>>(true);
assertType<IsExact<typeof a, string>>(true);
}

if (isIntersectionOf(objPreds)(a)) {
assertType<Equal<typeof a, { a: number } & { [b]: string }>>(true);
assertType<IsExact<typeof a, { a: number } & { [b]: string }>>(true);
}

if (isIntersectionOf(mixPreds)(a)) {
assertType<
Equal<
IsExact<
typeof a,
& ((...args: unknown[]) => unknown)
& { [b]: string }
Expand All @@ -144,7 +143,7 @@ Deno.test("isIntersectionOf<T>", async (t) => {

if (pred(a)) {
assertType<
Equal<
IsExact<
typeof a,
{ id: string } & ({ result: string } | { error: string })
>
Expand Down
19 changes: 9 additions & 10 deletions is/literal_of_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assertEquals } from "@std/assert";
import { assertType } from "@std/testing/types";
import type { Equal } from "../_testutil.ts";
import { assertType, type IsExact } from "@std/testing/types";
import { isLiteralOf } from "./literal_of.ts";

Deno.test("isLiteralOf<T>", async (t) => {
Expand Down Expand Up @@ -45,35 +44,35 @@ Deno.test("isLiteralOf<T>", async (t) => {
const a: unknown = undefined;

if (isLiteralOf("hello")(a)) {
assertType<Equal<typeof a, "hello">>(true);
assertType<IsExact<typeof a, "hello">>(true);
}

if (isLiteralOf(100)(a)) {
assertType<Equal<typeof a, 100>>(true);
assertType<IsExact<typeof a, 100>>(true);
}

if (isLiteralOf(100n)(a)) {
assertType<Equal<typeof a, 100n>>(true);
assertType<IsExact<typeof a, 100n>>(true);
}

if (isLiteralOf(true)(a)) {
assertType<Equal<typeof a, true>>(true);
assertType<IsExact<typeof a, true>>(true);
}

if (isLiteralOf(false)(a)) {
assertType<Equal<typeof a, false>>(true);
assertType<IsExact<typeof a, false>>(true);
}

if (isLiteralOf(null)(a)) {
assertType<Equal<typeof a, null>>(true);
assertType<IsExact<typeof a, null>>(true);
}

if (isLiteralOf(undefined)(a)) {
assertType<Equal<typeof a, undefined>>(true);
assertType<IsExact<typeof a, undefined>>(true);
}

if (isLiteralOf(s)(a)) {
assertType<Equal<typeof a, typeof s>>(true);
assertType<IsExact<typeof a, typeof s>>(true);
}
});
});
Loading