| 1 | import { describe, expect, test } from "alchemy-test"; |
| 2 | |
| 3 | import { |
| 4 | getSetValueAST, |
| 5 | isBooleanSchema, |
| 6 | isListSchema, |
| 7 | isMapSchema, |
| 8 | isNullishSchema, |
| 9 | isNullSchema, |
| 10 | isNumberSchema, |
| 11 | isRecordLikeSchema, |
| 12 | isSetSchema, |
| 13 | isStringSchema, |
| 14 | isUndefinedSchema, |
| 15 | } from "@/Schema"; |
| 16 | import * as S from "effect/Schema"; |
| 17 | |
| 18 | describe("isStringSchema", { tags: ["unit", "local"] }, () => { |
| 19 | test("string", () => { |
| 20 | expect(isStringSchema(S.String)).toBe(true); |
| 21 | }); |
| 22 | test("not string", () => { |
| 23 | expect(isStringSchema(S.Number)).toBe(false); |
| 24 | }); |
| 25 | }); |
| 26 | |
| 27 | describe("isNumberSchema", { tags: ["unit", "local"] }, () => { |
| 28 | test("number", () => { |
| 29 | expect(isNumberSchema(S.Number)).toBe(true); |
| 30 | }); |
| 31 | test("not number", () => { |
| 32 | expect(isNumberSchema(S.String)).toBe(false); |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | describe("isMapSchema", { tags: ["unit", "local"] }, () => { |
| 37 | test("map", () => { |
| 38 | expect(isMapSchema(S.ReadonlyMap(S.String, S.String))).toBe(true); |
| 39 | }); |
| 40 | test("not map", () => { |
| 41 | expect(isMapSchema(S.Record(S.String, S.String))).toBe(false); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe("isRecordLikeSchema", { tags: ["unit", "local"] }, () => { |
| 46 | for (const [key, value] of Object.entries({ |
| 47 | map: S.ReadonlyMap(S.String, S.String), |
| 48 | struct: S.Struct({ |
| 49 | key: S.String, |
| 50 | value: S.String, |
| 51 | }), |
| 52 | class: class Self extends S.Class<Self>("Self")({ |
| 53 | key: S.String, |
| 54 | value: S.String, |
| 55 | }) {}, |
| 56 | })) { |
| 57 | test(key, () => { |
| 58 | expect(isRecordLikeSchema(value)).toBe(true); |
| 59 | }); |
| 60 | } |
| 61 | }); |
| 62 | |
| 63 | describe("isListSchema", { tags: ["unit", "local"] }, () => { |
| 64 | test("list", () => { |
| 65 | expect(isListSchema(S.Array(S.String))).toBe(true); |
| 66 | }); |
| 67 | test("Map type is not list", () => { |
| 68 | expect(isListSchema(S.ReadonlyMap(S.String, S.String))).toBe(false); |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | describe("isSetSchema", { tags: ["unit", "local"] }, () => { |
| 73 | test("set", () => { |
| 74 | expect(isSetSchema(S.ReadonlySet(S.String))).toBe(true); |
| 75 | }); |
| 76 | test("getSetValueAST", () => { |
| 77 | const ast = getSetValueAST(S.ReadonlySet(S.String)); |
| 78 | expect(ast).toBeDefined(); |
| 79 | expect(isStringSchema(S.make(ast!))).toBe(true); |
| 80 | }); |
| 81 | }); |
| 82 | |
| 83 | describe("isNullSchema", { tags: ["unit", "local"] }, () => { |
| 84 | test("null", () => { |
| 85 | expect(isNullSchema(S.Null)).toBe(true); |
| 86 | }); |
| 87 | test("undefined", () => { |
| 88 | expect(isNullSchema(S.Undefined)).toBe(false); |
| 89 | }); |
| 90 | test("not null", () => { |
| 91 | expect(isNullSchema(S.String)).toBe(false); |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | describe("isUndefinedSchema", { tags: ["unit", "local"] }, () => { |
| 96 | test("undefined", () => { |
| 97 | expect(isUndefinedSchema(S.Undefined)).toBe(true); |
| 98 | }); |
| 99 | test("not undefined", () => { |
| 100 | expect(isUndefinedSchema(S.String)).toBe(false); |
| 101 | }); |
| 102 | }); |
| 103 | |
| 104 | describe("isNullishSchema", { tags: ["unit", "local"] }, () => { |
| 105 | test("null", () => { |
| 106 | expect(isNullishSchema(S.Null)).toBe(true); |
| 107 | }); |
| 108 | test("undefined", () => { |
| 109 | expect(isNullishSchema(S.Undefined)).toBe(true); |
| 110 | }); |
| 111 | test("not nullish", () => { |
| 112 | expect(isNullishSchema(S.String)).toBe(false); |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | describe("isBooleanSchema", { tags: ["unit", "local"] }, () => { |
| 117 | test("boolean", () => { |
| 118 | expect(isBooleanSchema(S.Boolean)).toBe(true); |
| 119 | }); |
| 120 | test("not boolean", () => { |
| 121 | expect(isBooleanSchema(S.String)).toBe(false); |
| 122 | }); |
| 123 | }); |