|
| 1 | +import { assertEquals } from "@std/assert"; |
| 2 | +import { assertType, type IsExact } from "@std/testing/types"; |
| 3 | +import { DenopsStub } from "@denops/test/stub"; |
| 4 | +import { composeMatchers, defineMatcher, type Matcher } from "./matcher.ts"; |
| 5 | + |
| 6 | +Deno.test("defineMatcher", () => { |
| 7 | + const matcher = defineMatcher(async function* () {}); |
| 8 | + assertEquals(typeof matcher.match, "function"); |
| 9 | + assertType<IsExact<typeof matcher, Matcher<unknown>>>(true); |
| 10 | +}); |
| 11 | + |
| 12 | +Deno.test("composeMatchers", async () => { |
| 13 | + const results: string[] = []; |
| 14 | + const matcher1 = defineMatcher(async function* (_denops, { items }) { |
| 15 | + results.push("matcher1"); |
| 16 | + yield* items.filter((item) => item.value.includes("1")); |
| 17 | + }); |
| 18 | + const matcher2 = defineMatcher(async function* (_denops, { items }) { |
| 19 | + results.push("matcher2"); |
| 20 | + yield* items.filter((item) => item.value.includes("2")); |
| 21 | + }); |
| 22 | + const matcher3 = defineMatcher(async function* (_denops, { items }) { |
| 23 | + results.push("matcher3"); |
| 24 | + yield* items.filter((item) => item.value.includes("3")); |
| 25 | + }); |
| 26 | + const matcher = composeMatchers(matcher2, matcher1, matcher3); |
| 27 | + const denops = new DenopsStub(); |
| 28 | + const params = { |
| 29 | + query: "", |
| 30 | + items: Array.from({ length: 1000 }).map((_, id) => ({ |
| 31 | + id, |
| 32 | + value: id.toString(), |
| 33 | + detail: undefined, |
| 34 | + })), |
| 35 | + }; |
| 36 | + const items = await Array.fromAsync(matcher.match(denops, params, {})); |
| 37 | + assertEquals(results, ["matcher2", "matcher1", "matcher3"]); |
| 38 | + assertEquals(items.map((item) => item.value), [ |
| 39 | + "123", |
| 40 | + "132", |
| 41 | + "213", |
| 42 | + "231", |
| 43 | + "312", |
| 44 | + "321", |
| 45 | + ]); |
| 46 | +}); |
0 commit comments