blob: ade1546e19a0bc9ce1e51ce4a232f0b6f83ea187 (
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
170
171
172
173
174
175
176
177
178
179
180
181
182
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"
}
|