|
| 1 | +import type { PSEnv, PSFnCallContext, PSList, PSValue } from "../types.ts"; |
| 2 | +import type { Operation } from "../deps.ts"; |
| 3 | +import { equal } from "../equal.ts"; |
| 4 | +import * as data from "../data.ts"; |
| 5 | + |
| 6 | +export interface TestResult { |
| 7 | + type: "pass" | "fail"; |
| 8 | + message: PSValue; |
| 9 | +} |
| 10 | + |
| 11 | +export function isTestResult(value: unknown): value is TestResult { |
| 12 | + return !!value && typeof value === "object" && |
| 13 | + ["pass", "fail"].includes((value as TestResult).type); |
| 14 | +} |
| 15 | + |
| 16 | +export const testing = data.map({ |
| 17 | + "test": data.fn(function* (cxt) { |
| 18 | + return data.external(yield* test(cxt)); |
| 19 | + }, { name: "definition" }), |
| 20 | + "toEqual": data.fn(function* (expected) { |
| 21 | + return data.fn(function* (actual) { |
| 22 | + if (equal(expected.arg, actual.arg).value) { |
| 23 | + return data.external({ |
| 24 | + type: "pass", |
| 25 | + message: data.map({ |
| 26 | + "equal to": expected.arg, |
| 27 | + }), |
| 28 | + }); |
| 29 | + } else { |
| 30 | + return data.external({ |
| 31 | + type: "fail", |
| 32 | + message: data.map({ |
| 33 | + "equal to": expected.arg, |
| 34 | + }), |
| 35 | + }); |
| 36 | + } |
| 37 | + }, { name: "actual" }); |
| 38 | + }, { name: "expected" }), |
| 39 | + "not": data.fn(function* (matcher) { |
| 40 | + return data.fn(function* (actual) { |
| 41 | + if (matcher.arg.type === "fn") { |
| 42 | + let result = yield* actual.env.call(matcher.arg, actual.arg); |
| 43 | + if (result.type === "external" && result.value.type == "fail") { |
| 44 | + return data.external({ |
| 45 | + type: "pass", |
| 46 | + message: data.map({ |
| 47 | + "not": result.value.message, |
| 48 | + }), |
| 49 | + }); |
| 50 | + } else { |
| 51 | + return data.external({ |
| 52 | + type: "fail", |
| 53 | + message: data.map({ |
| 54 | + "not": result.value.message, |
| 55 | + }), |
| 56 | + }); |
| 57 | + } |
| 58 | + } else { |
| 59 | + return data.external({ |
| 60 | + type: "fail", |
| 61 | + message: data.map({ |
| 62 | + "not": actual.arg, |
| 63 | + }), |
| 64 | + }); |
| 65 | + } |
| 66 | + }, { name: "actual" }); |
| 67 | + }, { name: "matcher" }), |
| 68 | +}); |
| 69 | + |
| 70 | +function* test({ arg, env }: PSFnCallContext): Operation<TestResult[]> { |
| 71 | + if (arg.type !== "list") { |
| 72 | + return [yield* step(arg, env)]; |
| 73 | + } else { |
| 74 | + let results: TestResult[] = []; |
| 75 | + for (let item of arg.value) { |
| 76 | + results.push(yield* step(item, env)); |
| 77 | + } |
| 78 | + return results; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function* step(arg: PSValue, env: PSEnv): Operation<TestResult> { |
| 83 | + if (arg.type === "map") { |
| 84 | + for (let [key, value] of arg.value.entries()) { |
| 85 | + if (key.value === "expect") { |
| 86 | + if (value.type === "list") { |
| 87 | + let [first, ...rest] = value.value ?? data.string("null"); |
| 88 | + let subject = yield* env.eval(first ?? data.string("")); |
| 89 | + let results: PSValue[] = []; |
| 90 | + let pass = true; |
| 91 | + let matchers = (yield* env.eval(data.list(rest))) as PSList; |
| 92 | + for (let matcher of matchers.value) { |
| 93 | + if (matcher.type === "fn") { |
| 94 | + let result = yield* env.call(matcher, subject); |
| 95 | + if ( |
| 96 | + result.type === "external" && result.value && |
| 97 | + result.value.type && result.value.message |
| 98 | + ) { |
| 99 | + if (result.value.type === "fail") { |
| 100 | + pass = false; |
| 101 | + } |
| 102 | + results.push(result.value.message); |
| 103 | + } else { |
| 104 | + results.push(result); |
| 105 | + } |
| 106 | + } else { |
| 107 | + results.push(matcher); |
| 108 | + } |
| 109 | + } |
| 110 | + return { |
| 111 | + type: pass ? "pass" : "fail", |
| 112 | + message: data.list([ |
| 113 | + first, |
| 114 | + ...results, |
| 115 | + ]), |
| 116 | + }; |
| 117 | + } else { |
| 118 | + return { |
| 119 | + type: "pass", |
| 120 | + message: value, |
| 121 | + }; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return { |
| 127 | + type: "pass", |
| 128 | + message: arg, |
| 129 | + }; |
| 130 | +} |
0 commit comments