@nudojs/cli 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/run-test.ts DELETED
@@ -1,92 +0,0 @@
1
- /**
2
- * nudo test — @nudo:case 即测试。
3
- *
4
- * directive case 带 `=> expected` 时,用 isSubtypeOf 校验推断结果;
5
- * 失败记 FAIL,进程退出码 1。无期望的 directive case 记 unchecked。
6
- */
7
-
8
- import { isSubtypeOf, typeValueToString, type TypeValue } from "@nudojs/core";
9
- import type { AnalysisResult } from "@nudojs/service";
10
-
11
- export type CaseTestOutcome = {
12
- fn: string;
13
- caseName: string;
14
- ok: boolean;
15
- unchecked: boolean;
16
- expected?: string;
17
- actual: string;
18
- };
19
-
20
- export type TestReport = {
21
- file: string;
22
- passed: number;
23
- failed: number;
24
- unchecked: number;
25
- outcomes: CaseTestOutcome[];
26
- };
27
-
28
- export function buildTestReport(file: string, result: AnalysisResult): TestReport {
29
- const outcomes: CaseTestOutcome[] = [];
30
- for (const fn of result.functions) {
31
- for (const c of fn.cases) {
32
- if (c.source !== "directive") continue;
33
- const actual = typeValueToString(c.result);
34
- if (!c.expected) {
35
- outcomes.push({
36
- fn: fn.name,
37
- caseName: c.name,
38
- ok: true,
39
- unchecked: true,
40
- actual,
41
- });
42
- continue;
43
- }
44
- const expected = typeValueToString(c.expected as TypeValue);
45
- let ok = false;
46
- try {
47
- ok = isSubtypeOf(c.result, c.expected as TypeValue);
48
- } catch {
49
- ok = false;
50
- }
51
- outcomes.push({
52
- fn: fn.name,
53
- caseName: c.name,
54
- ok,
55
- unchecked: false,
56
- expected,
57
- actual,
58
- });
59
- }
60
- }
61
- return {
62
- file,
63
- passed: outcomes.filter((o) => o.ok && !o.unchecked).length,
64
- failed: outcomes.filter((o) => !o.ok).length,
65
- unchecked: outcomes.filter((o) => o.unchecked).length,
66
- outcomes,
67
- };
68
- }
69
-
70
- export function formatTestReport(r: TestReport): string {
71
- const lines: string[] = [];
72
- lines.push(`nudo test ${r.file}`);
73
- if (r.passed + r.failed + r.unchecked === 0) {
74
- lines.push("No @nudo:case directives found.");
75
- return lines.join("\n");
76
- }
77
- lines.push(r.failed === 0 ? "PASS" : "FAILED");
78
- lines.push(` ${r.passed} passed · ${r.failed} failed · ${r.unchecked} unchecked`);
79
- lines.push("");
80
- for (const o of r.outcomes) {
81
- if (o.unchecked) {
82
- lines.push(` [unchecked] ${o.fn} case "${o.caseName}" → ${o.actual}`);
83
- } else if (o.ok) {
84
- lines.push(` [ok] ${o.fn} case "${o.caseName}" → ${o.actual}`);
85
- } else {
86
- lines.push(` [FAIL] ${o.fn} case "${o.caseName}"`);
87
- lines.push(` expected: ${o.expected}`);
88
- lines.push(` actual: ${o.actual}`);
89
- }
90
- }
91
- return lines.join("\n");
92
- }
@@ -1,96 +0,0 @@
1
- /**
2
- * TypeValue 项(term)身份旁路。
3
- *
4
- * core 的 TypeValue 无 term 字段;用 WeakMap 旁路挂载,零侵入。
5
- * 纪律:
6
- * - 禁止给共享单例(T.number 等)挂 term——必须先 clone 再 attach
7
- * - typeValueToAbs 优先读旁路;无旁路时按 kind 推导
8
- */
9
-
10
- import type { TypeValue } from "@nudojs/core";
11
- import { T } from "@nudojs/core";
12
- import type { Term } from "@nudojs/core";
13
- import { v as termVar, lit } from "@nudojs/core";
14
- import type { Pred } from "@nudojs/core";
15
-
16
- const termByTv = new WeakMap<object, Term>();
17
- const predByTv = new WeakMap<object, Pred>();
18
-
19
- export function attachTerm(tv: TypeValue, term: Term): void {
20
- if (tv && typeof tv === "object") termByTv.set(tv as object, term);
21
- }
22
-
23
- export function getTerm(tv: TypeValue): Term | undefined {
24
- if (tv && typeof tv === "object") return termByTv.get(tv as object);
25
- return undefined;
26
- }
27
-
28
- export function attachPred(tv: TypeValue, pred: Pred): void {
29
- if (tv && typeof tv === "object") predByTv.set(tv as object, pred);
30
- }
31
-
32
- export function getPred(tv: TypeValue): Pred | undefined {
33
- if (tv && typeof tv === "object") return predByTv.get(tv as object);
34
- return undefined;
35
- }
36
-
37
- /**
38
- * 为参数绑定克隆 TypeValue 并挂 term。
39
- * - primitive number → clone + var(name)
40
- * - literal number → clone + lit(value)(与 kind 推导一致,但显式)
41
- * - refined → clone + 尽量挂 base term + pred
42
- */
43
- export function tagParamArg(arg: TypeValue, paramName: string): TypeValue {
44
- if (!paramName || paramName.startsWith("...")) return arg;
45
- // 函数/实例不 clone:浅拷贝会丢 _signature / 原型表(Promise executor 等)
46
- if (arg.kind === "function" || arg.kind === "instance") return arg;
47
- const cloned = cloneTypeValue(arg);
48
- if (cloned.kind === "primitive" && cloned.type === "number") {
49
- attachTerm(cloned, termVar(paramName));
50
- } else if (cloned.kind === "literal" && typeof cloned.value === "number") {
51
- attachTerm(cloned, lit(cloned.value));
52
- } else if (cloned.kind === "refined") {
53
- attachTerm(cloned, termVar(paramName));
54
- }
55
- return cloned;
56
- }
57
-
58
- function cloneTypeValue(tv: TypeValue): TypeValue {
59
- switch (tv.kind) {
60
- case "literal":
61
- return { kind: "literal", value: tv.value };
62
- case "primitive":
63
- return { kind: "primitive", type: tv.type };
64
- case "refined":
65
- return { kind: "refined", base: tv.base, refinement: tv.refinement };
66
- case "object":
67
- return { kind: "object", properties: tv.properties, id: tv.id };
68
- case "array":
69
- return { kind: "array", element: tv.element };
70
- case "tuple":
71
- return { kind: "tuple", elements: tv.elements };
72
- case "function":
73
- return {
74
- kind: "function",
75
- params: tv.params,
76
- body: tv.body,
77
- closure: tv.closure,
78
- };
79
- case "promise":
80
- return { kind: "promise", value: tv.value };
81
- case "instance":
82
- return {
83
- kind: "instance",
84
- className: tv.className,
85
- properties: tv.properties,
86
- };
87
- case "union":
88
- return { kind: "union", members: tv.members };
89
- case "never":
90
- return { kind: "never" };
91
- case "unknown":
92
- return { kind: "unknown" };
93
- default:
94
- return tv;
95
- }
96
- }