diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-02-06 06:18:58 +0000 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-02-06 06:18:58 +0000 |
| commit | aacf6dd9ebdb4d55b432ea1d4213093fe35e0ad1 (patch) | |
| tree | 25926b10c10bc36133cfd1a337190dd003088e2e /spec/cli/README.md | |
| parent | 1f83d504bc344ffd3c8b4120b3865fd6c11a9e2d (diff) | |
| download | yuescript-aacf6dd9ebdb4d55b432ea1d4213093fe35e0ad1.tar.gz yuescript-aacf6dd9ebdb4d55b432ea1d4213093fe35e0ad1.tar.bz2 yuescript-aacf6dd9ebdb4d55b432ea1d4213093fe35e0ad1.zip | |
test: add comprehensive CLI test suite
Add 56 test cases across 4 test suites to verify the yue command line
tool functionality:
- Basic options test: -h, --help, -v, --version flags
- Compilation test: file/directory compilation with various options
- Error handling test: syntax errors, file not found, edge cases
- Execution test: -e option, script arguments, macros
The test framework includes helper functions for assertions and test
environment setup. All tests can be run via `bash spec/cli/run_all_tests.sh`.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'spec/cli/README.md')
| -rw-r--r-- | spec/cli/README.md | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/spec/cli/README.md b/spec/cli/README.md new file mode 100644 index 0000000..ea14e72 --- /dev/null +++ b/spec/cli/README.md | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | # YueScript CLI Tests | ||
| 2 | |||
| 3 | 测试 YueScript 命令行工具的功能。 | ||
| 4 | |||
| 5 | ## 测试结构 | ||
| 6 | |||
| 7 | ``` | ||
| 8 | spec/cli/ | ||
| 9 | ├── cli_test_helper.sh # 测试辅助函数库 | ||
| 10 | ├── test_basic_options.sh # 基本选项测试 | ||
| 11 | ├── test_compilation.sh # 编译功能测试 | ||
| 12 | ├── test_error_handling.sh # 错误处理测试 | ||
| 13 | ├── test_execution.sh # 代码执行测试 | ||
| 14 | ├── run_all_tests.sh # 运行所有测试 | ||
| 15 | └── README.md # 本文件 | ||
| 16 | ``` | ||
| 17 | |||
| 18 | ## 运行测试 | ||
| 19 | |||
| 20 | ### 前置要求 | ||
| 21 | |||
| 22 | 确保已编译 yue 二进制文件: | ||
| 23 | |||
| 24 | ```bash | ||
| 25 | make debug | ||
| 26 | ``` | ||
| 27 | |||
| 28 | ### 运行所有测试 | ||
| 29 | |||
| 30 | ```bash | ||
| 31 | cd spec/cli | ||
| 32 | bash run_all_tests.sh | ||
| 33 | ``` | ||
| 34 | |||
| 35 | ### 运行单个测试套件 | ||
| 36 | |||
| 37 | ```bash | ||
| 38 | # 测试基本选项 | ||
| 39 | bash test_basic_options.sh | ||
| 40 | |||
| 41 | # 测试编译功能 | ||
| 42 | bash test_compilation.sh | ||
| 43 | |||
| 44 | # 测试错误处理 | ||
| 45 | bash test_error_handling.sh | ||
| 46 | |||
| 47 | # 测试代码执行 | ||
| 48 | bash test_execution.sh | ||
| 49 | ``` | ||
| 50 | |||
| 51 | ### 指定 yue 二进制文件路径 | ||
| 52 | |||
| 53 | 如果二进制文件不在默认位置 (`./bin/debug/yue`),可以设置环境变量: | ||
| 54 | |||
| 55 | ```bash | ||
| 56 | YUE_BIN=/path/to/yue bash run_all_tests.sh | ||
| 57 | ``` | ||
| 58 | |||
| 59 | ## 测试覆盖范围 | ||
| 60 | |||
| 61 | ### 1. 基本选项测试 (test_basic_options.sh) | ||
| 62 | |||
| 63 | - `-h, --help`: 帮助信息 | ||
| 64 | - `-v, --version`: 版本信息 | ||
| 65 | - 无效选项的错误处理 | ||
| 66 | |||
| 67 | ### 2. 编译功能测试 (test_compilation.sh) | ||
| 68 | |||
| 69 | - 单文件编译到标准输出 (`-p`) | ||
| 70 | - 单文件编译到磁盘 | ||
| 71 | - 自定义输出文件 (`-o`) | ||
| 72 | - 目标目录编译 (`-t`) | ||
| 73 | - 目录递归编译 | ||
| 74 | - 保留行号 (`-l`) | ||
| 75 | - 使用空格代替制表符 (`-s`) | ||
| 76 | - 代码压缩 (`-m`) | ||
| 77 | - 标准输入/输出编译 | ||
| 78 | - 全局变量转储 (`-g`) | ||
| 79 | - 编译性能测试 (`-b`) | ||
| 80 | - 目标 Lua 版本 (`--target`) | ||
| 81 | |||
| 82 | ### 3. 错误处理测试 (test_error_handling.sh) | ||
| 83 | |||
| 84 | - 文件不存在错误 | ||
| 85 | - 语法错误 | ||
| 86 | - 无效选项组合 | ||
| 87 | - 空文件处理 | ||
| 88 | - 仅注释文件 | ||
| 89 | - 无效的目标版本 | ||
| 90 | - 复杂的合法代码 | ||
| 91 | - Unicode 处理 | ||
| 92 | - 超长行处理 | ||
| 93 | - 深层嵌套代码 | ||
| 94 | - 宏错误处理 | ||
| 95 | |||
| 96 | ### 4. 代码执行测试 (test_execution.sh) | ||
| 97 | |||
| 98 | - 内联代码执行 (`-e`) | ||
| 99 | - 计算表达式 | ||
| 100 | - 字符串插值 | ||
| 101 | - YueScript 文件执行 | ||
| 102 | - Lua 文件执行 | ||
| 103 | - 脚本参数传递 | ||
| 104 | - 编译器选项传递 | ||
| 105 | - 表操作 | ||
| 106 | - 函数定义与调用 | ||
| 107 | - 类与对象 | ||
| 108 | - 导入语句 | ||
| 109 | - 错误处理 | ||
| 110 | - 导出语句 | ||
| 111 | - 宏展开 | ||
| 112 | |||
| 113 | ## 测试辅助函数 | ||
| 114 | |||
| 115 | `cli_test_helper.sh` 提供以下函数: | ||
| 116 | |||
| 117 | - `check_yue_binary`: 检查 yue 二进制文件是否存在 | ||
| 118 | - `assert_success`: 断言命令成功 | ||
| 119 | - `assert_failure`: 断言命令失败 | ||
| 120 | - `assert_output_contains`: 断言输出包含指定字符串 | ||
| 121 | - `assert_output_equals`: 断言输出等于指定字符串 | ||
| 122 | - `assert_file_exists`: 断言文件存在 | ||
| 123 | - `print_summary`: 打印测试摘要 | ||
| 124 | - `setup_test_env`: 设置测试环境 | ||
| 125 | - `create_test_file`: 创建测试文件 | ||
| 126 | |||
| 127 | ## 添加新测试 | ||
| 128 | |||
| 129 | 1. 在对应的测试文件中添加新的测试用例 | ||
| 130 | 2. 使用提供的辅助函数来断言结果 | ||
| 131 | 3. 确保测试描述清晰明了 | ||
| 132 | 4. 运行测试验证功能 | ||
| 133 | |||
| 134 | 示例: | ||
| 135 | |||
| 136 | ```bash | ||
| 137 | echo "Testing new feature..." | ||
| 138 | cat > "$TMP_DIR/new_test.yue" << 'EOF' | ||
| 139 | -- test code here | ||
| 140 | EOF | ||
| 141 | |||
| 142 | assert_output_contains "New feature should work" "expected output" $YUE_BIN "$TMP_DIR/new_test.yue" | ||
| 143 | ``` | ||
| 144 | |||
| 145 | ## 持续集成 | ||
| 146 | |||
| 147 | 这些测试可以集成到 CI/CD 流程中,确保每次代码变更都不会破坏命令行工具的功能。 | ||
