# -*- makefile -*- mode for emacs
# @(#) $Id: Makefile,v 1.4 2003/06/08 10:51:20 davidcok Exp $

# Copyright (C) 1998-2001 Iowa State University

# This file is part of JML

# JML is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# JML is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with JML; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

#
# Developer's Makefile for testing JML specs
#

# Author: David Cok  

## ----------------------------------------------------------------- 
## At the bottom of this file we import $(TOPDIR)/Make.ProjDefs.  See the
## that file for substantial documentation.
## ----------------------------------------------------------------- 

TOPDIR =	../../../../..

PACKAGE = 	org/multijava/mjdoc/testcase/misc2

## -------------------------------------------------------------
## Goals

default:	runtests

clean-this:	local-clean

local-clean:
	$(RM) *.*-ckd core file-list.junk *-expected.old \
		*.html package-list stylesheet.css
	$(RM) -r $(JAVADOCLOC)
	-$(RM) `cat .cvsignore`


## -------------------------------------------------------------
## Testing


JMLFLAGS = 

DIFF = diff -b

.SUFFIXES: .html .html-expected .s .s-expected .java-x

all: check

check: runtests-this-junit

runtests-this-junit: junittests

runtests-this-nojunit: default-option-tests nondefault-option-tests

junittests-before: testprep buildtests

# javadoc executable
JAVADOC = javadoc -xnodate

# Location of where to put html files - this should be a TEMPORARY directory
# DON'T USE . (i.e., current directory) - SEE the local-clean target

JAVADOCLOC = javadocs

DEFAULTFLAGS = -private -quiet -nonavbar

TESTFILES = DefaultOptionsTestSuite NonDefaultOptionsTestSuite 

MJDOC = java org.multijava.mjdoc.Main

# The run of MJDOC in this target acts on all Test* files that have
# a common set of options - so nothing that has a JMLFLAGS line in the
# file, as well as nothing in the skip file.

NON_DEFAULT_TESTS = grep --files-with-match JMLFLAGS `ls Test*.java | grep -v -f skip `

DEFAULT_OPTION_TESTS = grep --files-without-match JMLFLAGS `ls Test*.java | grep -v -f skip `


default-option-tests: testprep 
	@$(MJDOC) -xnodate -d $(JAVADOCLOC) ${DEFAULT_FLAGS} \
		`cat listTestsWithDefaultOptions` 
	@for f in `cat listTestsWithDefaultOptions` ; \
	do \
		if [ ! "$(QUIET)" ]; then echo Checking $${f}; \
					  else printf "." ; fi; \
			$(MAKE) $${f}-check ;\
	done
	@if [ "$(QUIET)" ]; then echo; fi;
	@echo End testing javafiles

# Files with custom jmldoc options have to be processed individually
nondefault-option-tests: testprep
	-@for f in `cat listTestsWithNonDefaultOptions` ;\
	do \
		$(MAKE) $${f}-test ;\
	done
	@if [ "$(QUIET)" ]; then echo; fi;

%.java-check:
	@for f in `(grep JMLOUTPUT $(@F:%.java-check=%.java) \
		|| echo $(@F:%.java-check=%.html) ) \
		| sed -e 's?//#JMLOUTPUT:??'`; \
	do \
	    grep -q JMLOUTPUT $(@F:%.java-check=%.java) && \
		(if [ ! "$(QUIET)" ]; then echo ... Comparing $${f} ; fi; ); \
	    if [ -e $${f}-expected ]; then \
		./diffgen $(JAVADOCLOC)/$${f} $${f}-expected ;\
		$(RM) temp1 temp2 ;\
	    else echo ... Updating $${f}-expected; \
		mv $(JAVADOCLOC)/$${f} $${f}-expected ;\
	    fi; \
	done


.PHONY: testprep
testprep: 
	@rm -rf $(JAVADOCLOC)
	@touch skip
	@($(NON_DEFAULT_TESTS) > listTestsWithNonDefaultOptions) \
		|| touch listTestsWithNonDefaultOptions
	@($(DEFAULT_OPTION_TESTS) > listTestsWithDefaultOptions ) \
		|| touch listTestsWithDefaultOptions

.PHONY: individual-tests
individual-tests: testprep
	-@for f in `ls Test*.java | grep -v -f skip` ; \
	do \
		$(MAKE) $${f}-test ; \
	done
	@if [ "$(QUIET)" ]; then echo; fi;
	@echo End testing javafiles

%.java-test: 
	@if [ ! "$(QUIET)" ]; \
		then echo Checking $(@F:%.java-test=%.java) ... ; \
		else printf "." ; \
		fi
	@$(MJDOC) -xnodate -d $(JAVADOCLOC)   \
		`(grep JMLFLAGS $(@F:%.java-test=%.java) \
		|| echo ${DEFAULTFLAGS} ) \
		| sed -e 's?//#JMLFLAGS:??' \
		| sed -e "s?\\$$JDKDOCDIR?${JDKDOCDIR}?" ` \
		$(@F:%.java-test=%.java)
	@$(MAKE) $(@F:%.java-test=%.java-check)

%.html-update: 
	rm $(@F:%.html-update=%.html-expected)
	$(MAKE) $(@F:%.html-update=%.java-test)


create expected:
	-for f in `ls *.java` ; \
	do \
	  echo Checking $$f ... ; \
	  $(MV) $${f}-expected $${f}-expected.old ; \
	  $(JML) $(JMLFLAGS) $$f 2>&1 \
	     | $(PATHTOFILE) > $${f}-expected ; \
	$(DIFF) $${f}-expected $${f}-expected.old && echo 'Unchanged!';\
	done 2>&1 | tee create.out-ckd


## -------------------------------------------------------------
## Global defaults

include $(TOPDIR)/Make.ProjDefs

## overrides

# use jmldoc for the "jml" program in this directory
JML = java org.jmlspecs.jmldoc.Main

