test.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
#!/usr/bin/env bash
set -euo pipefail
### Test cases
#
run_all_tests() {
run_test helloworld.xyw -- assert_output_equals $'Hello, World!\n'
run_test d6.xyw -- assert_output_in_range 1 6
run_test on-error.xyw -- assert_output_equals $'Error: Division by zero.\n'
run_test print-args.xyw test1 test2 -- assert_output_equals $'print-args.xim, test1, test2\n'
run_test factorial8.xyw -- assert_output_equals $'120\n'
run_test factorial16.xyw -- assert_output_equals $'5040\n'
run_test fileops.xyw -- assert_output_equals $'This is a test file.\nIt has multiple lines.\n'
# Cleanup test file
rm -f "$EXAMPLES_DIR/test.txt"
run_test launcher.xyw -- assert_output_equals $'Executing: print-args.xim hello world\nprint-args.xim, hello, world\nExecuting: helloworld.xim\nHello, World!\nDone!\n'
run_test cmdexec.xyw -- assert_output_matches $'MINGW|Linux|BSD|Darwin'
}
### Helper functions
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EXAMPLES_DIR="$ROOT_DIR/examples"
fail() {
echo "ERROR: $*" >&2
exit 1
}
dump_file_if_exists() {
local label="$1"
local path="$2"
if [[ -f "$path" ]]; then
(tr -d '\r' <"$path" | cat -v >&2 || true)
fi
}
need_file() {
local path="$1"
[[ -f "$path" ]] || fail "Missing file: $path"
}
build_xyw() {
# The Makefile relies on GNU make extensions (ifeq, :=, $(shell ...)),
# which BSD make (e.g. NetBSD/FreeBSD's default `make`) cannot parse,
# use "gmake" if available to force GNU make if available
local make_cmd=""
if command -v gmake >/dev/null 2>&1; then
make_cmd="gmake"
elif command -v make >/dev/null 2>&1; then
make_cmd="make"
fi
if [[ -n "$make_cmd" ]]; then
(cd "$ROOT_DIR" && "$make_cmd" xyw)
else
echo "WARN: 'make'/'gmake' not found; using existing xyw binary if present." >&2
fi
}
clean_examples_images() {
# Only clean generated images in examples/.
# (Keeps repository-wide behavior conservative.)
shopt -s nullglob
local images=("$EXAMPLES_DIR"/*.xim)
shopt -u nullglob
if ((${#images[@]} > 0)); then
rm -f "${images[@]}"
fi
}
find_xyw_bin() {
if [[ -x "$ROOT_DIR/xyw" ]]; then
echo "$ROOT_DIR/xyw"
return 0
fi
if [[ -x "$ROOT_DIR/xyw.exe" ]]; then
echo "$ROOT_DIR/xyw.exe"
return 0
fi
fail "Could not find executable xyw binary (expected $ROOT_DIR/xyw or $ROOT_DIR/xyw.exe)."
}
assemble_in_examples_dir() {
local xyw_bin="$1"
local src_basename="$2" # e.g. helloworld.xyw
need_file "$EXAMPLES_DIR/$src_basename"
local asm_log="$TMPDIR/assemble.${src_basename%.xyw}.log"
if ! (cd "$EXAMPLES_DIR" && "$xyw_bin" "$src_basename" >"$asm_log" 2>&1); then
dump_file_if_exists "Assembly output" "$asm_log"
fail "Assembly failed for $src_basename"
fi
# Show assembler output, but keep this function's stdout reserved for the
# produced .xim basename (so callers can capture it).
dump_file_if_exists "Assemble $src_basename" "$asm_log"
local img_basename="${src_basename%.xyw}.xim"
need_file "$EXAMPLES_DIR/$img_basename"
echo "$img_basename"
}
# Set by run_image_capture_stdout after each run, for assertions that
# care about the process exit code (e.g. assert_exit_code) or stderr
# (e.g. assert_stderr_contains-style checks).
LAST_EXIT_CODE=0
LAST_STDERR_FILE=""
# Set to "1" by run_test before calling run_image_capture_stdout when the
# chosen assertion function is expected to handle a non-zero exit code
# itself (e.g. assert_exit_code). Otherwise a non-zero exit is treated as
# a hard failure, same as before.
ALLOW_NONZERO_EXIT="0"
run_image_capture_stdout() {
local xyw_bin="$1"
local img_basename="$2" # e.g. helloworld.xim
local out_file="$3"
shift 3
local run_args=("$@")
need_file "$EXAMPLES_DIR/$img_basename"
local run_log="$TMPDIR/run.${img_basename}.log"
local rc=0
(cd "$EXAMPLES_DIR" && "$xyw_bin" "$img_basename" "${run_args[@]+"${run_args[@]}"}" >"$out_file" 2>"$run_log") || rc=$?
LAST_EXIT_CODE="$rc"
LAST_STDERR_FILE="$run_log"
if [[ "$rc" -ne 0 && "$ALLOW_NONZERO_EXIT" != "1" ]]; then
dump_file_if_exists "Runtime stderr" "$run_log"
dump_file_if_exists "Runtime stdout" "$out_file"
fail "Execution failed for $img_basename (exit code $rc)"
fi
}
normalize_crlf() {
local in_file="$1"
local out_file="$2"
tr -d '\r' <"$in_file" >"$out_file"
}
### Assertion functions
assert_file_equals() {
local actual_file="$1"
local expected_file="$2"
if ! cmp -s "$actual_file" "$expected_file"; then
echo "--- Expected (cat -v) ---" >&2
cat -v "$expected_file" >&2 || true
echo "--- Actual (cat -v) ---" >&2
cat -v "$actual_file" >&2 || true
fail "Output did not match expectation."
fi
}
assert_output_equals() {
local stdout_file="$1"
local expected_string="$2"
local expected_file
expected_file="$TMPDIR/expected.$$.${RANDOM}.txt"
printf '%s' "$expected_string" >"$expected_file"
assert_file_equals "$stdout_file" "$expected_file"
rm -f "$expected_file"
}
assert_output_in_range() {
local stdout_file="$1"
local min_value="$2"
local max_value="$3"
local value
value="$(tr -d '\n\t ' <"$stdout_file")"
if [[ ! "$value" =~ ^-?[0-9]+$ ]]; then
echo "--- Actual (cat -v) ---" >&2
cat -v "$stdout_file" >&2 || true
fail "Output is not an integer: '$value'"
fi
if (( value < min_value || value > max_value )); then
echo "--- Actual (cat -v) ---" >&2
cat -v "$stdout_file" >&2 || true
fail "Output out of range (expected ${min_value}..${max_value}): $value"
fi
}
assert_output_in_set() {
local stdout_file="$1"
shift
local candidates=("$@")
if ((${#candidates[@]} == 0)); then
fail "assert_output_in_set requires at least one candidate string"
fi
local candidate expected_file
for candidate in "${candidates[@]}"; do
expected_file="$TMPDIR/expected_set.$$.${RANDOM}.txt"
printf '%s' "$candidate" >"$expected_file"
if cmp -s "$stdout_file" "$expected_file"; then
rm -f "$expected_file"
return 0
fi
rm -f "$expected_file"
done
echo "--- Actual (cat -v) ---" >&2
cat -v "$stdout_file" >&2 || true
echo "--- Expected one of ---" >&2
for candidate in "${candidates[@]}"; do
printf '%s\n' "$candidate" | cat -v >&2 || true
echo "---" >&2
done
fail "Output did not match any candidate."
}
assert_output_matches() {
local stdout_file="$1"
local pattern="$2"
local content
content="$(cat "$stdout_file")"
if [[ ! "$content" =~ $pattern ]]; then
echo "--- Actual (cat -v) ---" >&2
cat -v "$stdout_file" >&2 || true
fail "Output did not match pattern: $pattern"
fi
}
assert_exit_code() {
local stdout_file="$1"
local expected_code="$2"
if [[ "$LAST_EXIT_CODE" -ne "$expected_code" ]]; then
echo "--- Actual stdout (cat -v) ---" >&2
dump_file_if_exists "stdout" "$stdout_file"
dump_file_if_exists "stderr" "$LAST_STDERR_FILE"
fail "Exit code mismatch: expected $expected_code, got $LAST_EXIT_CODE"
fi
}
assert_output_equals_file() {
local stdout_file="$1"
local expected_path="$2"
local resolved=""
if [[ "$expected_path" == /* && -f "$expected_path" ]]; then
resolved="$expected_path"
elif [[ -f "$EXAMPLES_DIR/$expected_path" ]]; then
resolved="$EXAMPLES_DIR/$expected_path"
elif [[ -f "$ROOT_DIR/$expected_path" ]]; then
resolved="$ROOT_DIR/$expected_path"
else
fail "Expected output file not found: $expected_path"
fi
assert_file_equals "$stdout_file" "$resolved"
}
### Test runner
# Usage: run_test src.xyw [run_args...] -- assert_fn [assert_args...]
# If no '--' is provided, the second argument is treated as assert_fn.
run_test() {
local src_basename="$1"
shift
local run_args=()
local assert_fn=""
local assert_args=()
# Parse arguments: collect run_args until '--', then assert_fn and assert_args
while (($# > 0)); do
if [[ "$1" == "--" ]]; then
shift
break
fi
run_args+=("$1")
shift
done
# If we found '--', remaining args are assert_fn + assert_args
# Otherwise, run_args[0] is the assert_fn and rest are assert_args
if (($# > 0)); then
assert_fn="$1"
shift
assert_args=("$@")
else
# No '--' found; first element of run_args is assert_fn
if ((${#run_args[@]} == 0)); then
fail "Missing assertion function for $src_basename"
fi
assert_fn="${run_args[0]}"
assert_args=("${run_args[@]:1}")
run_args=()
fi
if ! declare -F "$assert_fn" >/dev/null 2>&1; then
fail "Assertion function not found: $assert_fn"
fi
need_file "$EXAMPLES_DIR/$src_basename"
local img_basename
img_basename="$(assemble_in_examples_dir "$XYW_BIN" "$src_basename")"
local raw_out="$TMPDIR/${src_basename%.xyw}.raw"
local norm_out="$TMPDIR/${src_basename%.xyw}.norm"
if [[ "$assert_fn" == "assert_exit_code" ]]; then
ALLOW_NONZERO_EXIT="1"
else
ALLOW_NONZERO_EXIT="0"
fi
run_image_capture_stdout "$XYW_BIN" "$img_basename" "$raw_out" "${run_args[@]+"${run_args[@]}"}"
normalize_crlf "$raw_out" "$norm_out"
"$assert_fn" "$norm_out" "${assert_args[@]+"${assert_args[@]}"}"
echo "-> PASS: $src_basename"
}
### Main
main() {
need_file "$ROOT_DIR/Makefile"
build_xyw
clean_examples_images
TMPDIR="$(mktemp -d 2>/dev/null || mktemp -d -t xywtest)"
trap "rm -rf \"$TMPDIR\"" EXIT
XYW_BIN="$(find_xyw_bin)"
echo ""
run_all_tests
echo -e "\n=> All tests passed."
}
main "$@"
|