11import { describe , expect , test } from "bun:test" ;
2- import { z } from "zod" ;
32import { compile , expr , variable } from "./index" ;
43
54describe ( "compile 单元测试" , ( ) => {
65 describe ( "编译输出格式" , ( ) => {
76 test ( "基本结构: [变量名数组, ...表达式]" , ( ) => {
8- const x = variable ( z . number ( ) ) ;
9- const y = variable ( z . number ( ) ) ;
7+ const x = variable < number > ( ) ;
8+ const y = variable < number > ( ) ;
109
1110 const sum = expr ( { x, y } ) ( "x + y" ) ;
1211 const result = compile ( sum , { x, y } ) ;
@@ -17,8 +16,8 @@ describe("compile 单元测试", () => {
1716 } ) ;
1817
1918 test ( "变量占位符替换" , ( ) => {
20- const x = variable ( z . number ( ) ) ;
21- const y = variable ( z . number ( ) ) ;
19+ const x = variable < number > ( ) ;
20+ const y = variable < number > ( ) ;
2221
2322 const sum = expr ( { x, y } ) ( "x + y" ) ;
2423 const result = compile ( sum , { x, y } ) ;
@@ -28,9 +27,9 @@ describe("compile 单元测试", () => {
2827 } ) ;
2928
3029 test ( "变量顺序与声明顺序一致" , ( ) => {
31- const a = variable ( z . number ( ) ) ;
32- const b = variable ( z . number ( ) ) ;
33- const c = variable ( z . number ( ) ) ;
30+ const a = variable < number > ( ) ;
31+ const b = variable < number > ( ) ;
32+ const c = variable < number > ( ) ;
3433
3534 const expr1 = expr ( { a, b, c } ) ( "c + b + a" ) ;
3635 const result = compile ( expr1 , { a, b, c } ) ;
@@ -39,8 +38,8 @@ describe("compile 单元测试", () => {
3938 } ) ;
4039
4140 test ( "相似变量名正确区分" , ( ) => {
42- const x = variable ( z . number ( ) ) ;
43- const xy = variable ( z . number ( ) ) ;
41+ const x = variable < number > ( ) ;
42+ const xy = variable < number > ( ) ;
4443
4544 // xy 不应该被 x 的替换影响
4645 const e = expr ( { xy, x } ) ( "xy + x" ) ;
@@ -53,8 +52,8 @@ describe("compile 单元测试", () => {
5352
5453 describe ( "错误检测" , ( ) => {
5554 test ( "检测未定义的变量引用" , ( ) => {
56- const x = variable ( z . number ( ) ) ;
57- const y = variable ( z . number ( ) ) ;
55+ const x = variable < number > ( ) ;
56+ const y = variable < number > ( ) ;
5857
5958 const sum = expr ( { x, y } ) ( "x + y + z" ) ;
6059
@@ -64,7 +63,7 @@ describe("compile 单元测试", () => {
6463 } ) ;
6564
6665 test ( "检测表达式中引用不存在的上下文变量" , ( ) => {
67- const x = variable ( z . number ( ) ) ;
66+ const x = variable < number > ( ) ;
6867
6968 const e1 = expr ( { x } ) ( "x + 1" ) ;
7069 const e2 = expr ( { e1 } ) ( "e1 + e2" ) ; // e2 不存在
@@ -77,8 +76,8 @@ describe("compile 单元测试", () => {
7776
7877 describe ( "内联优化输出" , ( ) => {
7978 test ( "内联模式减少表达式数量" , ( ) => {
80- const x = variable ( z . number ( ) ) ;
81- const y = variable ( z . number ( ) ) ;
79+ const x = variable < number > ( ) ;
80+ const y = variable < number > ( ) ;
8281
8382 const sum = expr ( { x, y } ) ( "x + y" ) ;
8483 const product = expr ( { x, y } ) ( "x * y" ) ;
@@ -92,8 +91,8 @@ describe("compile 单元测试", () => {
9291 } ) ;
9392
9493 test ( "非内联模式保留所有中间表达式" , ( ) => {
95- const x = variable ( z . number ( ) ) ;
96- const y = variable ( z . number ( ) ) ;
94+ const x = variable < number > ( ) ;
95+ const y = variable < number > ( ) ;
9796
9897 const sum = expr ( { x, y } ) ( "x + y" ) ;
9998 const product = expr ( { x, y } ) ( "x * y" ) ;
@@ -111,8 +110,8 @@ describe("compile 单元测试", () => {
111110
112111 describe ( "AST 规范化" , ( ) => {
113112 test ( "输出不包含多余空格" , ( ) => {
114- const x = variable ( z . number ( ) ) ;
115- const y = variable ( z . number ( ) ) ;
113+ const x = variable < number > ( ) ;
114+ const y = variable < number > ( ) ;
116115
117116 // 原始表达式有空格
118117 const e = expr ( { x, y } ) ( "x + y * 2" ) ;
@@ -123,8 +122,8 @@ describe("compile 单元测试", () => {
123122 } ) ;
124123
125124 test ( "保留必要的括号" , ( ) => {
126- const x = variable ( z . number ( ) ) ;
127- const y = variable ( z . number ( ) ) ;
125+ const x = variable < number > ( ) ;
126+ const y = variable < number > ( ) ;
128127
129128 const e = expr ( { x, y } ) ( "(x + y) * 2" ) ;
130129 const result = compile ( e , { x, y } ) ;
0 commit comments