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
| | COMPILER_PATH="../../olang"
TEST_FILE="$1"
PROGRAM="$TEST_FILE.test"
COMPILER_OUTPUT_FILE="$TEST_FILE.test.compiler_output"
COMPILER_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_compiler_output"
AST_OUTPUT_FILE="$TEST_FILE.test.ast_output"
AST_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_ast_output"
TOKENS_OUTPUT_FILE="$TEST_FILE.test.tokens_output"
TOKENS_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_tokens_output"
TOKENS_OUTPUT_FILE="$TEST_FILE.test.tokens_output"
TOKENS_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_tokens_output"
PROGRAM_OUTPUT="$TEST_FILE.test.program_output"
extract_comment() {
tag="$1"
comment="# $tag: "
cat $TEST_FILE | grep "$comment" | sed -e "s/$comment//"
}
# UI
COLOR_RED=1
COLOR_GREEN=2
COLOR_YELLOW=3
COLOR_BLUE=4
COLOR_CYAN=6
COLOR_GRAY=7
colored() {
text="$1"
color="$2"
if [ -t 1 ]; then
if tput setaf 1 &> /dev/null; then
printf "$(tput setaf $color)$text$(tput sgr0)"
return
fi
fi
printf "$text"
}
print_test_description() {
colored "$TEST_FILE: \n" $COLOR_CYAN
colored "$(extract_comment spec)\n\n" $COLOR_GRAY
}
print_passed() {
context="$1"
printf "$context: "
colored "passed" $COLOR_GREEN
echo
}
print_failed() {
context="$1"
printf "$context: "
colored "failed" $COLOR_RED
echo
}
print_skiped() {
context="$1"
printf "$context: "
colored "not set" $COLOR_GRAY
echo
}
# Assertions
expect_exit_code() {
context="$1"
expected="$2"
actual="$3"
if [ -z "$expected" ]; then
print_skiped "$context"
echo "no expected exit code set, it exited with \"$actual\"."
else
if [ "$expected" = "$actual" ]; then
print_passed "$context"
else
print_failed "$context"
colored "Expected exit code: " $COLOR_YELLOW
colored "$expected" $COLOR_GRAY
colored ", actual: " $COLOR_YELLOW
colored "$actual\n" $COLOR_GRAY
exit 1
fi
fi
}
expect_output() {
context="$1"
expected_file="$2"
actual_file="$3"
if [ "$(cat $expected_file | wc -l)" = "0" ]; then
print_skiped "$context"
return
fi
if cmp -s "$expected_file" "$actual_file"; then
print_passed "$context"
else
if [ "$(cat "$expected_file")" = "%empty%" ]; then
if [ "$(cat $actual_file | wc -c)" = "0" ]; then
print_passed "$context"
return
fi
fi
print_failed "$context"
colored "$context not match:\n" $COLOR_YELLOW
diff "$actual_file" "$expected_file" -u --color
exit 1
fi
}
cleanup() {
rm -f $PROGRAM*
}
test_compiler() {
$COMPILER_PATH $TEST_FILE -o $PROGRAM > "$COMPILER_OUTPUT_FILE" 2>&1
EXIT=$?
EXPECTED_EXIT=$(extract_comment compiler_exit)
expect_exit_code compiler_exit "$EXPECTED_EXIT" "$EXIT"
extract_comment compiler_output > $COMPILER_EXPECT_OUTPUT_FILE
expect_output compiler_output "$COMPILER_EXPECT_OUTPUT_FILE" "$COMPILER_OUTPUT_FILE"
if [ "$EXIT" = "1" ]; then
echo "program execution skiped since the compiler failed as expected."
cleanup
exit 0
fi
}
test_ast() {
$COMPILER_PATH $TEST_FILE --dump-ast > "$AST_OUTPUT_FILE" 2>&1
extract_comment ast > $AST_EXPECT_OUTPUT_FILE
expect_output "ast" "$AST_EXPECT_OUTPUT_FILE" "$AST_OUTPUT_FILE"
}
test_tokens() {
extract_comment tokens > $TOKENS_EXPECT_OUTPUT_FILE
TOKEN_LINES=$(cat $TOKENS_EXPECT_OUTPUT_FILE | wc -l)
$COMPILER_PATH $TEST_FILE --dump-tokens | head -n $TOKEN_LINES > "$TOKENS_OUTPUT_FILE" 2>&1
expect_output tokens $TOKENS_OUTPUT_FILE $TOKENS_EXPECT_OUTPUT_FILE
print_passed tokens
}
test_program() {
./$PROGRAM
EXIT=$?
EXPECTED_EXIT=$(extract_comment program_exit)
expect_exit_code "program_exit" "$EXPECTED_EXIT" "$EXIT"
}
main() {
print_test_description
test_compiler
test_program
test_tokens
test_ast
cleanup
}
main
|