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_compilation.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_compilation.sh')
| -rwxr-xr-x | spec/cli/test_compilation.sh | 148 |
1 files changed, 148 insertions, 0 deletions
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 | |||
| 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 Compilation Functionality" | ||
| 17 | echo "========================================" | ||
| 18 | echo "" | ||
| 19 | |||
| 20 | # Test 1: Compile a simple file to stdout | ||
| 21 | echo "Testing simple file compilation to stdout..." | ||
| 22 | cat > "$TMP_DIR/simple.yue" << 'EOF' | ||
| 23 | print "Hello, World!" | ||
| 24 | EOF | ||
| 25 | |||
| 26 | assert_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 | ||
| 29 | echo "" | ||
| 30 | echo "Testing file compilation to disk..." | ||
| 31 | cat > "$TMP_DIR/test1.yue" << 'EOF' | ||
| 32 | x = 1 + 2 | ||
| 33 | print x | ||
| 34 | EOF | ||
| 35 | |||
| 36 | assert_success "Compile test1.yue to disk" $YUE_BIN "$TMP_DIR/test1.yue" | ||
| 37 | assert_file_exists "Output file test1.lua should exist" "$TMP_DIR/test1.lua" | ||
| 38 | |||
| 39 | # Test 3: Compile with -o option | ||
| 40 | echo "" | ||
| 41 | echo "Testing compilation with -o option..." | ||
| 42 | cat > "$TMP_DIR/test2.yue" << 'EOF' | ||
| 43 | x = 10 | ||
| 44 | print x | ||
| 45 | EOF | ||
| 46 | |||
| 47 | assert_success "Compile with -o option" $YUE_BIN -o "$TMP_DIR/output.lua" "$TMP_DIR/test2.yue" | ||
| 48 | assert_file_exists "Custom output file should exist" "$TMP_DIR/output.lua" | ||
| 49 | |||
| 50 | # Test 4: Compile directory with -t option (target directory) | ||
| 51 | echo "" | ||
| 52 | echo "Testing compilation with -t option..." | ||
| 53 | mkdir -p "$TMP_DIR/src" | ||
| 54 | mkdir -p "$TMP_DIR/build" | ||
| 55 | cat > "$TMP_DIR/src/test3.yue" << 'EOF' | ||
| 56 | print "test" | ||
| 57 | EOF | ||
| 58 | |||
| 59 | assert_success "Compile directory with -t option" $YUE_BIN "$TMP_DIR/src" -t "$TMP_DIR/build" | ||
| 60 | assert_file_exists "Output should be in target directory" "$TMP_DIR/build/test3.lua" | ||
| 61 | |||
| 62 | # Test 5: Compile directory recursively | ||
| 63 | echo "" | ||
| 64 | echo "Testing directory compilation..." | ||
| 65 | mkdir -p "$TMP_DIR/project/src" | ||
| 66 | mkdir -p "$TMP_DIR/project/build" | ||
| 67 | |||
| 68 | cat > "$TMP_DIR/project/src/file1.yue" << 'EOF' | ||
| 69 | print "file1" | ||
| 70 | EOF | ||
| 71 | |||
| 72 | cat > "$TMP_DIR/project/src/file2.yue" << 'EOF' | ||
| 73 | print "file2" | ||
| 74 | EOF | ||
| 75 | |||
| 76 | assert_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 | ||
| 78 | assert_file_exists "file1.lua should exist" "$TMP_DIR/project/build/file1.lua" | ||
| 79 | assert_file_exists "file2.lua should exist" "$TMP_DIR/project/build/file2.lua" | ||
| 80 | |||
| 81 | # Test 6: Compile with line numbers (-l) | ||
| 82 | echo "" | ||
| 83 | echo "Testing compilation with line numbers..." | ||
| 84 | cat > "$TMP_DIR/test_line.yue" << 'EOF' | ||
| 85 | print "line test" | ||
| 86 | EOF | ||
| 87 | |||
| 88 | assert_success "Compile with line numbers" $YUE_BIN -l "$TMP_DIR/test_line.yue" -o "$TMP_DIR/test_line.lua" | ||
| 89 | assert_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) | ||
| 92 | echo "" | ||
| 93 | echo "Testing compilation with spaces..." | ||
| 94 | cat > "$TMP_DIR/test_spaces.yue" << 'EOF' | ||
| 95 | x = 1 | ||
| 96 | EOF | ||
| 97 | |||
| 98 | assert_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) | ||
| 101 | echo "" | ||
| 102 | echo "Testing compilation with minify..." | ||
| 103 | cat > "$TMP_DIR/test_minify.yue" << 'EOF' | ||
| 104 | -- this is a comment | ||
| 105 | x = 1 + 2 | ||
| 106 | print x | ||
| 107 | EOF | ||
| 108 | |||
| 109 | assert_success "Compile with minify option" $YUE_BIN -m "$TMP_DIR/test_minify.yue" -o "$TMP_DIR/test_minify.lua" | ||
| 110 | assert_file_exists "Minified file should exist" "$TMP_DIR/test_minify.lua" | ||
| 111 | |||
| 112 | # Test 9: Stdin/stdout compilation | ||
| 113 | echo "" | ||
| 114 | echo "Testing stdin/stdout compilation..." | ||
| 115 | echo 'print "stdin test"' | assert_output_contains "Compile from stdin" "print" $YUE_BIN - | ||
| 116 | |||
| 117 | # Test 10: Glob variable dumping (-g) | ||
| 118 | echo "" | ||
| 119 | echo "Testing global variable dumping..." | ||
| 120 | cat > "$TMP_DIR/test_globals.yue" << 'EOF' | ||
| 121 | local x = 1 | ||
| 122 | print unknown_global | ||
| 123 | EOF | ||
| 124 | |||
| 125 | # -g dumps globals in format: NAME LINE COLUMN | ||
| 126 | assert_output_contains "Dump global variables" "unknown_global" $YUE_BIN -g "$TMP_DIR/test_globals.yue" | ||
| 127 | |||
| 128 | # Test 11: Benchmark compilation (-b) | ||
| 129 | echo "" | ||
| 130 | echo "Testing benchmark compilation..." | ||
| 131 | cat > "$TMP_DIR/test_bench.yue" << 'EOF' | ||
| 132 | print "benchmark" | ||
| 133 | EOF | ||
| 134 | |||
| 135 | assert_output_contains "Benchmark should show compile time" "Compile time:" $YUE_BIN -b "$TMP_DIR/test_bench.yue" | ||
| 136 | |||
| 137 | # Test 12: Target version option | ||
| 138 | echo "" | ||
| 139 | echo "Testing target version option..." | ||
| 140 | cat > "$TMP_DIR/test_target.yue" << 'EOF' | ||
| 141 | print "target test" | ||
| 142 | EOF | ||
| 143 | |||
| 144 | assert_success "Compile with target 5.1" $YUE_BIN "$TMP_DIR/test_target.yue" -o "$TMP_DIR/test_target.lua" --target 5.1 | ||
| 145 | assert_file_exists "Target version compilation should succeed" "$TMP_DIR/test_target.lua" | ||
| 146 | |||
| 147 | echo "" | ||
| 148 | print_summary | ||
