|
| 1 | +import { describe, expect, it, test } from "@jest/globals"; |
| 2 | +import { expectType } from "tsd"; |
| 3 | + |
| 4 | +import * as index from "./index"; |
| 5 | +import * as solution from "./solution"; |
| 6 | + |
| 7 | +const { isFruitBearingCactus, pickFruitBearingCacti } = process.env |
| 8 | + .TEST_SOLUTIONS |
| 9 | + ? solution |
| 10 | + : (index as typeof solution); |
| 11 | + |
| 12 | +describe(isFruitBearingCactus, () => { |
| 13 | + describe("types", () => { |
| 14 | + test("function type", () => { |
| 15 | + expectType< |
| 16 | + (data: solution.Cactus) => data is solution.FruitBearingCactus |
| 17 | + >(isFruitBearingCactus); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it.each<[solution.Cactus, boolean]>([ |
| 22 | + [{ picked: false, state: "dormant" }, false], |
| 23 | + [{ picked: true, state: "dormant" }, false], |
| 24 | + [{ flowers: "small", state: "flowering" }, false], |
| 25 | + [{ flowers: "medium", state: "flowering" }, false], |
| 26 | + [{ flowers: "large", state: "flowering" }, false], |
| 27 | + [{ fruits: 0, state: "fruit-bearing" }, true], |
| 28 | + [{ fruits: 1, state: "fruit-bearing" }, true], |
| 29 | + [{ fruits: 2, state: "fruit-bearing" }, true], |
| 30 | + ])("when given %j, returns %j", (input, expected) => { |
| 31 | + expect(isFruitBearingCactus(input)).toBe(expected); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +describe(pickFruitBearingCacti, () => { |
| 36 | + describe("types", () => { |
| 37 | + test("function type", () => { |
| 38 | + expectType<(data: solution.Cactus[]) => solution.FruitBearingCactus[]>( |
| 39 | + pickFruitBearingCacti |
| 40 | + ); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it.each<[solution.Cactus[], solution.Cactus[]]>([ |
| 45 | + [[], []], |
| 46 | + [[{ picked: true, state: "dormant" }], []], |
| 47 | + [[{ flowers: "small", state: "flowering" }], []], |
| 48 | + [[{ flowers: "medium", state: "flowering" }], []], |
| 49 | + [[{ flowers: "large", state: "flowering" }], []], |
| 50 | + [ |
| 51 | + [{ fruits: 0, state: "fruit-bearing" }], |
| 52 | + [{ fruits: 0, state: "fruit-bearing" }], |
| 53 | + ], |
| 54 | + [ |
| 55 | + [{ fruits: 1, state: "fruit-bearing" }], |
| 56 | + [{ fruits: 1, state: "fruit-bearing" }], |
| 57 | + ], |
| 58 | + [ |
| 59 | + [{ fruits: 2, state: "fruit-bearing" }], |
| 60 | + [{ fruits: 2, state: "fruit-bearing" }], |
| 61 | + ], |
| 62 | + [ |
| 63 | + [ |
| 64 | + { picked: true, state: "dormant" }, |
| 65 | + { flowers: "small", state: "flowering" }, |
| 66 | + ], |
| 67 | + [], |
| 68 | + ], |
| 69 | + [ |
| 70 | + [ |
| 71 | + { picked: true, state: "dormant" }, |
| 72 | + { flowers: "small", state: "flowering" }, |
| 73 | + { flowers: "medium", state: "flowering" }, |
| 74 | + { flowers: "large", state: "flowering" }, |
| 75 | + { fruits: 0, state: "fruit-bearing" }, |
| 76 | + ], |
| 77 | + [{ fruits: 0, state: "fruit-bearing" }], |
| 78 | + ], |
| 79 | + ])("when given %j, returns %j", (input, expected) => { |
| 80 | + expect(pickFruitBearingCacti(input)).toEqual(expected); |
| 81 | + }); |
| 82 | +}); |
0 commit comments