示例工程 26-testing-and-lint

26-testing-and-lint/02-error-assertions/README.md

markdown · 0.6 KB

# 02 · error-assertions
 
**演示**:`pcall` 捕获错误后断言消息内容,而不是只断言「失败了」。
**运行时**:`lua54`、`luajit`。
**运行**:
 
```sh
.tools/bin/lua54 scripts/run-example.lua examples/26-testing-and-lint/02-error-assertions
.tools/bin/lua54 scripts/test-example.lua examples/26-testing-and-lint/02-error-assertions
```
 
**预期输出**(lua 5.4.9 / macOS arm64):
 
```text
ok=false message=not a number: x
```
 
**看点**:错误消息是契约的一部分;测试它,避免悄悄变成别的错误。
**注意**:不要把整个 traceback 写进断言,只匹配稳定片段。