From aacf6dd9ebdb4d55b432ea1d4213093fe35e0ad1 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 6 Feb 2026 06:18:58 +0000 Subject: 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 --- spec/cli/test_execution.sh | 159 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100755 spec/cli/test_execution.sh (limited to 'spec/cli/test_execution.sh') 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 @@ +#!/bin/bash +# Test Code Execution for YueScript CLI +# Tests: -e option, executing files, script arguments + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$SCRIPT_DIR/cli_test_helper.sh" + +# Check binary +check_yue_binary + +# Setup test environment +setup_test_env +TMP_DIR=$(get_test_tmp_dir) + +echo "========================================" +echo "Testing Code Execution" +echo "========================================" +echo "" + +# Test 1: Execute inline code +echo "Testing inline code execution..." +assert_output_contains "Execute inline code" "123" $YUE_BIN -e 'print 123' + +# Test 2: Execute inline code with calculations +echo "" +echo "Testing inline code with calculations..." +assert_output_contains "Execute calculation" "5" $YUE_BIN -e 'print 2 + 3' + +# Test 3: Execute inline code with string interpolation +echo "" +echo "Testing string interpolation..." +assert_output_contains "String interpolation" "Hello, World" $YUE_BIN -e 'name = "World"; print "Hello, #{name}"' + +# Test 4: Execute YueScript file with -e +echo "" +echo "Testing file execution with -e..." +cat > "$TMP_DIR/exec_test.yue" << 'EOF' +x = 10 +y = 20 +print x + y +EOF + +assert_output_contains "Execute file" "30" $YUE_BIN -e "$TMP_DIR/exec_test.yue" + +# Test 5: Execute Lua file +echo "" +echo "Testing Lua file execution..." +cat > "$TMP_DIR/test.lua" << 'EOF' +print("Lua execution") +EOF + +assert_output_contains "Execute Lua file" "Lua execution" $YUE_BIN -e "$TMP_DIR/test.lua" + +# Test 6: Test with script arguments +echo "" +echo "Testing script arguments..." +cat > "$TMP_DIR/args_test.yue" << 'EOF' +import arg +print arg[1] +print arg[2] +EOF + +assert_output_contains "First argument" "first" $YUE_BIN -e "$TMP_DIR/args_test.yue" first second +assert_output_contains "Second argument" "second" $YUE_BIN -e "$TMP_DIR/args_test.yue" first second + +# Test 7: Test with compiler options via --key=value +echo "" +echo "Testing compiler options in execute mode..." +cat > "$TMP_DIR/options_test.yue" << 'EOF' +print "test" +EOF + +assert_success "Execute with compiler option" $YUE_BIN -e "$TMP_DIR/options_test.yue" --reserve_line_number=true + +# Test 8: Execute code with table operations +echo "" +echo "Testing table operations..." +assert_output_contains "Table operations" "3" $YUE_BIN -e 't = {1, 2, 3}; print #t' + +# Test 9: Execute code with function definition +echo "" +echo "Testing function definition..." +cat > "$TMP_DIR/func_test.yue" << 'EOF' +double = (x) -> x * 2 +print double(5) +EOF + +assert_output_contains "Function execution" "10" $YUE_BIN -e "$TMP_DIR/func_test.yue" + +# Test 10: Execute code with class +echo "" +echo "Testing class execution..." +cat > "$TMP_DIR/class_test.yue" << 'EOF' +class Point + new: (@x, @y) => + + distance: => + math.sqrt(@x * @x + @y * @y) + +p = Point(3, 4) +print p\distance! +EOF + +assert_output_contains "Class method execution" "5" $YUE_BIN -e "$TMP_DIR/class_test.yue" + +# Test 11: Execute with import +echo "" +echo "Testing import in execute mode..." +cat > "$TMP_DIR/import_test.yue" << 'EOF' +print "test" +EOF + +# Note: This test depends on how imports are set up +assert_success "Execute with import" $YUE_BIN -e "$TMP_DIR/import_test.yue" --path="$TMP_DIR" + +# Test 12: Execute code with error handling +echo "" +echo "Testing error handling in executed code..." +cat > "$TMP_DIR/error_test.yue" << 'EOF' +ok, err = pcall -> + error "test error" + +if not ok + print "caught: " .. err +EOF + +assert_output_contains "Error handling" "caught:" $YUE_BIN -e "$TMP_DIR/error_test.yue" + +# Test 13: Execute with export statement +echo "" +echo "Testing export in execute mode..." +cat > "$TMP_DIR/export_test.yue" << 'EOF' +export value = 42 +print value +EOF + +assert_output_contains "Export value" "42" $YUE_BIN -e "$TMP_DIR/export_test.yue" + +# Test 14: Execute with macro +echo "" +echo "Testing macro in execute mode..." +cat > "$TMP_DIR/macro_test.yue" << 'EOF' +macro double = (x) -> "#{x} * 2" + +result = $double 21 +print result +EOF + +assert_output_contains "Macro execution" "42" $YUE_BIN -e "$TMP_DIR/macro_test.yue" + +# Test 15: Execute with string literal +echo "" +echo "Testing string literals..." +# Use grep without -F to match patterns with newlines +# We'll match just the first part to avoid newline issues +assert_output_contains "String with escape" "line1" $YUE_BIN -e 'print "line1\nline2"' + +echo "" +print_summary -- cgit v1.2.3-55-g6feb