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/test_execution.sh | |
| 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/test_execution.sh')
| -rwxr-xr-x | spec/cli/test_execution.sh | 159 |
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 | |||
| 5 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| 6 | source "$SCRIPT_DIR/cli_test_helper.sh" | ||
| 7 | |||
| 8 | # Check binary | ||
| 9 | check_yue_binary | ||
| 10 | |||
| 11 | # Setup test environment | ||
| 12 | setup_test_env | ||
| 13 | TMP_DIR=$(get_test_tmp_dir) | ||
| 14 | |||
| 15 | echo "========================================" | ||
| 16 | echo "Testing Code Execution" | ||
| 17 | echo "========================================" | ||
| 18 | echo "" | ||
| 19 | |||
| 20 | # Test 1: Execute inline code | ||
| 21 | echo "Testing inline code execution..." | ||
| 22 | assert_output_contains "Execute inline code" "123" $YUE_BIN -e 'print 123' | ||
| 23 | |||
| 24 | # Test 2: Execute inline code with calculations | ||
| 25 | echo "" | ||
| 26 | echo "Testing inline code with calculations..." | ||
| 27 | assert_output_contains "Execute calculation" "5" $YUE_BIN -e 'print 2 + 3' | ||
| 28 | |||
| 29 | # Test 3: Execute inline code with string interpolation | ||
| 30 | echo "" | ||
| 31 | echo "Testing string interpolation..." | ||
| 32 | assert_output_contains "String interpolation" "Hello, World" $YUE_BIN -e 'name = "World"; print "Hello, #{name}"' | ||
| 33 | |||
| 34 | # Test 4: Execute YueScript file with -e | ||
| 35 | echo "" | ||
| 36 | echo "Testing file execution with -e..." | ||
| 37 | cat > "$TMP_DIR/exec_test.yue" << 'EOF' | ||
| 38 | x = 10 | ||
| 39 | y = 20 | ||
| 40 | print x + y | ||
| 41 | EOF | ||
| 42 | |||
| 43 | assert_output_contains "Execute file" "30" $YUE_BIN -e "$TMP_DIR/exec_test.yue" | ||
| 44 | |||
| 45 | # Test 5: Execute Lua file | ||
| 46 | echo "" | ||
| 47 | echo "Testing Lua file execution..." | ||
| 48 | cat > "$TMP_DIR/test.lua" << 'EOF' | ||
| 49 | print("Lua execution") | ||
| 50 | EOF | ||
| 51 | |||
| 52 | assert_output_contains "Execute Lua file" "Lua execution" $YUE_BIN -e "$TMP_DIR/test.lua" | ||
| 53 | |||
| 54 | # Test 6: Test with script arguments | ||
| 55 | echo "" | ||
| 56 | echo "Testing script arguments..." | ||
| 57 | cat > "$TMP_DIR/args_test.yue" << 'EOF' | ||
| 58 | import arg | ||
| 59 | print arg[1] | ||
| 60 | print arg[2] | ||
| 61 | EOF | ||
| 62 | |||
| 63 | assert_output_contains "First argument" "first" $YUE_BIN -e "$TMP_DIR/args_test.yue" first second | ||
| 64 | assert_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 | ||
| 67 | echo "" | ||
| 68 | echo "Testing compiler options in execute mode..." | ||
| 69 | cat > "$TMP_DIR/options_test.yue" << 'EOF' | ||
| 70 | print "test" | ||
| 71 | EOF | ||
| 72 | |||
| 73 | assert_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 | ||
| 76 | echo "" | ||
| 77 | echo "Testing table operations..." | ||
| 78 | assert_output_contains "Table operations" "3" $YUE_BIN -e 't = {1, 2, 3}; print #t' | ||
| 79 | |||
| 80 | # Test 9: Execute code with function definition | ||
| 81 | echo "" | ||
| 82 | echo "Testing function definition..." | ||
| 83 | cat > "$TMP_DIR/func_test.yue" << 'EOF' | ||
| 84 | double = (x) -> x * 2 | ||
| 85 | print double(5) | ||
| 86 | EOF | ||
| 87 | |||
| 88 | assert_output_contains "Function execution" "10" $YUE_BIN -e "$TMP_DIR/func_test.yue" | ||
| 89 | |||
| 90 | # Test 10: Execute code with class | ||
| 91 | echo "" | ||
| 92 | echo "Testing class execution..." | ||
| 93 | cat > "$TMP_DIR/class_test.yue" << 'EOF' | ||
| 94 | class Point | ||
| 95 | new: (@x, @y) => | ||
| 96 | |||
| 97 | distance: => | ||
| 98 | math.sqrt(@x * @x + @y * @y) | ||
| 99 | |||
| 100 | p = Point(3, 4) | ||
| 101 | print p\distance! | ||
| 102 | EOF | ||
| 103 | |||
| 104 | assert_output_contains "Class method execution" "5" $YUE_BIN -e "$TMP_DIR/class_test.yue" | ||
| 105 | |||
| 106 | # Test 11: Execute with import | ||
| 107 | echo "" | ||
| 108 | echo "Testing import in execute mode..." | ||
| 109 | cat > "$TMP_DIR/import_test.yue" << 'EOF' | ||
| 110 | print "test" | ||
| 111 | EOF | ||
| 112 | |||
| 113 | # Note: This test depends on how imports are set up | ||
| 114 | assert_success "Execute with import" $YUE_BIN -e "$TMP_DIR/import_test.yue" --path="$TMP_DIR" | ||
| 115 | |||
| 116 | # Test 12: Execute code with error handling | ||
| 117 | echo "" | ||
| 118 | echo "Testing error handling in executed code..." | ||
| 119 | cat > "$TMP_DIR/error_test.yue" << 'EOF' | ||
| 120 | ok, err = pcall -> | ||
| 121 | error "test error" | ||
| 122 | |||
| 123 | if not ok | ||
| 124 | print "caught: " .. err | ||
| 125 | EOF | ||
| 126 | |||
| 127 | assert_output_contains "Error handling" "caught:" $YUE_BIN -e "$TMP_DIR/error_test.yue" | ||
| 128 | |||
| 129 | # Test 13: Execute with export statement | ||
| 130 | echo "" | ||
| 131 | echo "Testing export in execute mode..." | ||
| 132 | cat > "$TMP_DIR/export_test.yue" << 'EOF' | ||
| 133 | export value = 42 | ||
| 134 | print value | ||
| 135 | EOF | ||
| 136 | |||
| 137 | assert_output_contains "Export value" "42" $YUE_BIN -e "$TMP_DIR/export_test.yue" | ||
| 138 | |||
| 139 | # Test 14: Execute with macro | ||
| 140 | echo "" | ||
| 141 | echo "Testing macro in execute mode..." | ||
| 142 | cat > "$TMP_DIR/macro_test.yue" << 'EOF' | ||
| 143 | macro double = (x) -> "#{x} * 2" | ||
| 144 | |||
| 145 | result = $double 21 | ||
| 146 | print result | ||
| 147 | EOF | ||
| 148 | |||
| 149 | assert_output_contains "Macro execution" "42" $YUE_BIN -e "$TMP_DIR/macro_test.yue" | ||
| 150 | |||
| 151 | # Test 15: Execute with string literal | ||
| 152 | echo "" | ||
| 153 | echo "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 | ||
| 156 | assert_output_contains "String with escape" "line1" $YUE_BIN -e 'print "line1\nline2"' | ||
| 157 | |||
| 158 | echo "" | ||
| 159 | print_summary | ||
