- tests/ffi.cat: load failure, missing symbol, invalid signature and return type (all -> nil), pow/floor/sqrt/abs/strlen conversions, void return -> nil, pointer spelling, callable library - 16 new assertions wired into run_tests.sh (69 total, all green) - (note: '!=' is not catscript syntax; the not-equal operator is '~=')
167 lines
6.8 KiB
Bash
167 lines
6.8 KiB
Bash
#!/bin/sh
|
|
# tests/run_tests.sh -- automated catscript test suite.
|
|
#
|
|
# Runs each example and asserts expected stdout, then runs the
|
|
# self-hosted front-end tests (lexer/parser/compiler written in
|
|
# catscript itself). Exits non-zero on the first failure.
|
|
|
|
BIN="./build/catscript/catscript"
|
|
pass=0
|
|
fail=0
|
|
|
|
check() {
|
|
name="$1"; expected="$2"; got="$3"
|
|
if [ "$expected" = "$got" ]; then
|
|
echo "ok - $name"
|
|
pass=$((pass + 1))
|
|
else
|
|
echo "FAIL - $name"
|
|
echo " expected: [$expected]"
|
|
echo " got: [$got]"
|
|
fail=$((fail + 1))
|
|
fi
|
|
}
|
|
|
|
# ---- examples (core language) ----------------------------------------
|
|
|
|
out=`$BIN examples/hello.cat`
|
|
check "hello-1" "hello from catscript" "`echo "$out" | sed -n 1p`"
|
|
check "hello-2" "2 + 3 = 5" "`echo "$out" | sed -n 2p`"
|
|
|
|
out=`$BIN examples/arith.cat`
|
|
check "arith-add" "a + b = 13" "`echo "$out" | sed -n 1p`"
|
|
check "arith-div" "a / b = 3.33333" "`echo "$out" | sed -n 4p`"
|
|
|
|
out=`$BIN examples/text.cat`
|
|
check "text-concat" "hello, cat" "`echo "$out" | sed -n 1p`"
|
|
|
|
out=`$BIN examples/control.cat`
|
|
check "classify-neg" "negative" "`echo "$out" | sed -n 1p`"
|
|
check "classify-zero" "zero" "`echo "$out" | sed -n 2p`"
|
|
check "classify-pos" "positive" "`echo "$out" | sed -n 3p`"
|
|
check "while-sum" "sum 1..5 = 15" "`echo "$out" | sed -n 4p`"
|
|
check "for-sum" "sum 1..4 = 10" "`echo "$out" | sed -n 5p`"
|
|
|
|
out=`$BIN examples/oop.cat`
|
|
check "oop-method" "counter: 3" "`echo "$out" | sed -n 1p`"
|
|
|
|
out=`$BIN examples/toml.cat`
|
|
check "toml-title" "title: catscript" "`echo "$out" | sed -n 1p`"
|
|
check "toml-version" "version: 1" "`echo "$out" | sed -n 2p`"
|
|
check "toml-pi" "pi: 3.14" "`echo "$out" | sed -n 3p`"
|
|
|
|
# ---- cpk: the cat package manager (first real catscript program) -----
|
|
|
|
out=`$BIN examples/cpk.cat`
|
|
check "cpk-name" "package: hello-world" "`echo "$out" | sed -n 1p`"
|
|
check "cpk-version" "version: 0.1.0" "`echo "$out" | sed -n 2p`"
|
|
|
|
# ---- file family: AbstractFile / File / StringIO / BytesIO ----------
|
|
|
|
out=`$BIN examples/files.cat`
|
|
check "file-stringio" "stringio: hello, catscript" "`echo "$out" | sed -n 1p`"
|
|
check "file-bytesio" "bytesio: bytes io" "`echo "$out" | sed -n 2p`"
|
|
check "file-open" "file: line one" "`echo "$out" | sed -n 3p`"
|
|
check "file-hints" "n=3 buf=abc" "`echo "$out" | sed -n 5p`"
|
|
|
|
# ---- generators / iterators (yield, next, for..in) ------------------
|
|
|
|
out=`$BIN examples/generators.cat`
|
|
check "gen-type" "type: generator" "`echo "$out" | sed -n 1p`"
|
|
check "gen-next-1" "next: 1" "`echo "$out" | sed -n 2p`"
|
|
check "gen-next-nil" "next: nil" "`echo "$out" | sed -n 5p`"
|
|
check "gen-forin-sum" "sum 1..5 = 15" "`echo "$out" | sed -n 6p`"
|
|
check "gen-break-lazy" "taken < 3: 3" "`echo "$out" | sed -n 7p`"
|
|
|
|
# ---- errors (raise, try, except) ------------------------------------
|
|
|
|
out=`$BIN examples/errors.cat`
|
|
check "err-ok" "ok: 42" "`echo "$out" | sed -n 1p`"
|
|
check "err-catch" "caught: boom" "`echo "$out" | sed -n 2p`"
|
|
check "err-struct-code" "code: 42" "`echo "$out" | sed -n 3p`"
|
|
check "err-struct-what" "what: custom" "`echo "$out" | sed -n 4p`"
|
|
check "err-propagate" "propagated: negative not allowed" "`echo "$out" | sed -n 5p`"
|
|
check "err-survive" "still running" "`echo "$out" | sed -n 6p`"
|
|
|
|
# ---- slots: C-known object layout -----------------------------------
|
|
|
|
out=`$BIN examples/slots.cat`
|
|
check "slot-path" "path: /tmp/catscript_slots_demo.txt" "`echo "$out" | sed -n 1p`"
|
|
check "slot-mode" "mode: 2" "`echo "$out" | sed -n 2p`"
|
|
check "slot-closed" "closed: false" "`echo "$out" | sed -n 3p`"
|
|
check "slot-closed-2" "closed: true" "`echo "$out" | sed -n 4p`"
|
|
|
|
# ---- class / extends / use (import) ---------------------------------
|
|
|
|
out=`$BIN examples/classes.cat`
|
|
check "class-method" "cat makes a sound" "`echo "$out" | sed -n 1p`"
|
|
check "class-attr" "cat" "`echo "$out" | sed -n 2p`"
|
|
check "class-override" "Rex barks" "`echo "$out" | sed -n 3p`"
|
|
check "class-inherit" "Rex" "`echo "$out" | sed -n 4p`"
|
|
check "class-use" "double 21 = 42" "`echo "$out" | sed -n 5p`"
|
|
|
|
# ---- std/ tree modules (use "std/...") -------------------------------
|
|
|
|
out=`$BIN tests/std.cat`
|
|
check "std-math" "math double: 42" "`echo "$out" | sed -n 1p`"
|
|
check "std-io" "io join: a-b-c" "`echo "$out" | sed -n 4p`"
|
|
check "std-fs" "fs copy: true" "`echo "$out" | sed -n 6p`"
|
|
|
|
# ---- ffi: call native code (ctypes/CDLL-like) -----------------------
|
|
|
|
out=`$BIN examples/ffi.cat`
|
|
check "ffi-pow" "pow(2,10) = 1024" "`echo "$out" | sed -n 1p`"
|
|
check "ffi-sqrt" "sqrt(144) = 12" "`echo "$out" | sed -n 2p`"
|
|
check "ffi-getpid" "getpid > 0: true" "`echo "$out" | sed -n 3p`"
|
|
check "ffi-strlen" "strlen: 9" "`echo "$out" | sed -n 4p`"
|
|
check "ffi-abs" "abs(-42) = 42" "`echo "$out" | sed -n 5p`"
|
|
|
|
# ---- ffi edge cases (deterministic) ---------------------------------
|
|
|
|
out=`$BIN tests/ffi.cat`
|
|
check "ffi-badload-nil" "bad load nil: true" "`echo "$out" | sed -n 1p`"
|
|
check "ffi-badload-type" "bad load type: nil" "`echo "$out" | sed -n 2p`"
|
|
check "ffi-bound" "pow bound: true" "`echo "$out" | sed -n 3p`"
|
|
check "ffi-missing-sym" "missing sym nil: true" "`echo "$out" | sed -n 4p`"
|
|
check "ffi-badsig" "bad sig nil: true" "`echo "$out" | sed -n 5p`"
|
|
check "ffi-badret" "bad rettype nil: true" "`echo "$out" | sed -n 6p`"
|
|
check "ffi-pow-2-10" "pow 2,10: 1024" "`echo "$out" | sed -n 7p`"
|
|
check "ffi-pow-3-4" "pow 3,4: 81" "`echo "$out" | sed -n 8p`"
|
|
check "ffi-floor" "floor 3.7: 3" "`echo "$out" | sed -n 9p`"
|
|
check "ffi-sqrt-144" "sqrt 144: 12" "`echo "$out" | sed -n 10p`"
|
|
check "ffi-abs-neg" "abs -42: 42" "`echo "$out" | sed -n 11p`"
|
|
check "ffi-strlen-abc" "strlen abc: 3" "`echo "$out" | sed -n 12p`"
|
|
check "ffi-strlen-empty" "strlen empty: 0" "`echo "$out" | sed -n 13p`"
|
|
check "ffi-void-nil" "void ret nil: true" "`echo "$out" | sed -n 14p`"
|
|
check "ffi-strchr-bound" "strchr bound: true" "`echo "$out" | sed -n 15p`"
|
|
check "ffi-lib-callable" "cos 0: 1" "`echo "$out" | sed -n 16p`"
|
|
# ---- bytecode dump ---------------------------------------------------
|
|
|
|
out=`$BIN --dump examples/hello.cat`
|
|
case "$out" in
|
|
*"CONST"*) check "bytecode-dump" "yes" "yes" ;;
|
|
*) check "bytecode-dump" "CONST present" "missing" ;;
|
|
esac
|
|
|
|
# ---- self-hosted front-end (written in catscript) -------------------
|
|
|
|
out=`$BIN tests/selfhost_lexer.cat`
|
|
case "$out" in
|
|
*"NAME [x]"*) check "selfhost-lexer" "yes" "yes" ;;
|
|
*) check "selfhost-lexer" "NAME token" "missing" ;;
|
|
esac
|
|
|
|
out=`$BIN tests/selfhost_parser.cat`
|
|
check "selfhost-parser" "parsed 3 statements" "`echo "$out" | sed -n 1p`"
|
|
|
|
out=`$BIN tests/selfhost_compile.cat`
|
|
check "selfhost-compile-1" "compiled ok (21 instrs)" "`echo "$out" | sed -n 1p`"
|
|
case "$out" in
|
|
*"sum = 42"*) check "selfhost-compile-2" "yes" "yes" ;;
|
|
*) check "selfhost-compile-2" "sum = 42" "missing" ;;
|
|
esac
|
|
|
|
echo ""
|
|
echo "$pass passed, $fail failed"
|
|
[ "$fail" -eq 0 ]
|