aboutsummaryrefslogtreecommitdiff
path: root/spec/cli/test_execution.sh
blob: fedfbeadcefa100e2c15660a4bc2be8c8cc618c4 (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
#!/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