aboutsummaryrefslogtreecommitdiff
path: root/spec/cli/test_error_handling.sh
blob: 1ff16422e2b82293661e9358f7d3e8ffcb6dcbcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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