# $Source: /cvsroot/flexml/flexml/testbed/Makefile,v $

# regression testbed Makefile

#####################################################
#  Fixed definitions -- do not modify for new tests
#####################################################

include ../Makefile.defs

MAKEUTILS_DIR = .

# If UNIT_TESTS is     test_foo
# and test_foo_out is  bar baz
# Then the created files (that need to be deleted by 'make clean'
# will be
# test_foo  foo.std{err,out} foo.std{err,out}.diff  bar baz  bar.diff baz.diff
TEST_INTERMEDIATES = \
	$(UNIT_TESTS) \
	$(UNIT_TESTS:test_%=%.stdout) $(UNIT_TESTS:test_%=%.stdout.diff) \
	$(UNIT_TESTS:test_%=.test_%.stdout.expected) \
	$(UNIT_TESTS:test_%=%.stderr) $(UNIT_TESTS:test_%=%.stderr.diff) \
	$(UNIT_TESTS:test_%=.test_%.stderr.expected) \
	$(foreach var,$(UNIT_TESTS:%=%_out),$(foreach o,$($(var)),$o $(o).diff))

# Compares test.out.std[out,err] with expected values
COMPARE_OUT = $(MAKEUTILS_DIR)/CompareOut.pl

#CC = /usr/bin/gcc-3.3 -Wall
#CFLAGS = -O2 -g
#FLEXDEBUG = -d

FLEXML = ../flexml -s ../skel -T../flexml-act

.PHONY : test
.PHONY : clean

#####################################################
#  test definitions -- add new tests here
#####################################################

EXES = \
	biparser \
	init_header \
	missing-att \
	multiple-att \
	mixed-enumatt \
	mixed-enumatt2 \
	mixed-stratt \
	mixed-stratt-def \
	mixed1 \
	multi-parser-run


INTERMEDIATES = $(EXES) $(EXES:%=%.c) $(EXES:%=%.l)

# leave this alphabetically sorted for easy scanning
UNIT_TESTS = \
	test_biparser \
	test_init_header \
	test_missing-att \
	test_multiple-att \
	test_mixed-enumatt \
	test_mixed-enumatt2 \
	test_mixed-stratt \
	test_mixed-stratt-def \
	test_mixed1 \
	test_multi-parser-run

test : $(UNIT_TESTS)

# Test init_header
test_init_header_cmd  = ./init_header < init_header.in
test_init_header_deps = init_header init_header.in

# Test missing REQUIRED attribute
test_missing-att_cmd  = ./missing-att < missing-att.in
test_missing-att_deps = missing-att missing-att.in

# Test multiply defined attribute
test_multiple-att_cmd  = ./multiple-att < multiple-att.in
test_multiple-att_deps = multiple-att multiple-att.in

# Test mixed1
test_mixed1_cmd  = ./mixed1 < mixed1.in
test_mixed1_deps = mixed1 mixed1.in

# Test mixed-enumatt (mixed content + enumerated attribute)
test_mixed-enumatt_cmd  = ./mixed-enumatt < mixed-enumatt.in
test_mixed-enumatt_deps = mixed-enumatt mixed-enumatt.in

# Test mixed-enumatt2 (mixed content + enumerated attribute, eval'ed in end tag)
test_mixed-enumatt2_cmd  = ./mixed-enumatt2 < mixed-enumatt2.in
test_mixed-enumatt2_deps = mixed-enumatt2 mixed-enumatt2.in

# Test mixed-stratt (mixed content + string attribute)
test_mixed-stratt_cmd  = ./mixed-stratt < mixed-stratt.in
test_mixed-stratt_deps = mixed-stratt mixed-stratt.in

# Test mixed-stratt (mixed content + default string attribute)
test_mixed-stratt-def_cmd  = ./mixed-stratt-def < mixed-stratt-def.in
test_mixed-stratt-def_deps = mixed-stratt-def mixed-stratt-def.in

# Test multi-parser-run (Run the parser several times on "different" files)
test_multi-parser-run_cmd = ./multi-parser-run multi-parser-run.in
test_multi-parser-run_deps = multi-parser-run multi-parser-run.in

# Test biparser (Have two parsers in the same code)
test_biparser_cmd = ./biparser biparser-one.in biparser-two.in
test_biparser_deps = biparser biparser-one.in biparser-two.in

biparser: biparser-one.dtd biparser-two.dtd biparser.precious.c
	for n in one two ; do \
	  $(FLEXML) -P$$n -H biparser-$$n.h -L biparser-$$n.dtd; \
	  $(FLEXML) -P$$n -S biparser-$$n.l -L biparser-$$n.dtd; \
	  $(FLEX) -s -L -P$${n}_ -obiparser-$$n.c biparser-$$n.l; \
	done
	$(CC) $(CFLAGS) -o biparser  \
              biparser-one.c biparser-two.c \
	      biparser.precious.c
CLEANFILES+= biparser-one.h biparser-one.l biparser-one.c \
	     biparser-two.h biparser-two.l biparser-two.c

init_header : init_header.act init_header.dtd init_header.h
	$(FLEXML) -b 1000 -A  -i$@.h -a$@.act $@.dtd
	$(FLEX) -s -L -o$@.c $@.l
	$(CC) $(CFLAGS) -o $@ $@.c

% : %.dtd %.act
	$(FLEXML) -b 1000 -A  -a$@.act $@.dtd
	$(FLEX) -s -L -o$@.c $@.l
	$(CC) $(CFLAGS) -o $@ $@.c

##############################################################
#  test build rules -- add override build rules here if needed
##############################################################

#mixed1: mixed1.dtd mixed1.act
#	$(FLEXML) -b 500 -A  -amixed1.act mixed1.dtd
#	$(FLEX) -s -L -o mixed1.c mixed1.l > mixed1.c
#	$(CC) $(CFLAGS) -o $@ mixed1.c


#####################################################
#  test infrastructure -- do not modify for new tests
#####################################################

clean :
	rm -f $(TEST_INTERMEDIATES) $(INTERMEDIATES) $(CLEANFILES)

#  When we are building test_foo, the variable $(TEST_BASE) is "foo"
test_% : TEST_BASE = $(@:test_%=%)

ifndef distribute_deps
# This will take a target and dependency list, and evaluate each dependency
# individually. This only needs to be used on dependency lists that are to
# be in an $(eval ...) function. This is a work-around for a bug in GNU Make
# 3.80, which fails when $(eval)'ing dependency lists longer than about 160
# characters.
#   1. target
#   2. dependency list
define distribute_deps
$(foreach dep,$(2),$(eval $(1) : $(dep)))
endef
endif

define unit_test_deps_t
$(1) : .$(1).stdout.expected
$(1) : .$(1).stderr.expected
$$(call distribute_deps,$(1),$($(1)_deps))
endef

$(foreach test,$(UNIT_TESTS),$(eval $(call unit_test_deps_t,$(test))))


# To build .test_foo.std{err,out}.expected --
#    If foo.std{err,out}.expected exists, symlink to it
#    Else, .test_foo.std{err,out}.expected is a new empty file
# These rules are invoked from rules in the Extradeps section.
# They must be two separate rules (else 'make test' not idempotent
# -- see bug #105)
.test_%.stderr.expected:
	@if [ -f  $(@:.test_%=%) ]; \
		then $(SYMLINK) $(@:.test_%=%) $@; \
		else touch $@; \
	fi
.test_%.stdout.expected:
	@if [ -f  $(@:.test_%=%) ]; \
		then $(SYMLINK) $(@:.test_%=%) $@; \
		else touch $@; \
	fi


# Unit test rule.  Expects:
# UNIT_TESTS = test_foo test_bar    (test_ prefix is necessary)
# test_foo_cmd  =  command to run
# test_foo_deps =  dependencies (i.e. inputs to the test, and the executable)
# test_foo_out  =  files and directories created by running the command
#                  with corresponding .expected files for comparison 

$(UNIT_TESTS) :
	@$(test_$(@:test_%=%)_cmd) > $(@:test_%=%).stdout \
	    2>$(@:test_%=%).stderr || true
	@$(PERL) $(COMPARE_OUT) -p '$($(@)_diff_prog)' -- \
	    $($(@)_diff_opts) $(@:test_%=%) $($(@)_out) | tee $@

