Commit b5441439 authored by Pietro Abate's avatar Pietro Abate

add python based unit tests

parent c9ad3e5c
include "tests/cql/TREE/type-TREE.cd";;
include "type-TREE.cd";;
(* include "tests/cql/TREE/q4.cd";; *)
(* How many top-level sections are in Book1? *)
......
cduceexec () {
file=$1
exec=$2
base=`basename $file`
dir=`dirname $file`
mkdir -p _tests/$dir
$exec -I $dir -c --verbose $file > _tests/$dir/${base%cd}cdi
echo "$exec -I $dir -c --verbose $file > _tests/$dir/${base%cd}cdi"
}
TESTSMISC="misc/addrbook.cd misc/biblio.cd\
misc/fixml4.3v20020920.cd misc/funs.cd misc/IFX130.cd\
misc/integers.cd misc/lazy.cd misc/mc.cd\
misc/memento.cd misc/notes.cd misc/ns.cd\
misc/overloading.cd misc/overloading2.cd misc/patterns.cd\
misc/security.cd misc/str.cd misc/stress_opt_arg.cd\
misc/stress_opt_seq.cd misc/vouill.cd misc/web.cd\
misc/wp.cd misc/xtrans.cd misc/ref.cd misc/xhtml.cd"
TESTSCQL="cql/TREE/*.cd cql/XMP/*.cd"
TESTSXSLRMARK="xsltmark/identity.cd"
TESTSTDLIB="stdlib/*.cd"
TESTPOLY="poly/*.cd"
for f in $TESTSMISC $TESTSXSLRMARK; do
cduceexec $f cduce
done
for f in $TESTSCQL; do
dir=`dirname $f`
file=`basename $f`
for i in `find $dir -name $file`; do
cduceexec $i cduce
done
done
for f in $TESTSTDLIB; do
dir=`dirname $f`
file=`basename $f`
for i in `find $dir -name $file`; do
cduceexec $i ../cduce
done
done
#Name: unique id for the test
#Comment: A comment ...
#Expected: file containing the expected result
#Input: input files. always appended at the end of the $Cmd string
#Cmd: command to be run
#Type: tpye of the input/output format. Available text
#Printer: printer to display differences. Available text
Name: misc
Group: misc
Input: misc/*.cd
Cmd: cduce -c --verbose -I misc
#!/usr/bin/python
import unittest
from subprocess import Popen, PIPE
import difflib
import uuid
import os,sys,time
import argparse
from itertools import groupby, ifilter
import yaml, urllib
import filecmp
import cStringIO
import glob
from sets import Set
def diff_aux(expectedfile,resultfile,parser):
if filecmp.cmp(expectedfile,resultfile) :
return True
else :
expected = parser(expectedfile)
result = parser(resultfile)
matcher = difflib.SequenceMatcher(None, expected, result)
if matcher.ratio() == 1.0 :
print "Warning ! Expected result and actual result are not identical."
print "The order is not the same."
return True
else :
if verbose > 2 :
diff = difflib.unified_diff(open(expectedfile).readlines(),open(resultfile).readlines())
sys.stdout.writelines(list(diff))
return False
def parse822(f) :
cnf_fields = []
def cnf(k,s) :
if k in cnf_fields :
l = s.split(',')
ll = map(lambda s : s.split('|'), l)
return ll
else :
return s
records = []
for empty, record in groupby(ifilter(lambda s: not s.startswith('#'),open(f)), key=str.isspace):
if not empty:
l = map(lambda s : tuple(s.split(': ')), record)
l = map(lambda (k,v) : (k,v.rstrip()), l)
# we ignore the preamble here ...
if ('preamble' not in l[0]) and (len(l) > 0):
pairs = ((k, cnf(k,v.strip())) for k,v in l)
records.append((pairs))
l = sorted([sorted(e) for e in records])
return [frozenset(e) for e in l]
def parsetext(f) :
l = sorted(open(f).readlines())
return [frozenset(sorted(e)) for e in l]
def diff_text(expectedfile,resultfile):
return diff_aux(expectedfile,resultfile,parsetext)
def test_application(self,expected_file,cmd,diff):
uid = uuid.uuid1()
if not os.path.exists("tmp"):
os.makedirs("tmp")
output_file = "tmp/%s.cdi" % uid
output = open(output_file,'w')
p = Popen(cmd, stdout=output)
p.communicate()
d = diff(expected_file,output_file)
output.close()
os.remove(output_file)
self.assertTrue(d)
def suite(f,runtest,rungroup):
suite = unittest.TestSuite()
groups = Set()
tests = Set()
groupFound=False
testsFound=False
for stanza in parse822(f):
s = dict(stanza)
groups.add(s['Group'])
if (len(runtest) == 0 and len(rungroup) == 0) :
suite.addTest(CDuceTests(s))
elif s['Name'] in runtest :
testFound=True
suite.addTest(CDuceTests(s))
elif len(rungroup) > 0 and s['Group'] in rungroup :
groupFound=True
suite.addTest(CDuceTests(s))
if len(runtest) != 0 and testFound == False :
print "Test(s) [%s] Not found" % (','.join(str(p) for p in runtest))
print "Tests available [%s]" % (','.join(str(p) for p in tests))
if len(rungroup) != 0 and groupFound == False :
print "Group(s) [%s] Not found" % (','.join(str(p) for p in rungroup))
print "Groups available [%s]" % (','.join(str(p) for p in groups))
return suite
class CDuceTests(unittest.TestCase):
def __init__(self, test):
super(CDuceTests, self).__init__()
self.name = test['Name']
self.comment = test['Comment'] if 'Comment' in test else None
self.expected = test['Expected'] if 'Expected' in test else None
self.inputfiles = glob.glob(test['Input'])
self.cmd = test['Cmd'].split(' ')
self.difftype = diff_text
if self.expected is not None :
if os.path.isfile(self.expected) and (len(self.inputfiles) == 1) :
expected = self.expected
else :
if os.path.isdir(self.expected) and len(self.inputfiles) > 1 :
expected = "%s" % (self.expected)
else :
print "1"
exit(-1)
else :
if len(self.inputfiles) == 1 :
expected = "_tests/%si" % infile
else :
expected = "_tests"
self.expected = expected
def shortDescription(self):
if self.comment :
return "Description = " + self.comment + "\n" + ("Cmd = ") + " ".join(self.cmd) + "\nExpected file = %s" % self.expected + "\n"
else :
return "Test = %s" % self.name + "\n" + ("Cmd = ") + " ".join(self.cmd) + "\nExpected file = %s" % self.expected + "\n"
def runTest(self):
for infile in self.inputfiles :
cmd = self.cmd + [ infile ]
expected = "%s/%si" % (self.expected,infile)
print cmd
print expected
test_application(self,expected,cmd,self.difftype)
def main():
global verbose
parser = argparse.ArgumentParser(description='CDuce unit test')
parser.add_argument('-v', '--verbose', type=int, nargs=1, default=2)
parser.add_argument('--runtest', nargs='+', default=[])
parser.add_argument('--rungroup', nargs=1, default=[])
parser.add_argument('inputfile', type=str, nargs=1, help="test file")
args = parser.parse_args()
verbose = args.verbose
unittest.TextTestRunner(verbosity=args.verbose).run(suite(args.inputfile[0],args.runtest,args.rungroup))
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment