aboutsummaryrefslogtreecommitdiff
path: root/spec/cli/test_execution.sh
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 /spec/cli/test_execution.sh
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>
Diffstat (limited to 'spec/cli/test_execution.sh')
-rwxr-xr-xspec/cli/test_execution.sh159
1 files changed, 159 insertions, 0 deletions
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