测试系统
Alia 的测试系统提供 test、assert、inspect、breakpoint 四个关键字,用于编写和运行测试。
1. 测试关键字
test — 声明测试块
test <String what> <Stack content>
test 用于声明一个测试块,包含测试名称和测试内容。
assert — 断言
assert <Boolean what>
assert <Boolean what> error <String errorMessage>
assert 用于断言一个条件为真,如果条件为假则抛出错误。error 关键字用于指定断言失败时的错误信息。
inspect — 生成相等断言
inspect <Value what>
inspect <Value what> error <String errorMessage>
inspect 会在测试代码的当前位置生成一个相等断言,自动将表达式转为与其执行结果的比较。例如 const v = 2 之后的 inspect v error '错误' 转换为 assert v == 2 '错误'。error 关键字用于指定断言失败时的错误信息。
breakpoint — 断点
breakpoint
breakpoint 进入交互式 Aliaẞhell,当前上下文保持不变。输入的代码会即时执行,并且可直接访问当前作用域中的变量。按 Ctrl+C 可退出 Shell。
2. 基本用法
基本断言
test 'basic-assert' {
assert 1 + 1 == 2
assert 1 + 1 == 2 error '加法断言失败'
assert 'hello' .. ' ' .. 'world' == 'hello world' error '字符串拼接失败'
}
test 'numeric-assertions' {
assert 10 > 5
assert 10 > 5 error '数值比较失败'
assert 3 * 4 == 12 error '乘法断言失败'
assert 10 % 3 == 1 error '取模断言失败'
}
生成断言
test 'inspect-basics' {
const a = 1
inspect a
inspect a error '变量值检查失败'
const b = 2 + 2
inspect b
inspect b error '表达式值检查失败'
}
空值断言
Ok 判定表达式是否非空。在 assert 中,Ok 后接变量时须使用括号包裹,否则无法解析。
test 'null-check' {
let value = null
assert !(Ok value) error 'value 应为空'
value = 42
assert (Ok value) error 'value 不应为空'
}
3. 与错误处理的交互
test 'error-handling' {
let error_caught = false
try {
throw '测试错误'
} catch e {
error_caught = true
}
assert error_caught == true error '错误捕获失败'
}
test 'nested-error-handling' {
let outer_error = null
try {
try {
throw '内层错误'
} catch inner {
outer_error = inner
throw '外层错误'
}
} catch outer {
outer_error = outer
}
assert outer_error == '外层错误' error '嵌套错误处理失败'
}
4. 条件与循环中的断言
test 'conditional-assertions' {
const n = 5
let result = 0
if n > 0 {
result = 1
}
assert result == 1 error '条件分支断言失败'
}
test 'loop-assertions' {
let sum = 0
for { let i = 0 } i < 5 { i++ } {
sum = sum + i
}
assert sum == 10 error '循环求和失败'
}
5. 全局模式接口
Alia 提供三个维度的执行模式,在调用时通过 options 参数传入:
解释/编译维度 (interpretMode)
| 模式 | 描述 |
|---|---|
'interpret' | 解释执行模式(默认) |
'compile' | 编译模式 |
运行/测试维度 (executeMode)
| 模式 | 描述 |
|---|---|
'run' | 运行模式(默认)— 直接跳过所有测试块,不执行 |
'test' | 测试模式 — 执行测试块,可配合 testFilters 过滤特定测试模块 |
代码来源维度 (sourceMode)
| 模式 | 描述 |
|---|---|
'code' | 直接代码字符串(默认) |
'file' | 从文件读取代码 |
'project' | 从项目读取代码 |
行为说明
- 在
run模式中,test块被跳过,不执行任何测试代码 - 在
test模式中,test块被执行,在隔离的执行栈中运行 assert或inspect失败会抛出错误,导致整个测试失败testFilters为空数组时表示执行所有测试;否则仅执行过滤列表中的测试- 每个测试执行后输出
[TEST PASSED]或[TEST FAILED]标记
6. 运行测试
测试入口位于 Base/Executor/Tests/test.js,通过目标与选项控制执行范围。
node Tests/test.js [target] [options]
| 目标 | 说明 |
|---|---|
native | 运行 Alia 解释器测试 |
compile-js | 运行编译到 JS 的测试 |
compile-nodejs | 运行编译到 Node.js 的测试 |
compile-rs | 运行编译到 Rust 的测试 |
system | 运行 JS 系统单元测试 |
all | 运行全部测试(默认) |
| 选项 | 说明 |
|---|---|
--filters, -f <patterns> | 按文件名过滤(逗号分隔) |
--tests, -t <patterns> | 按测试块名过滤(逗号分隔) |
--run, -r | 运行模式,跳过测试块 |
--errors-only, -e | 仅输出错误 |
--help, -h | 显示帮助 |
Base/Executor 提供的 npm 脚本:
npm test # 运行全部测试
npm run test:native # 运行解释器测试
npm run test:compile-js # 运行 JS 编译测试
npm run test:compile-nodejs # 运行 Node.js 编译测试
npm run test:compile-rs # 运行 Rust 编译测试
npm run test:system # 运行系统单元测试
7. 完整示例
~ Alia 测试系统完整示例
test 'basic-assert' {
assert 1 + 1 == 2
assert 1 + 1 == 2 error '加法断言失败'
assert 'hello' .. ' ' .. 'world' == 'hello world' error '字符串拼接失败'
}
test 'inspect-basics' {
const a = 1
inspect a
inspect a error '变量值检查失败'
const b = 2 + 2
inspect b
inspect b error '表达式值检查失败'
}
test 'error-handling' {
let error_caught = false
try {
throw '测试错误'
} catch e {
error_caught = true
}
assert error_caught == true error '错误捕获失败'
}
test 'multiple-assertions' {
const x = 10
assert x > 0 error '正数断言失败'
assert x < 20 error '范围检查失败'
assert x == 10 error '精确值检查失败'
}