|
| 1 | +import { assertEquals } from "@std/assert"; |
| 2 | +import { assertType, type IsExact } from "@std/testing/types"; |
| 3 | + |
| 4 | +import { defineFilter, type Filter, type FilterParams } from "./filter.ts"; |
| 5 | +import type { Source } from "./source.ts"; |
| 6 | +import type { Curator } from "./curator.ts"; |
| 7 | + |
| 8 | +Deno.test("Filter", async (t) => { |
| 9 | + await t.step("with Source", async (t) => { |
| 10 | + const filter = (() => {}) as Filter<{ a: string }>; |
| 11 | + |
| 12 | + await t.step("passed type is equal to the type restriction", () => { |
| 13 | + const modified = filter({} as Source<{ a: string }>); |
| 14 | + assertType< |
| 15 | + IsExact< |
| 16 | + typeof modified, |
| 17 | + Source<{ a: string }> |
| 18 | + > |
| 19 | + >(true); |
| 20 | + }); |
| 21 | + |
| 22 | + await t.step("passed type establishes the type restriction", () => { |
| 23 | + const modified = filter({} as Source<{ a: string; b: string }>); |
| 24 | + assertType< |
| 25 | + IsExact< |
| 26 | + typeof modified, |
| 27 | + Source<{ a: string; b: string }> |
| 28 | + > |
| 29 | + >(true); |
| 30 | + }); |
| 31 | + |
| 32 | + await t.step( |
| 33 | + "passed type does not establish the type restriction", |
| 34 | + () => { |
| 35 | + // @ts-expect-error: 'a' is missing |
| 36 | + filter({} as Source<{ b: string }>); |
| 37 | + }, |
| 38 | + ); |
| 39 | + |
| 40 | + await t.step( |
| 41 | + "check if the type constraint correctly triggers the type checking", |
| 42 | + () => { |
| 43 | + const filter1 = (() => {}) as Filter<{ a: string }>; |
| 44 | + const filter2 = (() => {}) as Filter<{ b: string }>; |
| 45 | + const filter3 = (() => {}) as Filter<{ c: string }>; |
| 46 | + function strictFunction<T extends { a: string }>(_: Filter<T>) {} |
| 47 | + strictFunction(filter1); |
| 48 | + // @ts-expect-error: 'a' is missing |
| 49 | + strictFunction(filter2); |
| 50 | + // @ts-expect-error: 'a' is missing |
| 51 | + strictFunction(filter3); |
| 52 | + }, |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + await t.step("with Curator", async (t) => { |
| 57 | + const filter = (() => {}) as Filter<{ a: string }>; |
| 58 | + |
| 59 | + await t.step("passed type is equal to the type restriction", () => { |
| 60 | + const modified = filter({} as Curator<{ a: string }>); |
| 61 | + assertType< |
| 62 | + IsExact< |
| 63 | + typeof modified, |
| 64 | + Curator<{ a: string }> |
| 65 | + > |
| 66 | + >(true); |
| 67 | + }); |
| 68 | + |
| 69 | + await t.step("passed type establishes the type restriction", () => { |
| 70 | + const modified = filter({} as Curator<{ a: string; b: string }>); |
| 71 | + assertType< |
| 72 | + IsExact< |
| 73 | + typeof modified, |
| 74 | + Curator<{ a: string; b: string }> |
| 75 | + > |
| 76 | + >(true); |
| 77 | + }); |
| 78 | + |
| 79 | + await t.step( |
| 80 | + "passed type does not establish the type restriction", |
| 81 | + () => { |
| 82 | + // @ts-expect-error: 'a' is missing |
| 83 | + filter({} as Curator<{ b: string }>); |
| 84 | + }, |
| 85 | + ); |
| 86 | + |
| 87 | + await t.step( |
| 88 | + "check if the type constraint correctly triggers the type checking", |
| 89 | + () => { |
| 90 | + const filter1 = (() => {}) as Filter<{ a: string }>; |
| 91 | + const filter2 = (() => {}) as Filter<{ b: string }>; |
| 92 | + const filter3 = (() => {}) as Filter<{ c: string }>; |
| 93 | + function strictFunction<T extends { a: string }>(_: Filter<T>) {} |
| 94 | + strictFunction(filter1); |
| 95 | + // @ts-expect-error: 'a' is missing |
| 96 | + strictFunction(filter2); |
| 97 | + // @ts-expect-error: 'a' is missing |
| 98 | + strictFunction(filter3); |
| 99 | + }, |
| 100 | + ); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +Deno.test("defineFilter", async (t) => { |
| 105 | + await t.step("without type constraint", () => { |
| 106 | + const filter = defineFilter(async function* (_denops, params) { |
| 107 | + // @ts-expect-error: `params` is not type restrained |
| 108 | + const _: FilterParams<{ a: string }> = params; |
| 109 | + yield* []; |
| 110 | + }); |
| 111 | + assertEquals(typeof filter, "function"); |
| 112 | + assertType<IsExact<typeof filter, Filter<unknown>>>(true); |
| 113 | + }); |
| 114 | + |
| 115 | + await t.step("without type constraint T", () => { |
| 116 | + const filter = defineFilter<{ a: string }>( |
| 117 | + async function* (_denops, params) { |
| 118 | + const _: FilterParams<{ a: string }> = params; |
| 119 | + yield* []; |
| 120 | + }, |
| 121 | + ); |
| 122 | + assertEquals(typeof filter, "function"); |
| 123 | + assertType<IsExact<typeof filter, Filter<{ a: string }>>>(true); |
| 124 | + }); |
| 125 | +}); |
0 commit comments