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_error_handling.sh | 169 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100755 spec/cli/test_error_handling.sh (limited to 'spec/cli/test_error_handling.sh') 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 @@ +#!/bin/bash +# Test Error Handling for YueScript CLI +# Tests: Syntax errors, file not found, invalid options + +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 Error Handling" +echo "========================================" +echo "" + +# Test 1: Non-existent file +echo "Testing non-existent file..." +assert_failure "Compiling non-existent file should fail" $YUE_BIN "$TMP_DIR/nonexistent.yue" + +# Test 2: Syntax error in YueScript code +echo "" +echo "Testing syntax error..." +cat > "$TMP_DIR/syntax_error.yue" << 'EOF' +print "unclosed string +EOF + +assert_failure "Compiling file with syntax error should fail" $YUE_BIN "$TMP_DIR/syntax_error.yue" + +# Test 3: Invalid option combination +echo "" +echo "Testing invalid option combinations..." +cat > "$TMP_DIR/test1.yue" << 'EOF' +print "test" +EOF + +cat > "$TMP_DIR/test2.yue" << 'EOF' +print "test2" +EOF + +# -o with multiple files should fail +assert_failure "-o with multiple input files should fail" $YUE_BIN -o output.lua "$TMP_DIR/test1.yue" "$TMP_DIR/test2.yue" 2>&1 || true + +# Test 4: Empty file +echo "" +echo "Testing empty file..." +touch "$TMP_DIR/empty.yue" +assert_success "Compiling empty file should succeed" $YUE_BIN "$TMP_DIR/empty.yue" + +# Test 5: File with only comments +echo "" +echo "Testing file with only comments..." +cat > "$TMP_DIR/only_comments.yue" << 'EOF' +-- This is a comment +-- Another comment +EOF + +assert_success "Compiling file with only comments should succeed" $YUE_BIN "$TMP_DIR/only_comments.yue" + +# Test 6: Invalid Lua target version +echo "" +echo "Testing invalid target version..." +cat > "$TMP_DIR/test_target.yue" << 'EOF' +print "test" +EOF + +# Note: Invalid target versions may be silently accepted or ignored +assert_success "Invalid target version is accepted (may be ignored)" $YUE_BIN "$TMP_DIR/test_target.yue" --target 9.9 + +# Test 7: Complex YueScript with various features +echo "" +echo "Testing complex valid YueScript..." +cat > "$TMP_DIR/complex.yue" << 'EOF' +-- Class definition +class MyClass + new: (@value) => + + get_value: => + @value + + set_value: (@value) => + +-- Table comprehension +numbers = [1, 2, 3, 4, 5] +squared = [x * x for x in numbers] + +-- String interpolation +name = "YueScript" +message = "Hello, #{name}!" + +-- Export statement +export MyClass, squared, message +EOF + +assert_success "Compiling complex YueScript should succeed" $YUE_BIN "$TMP_DIR/complex.yue" + +# Test 8: Watch mode with file (should fail) +echo "" +echo "Testing watch mode restrictions..." +assert_failure "Watch mode with file input should fail" $YUE_BIN -w "$TMP_DIR/test1.yue" 2>&1 || true + +# Test 9: Invalid use of stdin/stdout +echo "" +echo "Testing invalid stdin usage..." +assert_failure "Stdin with additional arguments should fail" $YUE_BIN - "$TMP_DIR/test1.yue" 2>&1 || true + +# Test 10: Check for proper error messages +echo "" +echo "Testing error message quality..." +cat > "$TMP_DIR/error_msg.yue" << 'EOF' +undef_var = undefined_value +EOF + +assert_output_contains "Error message should contain file name" "error_msg.yue" $YUE_BIN "$TMP_DIR/error_msg.yue" || true + +# Test 11: Unicode handling +echo "" +echo "Testing Unicode in source files..." +cat > "$TMP_DIR/unicode.yue" << 'EOF' +-- Unicode characters +message = "你好世界 🌍" +print message +EOF + +assert_success "Compiling file with Unicode should succeed" $YUE_BIN "$TMP_DIR/unicode.yue" + +# Test 12: Very long line +echo "" +echo "Testing very long line..." +# Generate a long line using printf instead of python +LONG_LINE="x = " +for i in $(seq 1 100); do + LONG_LINE="${LONG_LINE}1 + " +done +LONG_LINE="${LONG_LINE}1" +echo "$LONG_LINE" > "$TMP_DIR/long_line.yue" +assert_success "Compiling file with very long line should succeed" $YUE_BIN "$TMP_DIR/long_line.yue" + +# Test 13: Deep nesting +echo "" +echo "Testing deeply nested code..." +cat > "$TMP_DIR/deep_nested.yue" << 'EOF' +if true + if true + if true + if true + if true + print "deep" +EOF + +assert_success "Compiling deeply nested code should succeed" $YUE_BIN "$TMP_DIR/deep_nested.yue" + +# Test 14: Invalid syntax in macro +echo "" +echo "Testing macro error handling..." +cat > "$TMP_DIR/macro_error.yue" << 'EOF' +macro bad_macro + error: invalid syntax here + +print "test" +EOF + +assert_failure "Compiling file with invalid macro should fail" $YUE_BIN "$TMP_DIR/macro_error.yue" + +echo "" +print_summary -- cgit v1.2.3-55-g6feb