| 50 | * @since 4.0.0 |
| 51 | */ |
| 52 | export class Asserts<S extends Schema.Constraint> { |
| 53 | /** |
| 54 | * Static helpers for comparing schema AST structures. |
| 55 | * |
| 56 | * **When to use** |
| 57 | * |
| 58 | * Use to assert that two schema field or tuple element definitions produce |
| 59 | * the same AST structure. |
| 60 | * |
| 61 | * **Details** |
| 62 | * |
| 63 | * `ast.fields.equals(a, b)` compares struct field ASTs via `assert.deepStrictEqual`. `ast.elements.equals(a, b)` compares tuple element ASTs via `assert.deepStrictEqual`. |
| 64 | * |
| 65 | * **Example** (Comparing struct fields) |
| 66 | * |
| 67 | * ```ts import.meta.vitest |
| 68 | * import { Schema } from "effect" |
| 69 | * import { TestSchema } from "effect/testing" |
| 70 | * |
| 71 | * const fieldsA = { name: Schema.String } |
| 72 | * const fieldsB = { name: Schema.String } |
| 73 | * TestSchema.Asserts.ast.fields.equals(fieldsA, fieldsB) // => undefined |
| 74 | * ``` |
| 75 | */ |
| 76 | static ast = { |
| 77 | fields: { |
| 78 | equals: (a: Schema.Struct.Fields, b: Schema.Struct.Fields) => { |
| 79 | assert.deepStrictEqual(Record.map(a, SchemaAST.getAST), Record.map(b, SchemaAST.getAST)) |
| 80 | } |
| 81 | }, |
| 82 | elements: { |
| 83 | equals: (a: Schema.Tuple.Elements, b: Schema.Tuple.Elements) => { |
| 84 | assert.deepStrictEqual(a.map(SchemaAST.getAST), b.map(SchemaAST.getAST)) |
| 85 | } |
| 86 | } |
| 87 | } as const |
| 88 | |
| 89 | readonly schema: S |
| 90 | constructor(schema: S) { |
| 91 | this.schema = schema |
| 92 | } |
| 93 | /** |
| 94 | * Returns an object with `succeed` and `fail` helpers for testing the schema's `make` operation. |
| 95 | * |
| 96 | * **When to use** |
| 97 | * |
| 98 | * Use to assert how `Schema.make` accepts, transforms, or rejects |
| 99 | * construction input for this schema. |
| 100 | * |
| 101 | * **Details** |
| 102 | * |
| 103 | * `succeed(input)` asserts make returns the input unchanged. `succeed(input, expected)` asserts make returns `expected`. `fail(input, message)` asserts make fails with `message`. |
| 104 | * |
| 105 | * **Example** (Testing make) |
| 106 | * |
| 107 | * ```ts import.meta.vitest |
| 108 | * import { Schema } from "effect" |
| 109 | * import { TestSchema } from "effect/testing" |