# Run all the tests.
#
# There is also a pair of files bst-t01-minimal.{aux,bst}.  Running BibTeX
# and beastie on bst-t01-minimal.aux produce roughly the same results,
# (although BibTeX does not seem to define type$ for unknown entry
# types, as it seems to promise).
#
# This file is part of Beastie <https://purl.org/nxg/dist/beastie>
# SPDX-FileCopyrightText: 2024 Norman Gray <https://nxg.me.uk>
# SPDX-License-Identifier: BSD-2-Clause

BEASTIE=../beastie

CC=@CC@
CPPFLAGS=@CPPFLAGS@
CFLAGS=@CFLAGS@ -std=c99
LDFLAGS=@LDFLAGS@
LIBS=@LIBS@

CTESTS=test-unicode test-util

# don't include a dependency on $(BEASTIE) here:
# this can induce a spurious attempt to remake it at test time
# (possibly depending on the default make rules?)
check: $(CTESTS)
	$(BEASTIE) s7unit.scm
	@for exe in $(CTESTS); do echo "$$exe ..."; ./$$exe; done

# The following is an alternative way of running the tests
check-shell:
	@all_ok=:; \
	  for t in test-*.scm; do \
	    echo $$t...; $(BEASTIE) $$t || all_ok=false; done; \
	  if $$all_ok; then echo "[30;42m OK [0m"; else echo "[37;41m FAILED [0m"; fi; \
	  $$all_ok

# Tests of different ways of calling beastie
tmp-call-bib.bib: $(BEASTIE)
	$(BEASTIE) bib-t01-simple.bib >$@
tmp-call-bib.json: $(BEASTIE)
	$(BEASTIE) -O json bib-t01-simple.bib >$@
tmp-call-aux.aux:
	printf "\\relax\n\\citation{*}\n\\bibstyle{bst-t01-minimal}\n\\bibdata{bib-t01-simple}\n" >$@
# the following should read tmp-call-aux.aux, and write to tmp-call-aux.bbl (not to stdout)
tmp-call-aux.bbl: tmp-call-aux.aux $(BEASTIE)
	$(BEASTIE) tmp-call-aux

# The bibtex-comparison target is only partially for testing beastie.
# It is principally for testing that the testcases in
# $(BSTTESTS:=.scm) either do match BibTeX,
# or don't in ways that we accept or prefer.
#
# Thus, while all of the test-btxhak-internal tests are expected to
# match between beastie and BibTeX, quite a few of the comparisons in
# test-authorlist.scm don't match.
BSTTESTS=test-btxhak-internal test-authorlist

bibtex-comparison: tmp-comparison/out.txt
	cat $<
	@echo "See $(BSTTESTS:%=tmp-comparison/%.blg) for details"

# extract (testbtx ...) forms to foo.inc,
# include this in bst-testcases.bst.in to make foo.bst
tmp-comparison/%.bst: %.scm extract-bst-tests.scm bst-testcases.bst.in
	test -d tmp-comparison || mkdir tmp-comparison
	printf '\\bibstyle{%s}\n' $* >tmp-comparison/$*.aux
	$(BEASTIE) extract-bst-tests.scm <$< >tmp-comparison/$*.inc
	sed '/FUNCTION.*run.test.cases/r 'tmp-comparison/$*.inc bst-testcases.bst.in >tmp-comparison/$*.bst

# run foo.bst through bibtex
tmp-comparison/out.txt: $(BSTTESTS:%=tmp-comparison/%.bst)
	cd tmp-comparison; \
	  for aux in $(BSTTESTS); do \
	    echo "Match with $$aux..."; \
	    bibtex $$aux >$$aux.stdout; \
	    grep '^Result' $$aux.blg; \
	  done >out.txt || true

# bundle all of the test material
# (used by the nonicu/beastie target in ../Makefile)
build/dist-test.tar: .
	test -d build || mkdir build
	tar cf $@ Makefile.in s7unit.scm test-*.scm alltypes.bib \
	  aux-* bib-* bibinc* bst-* gruber-*.md extract-bst-tests.scm \
	  c-unit.c c-unit.h $(CTESTS:=.c)

c-unit.o: c-unit.c
	$(CC) -c $(CPPFLAGS) -g -o $@ c-unit.c

# for $(CTESTS)
test-%: test-%.c c-unit.o ../libbeastie.a
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -g -I.. -o $@ $< c-unit.o -L.. -lbeastie $(LIBS)

clean:
	rm -Rf tmp-* $(CTESTS) *.o build
