Skip to content

Commit 61bdd6e

Browse files
committed
chore: add script to run test suite
1 parent ad27f8c commit 61bdd6e

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LDFLAGS = -lcriterion
55

66
SOURCES = src/scanner.c src/chunk.c src/compiler.c src/debug.c src/memory.c src/value.c src/vm.c src/object.c src/table.c src/native_fn.c
77
TEST_SOURCES = tests/scanner_test.c
8+
EXAMPLE_RUNNER = tests/run_examples.sh
89

910
all: clox
1011

@@ -22,5 +23,8 @@ testf: $(TEST_SOURCES) $(SOURCES)
2223
@$(CC) $(CTEST_FLAGS) -o test_runner $(TEST_SOURCES) $(SOURCES) -I. $(LDFLAGS)
2324
@./test_runner --fail-fast
2425

26+
examples: clox
27+
@$(EXAMPLE_RUNNER)
28+
2529
clean:
2630
@rm -f test_runner clox

tests/run_examples.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
5+
CLOX_BIN="$ROOT_DIR/clox"
6+
7+
if [[ ! -x "$CLOX_BIN" ]]; then
8+
echo "clox binary not found. Run: make clox"
9+
exit 1
10+
fi
11+
12+
PASS=0
13+
FAIL=0
14+
TMP_STDOUT="/tmp/clox_example_stdout.txt"
15+
TMP_STDERR="/tmp/clox_example_stderr.txt"
16+
trap 'rm -f "$TMP_STDOUT" "$TMP_STDERR"' EXIT
17+
18+
run_case() {
19+
local file="$1"
20+
local expected="$2"
21+
local input_data="${3:-}"
22+
23+
local rc
24+
if [[ -n "$input_data" ]]; then
25+
rc=0
26+
timeout 20s "$CLOX_BIN" "$ROOT_DIR/$file" <<<"$input_data" >"$TMP_STDOUT" 2>"$TMP_STDERR" || rc=$?
27+
else
28+
rc=0
29+
timeout 20s "$CLOX_BIN" "$ROOT_DIR/$file" </dev/null >"$TMP_STDOUT" 2>"$TMP_STDERR" || rc=$?
30+
fi
31+
32+
local ok=0
33+
case "$expected" in
34+
ok)
35+
[[ "$rc" -eq 0 ]] && ok=1
36+
;;
37+
compile_error)
38+
[[ "$rc" -eq 65 ]] && ok=1
39+
;;
40+
runtime_error)
41+
[[ "$rc" -eq 70 ]] && ok=1
42+
;;
43+
nonzero)
44+
[[ "$rc" -ne 0 ]] && ok=1
45+
;;
46+
esac
47+
48+
if [[ "$ok" -eq 1 ]]; then
49+
echo "PASS $file (exit=$rc expected=$expected)"
50+
PASS=$((PASS + 1))
51+
else
52+
echo "FAIL $file (exit=$rc expected=$expected)"
53+
if [[ -s "$TMP_STDOUT" ]]; then
54+
echo "[stdout]"
55+
cat "$TMP_STDOUT"
56+
fi
57+
if [[ -s "$TMP_STDERR" ]]; then
58+
echo "[stderr]"
59+
cat "$TMP_STDERR"
60+
fi
61+
FAIL=$((FAIL + 1))
62+
fi
63+
}
64+
65+
run_case "examples/anonymous_functions.lox" "ok"
66+
run_case "examples/classes.lox" "ok"
67+
run_case "examples/closures.lox" "ok"
68+
run_case "examples/control_flow.lox" "ok"
69+
run_case "examples/example_fibonacci.lox" "ok"
70+
run_case "examples/example_quiz.lox" "ok" $'4\nParis\nno'
71+
run_case "examples/example_user_db.lox" "ok" $'Alice\nalice@example.com\nBob\nbob@example.com'
72+
run_case "examples/fibonacci.lox" "ok"
73+
run_case "examples/functions.lox" "ok"
74+
run_case "examples/inheritance.lox" "ok"
75+
run_case "examples/loops.lox" "ok"
76+
run_case "examples/native_input.lox" "ok" $'24\nyes'
77+
run_case "examples/native_lox.lox" "ok"
78+
run_case "examples/native_scan.lox" "ok" $'Aymane'
79+
run_case "examples/operators.lox" "ok"
80+
run_case "examples/static_methods.lox" "ok"
81+
run_case "examples/super.lox" "ok"
82+
83+
run_case "examples/errors/parse_error.lox" "compile_error"
84+
run_case "examples/errors/runtime_error_division_by_zero.lox" "runtime_error"
85+
run_case "examples/errors/runtime_error_undefined_variable.lox" "runtime_error"
86+
run_case "examples/errors/type_error.lox" "runtime_error"
87+
88+
echo "Summary: pass=$PASS fail=$FAIL total=$((PASS + FAIL))"
89+
90+
if [[ "$FAIL" -ne 0 ]]; then
91+
exit 1
92+
fi

0 commit comments

Comments
 (0)