-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_tests.sh
executable file
·151 lines (128 loc) · 2.81 KB
/
run_tests.sh
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
#!/usr/bin/env bash
# GNU grep is ggrep on macOS
if [ "$(uname)" == "Darwin" ]; then
GREP="ggrep"
TEMP_DIR="${TMPDIR}cppsh"
TEST_DIR="${TMPDIR}cppsh_test"
else
TEMP_DIR="/tmp/cppsh"
TEST_DIR="/tmp/cppsh_test"
GREP="grep"
fi
function assert {
[[ ! "$1" ]] && echo "assertion failed" && exit 1
}
function pleaseRemove {
if [ -f "$1" ]; then
rm "$1"
fi
if [ -d "$1" ]; then
rm -rf "$1"
fi
}
function doPrepare {
pleaseRemove "$TEMP_DIR"
pleaseRemove "$TEST_DIR"
mkdir -p "$TEST_DIR"
}
# testWithOutput source expected
function testWithOutput {
echo -n "$1" > "$TEST_DIR/simple.cpp"
RES=$(./cppsh "$TEST_DIR/simple.cpp")
# ./cppsh "$TEST_DIR/simple.cpp"
assert "$(echo "$RES" | "$GREP" "$2")"
}
function testSimple {
local OUT="Hello from testSimple"
local SRC="#!/usr/bin/env cppsh
#include <iostream>
int main() {
std::cout << \"$OUT\" << std::endl;
return 0;
}
"
testWithOutput "$SRC" "$OUT"
}
function testCompileError {
local OUT="Cannot compile testCompileError"
local SRC="#!/usr/bin/env cppsh
#error \"$OUT\"
"
testWithOutput "$SRC" "$OUT"
}
function testNoSource {
pleaseRemove "$TEST_DIR/simple.cpp"
RES=$(./cppsh "$TEST_DIR/simple.cpp")
assert "$(echo "$RES" | "$GREP" "Hello from simple.cpp")"
}
function testCpp11 {
local OUT="Hello from testSimple"
local SRC="#!/usr/bin/env cppsh
#include <iostream>
int main() {
for( const auto& i : { 1, 2, 3 } ) {
std::cout << i << \"$OUT\" << std::endl;
}
return 0;
}
"
testWithOutput "$SRC" "$OUT"
}
function testCpp14 {
local OUT="Hello from testSimple"
local SRC="#!/usr/bin/env cppsh
#include <iostream>
#include <chrono>
using namespace std::chrono_literals;
int main() {
auto tenHours = 10h;
std::cout << tenHours.count() << \"$OUT\" << std::endl;
return 0;
}
"
testWithOutput "$SRC" "$OUT"
}
function testCpp17 {
local OUT="Hello from testSimple"
local SRC="#!/usr/bin/env cppsh
#include <iostream>
#include <optional>
#include <string>
int main() {
std::optional<std::string> test = \"opt: \";
std::cout << *test << \"$OUT\" << std::endl;
return 0;
}
"
testWithOutput "$SRC" "$OUT"
}
function testCpp20 {
local OUT="Hello from testSimple"
local SRC="#!/usr/bin/env cppsh
#include <iostream>
#include <numbers>
int main() {
if( std::numbers::pi > 3.f ) {
std::cout << std::numbers::pi << \"$OUT\" << std::endl;
}
return 0;
}
"
testWithOutput "$SRC" "$OUT"
}
doPrepare
echo "Running testSimple"
testSimple
echo "Running testCompileError"
testCompileError
echo "Running testNoSource"
testNoSource
echo "Running testCpp11"
testCpp11
echo "Running testCpp14"
testCpp14
echo "Running testCpp17"
testCpp17
echo "Running testCpp20"
testCpp20
echo "Tests succeeded"