. "$BST_LIB"
require m4 m4/present

tmp=${TMPDIR:-/tmp}/bst-m4.$$
in="$tmp.m4"; inc="$tmp.inc.m4"

# m4 copies input to output, expanding macros as it goes. Its quote characters
# are ` ' by default; changequote([,]) swaps them for [] where a test would
# otherwise need unreadable shell quoting.

# Define a macro, then expand it: define() itself expands to nothing, so the
# whole output is the expansion of x.
check_eq m4/define 1 "$(echo 'define(x,1)x' | m4 2>&1)"

# eval() — m4's integer arithmetic builtin.
printf '%s\n' 'eval(6*7)' > "$in"
check_out m4/eval "42" m4 "$in"

# -D defines a macro from the command line.
printf '%s\n' 'FOO' > "$in"
check_out m4/cmdline-define "hello" m4 -DFOO=hello "$in"

# include() splices another file into the stream — exercises m4's file reads.
printf '%s\n' 'included-text' > "$inc"
printf 'include(%s)\n' "$inc" > "$in"
check_out m4/include "included-text" m4 "$in"

# ifdef() branches on whether a macro is defined.
printf '%s\n' 'changequote([,])define(A,1)ifdef([A],[yes],[no])' > "$in"
check_out m4/ifdef "yes" m4 "$in"

# A fatal parse error (unterminated argument list) must diagnose and exit 1 —
# m4's error path reaches stderr and the exit status on this substrate.
check_rc m4/badinput 1 sh -c "echo 'define(' | m4"

rm -f "$in" "$inc"
