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/README.md | 147 ++++++++++++++++++++++++++++++++ spec/cli/cli_test_helper.sh | 183 ++++++++++++++++++++++++++++++++++++++++ spec/cli/run_all_tests.sh | 77 +++++++++++++++++ spec/cli/test_basic_options.sh | 38 +++++++++ spec/cli/test_compilation.sh | 148 ++++++++++++++++++++++++++++++++ spec/cli/test_error_handling.sh | 169 +++++++++++++++++++++++++++++++++++++ spec/cli/test_execution.sh | 159 ++++++++++++++++++++++++++++++++++ 7 files changed, 921 insertions(+) create mode 100644 spec/cli/README.md create mode 100755 spec/cli/cli_test_helper.sh create mode 100755 spec/cli/run_all_tests.sh create mode 100755 spec/cli/test_basic_options.sh create mode 100755 spec/cli/test_compilation.sh create mode 100755 spec/cli/test_error_handling.sh create mode 100755 spec/cli/test_execution.sh (limited to 'spec/cli') diff --git a/spec/cli/README.md b/spec/cli/README.md new file mode 100644 index 0000000..ea14e72 --- /dev/null +++ b/spec/cli/README.md @@ -0,0 +1,147 @@ +# YueScript CLI Tests + +测试 YueScript 命令行工具的功能。 + +## 测试结构 + +``` +spec/cli/ +├── cli_test_helper.sh # 测试辅助函数库 +├── test_basic_options.sh # 基本选项测试 +├── test_compilation.sh # 编译功能测试 +├── test_error_handling.sh # 错误处理测试 +├── test_execution.sh # 代码执行测试 +├── run_all_tests.sh # 运行所有测试 +└── README.md # 本文件 +``` + +## 运行测试 + +### 前置要求 + +确保已编译 yue 二进制文件: + +```bash +make debug +``` + +### 运行所有测试 + +```bash +cd spec/cli +bash run_all_tests.sh +``` + +### 运行单个测试套件 + +```bash +# 测试基本选项 +bash test_basic_options.sh + +# 测试编译功能 +bash test_compilation.sh + +# 测试错误处理 +bash test_error_handling.sh + +# 测试代码执行 +bash test_execution.sh +``` + +### 指定 yue 二进制文件路径 + +如果二进制文件不在默认位置 (`./bin/debug/yue`),可以设置环境变量: + +```bash +YUE_BIN=/path/to/yue bash run_all_tests.sh +``` + +## 测试覆盖范围 + +### 1. 基本选项测试 (test_basic_options.sh) + +- `-h, --help`: 帮助信息 +- `-v, --version`: 版本信息 +- 无效选项的错误处理 + +### 2. 编译功能测试 (test_compilation.sh) + +- 单文件编译到标准输出 (`-p`) +- 单文件编译到磁盘 +- 自定义输出文件 (`-o`) +- 目标目录编译 (`-t`) +- 目录递归编译 +- 保留行号 (`-l`) +- 使用空格代替制表符 (`-s`) +- 代码压缩 (`-m`) +- 标准输入/输出编译 +- 全局变量转储 (`-g`) +- 编译性能测试 (`-b`) +- 目标 Lua 版本 (`--target`) + +### 3. 错误处理测试 (test_error_handling.sh) + +- 文件不存在错误 +- 语法错误 +- 无效选项组合 +- 空文件处理 +- 仅注释文件 +- 无效的目标版本 +- 复杂的合法代码 +- Unicode 处理 +- 超长行处理 +- 深层嵌套代码 +- 宏错误处理 + +### 4. 代码执行测试 (test_execution.sh) + +- 内联代码执行 (`-e`) +- 计算表达式 +- 字符串插值 +- YueScript 文件执行 +- Lua 文件执行 +- 脚本参数传递 +- 编译器选项传递 +- 表操作 +- 函数定义与调用 +- 类与对象 +- 导入语句 +- 错误处理 +- 导出语句 +- 宏展开 + +## 测试辅助函数 + +`cli_test_helper.sh` 提供以下函数: + +- `check_yue_binary`: 检查 yue 二进制文件是否存在 +- `assert_success`: 断言命令成功 +- `assert_failure`: 断言命令失败 +- `assert_output_contains`: 断言输出包含指定字符串 +- `assert_output_equals`: 断言输出等于指定字符串 +- `assert_file_exists`: 断言文件存在 +- `print_summary`: 打印测试摘要 +- `setup_test_env`: 设置测试环境 +- `create_test_file`: 创建测试文件 + +## 添加新测试 + +1. 在对应的测试文件中添加新的测试用例 +2. 使用提供的辅助函数来断言结果 +3. 确保测试描述清晰明了 +4. 运行测试验证功能 + +示例: + +```bash +echo "Testing new feature..." +cat > "$TMP_DIR/new_test.yue" << 'EOF' +-- test code here +EOF + +assert_output_contains "New feature should work" "expected output" $YUE_BIN "$TMP_DIR/new_test.yue" +``` + +## 持续集成 + +这些测试可以集成到 CI/CD 流程中,确保每次代码变更都不会破坏命令行工具的功能。 diff --git a/spec/cli/cli_test_helper.sh b/spec/cli/cli_test_helper.sh new file mode 100755 index 0000000..ade1546 --- /dev/null +++ b/spec/cli/cli_test_helper.sh @@ -0,0 +1,183 @@ +#!/bin/bash +# CLI Test Helper for YueScript +# Provides utility functions for testing the yue command line tool + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Test counters +TESTS_RUN=0 +TESTS_PASSED=0 +TESTS_FAILED=0 + +# Get the yue binary path +YUE_BIN="${YUE_BIN:-./bin/debug/yue}" + +# Check if yue binary exists +check_yue_binary() { + if [ ! -f "$YUE_BIN" ]; then + echo -e "${RED}Error: yue binary not found at $YUE_BIN${NC}" + echo "Please build the project first or set YUE_BIN environment variable" + exit 1 + fi + if [ ! -x "$YUE_BIN" ]; then + echo -e "${RED}Error: yue binary is not executable${NC}" + exit 1 + fi +} + +# Assert that a command succeeds +assert_success() { + local description="$1" + shift + TESTS_RUN=$((TESTS_RUN + 1)) + + if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + else + echo -e "${RED}✗${NC} $description" + echo -e " ${YELLOW}Exit code: $?${NC}" + echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}" + echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi +} + +# Assert that a command fails +assert_failure() { + local description="$1" + shift + TESTS_RUN=$((TESTS_RUN + 1)) + + if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then + echo -e "${RED}✗${NC} $description (expected failure but succeeded)" + echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}" + echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + else + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + fi +} + +# Assert that output contains expected string +assert_output_contains() { + local description="$1" + local expected="$2" + shift 2 + TESTS_RUN=$((TESTS_RUN + 1)) + + if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then + if grep -qF -- "$expected" /tmp/test_stdout.txt || grep -qF -- "$expected" /tmp/test_stderr.txt; then + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + else + echo -e "${RED}✗${NC} $description (output doesn't contain '$expected')" + echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}" + echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi + else + echo -e "${RED}✗${NC} $description (command failed)" + echo -e " ${YELLOW}Exit code: $?${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi +} + +# Assert that output equals expected string +assert_output_equals() { + local description="$1" + local expected="$2" + shift 2 + TESTS_RUN=$((TESTS_RUN + 1)) + + if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then + local actual=$(cat /tmp/test_stdout.txt) + if [ "$actual" = "$expected" ]; then + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + else + echo -e "${RED}✗${NC} $description (output mismatch)" + echo -e " ${YELLOW}Expected: '$expected'${NC}" + echo -e " ${YELLOW}Actual: '$actual'${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi + else + echo -e "${RED}✗${NC} $description (command failed)" + echo -e " ${YELLOW}Exit code: $?${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi +} + +# Assert file exists +assert_file_exists() { + local description="$1" + local filepath="$2" + TESTS_RUN=$((TESTS_RUN + 1)) + + if [ -f "$filepath" ]; then + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + else + echo -e "${RED}✗${NC} $description (file not found: $filepath)" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi +} + +# Print test summary +print_summary() { + echo "" + echo "======================================" + echo "Test Summary" + echo "======================================" + echo "Total tests: $TESTS_RUN" + echo -e "Passed: ${GREEN}$TESTS_PASSED${NC}" + echo -e "Failed: ${RED}$TESTS_FAILED${NC}" + echo "======================================" + + if [ $TESTS_FAILED -eq 0 ]; then + echo -e "${GREEN}All tests passed!${NC}" + return 0 + else + echo -e "${RED}Some tests failed!${NC}" + return 1 + fi +} + +# Setup test environment +setup_test_env() { + # Create temporary directory for test files + TEST_TMP_DIR=$(mktemp -d) + export TEST_TMP_DIR + trap "rm -rf $TEST_TMP_DIR" EXIT + echo "Test directory: $TEST_TMP_DIR" +} + +# Get test tmp dir +get_test_tmp_dir() { + echo "$TEST_TMP_DIR" +} + +# Create a test yue file +create_test_file() { + local filepath="$1" + local content="$2" + mkdir -p "$(dirname "$filepath")" + echo "$content" > "$filepath" +} diff --git a/spec/cli/run_all_tests.sh b/spec/cli/run_all_tests.sh new file mode 100755 index 0000000..0067bba --- /dev/null +++ b/spec/cli/run_all_tests.sh @@ -0,0 +1,77 @@ +#!/bin/bash +# Main test runner for YueScript CLI +# Runs all CLI test suites + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +echo -e "${BLUE}======================================" +echo "YueScript CLI Test Suite" +echo "======================================${NC}" +echo "" + +# Check if yue binary exists +YUE_BIN="${YUE_BIN:-./bin/debug/yue}" +if [ ! -f "$YUE_BIN" ]; then + echo -e "${RED}Error: yue binary not found at $YUE_BIN${NC}" + echo "Please build the project first:" + echo " make debug" + echo "Or set YUE_BIN environment variable" + exit 1 +fi + +# Track overall results +TOTAL_SUITES=0 +PASSED_SUITES=0 +FAILED_SUITES=0 + +# Function to run a test suite +run_test_suite() { + local suite_name="$1" + local suite_file="$2" + + TOTAL_SUITES=$((TOTAL_SUITES + 1)) + echo "" + echo -e "${BLUE}Running: $suite_name${NC}" + echo "======================================" + + if bash "$suite_file"; then + echo -e "${GREEN}✓ $suite_name PASSED${NC}" + PASSED_SUITES=$((PASSED_SUITES + 1)) + return 0 + else + echo -e "${RED}✗ $suite_name FAILED${NC}" + FAILED_SUITES=$((FAILED_SUITES + 1)) + return 1 + fi +} + +# Run all test suites +run_test_suite "Basic Options Test" "$SCRIPT_DIR/test_basic_options.sh" +run_test_suite "Compilation Test" "$SCRIPT_DIR/test_compilation.sh" +run_test_suite "Error Handling Test" "$SCRIPT_DIR/test_error_handling.sh" +run_test_suite "Execution Test" "$SCRIPT_DIR/test_execution.sh" + +# Print final summary +echo "" +echo -e "${BLUE}======================================" +echo "Final Test Suite Summary" +echo "======================================${NC}" +echo "Total test suites: $TOTAL_SUITES" +echo -e "Passed: ${GREEN}$PASSED_SUITES${NC}" +echo -e "Failed: ${RED}$FAILED_SUITES${NC}" +echo "======================================" + +if [ $FAILED_SUITES -eq 0 ]; then + echo -e "${GREEN}All test suites passed!${NC}" + exit 0 +else + echo -e "${RED}Some test suites failed!${NC}" + exit 1 +fi diff --git a/spec/cli/test_basic_options.sh b/spec/cli/test_basic_options.sh new file mode 100755 index 0000000..81cfaf7 --- /dev/null +++ b/spec/cli/test_basic_options.sh @@ -0,0 +1,38 @@ +#!/bin/bash +# Test Basic Options for YueScript CLI +# Tests: -h, --help, -v, --version + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$SCRIPT_DIR/cli_test_helper.sh" + +# Check binary +check_yue_binary + +echo "========================================" +echo "Testing Basic Options" +echo "========================================" +echo "" + +# Test 1: Help flag -h +echo "Testing -h flag..." +assert_output_contains "Help flag -h should show usage" "Usage: yue" $YUE_BIN -h + +# Test 2: Help flag --help +assert_output_contains "Help flag --help should show usage" "Usage: yue" $YUE_BIN --help + +# Test 3: Version flag -v +assert_output_contains "Version flag -v should show version" "Yuescript version:" $YUE_BIN -v + +# Test 4: Version flag --version +assert_output_contains "Version flag --version should show version" "Yuescript version:" $YUE_BIN --version + +# Test 5: Verify help contains expected sections +echo "" +echo "Testing help content..." +# Use grep -F to search for fixed strings without interpreting special characters +assert_output_contains "Help should show compile options" "output-to" $YUE_BIN --help +assert_output_contains "Help should show minify option" "minify" $YUE_BIN --help +assert_output_contains "Help should show execute option" "execute" $YUE_BIN --help + +echo "" +print_summary 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 @@ +#!/bin/bash +# Test Compilation Functionality for YueScript CLI +# Tests: File compilation, directory compilation, output 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 Compilation Functionality" +echo "========================================" +echo "" + +# Test 1: Compile a simple file to stdout +echo "Testing simple file compilation to stdout..." +cat > "$TMP_DIR/simple.yue" << 'EOF' +print "Hello, World!" +EOF + +assert_output_contains "Compile simple file to stdout" "print" $YUE_BIN -p "$TMP_DIR/simple.yue" + +# Test 2: Compile a simple file to disk +echo "" +echo "Testing file compilation to disk..." +cat > "$TMP_DIR/test1.yue" << 'EOF' +x = 1 + 2 +print x +EOF + +assert_success "Compile test1.yue to disk" $YUE_BIN "$TMP_DIR/test1.yue" +assert_file_exists "Output file test1.lua should exist" "$TMP_DIR/test1.lua" + +# Test 3: Compile with -o option +echo "" +echo "Testing compilation with -o option..." +cat > "$TMP_DIR/test2.yue" << 'EOF' +x = 10 +print x +EOF + +assert_success "Compile with -o option" $YUE_BIN -o "$TMP_DIR/output.lua" "$TMP_DIR/test2.yue" +assert_file_exists "Custom output file should exist" "$TMP_DIR/output.lua" + +# Test 4: Compile directory with -t option (target directory) +echo "" +echo "Testing compilation with -t option..." +mkdir -p "$TMP_DIR/src" +mkdir -p "$TMP_DIR/build" +cat > "$TMP_DIR/src/test3.yue" << 'EOF' +print "test" +EOF + +assert_success "Compile directory with -t option" $YUE_BIN "$TMP_DIR/src" -t "$TMP_DIR/build" +assert_file_exists "Output should be in target directory" "$TMP_DIR/build/test3.lua" + +# Test 5: Compile directory recursively +echo "" +echo "Testing directory compilation..." +mkdir -p "$TMP_DIR/project/src" +mkdir -p "$TMP_DIR/project/build" + +cat > "$TMP_DIR/project/src/file1.yue" << 'EOF' +print "file1" +EOF + +cat > "$TMP_DIR/project/src/file2.yue" << 'EOF' +print "file2" +EOF + +assert_success "Compile entire directory" $YUE_BIN "$TMP_DIR/project/src" -t "$TMP_DIR/project/build" +# Files are compiled directly in target directory, not preserving src subdir +assert_file_exists "file1.lua should exist" "$TMP_DIR/project/build/file1.lua" +assert_file_exists "file2.lua should exist" "$TMP_DIR/project/build/file2.lua" + +# Test 6: Compile with line numbers (-l) +echo "" +echo "Testing compilation with line numbers..." +cat > "$TMP_DIR/test_line.yue" << 'EOF' +print "line test" +EOF + +assert_success "Compile with line numbers" $YUE_BIN -l "$TMP_DIR/test_line.yue" -o "$TMP_DIR/test_line.lua" +assert_output_contains "Compiled file should have line comment" "yue" cat "$TMP_DIR/test_line.lua" + +# Test 7: Compile with spaces instead of tabs (-s) +echo "" +echo "Testing compilation with spaces..." +cat > "$TMP_DIR/test_spaces.yue" << 'EOF' +x = 1 +EOF + +assert_success "Compile with spaces option" $YUE_BIN -s "$TMP_DIR/test_spaces.yue" -o "$TMP_DIR/test_spaces.lua" + +# Test 8: Compile with minify (-m) +echo "" +echo "Testing compilation with minify..." +cat > "$TMP_DIR/test_minify.yue" << 'EOF' +-- this is a comment +x = 1 + 2 +print x +EOF + +assert_success "Compile with minify option" $YUE_BIN -m "$TMP_DIR/test_minify.yue" -o "$TMP_DIR/test_minify.lua" +assert_file_exists "Minified file should exist" "$TMP_DIR/test_minify.lua" + +# Test 9: Stdin/stdout compilation +echo "" +echo "Testing stdin/stdout compilation..." +echo 'print "stdin test"' | assert_output_contains "Compile from stdin" "print" $YUE_BIN - + +# Test 10: Glob variable dumping (-g) +echo "" +echo "Testing global variable dumping..." +cat > "$TMP_DIR/test_globals.yue" << 'EOF' +local x = 1 +print unknown_global +EOF + +# -g dumps globals in format: NAME LINE COLUMN +assert_output_contains "Dump global variables" "unknown_global" $YUE_BIN -g "$TMP_DIR/test_globals.yue" + +# Test 11: Benchmark compilation (-b) +echo "" +echo "Testing benchmark compilation..." +cat > "$TMP_DIR/test_bench.yue" << 'EOF' +print "benchmark" +EOF + +assert_output_contains "Benchmark should show compile time" "Compile time:" $YUE_BIN -b "$TMP_DIR/test_bench.yue" + +# Test 12: Target version option +echo "" +echo "Testing target version option..." +cat > "$TMP_DIR/test_target.yue" << 'EOF' +print "target test" +EOF + +assert_success "Compile with target 5.1" $YUE_BIN "$TMP_DIR/test_target.yue" -o "$TMP_DIR/test_target.lua" --target 5.1 +assert_file_exists "Target version compilation should succeed" "$TMP_DIR/test_target.lua" + +echo "" +print_summary 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 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