kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

run_driver.sh (4466B)


      1 #!/bin/sh
      2 # test/gram/run_driver.sh — `kit gram` CLI behavior smoke (no codegen compile).
      3 # Covers stdin input, --dump-sexpr, --sample-tokens, default output naming, and
      4 # exit codes. The heavy generate->compile->run coverage lives in run.sh.
      5 set -u
      6 
      7 KIT=${KIT:?set KIT to the kit binary}
      8 DIR=${KIT_GRAM_BUILD:-build/test/gram-driver}
      9 SRC=test/gram
     10 mkdir -p "$DIR"
     11 pass=0
     12 fail=0
     13 
     14 ok() { pass=$((pass + 1)); printf '  ok   %s\n' "$1"; }
     15 bad() { fail=$((fail + 1)); printf '  FAIL %s\n' "$1"; }
     16 
     17 # expect_exit <code> <desc> -- <cmd...>
     18 expect_exit() {
     19   _want=$1; _desc=$2; shift 3
     20   "$@" >"$DIR/out" 2>"$DIR/err"
     21   _got=$?
     22   if [ "$_got" -eq "$_want" ]; then ok "$_desc"; else
     23     bad "$_desc (want exit $_want, got $_got)"
     24     sed 's/^/      /' "$DIR/err" | tail -4
     25   fi
     26 }
     27 
     28 echo "== kit gram CLI smoke =="
     29 
     30 # --help and no-args both succeed / show usage.
     31 expect_exit 0 "--help" -- "$KIT" gram --help
     32 
     33 # User-facing help must not leak the source-project name "gramgen".
     34 if "$KIT" gram --help 2>&1 | grep -qi gramgen; then
     35   bad "--help leaks internal name 'gramgen'"
     36 else
     37   ok "--help has no 'gramgen' leak"
     38 fi
     39 
     40 # Missing grammar operand is a usage error (exit 2).
     41 expect_exit 2 "no grammar => usage error" -- "$KIT" gram --dump-sexpr
     42 if grep -q "missing grammar input" "$DIR/err"; then
     43   ok "missing grammar diagnostic"
     44 else
     45   bad "missing grammar diagnostic"
     46 fi
     47 
     48 # Structured usage errors name the option/value before the synopsis and use
     49 # the shared bounded suggestion helper without touching outputs.
     50 rm -f "$DIR/should-not-exist.c"
     51 expect_exit 2 "unknown option => usage error" -- "$KIT" gram \
     52   -o "$DIR/should-not-exist.c" --hlep "$SRC/calc.ebnf"
     53 if grep -q "unknown option: --hlep; did you mean '--help'" "$DIR/err"; then
     54   ok "unknown option diagnostic + suggestion"
     55 else
     56   bad "unknown option diagnostic + suggestion"
     57 fi
     58 if [ ! -e "$DIR/should-not-exist.c" ]; then
     59   ok "usage parse completes before output"
     60 else
     61   bad "usage parse completes before output"
     62 fi
     63 
     64 expect_exit 2 "missing -o value => usage error" -- "$KIT" gram -o
     65 if grep -q "option requires an argument: -o" "$DIR/err"; then
     66   ok "missing option argument diagnostic"
     67 else
     68   bad "missing option argument diagnostic"
     69 fi
     70 
     71 expect_exit 2 "invalid numeric => usage error" -- "$KIT" gram \
     72   --samples nope "$SRC/calc.ebnf"
     73 if grep -q "invalid numeric value 'nope' for --samples" "$DIR/err"; then
     74   ok "invalid numeric diagnostic"
     75 else
     76   bad "invalid numeric diagnostic"
     77 fi
     78 
     79 expect_exit 2 "duplicate grammar => usage error" -- "$KIT" gram \
     80   "$SRC/calc.ebnf" "$SRC/features.ebnf"
     81 if grep -q "multiple grammar inputs" "$DIR/err"; then
     82   ok "duplicate grammar diagnostic"
     83 else
     84   bad "duplicate grammar diagnostic"
     85 fi
     86 
     87 cp "$SRC/calc.ebnf" "$DIR/-grammar.ebnf"
     88 (cd "$DIR" && "$KIT" gram --dump-sexpr -- -grammar.ebnf) \
     89   > "$DIR/dash.sexpr" 2> "$DIR/dash.err"
     90 if [ "$?" -eq 0 ] && grep -q '(grammar' "$DIR/dash.sexpr"; then
     91   ok "-- leading-dash grammar"
     92 else
     93   bad "-- leading-dash grammar"
     94 fi
     95 
     96 # Nonexistent input is an I/O error (exit 1).
     97 expect_exit 1 "missing file => error" -- "$KIT" gram /no/such/grammar.ebnf
     98 
     99 # --dump-sexpr to stdout.
    100 if "$KIT" gram --dump-sexpr "$SRC/calc.ebnf" >"$DIR/calc.sexpr" 2>"$DIR/derr" &&
    101   grep -q '(grammar' "$DIR/calc.sexpr"; then ok "--dump-sexpr stdout"; else bad "--dump-sexpr stdout"; fi
    102 
    103 # Grammar from stdin ("-").
    104 if "$KIT" gram --dump-sexpr - <"$SRC/calc.ebnf" >"$DIR/stdin.sexpr" 2>"$DIR/derr" &&
    105   grep -q '(grammar' "$DIR/stdin.sexpr"; then ok "stdin grammar"; else bad "stdin grammar"; fi
    106 
    107 # --sample-tokens: deterministic count of sample lines.
    108 if "$KIT" gram --sample-tokens --samples 3 --seed 7 "$SRC/features.ebnf" \
    109     >"$DIR/samples" 2>"$DIR/derr" && [ "$(wc -l <"$DIR/samples")" -eq 3 ]; then
    110   ok "--sample-tokens --samples 3"
    111 else
    112   bad "--sample-tokens --samples 3"
    113 fi
    114 
    115 # --sample-traces over a %machine.
    116 if "$KIT" gram --sample-traces conn --samples 2 --seed 1 "$SRC/machine.ebnf" \
    117     >"$DIR/traces" 2>"$DIR/derr" && [ "$(wc -l <"$DIR/traces")" -eq 2 ]; then
    118   ok "--sample-traces"
    119 else
    120   bad "--sample-traces"
    121 fi
    122 
    123 # Default output naming: no -o/--header derives <stem>.c / <stem>.h next to it.
    124 cp "$SRC/calc.ebnf" "$DIR/mygrammar.ebnf"
    125 if "$KIT" gram --prefix mg_ "$DIR/mygrammar.ebnf" >"$DIR/derr" 2>&1 &&
    126   [ -f "$DIR/mygrammar.c" ] && [ -f "$DIR/mygrammar.h" ]; then
    127   ok "default output naming"
    128 else
    129   bad "default output naming"
    130 fi
    131 
    132 echo
    133 echo "== kit gram CLI: $pass passed, $fail failed =="
    134 [ "$fail" -eq 0 ] || exit 1
    135 exit 0