#!/bin/bash
# 
#  File   : /home/cvs/cvsroot/projects/visualstate/scope/test/parser/run,v
#  Comment: Run parser test suite
#           nontype.vsr should parse but should fail semantic checks.
#
#  The idea is that tool run twice on the same file (in sense of double
#  application - application on the result) should produce an identical file.
#  Moreover no syntax error should be detected and only small differences
#  between original and processed vsp files (in -v test). 
#
#  run,v 1.12 2002/10/23 07:59:28 wasowski Exp


  # Initialize the testing environment (SANDBOX)

  if [[ $1 == "" ]]; then SCOPE="../../scope"; else SCOPE=$1; fi

  DIFFOPT=-qbBdw
  SANDBOX1=sandbox1 
  SANDBOX2=sandbox2 
  RESULTS=test_results
  PROJECTS="actions01.vsp actions02.vsp actions03.vsp actions04.vsp do01.vsp
  do02.vsp minimal.vsp systems03.vsp systems04.vsp systems05.vsp timer01.vsp
  unit01.vsp vars01.vsp AvSystem.vsp climate.vsp Clockradio.vsp Lift.vsp
  mobileflat.vsp peer.vsp Project.vsp nontyped.vsr" 

  echo > $RESULTS
 
  # function cleaning the disk from temporary files
  
  function tidyup {
  
  	rm -f $SANDBOX1/*.* 2>&1 >> $RESULTS 
 	rm -f $SANDBOX2/*.* 2>&1 >> $RESULTS 

  }
  
  # function running a scope tool with redirected result
  # $1 = scope options $2 = input file $3 = target directory

  function scopeit { 
 	
  	$SCOPE $1 $2 -d$3 2>&1 >> $RESULTS 
  }

  # compares files produced in two sandboxes
	
  function diff_sandboxes {
  
  	diff --exclude=CVS $DIFFOPT $SANDBOX1 $SANDBOX2 2>&1 1>> $RESULTS
  }

  function diff_files {
  	
	diff $DIFFOPT $1 $2 2>&1 1>> $RESULTS
  }

  # function performing a single anonymizer test

  function anontest {
        
	echo                  >> $RESULTS
  	echo "Testing: -a $1" >> $RESULTS 
	scopeit "-a" $1 $SANDBOX1
	
	if [ -e $SANDBOX1/*.vsp ]; then 
		#should only be 1 vsp file in sandbox1
		scopeit "-a" "$SANDBOX1/*.vsp" $SANDBOX2;
	else 
		#or if there aren't any should be exactly 1 vsr.
		scopeit "-a" "$SANDBOX1/*.vsr" $SANDBOX2; 
	fi

	diff_sandboxes
	tidyup
  }

  #function performing -v (reformatter+typechecker) test

  function vtest {
        
	echo                  >> $RESULTS 
  	echo "Testing: -v $1" >> $RESULTS 
	echo                  >> $RESULTS 
	scopeit "-v" $1 $SANDBOX1

	if [ -e $SANDBOX1/*.vsp ]; then
		#should be at most one vsp file in sandbox1
		echo "small differences in tag expected" 2>&1 >> $RESULTS
		diff_files $1 "$SANDBOX1/*.vsp"
		scopeit "-v" "$SANDBOX1/*.vsp" $SANDBOX2;
	else
		#if no vsp files then at most one vsr file
		diff_files $1 "$SANDBOX1/*.vsr"
		scopeit "-v" "$SANDBOX1/*.vsr" $SANDBOX2;
	fi

	diff_sandboxes
	tidyup
  }
		
  tidyup 

  echo "forwarding results to test_results"

  echo "This is not an automatic test. I know it is bad, but "  >> $RESULTS 
  echo "currently I do not have cheap ideas for better tests. " >> $RESULTS
  echo "You should read the file below and check if differences">> $RESULTS
  echo "only appear in -v test section between file names "     >> $RESULTS
  echo "inside of vsp filed."                                   >> $RESULTS
  echo                                                          >> $RESULTS


  for PNAME in $PROJECTS; do
  	  vtest $PNAME
	  anontest $PNAME
  done

  tidyup

  echo "Parser test done."
  echo "Parser test done." >> $RESULTS


