aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-02-06 06:18:58 +0000
committerLi Jin <dragon-fly@qq.com>2026-02-06 06:18:58 +0000
commitaacf6dd9ebdb4d55b432ea1d4213093fe35e0ad1 (patch)
tree25926b10c10bc36133cfd1a337190dd003088e2e
parent1f83d504bc344ffd3c8b4120b3865fd6c11a9e2d (diff)
downloadyuescript-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>
-rw-r--r--makefile6
-rw-r--r--spec/cli/README.md147
-rwxr-xr-xspec/cli/cli_test_helper.sh183
-rwxr-xr-xspec/cli/run_all_tests.sh77
-rwxr-xr-xspec/cli/test_basic_options.sh38
-rwxr-xr-xspec/cli/test_compilation.sh148
-rwxr-xr-xspec/cli/test_error_handling.sh169
-rwxr-xr-xspec/cli/test_execution.sh159
8 files changed, 927 insertions, 0 deletions
diff --git a/makefile b/makefile
index 33d02cf..83f9c5d 100644
--- a/makefile
+++ b/makefile
@@ -447,6 +447,12 @@ test: debug
447 @busted 447 @busted
448 @echo "Done!" 448 @echo "Done!"
449 449
450# Test CLI tool functionality
451.PHONY: test-cli
452test-cli: debug
453 @echo "Running CLI tests..."
454 @bash spec/cli/run_all_tests.sh
455
450# Test Yuescript compiler 456# Test Yuescript compiler
451.PHONY: gen 457.PHONY: gen
452gen: release 458gen: release
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```
8spec/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
25make debug
26```
27
28### 运行所有测试
29
30```bash
31cd spec/cli
32bash run_all_tests.sh
33```
34
35### 运行单个测试套件
36
37```bash
38# 测试基本选项
39bash test_basic_options.sh
40
41# 测试编译功能
42bash test_compilation.sh
43
44# 测试错误处理
45bash test_error_handling.sh
46
47# 测试代码执行
48bash test_execution.sh
49```
50
51### 指定 yue 二进制文件路径
52
53如果二进制文件不在默认位置 (`./bin/debug/yue`),可以设置环境变量:
54
55```bash
56YUE_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
1291. 在对应的测试文件中添加新的测试用例
1302. 使用提供的辅助函数来断言结果
1313. 确保测试描述清晰明了
1324. 运行测试验证功能
133
134示例:
135
136```bash
137echo "Testing new feature..."
138cat > "$TMP_DIR/new_test.yue" << 'EOF'
139-- test code here
140EOF
141
142assert_output_contains "New feature should work" "expected output" $YUE_BIN "$TMP_DIR/new_test.yue"
143```
144
145## 持续集成
146
147这些测试可以集成到 CI/CD 流程中,确保每次代码变更都不会破坏命令行工具的功能。
diff --git a/spec/cli/cli_test_helper.sh b/spec/cli/cli_test_helper.sh
new file mode 100755
index 0000000..ade1546
--- /dev/null
+++ b/spec/cli/cli_test_helper.sh
@@ -0,0 +1,183 @@
1#!/bin/bash
2# CLI Test Helper for YueScript
3# Provides utility functions for testing the yue command line tool
4
5# Colors for output
6RED='\033[0;31m'
7GREEN='\033[0;32m'
8YELLOW='\033[1;33m'
9NC='\033[0m' # No Color
10
11# Test counters
12TESTS_RUN=0
13TESTS_PASSED=0
14TESTS_FAILED=0
15
16# Get the yue binary path
17YUE_BIN="${YUE_BIN:-./bin/debug/yue}"
18
19# Check if yue binary exists
20check_yue_binary() {
21 if [ ! -f "$YUE_BIN" ]; then
22 echo -e "${RED}Error: yue binary not found at $YUE_BIN${NC}"
23 echo "Please build the project first or set YUE_BIN environment variable"
24 exit 1
25 fi
26 if [ ! -x "$YUE_BIN" ]; then
27 echo -e "${RED}Error: yue binary is not executable${NC}"
28 exit 1
29 fi
30}
31
32# Assert that a command succeeds
33assert_success() {
34 local description="$1"
35 shift
36 TESTS_RUN=$((TESTS_RUN + 1))
37
38 if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then
39 echo -e "${GREEN}✓${NC} $description"
40 TESTS_PASSED=$((TESTS_PASSED + 1))
41 return 0
42 else
43 echo -e "${RED}✗${NC} $description"
44 echo -e " ${YELLOW}Exit code: $?${NC}"
45 echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}"
46 echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}"
47 TESTS_FAILED=$((TESTS_FAILED + 1))
48 return 1
49 fi
50}
51
52# Assert that a command fails
53assert_failure() {
54 local description="$1"
55 shift
56 TESTS_RUN=$((TESTS_RUN + 1))
57
58 if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then
59 echo -e "${RED}✗${NC} $description (expected failure but succeeded)"
60 echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}"
61 echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}"
62 TESTS_FAILED=$((TESTS_FAILED + 1))
63 return 1
64 else
65 echo -e "${GREEN}✓${NC} $description"
66 TESTS_PASSED=$((TESTS_PASSED + 1))
67 return 0
68 fi
69}
70
71# Assert that output contains expected string
72assert_output_contains() {
73 local description="$1"
74 local expected="$2"
75 shift 2
76 TESTS_RUN=$((TESTS_RUN + 1))
77
78 if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then
79 if grep -qF -- "$expected" /tmp/test_stdout.txt || grep -qF -- "$expected" /tmp/test_stderr.txt; then
80 echo -e "${GREEN}✓${NC} $description"
81 TESTS_PASSED=$((TESTS_PASSED + 1))
82 return 0
83 else
84 echo -e "${RED}✗${NC} $description (output doesn't contain '$expected')"
85 echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}"
86 echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}"
87 TESTS_FAILED=$((TESTS_FAILED + 1))
88 return 1
89 fi
90 else
91 echo -e "${RED}✗${NC} $description (command failed)"
92 echo -e " ${YELLOW}Exit code: $?${NC}"
93 TESTS_FAILED=$((TESTS_FAILED + 1))
94 return 1
95 fi
96}
97
98# Assert that output equals expected string
99assert_output_equals() {
100 local description="$1"
101 local expected="$2"
102 shift 2
103 TESTS_RUN=$((TESTS_RUN + 1))
104
105 if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then
106 local actual=$(cat /tmp/test_stdout.txt)
107 if [ "$actual" = "$expected" ]; then
108 echo -e "${GREEN}✓${NC} $description"
109 TESTS_PASSED=$((TESTS_PASSED + 1))
110 return 0
111 else
112 echo -e "${RED}✗${NC} $description (output mismatch)"
113 echo -e " ${YELLOW}Expected: '$expected'${NC}"
114 echo -e " ${YELLOW}Actual: '$actual'${NC}"
115 TESTS_FAILED=$((TESTS_FAILED + 1))
116 return 1
117 fi
118 else
119 echo -e "${RED}✗${NC} $description (command failed)"
120 echo -e " ${YELLOW}Exit code: $?${NC}"
121 TESTS_FAILED=$((TESTS_FAILED + 1))
122 return 1
123 fi
124}
125
126# Assert file exists
127assert_file_exists() {
128 local description="$1"
129 local filepath="$2"
130 TESTS_RUN=$((TESTS_RUN + 1))
131
132 if [ -f "$filepath" ]; then
133 echo -e "${GREEN}✓${NC} $description"
134 TESTS_PASSED=$((TESTS_PASSED + 1))
135 return 0
136 else
137 echo -e "${RED}✗${NC} $description (file not found: $filepath)"
138 TESTS_FAILED=$((TESTS_FAILED + 1))
139 return 1
140 fi
141}
142
143# Print test summary
144print_summary() {
145 echo ""
146 echo "======================================"
147 echo "Test Summary"
148 echo "======================================"
149 echo "Total tests: $TESTS_RUN"
150 echo -e "Passed: ${GREEN}$TESTS_PASSED${NC}"
151 echo -e "Failed: ${RED}$TESTS_FAILED${NC}"
152 echo "======================================"
153
154 if [ $TESTS_FAILED -eq 0 ]; then
155 echo -e "${GREEN}All tests passed!${NC}"
156 return 0
157 else
158 echo -e "${RED}Some tests failed!${NC}"
159 return 1
160 fi
161}
162
163# Setup test environment
164setup_test_env() {
165 # Create temporary directory for test files
166 TEST_TMP_DIR=$(mktemp -d)
167 export TEST_TMP_DIR
168 trap "rm -rf $TEST_TMP_DIR" EXIT
169 echo "Test directory: $TEST_TMP_DIR"
170}
171
172# Get test tmp dir
173get_test_tmp_dir() {
174 echo "$TEST_TMP_DIR"
175}
176
177# Create a test yue file
178create_test_file() {
179 local filepath="$1"
180 local content="$2"
181 mkdir -p "$(dirname "$filepath")"
182 echo "$content" > "$filepath"
183}
diff --git a/spec/cli/run_all_tests.sh b/spec/cli/run_all_tests.sh
new file mode 100755
index 0000000..0067bba
--- /dev/null
+++ b/spec/cli/run_all_tests.sh
@@ -0,0 +1,77 @@
1#!/bin/bash
2# Main test runner for YueScript CLI
3# Runs all CLI test suites
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6
7# Colors
8RED='\033[0;31m'
9GREEN='\033[0;32m'
10YELLOW='\033[1;33m'
11BLUE='\033[0;34m'
12NC='\033[0m' # No Color
13
14echo -e "${BLUE}======================================"
15echo "YueScript CLI Test Suite"
16echo "======================================${NC}"
17echo ""
18
19# Check if yue binary exists
20YUE_BIN="${YUE_BIN:-./bin/debug/yue}"
21if [ ! -f "$YUE_BIN" ]; then
22 echo -e "${RED}Error: yue binary not found at $YUE_BIN${NC}"
23 echo "Please build the project first:"
24 echo " make debug"
25 echo "Or set YUE_BIN environment variable"
26 exit 1
27fi
28
29# Track overall results
30TOTAL_SUITES=0
31PASSED_SUITES=0
32FAILED_SUITES=0
33
34# Function to run a test suite
35run_test_suite() {
36 local suite_name="$1"
37 local suite_file="$2"
38
39 TOTAL_SUITES=$((TOTAL_SUITES + 1))
40 echo ""
41 echo -e "${BLUE}Running: $suite_name${NC}"
42 echo "======================================"
43
44 if bash "$suite_file"; then
45 echo -e "${GREEN}✓ $suite_name PASSED${NC}"
46 PASSED_SUITES=$((PASSED_SUITES + 1))
47 return 0
48 else
49 echo -e "${RED}✗ $suite_name FAILED${NC}"
50 FAILED_SUITES=$((FAILED_SUITES + 1))
51 return 1
52 fi
53}
54
55# Run all test suites
56run_test_suite "Basic Options Test" "$SCRIPT_DIR/test_basic_options.sh"
57run_test_suite "Compilation Test" "$SCRIPT_DIR/test_compilation.sh"
58run_test_suite "Error Handling Test" "$SCRIPT_DIR/test_error_handling.sh"
59run_test_suite "Execution Test" "$SCRIPT_DIR/test_execution.sh"
60
61# Print final summary
62echo ""
63echo -e "${BLUE}======================================"
64echo "Final Test Suite Summary"
65echo "======================================${NC}"
66echo "Total test suites: $TOTAL_SUITES"
67echo -e "Passed: ${GREEN}$PASSED_SUITES${NC}"
68echo -e "Failed: ${RED}$FAILED_SUITES${NC}"
69echo "======================================"
70
71if [ $FAILED_SUITES -eq 0 ]; then
72 echo -e "${GREEN}All test suites passed!${NC}"
73 exit 0
74else
75 echo -e "${RED}Some test suites failed!${NC}"
76 exit 1
77fi
diff --git a/spec/cli/test_basic_options.sh b/spec/cli/test_basic_options.sh
new file mode 100755
index 0000000..81cfaf7
--- /dev/null
+++ b/spec/cli/test_basic_options.sh
@@ -0,0 +1,38 @@
1#!/bin/bash
2# Test Basic Options for YueScript CLI
3# Tests: -h, --help, -v, --version
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6source "$SCRIPT_DIR/cli_test_helper.sh"
7
8# Check binary
9check_yue_binary
10
11echo "========================================"
12echo "Testing Basic Options"
13echo "========================================"
14echo ""
15
16# Test 1: Help flag -h
17echo "Testing -h flag..."
18assert_output_contains "Help flag -h should show usage" "Usage: yue" $YUE_BIN -h
19
20# Test 2: Help flag --help
21assert_output_contains "Help flag --help should show usage" "Usage: yue" $YUE_BIN --help
22
23# Test 3: Version flag -v
24assert_output_contains "Version flag -v should show version" "Yuescript version:" $YUE_BIN -v
25
26# Test 4: Version flag --version
27assert_output_contains "Version flag --version should show version" "Yuescript version:" $YUE_BIN --version
28
29# Test 5: Verify help contains expected sections
30echo ""
31echo "Testing help content..."
32# Use grep -F to search for fixed strings without interpreting special characters
33assert_output_contains "Help should show compile options" "output-to" $YUE_BIN --help
34assert_output_contains "Help should show minify option" "minify" $YUE_BIN --help
35assert_output_contains "Help should show execute option" "execute" $YUE_BIN --help
36
37echo ""
38print_summary
diff --git a/spec/cli/test_compilation.sh b/spec/cli/test_compilation.sh
new file mode 100755
index 0000000..4ddde5a
--- /dev/null
+++ b/spec/cli/test_compilation.sh
@@ -0,0 +1,148 @@
1#!/bin/bash
2# Test Compilation Functionality for YueScript CLI
3# Tests: File compilation, directory compilation, output options
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6source "$SCRIPT_DIR/cli_test_helper.sh"
7
8# Check binary
9check_yue_binary
10
11# Setup test environment
12setup_test_env
13TMP_DIR=$(get_test_tmp_dir)
14
15echo "========================================"
16echo "Testing Compilation Functionality"
17echo "========================================"
18echo ""
19
20# Test 1: Compile a simple file to stdout
21echo "Testing simple file compilation to stdout..."
22cat > "$TMP_DIR/simple.yue" << 'EOF'
23print "Hello, World!"
24EOF
25
26assert_output_contains "Compile simple file to stdout" "print" $YUE_BIN -p "$TMP_DIR/simple.yue"
27
28# Test 2: Compile a simple file to disk
29echo ""
30echo "Testing file compilation to disk..."
31cat > "$TMP_DIR/test1.yue" << 'EOF'
32x = 1 + 2
33print x
34EOF
35
36assert_success "Compile test1.yue to disk" $YUE_BIN "$TMP_DIR/test1.yue"
37assert_file_exists "Output file test1.lua should exist" "$TMP_DIR/test1.lua"
38
39# Test 3: Compile with -o option
40echo ""
41echo "Testing compilation with -o option..."
42cat > "$TMP_DIR/test2.yue" << 'EOF'
43x = 10
44print x
45EOF
46
47assert_success "Compile with -o option" $YUE_BIN -o "$TMP_DIR/output.lua" "$TMP_DIR/test2.yue"
48assert_file_exists "Custom output file should exist" "$TMP_DIR/output.lua"
49
50# Test 4: Compile directory with -t option (target directory)
51echo ""
52echo "Testing compilation with -t option..."
53mkdir -p "$TMP_DIR/src"
54mkdir -p "$TMP_DIR/build"
55cat > "$TMP_DIR/src/test3.yue" << 'EOF'
56print "test"
57EOF
58
59assert_success "Compile directory with -t option" $YUE_BIN "$TMP_DIR/src" -t "$TMP_DIR/build"
60assert_file_exists "Output should be in target directory" "$TMP_DIR/build/test3.lua"
61
62# Test 5: Compile directory recursively
63echo ""
64echo "Testing directory compilation..."
65mkdir -p "$TMP_DIR/project/src"
66mkdir -p "$TMP_DIR/project/build"
67
68cat > "$TMP_DIR/project/src/file1.yue" << 'EOF'
69print "file1"
70EOF
71
72cat > "$TMP_DIR/project/src/file2.yue" << 'EOF'
73print "file2"
74EOF
75
76assert_success "Compile entire directory" $YUE_BIN "$TMP_DIR/project/src" -t "$TMP_DIR/project/build"
77# Files are compiled directly in target directory, not preserving src subdir
78assert_file_exists "file1.lua should exist" "$TMP_DIR/project/build/file1.lua"
79assert_file_exists "file2.lua should exist" "$TMP_DIR/project/build/file2.lua"
80
81# Test 6: Compile with line numbers (-l)
82echo ""
83echo "Testing compilation with line numbers..."
84cat > "$TMP_DIR/test_line.yue" << 'EOF'
85print "line test"
86EOF
87
88assert_success "Compile with line numbers" $YUE_BIN -l "$TMP_DIR/test_line.yue" -o "$TMP_DIR/test_line.lua"
89assert_output_contains "Compiled file should have line comment" "yue" cat "$TMP_DIR/test_line.lua"
90
91# Test 7: Compile with spaces instead of tabs (-s)
92echo ""
93echo "Testing compilation with spaces..."
94cat > "$TMP_DIR/test_spaces.yue" << 'EOF'
95x = 1
96EOF
97
98assert_success "Compile with spaces option" $YUE_BIN -s "$TMP_DIR/test_spaces.yue" -o "$TMP_DIR/test_spaces.lua"
99
100# Test 8: Compile with minify (-m)
101echo ""
102echo "Testing compilation with minify..."
103cat > "$TMP_DIR/test_minify.yue" << 'EOF'
104-- this is a comment
105x = 1 + 2
106print x
107EOF
108
109assert_success "Compile with minify option" $YUE_BIN -m "$TMP_DIR/test_minify.yue" -o "$TMP_DIR/test_minify.lua"
110assert_file_exists "Minified file should exist" "$TMP_DIR/test_minify.lua"
111
112# Test 9: Stdin/stdout compilation
113echo ""
114echo "Testing stdin/stdout compilation..."
115echo 'print "stdin test"' | assert_output_contains "Compile from stdin" "print" $YUE_BIN -
116
117# Test 10: Glob variable dumping (-g)
118echo ""
119echo "Testing global variable dumping..."
120cat > "$TMP_DIR/test_globals.yue" << 'EOF'
121local x = 1
122print unknown_global
123EOF
124
125# -g dumps globals in format: NAME LINE COLUMN
126assert_output_contains "Dump global variables" "unknown_global" $YUE_BIN -g "$TMP_DIR/test_globals.yue"
127
128# Test 11: Benchmark compilation (-b)
129echo ""
130echo "Testing benchmark compilation..."
131cat > "$TMP_DIR/test_bench.yue" << 'EOF'
132print "benchmark"
133EOF
134
135assert_output_contains "Benchmark should show compile time" "Compile time:" $YUE_BIN -b "$TMP_DIR/test_bench.yue"
136
137# Test 12: Target version option
138echo ""
139echo "Testing target version option..."
140cat > "$TMP_DIR/test_target.yue" << 'EOF'
141print "target test"
142EOF
143
144assert_success "Compile with target 5.1" $YUE_BIN "$TMP_DIR/test_target.yue" -o "$TMP_DIR/test_target.lua" --target 5.1
145assert_file_exists "Target version compilation should succeed" "$TMP_DIR/test_target.lua"
146
147echo ""
148print_summary
diff --git a/spec/cli/test_error_handling.sh b/spec/cli/test_error_handling.sh
new file mode 100755
index 0000000..1ff1642
--- /dev/null
+++ b/spec/cli/test_error_handling.sh
@@ -0,0 +1,169 @@
1#!/bin/bash
2# Test Error Handling for YueScript CLI
3# Tests: Syntax errors, file not found, invalid options
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6source "$SCRIPT_DIR/cli_test_helper.sh"
7
8# Check binary
9check_yue_binary
10
11# Setup test environment
12setup_test_env
13TMP_DIR=$(get_test_tmp_dir)
14
15echo "========================================"
16echo "Testing Error Handling"
17echo "========================================"
18echo ""
19
20# Test 1: Non-existent file
21echo "Testing non-existent file..."
22assert_failure "Compiling non-existent file should fail" $YUE_BIN "$TMP_DIR/nonexistent.yue"
23
24# Test 2: Syntax error in YueScript code
25echo ""
26echo "Testing syntax error..."
27cat > "$TMP_DIR/syntax_error.yue" << 'EOF'
28print "unclosed string
29EOF
30
31assert_failure "Compiling file with syntax error should fail" $YUE_BIN "$TMP_DIR/syntax_error.yue"
32
33# Test 3: Invalid option combination
34echo ""
35echo "Testing invalid option combinations..."
36cat > "$TMP_DIR/test1.yue" << 'EOF'
37print "test"
38EOF
39
40cat > "$TMP_DIR/test2.yue" << 'EOF'
41print "test2"
42EOF
43
44# -o with multiple files should fail
45assert_failure "-o with multiple input files should fail" $YUE_BIN -o output.lua "$TMP_DIR/test1.yue" "$TMP_DIR/test2.yue" 2>&1 || true
46
47# Test 4: Empty file
48echo ""
49echo "Testing empty file..."
50touch "$TMP_DIR/empty.yue"
51assert_success "Compiling empty file should succeed" $YUE_BIN "$TMP_DIR/empty.yue"
52
53# Test 5: File with only comments
54echo ""
55echo "Testing file with only comments..."
56cat > "$TMP_DIR/only_comments.yue" << 'EOF'
57-- This is a comment
58-- Another comment
59EOF
60
61assert_success "Compiling file with only comments should succeed" $YUE_BIN "$TMP_DIR/only_comments.yue"
62
63# Test 6: Invalid Lua target version
64echo ""
65echo "Testing invalid target version..."
66cat > "$TMP_DIR/test_target.yue" << 'EOF'
67print "test"
68EOF
69
70# Note: Invalid target versions may be silently accepted or ignored
71assert_success "Invalid target version is accepted (may be ignored)" $YUE_BIN "$TMP_DIR/test_target.yue" --target 9.9
72
73# Test 7: Complex YueScript with various features
74echo ""
75echo "Testing complex valid YueScript..."
76cat > "$TMP_DIR/complex.yue" << 'EOF'
77-- Class definition
78class MyClass
79 new: (@value) =>
80
81 get_value: =>
82 @value
83
84 set_value: (@value) =>
85
86-- Table comprehension
87numbers = [1, 2, 3, 4, 5]
88squared = [x * x for x in numbers]
89
90-- String interpolation
91name = "YueScript"
92message = "Hello, #{name}!"
93
94-- Export statement
95export MyClass, squared, message
96EOF
97
98assert_success "Compiling complex YueScript should succeed" $YUE_BIN "$TMP_DIR/complex.yue"
99
100# Test 8: Watch mode with file (should fail)
101echo ""
102echo "Testing watch mode restrictions..."
103assert_failure "Watch mode with file input should fail" $YUE_BIN -w "$TMP_DIR/test1.yue" 2>&1 || true
104
105# Test 9: Invalid use of stdin/stdout
106echo ""
107echo "Testing invalid stdin usage..."
108assert_failure "Stdin with additional arguments should fail" $YUE_BIN - "$TMP_DIR/test1.yue" 2>&1 || true
109
110# Test 10: Check for proper error messages
111echo ""
112echo "Testing error message quality..."
113cat > "$TMP_DIR/error_msg.yue" << 'EOF'
114undef_var = undefined_value
115EOF
116
117assert_output_contains "Error message should contain file name" "error_msg.yue" $YUE_BIN "$TMP_DIR/error_msg.yue" || true
118
119# Test 11: Unicode handling
120echo ""
121echo "Testing Unicode in source files..."
122cat > "$TMP_DIR/unicode.yue" << 'EOF'
123-- Unicode characters
124message = "你好世界 🌍"
125print message
126EOF
127
128assert_success "Compiling file with Unicode should succeed" $YUE_BIN "$TMP_DIR/unicode.yue"
129
130# Test 12: Very long line
131echo ""
132echo "Testing very long line..."
133# Generate a long line using printf instead of python
134LONG_LINE="x = "
135for i in $(seq 1 100); do
136 LONG_LINE="${LONG_LINE}1 + "
137done
138LONG_LINE="${LONG_LINE}1"
139echo "$LONG_LINE" > "$TMP_DIR/long_line.yue"
140assert_success "Compiling file with very long line should succeed" $YUE_BIN "$TMP_DIR/long_line.yue"
141
142# Test 13: Deep nesting
143echo ""
144echo "Testing deeply nested code..."
145cat > "$TMP_DIR/deep_nested.yue" << 'EOF'
146if true
147 if true
148 if true
149 if true
150 if true
151 print "deep"
152EOF
153
154assert_success "Compiling deeply nested code should succeed" $YUE_BIN "$TMP_DIR/deep_nested.yue"
155
156# Test 14: Invalid syntax in macro
157echo ""
158echo "Testing macro error handling..."
159cat > "$TMP_DIR/macro_error.yue" << 'EOF'
160macro bad_macro
161 error: invalid syntax here
162
163print "test"
164EOF
165
166assert_failure "Compiling file with invalid macro should fail" $YUE_BIN "$TMP_DIR/macro_error.yue"
167
168echo ""
169print_summary
diff --git a/spec/cli/test_execution.sh b/spec/cli/test_execution.sh
new file mode 100755
index 0000000..fedfbea
--- /dev/null
+++ b/spec/cli/test_execution.sh
@@ -0,0 +1,159 @@
1#!/bin/bash
2# Test Code Execution for YueScript CLI
3# Tests: -e option, executing files, script arguments
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6source "$SCRIPT_DIR/cli_test_helper.sh"
7
8# Check binary
9check_yue_binary
10
11# Setup test environment
12setup_test_env
13TMP_DIR=$(get_test_tmp_dir)
14
15echo "========================================"
16echo "Testing Code Execution"
17echo "========================================"
18echo ""
19
20# Test 1: Execute inline code
21echo "Testing inline code execution..."
22assert_output_contains "Execute inline code" "123" $YUE_BIN -e 'print 123'
23
24# Test 2: Execute inline code with calculations
25echo ""
26echo "Testing inline code with calculations..."
27assert_output_contains "Execute calculation" "5" $YUE_BIN -e 'print 2 + 3'
28
29# Test 3: Execute inline code with string interpolation
30echo ""
31echo "Testing string interpolation..."
32assert_output_contains "String interpolation" "Hello, World" $YUE_BIN -e 'name = "World"; print "Hello, #{name}"'
33
34# Test 4: Execute YueScript file with -e
35echo ""
36echo "Testing file execution with -e..."
37cat > "$TMP_DIR/exec_test.yue" << 'EOF'
38x = 10
39y = 20
40print x + y
41EOF
42
43assert_output_contains "Execute file" "30" $YUE_BIN -e "$TMP_DIR/exec_test.yue"
44
45# Test 5: Execute Lua file
46echo ""
47echo "Testing Lua file execution..."
48cat > "$TMP_DIR/test.lua" << 'EOF'
49print("Lua execution")
50EOF
51
52assert_output_contains "Execute Lua file" "Lua execution" $YUE_BIN -e "$TMP_DIR/test.lua"
53
54# Test 6: Test with script arguments
55echo ""
56echo "Testing script arguments..."
57cat > "$TMP_DIR/args_test.yue" << 'EOF'
58import arg
59print arg[1]
60print arg[2]
61EOF
62
63assert_output_contains "First argument" "first" $YUE_BIN -e "$TMP_DIR/args_test.yue" first second
64assert_output_contains "Second argument" "second" $YUE_BIN -e "$TMP_DIR/args_test.yue" first second
65
66# Test 7: Test with compiler options via --key=value
67echo ""
68echo "Testing compiler options in execute mode..."
69cat > "$TMP_DIR/options_test.yue" << 'EOF'
70print "test"
71EOF
72
73assert_success "Execute with compiler option" $YUE_BIN -e "$TMP_DIR/options_test.yue" --reserve_line_number=true
74
75# Test 8: Execute code with table operations
76echo ""
77echo "Testing table operations..."
78assert_output_contains "Table operations" "3" $YUE_BIN -e 't = {1, 2, 3}; print #t'
79
80# Test 9: Execute code with function definition
81echo ""
82echo "Testing function definition..."
83cat > "$TMP_DIR/func_test.yue" << 'EOF'
84double = (x) -> x * 2
85print double(5)
86EOF
87
88assert_output_contains "Function execution" "10" $YUE_BIN -e "$TMP_DIR/func_test.yue"
89
90# Test 10: Execute code with class
91echo ""
92echo "Testing class execution..."
93cat > "$TMP_DIR/class_test.yue" << 'EOF'
94class Point
95 new: (@x, @y) =>
96
97 distance: =>
98 math.sqrt(@x * @x + @y * @y)
99
100p = Point(3, 4)
101print p\distance!
102EOF
103
104assert_output_contains "Class method execution" "5" $YUE_BIN -e "$TMP_DIR/class_test.yue"
105
106# Test 11: Execute with import
107echo ""
108echo "Testing import in execute mode..."
109cat > "$TMP_DIR/import_test.yue" << 'EOF'
110print "test"
111EOF
112
113# Note: This test depends on how imports are set up
114assert_success "Execute with import" $YUE_BIN -e "$TMP_DIR/import_test.yue" --path="$TMP_DIR"
115
116# Test 12: Execute code with error handling
117echo ""
118echo "Testing error handling in executed code..."
119cat > "$TMP_DIR/error_test.yue" << 'EOF'
120ok, err = pcall ->
121 error "test error"
122
123if not ok
124 print "caught: " .. err
125EOF
126
127assert_output_contains "Error handling" "caught:" $YUE_BIN -e "$TMP_DIR/error_test.yue"
128
129# Test 13: Execute with export statement
130echo ""
131echo "Testing export in execute mode..."
132cat > "$TMP_DIR/export_test.yue" << 'EOF'
133export value = 42
134print value
135EOF
136
137assert_output_contains "Export value" "42" $YUE_BIN -e "$TMP_DIR/export_test.yue"
138
139# Test 14: Execute with macro
140echo ""
141echo "Testing macro in execute mode..."
142cat > "$TMP_DIR/macro_test.yue" << 'EOF'
143macro double = (x) -> "#{x} * 2"
144
145result = $double 21
146print result
147EOF
148
149assert_output_contains "Macro execution" "42" $YUE_BIN -e "$TMP_DIR/macro_test.yue"
150
151# Test 15: Execute with string literal
152echo ""
153echo "Testing string literals..."
154# Use grep without -F to match patterns with newlines
155# We'll match just the first part to avoid newline issues
156assert_output_contains "String with escape" "line1" $YUE_BIN -e 'print "line1\nline2"'
157
158echo ""
159print_summary