run.sh raw
1 #!/bin/bash
2 # Restriction test suite for Moxie.
3 # Tests that removed builtins, types, and statements produce compile errors,
4 # and that valid Moxie patterns compile and run correctly.
5 set -euo pipefail
6
7 MOXIE="${MOXIE:-./moxie}"
8 MOXIE="$(cd "$(dirname "$MOXIE")" && pwd)/$(basename "$MOXIE")"
9 # MOXIEROOT must point to the legacy-spec source tree (two levels up from tests/restrict/).
10 export MOXIEROOT="$(cd "$(dirname "$0")/../.." && pwd)"
11 TESTDIR="$(cd "$(dirname "$0")" && pwd)"
12 TMPDIR=$(mktemp -d)
13 trap "rm -rf $TMPDIR" EXIT
14
15 export PATH="/usr/lib/llvm19/bin:$PATH"
16
17 pass=0
18 fail=0
19
20 # Create a temp module directory, copy a single .mx file into it as main.mx,
21 # and return the module directory path. Each test gets an isolated module.
22 setup_module() {
23 local src="$1"
24 local name=$(basename "$src" .mx)
25 local moddir="$TMPDIR/mod_$name"
26 mkdir -p "$moddir"
27 cp "$src" "$moddir/main.mx"
28 printf 'module %s\n\nmoxie 0.1.0\n' "$name" > "$moddir/moxie.mod"
29 echo "$moddir"
30 }
31
32 run_pass_test() {
33 local src="$1"
34 local name=$(basename "$src" .mx)
35 local moddir
36 moddir=$(setup_module "$src")
37 local bin="$moddir/$name"
38
39 if ! (cd "$moddir" && "$MOXIE" build -no-debug -o "$bin" . 2>"$moddir/err.log"); then
40 echo "FAIL $name: compilation failed"
41 cat "$moddir/err.log"
42 fail=$((fail + 1))
43 return
44 fi
45
46 local output
47 output=$(timeout 5 "$bin" 2>&1 || true)
48
49 if echo "$output" | grep -q "FAIL:"; then
50 echo "FAIL $name: wrong values"
51 echo "$output"
52 fail=$((fail + 1))
53 return
54 fi
55 if echo "$output" | grep -q "PASS:"; then
56 local passes=$(echo "$output" | grep -c "PASS:" || true)
57 echo "OK $name ($passes checks)"
58 pass=$((pass + 1))
59 else
60 echo "FAIL $name: no PASS markers in output"
61 echo "$output"
62 fail=$((fail + 1))
63 fi
64 }
65
66 run_fail_test() {
67 local src="$1"
68 local pattern="$2"
69 local name=$(basename "$src" .mx)
70 local moddir
71 moddir=$(setup_module "$src")
72 local bin="$moddir/$name"
73
74 local errout
75 if errout=$(cd "$moddir" && "$MOXIE" build -no-debug -o "$bin" . 2>&1); then
76 echo "FAIL $name: compilation should have failed"
77 fail=$((fail + 1))
78 return
79 fi
80
81 if echo "$errout" | grep -qi "$pattern"; then
82 echo "OK $name (rejected: $pattern)"
83 pass=$((pass + 1))
84 else
85 echo "FAIL $name: expected error containing '$pattern'"
86 echo "$errout"
87 fail=$((fail + 1))
88 fi
89 }
90
91 echo "=== Restriction positive tests ==="
92 for src in "$TESTDIR"/pass_*.mx; do
93 run_pass_test "$src"
94 done
95
96 echo ""
97 echo "=== Restriction negative tests ==="
98 run_fail_test "$TESTDIR/fail_new.mx" "new.*not allowed"
99 run_fail_test "$TESTDIR/fail_complex_builtin.mx" "complex.*not"
100 run_fail_test "$TESTDIR/fail_real.mx" "real.*not\|complex.*not"
101 run_fail_test "$TESTDIR/fail_imag.mx" "imag.*not\|complex.*not"
102 run_fail_test "$TESTDIR/fail_complex64.mx" "complex64"
103 run_fail_test "$TESTDIR/fail_complex128.mx" "complex128"
104 run_fail_test "$TESTDIR/fail_uintptr.mx" "uintptr"
105 run_fail_test "$TESTDIR/fail_fallthrough.mx" "fallthrough"
106 run_fail_test "$TESTDIR/fail_int.mx" "int.*not allowed"
107 run_fail_test "$TESTDIR/fail_uint.mx" "uint.*not allowed"
108 run_fail_test "$TESTDIR/fail_go_keyword.mx" "go.*not allowed"
109 run_fail_test "$TESTDIR/fail_empty_interface.mx" "empty interface.*not allowed"
110 run_fail_test "$TESTDIR/fail_any.mx" "any.*not allowed"
111 run_fail_test "$TESTDIR/fail_append_spread.mx" "append.*not allowed"
112 run_fail_test "$TESTDIR/fail_value_recv.mx" "value receiver.*not allowed"
113 run_fail_test "$TESTDIR/fail_unnamed_return.mx" "unnamed return.*not allowed"
114 run_fail_test "$TESTDIR/fail_pkg_var_init.mx" "package-level var.*not allowed"
115 run_fail_test "$TESTDIR/fail_named_slice.mx" "named slice.*not allowed"
116 run_fail_test "$TESTDIR/fail_named_map.mx" "named map.*not allowed"
117 run_fail_test "$TESTDIR/fail_shadow.mx" "shadows"
118 run_fail_test "$TESTDIR/fail_shadow_builtin.mx" "shadows"
119 run_fail_test "$TESTDIR/fail_hash_nonident.mx" "# must be followed"
120 run_fail_test "$TESTDIR/fail_hash_redecl.mx" "redeclared"
121 run_fail_test "$TESTDIR/fail_use_after_free.mx" "use after free"
122 run_fail_test "$TESTDIR/fail_init_in_main.mx" "init.*not allowed.*main"
123 run_fail_test "$TESTDIR/fail_init_select.mx" "select.*not allowed.*init"
124 run_fail_test "$TESTDIR/fail_init_spawn.mx" "spawn.*not allowed.*init"
125 run_fail_test "$TESTDIR/fail_global_mutate.mx" "package-level var.*outside init"
126
127 echo ""
128 echo "=== Results: $pass passed, $fail failed ==="
129 [ "$fail" -eq 0 ]
130