]> rtime.felk.cvut.cz Git - wvtest.git/blob - python/wvtest.py
Import wvtest for python from eqldata project.
[wvtest.git] / python / wvtest.py
1 import traceback
2 import os.path
3 import re
4 import sys
5
6 _registered = []
7 _tests = 0
8 _fails = 0
9
10 def wvtest(func):
11     ''' Use this decorator (@wvtest) in front of any function you want to run
12         as part of the unit test suite.  Then run:
13             python wvtestmain.py path/to/yourtest.py
14         to run all the @wvtest functions in that file.
15     '''
16     _registered.append(func)
17     return func
18
19 def _result(msg, tb, code):
20     global _tests, _fails
21     _tests += 1
22     if code != 'ok':
23         _fails += 1
24     (filename, line, func, text) = tb
25     filename = os.path.basename(filename)
26     msg = re.sub(r'\s+', ' ', str(msg))
27     sys.stderr.flush()
28     print '! %-70s %s' % ('%s:%-4d %s' % (filename, line, msg),
29                           code)
30     sys.stdout.flush()
31     
32
33 def _check(cond, msg = 'unknown', tb = None):
34     if tb == None: tb = traceback.extract_stack()[-3]
35     if cond:
36         _result(msg, tb, 'ok')
37     else:
38         _result(msg, tb, 'FAILED')
39     return cond
40
41 def _code():
42     (filename, line, func, text) = traceback.extract_stack()[-3]
43     text = re.sub(r'^\w+\((.*)\)$', r'\1', text);
44     return text
45
46 def WVPASS(cond = True):
47     ''' Throws an exception unless cond is true. '''
48     return _check(cond, _code())
49
50 def WVFAIL(cond = True):
51     ''' Throws an exception unless cond is false. '''
52     return _check(not cond, 'NOT(%s)' % _code())
53
54 def WVPASSEQ(a, b):
55     ''' Throws an exception unless a == b. '''
56     return _check(a == b, '%s == %s' % (repr(a), repr(b)))
57
58 def WVPASSNE(a, b):
59     ''' Throws an exception unless a != b. '''
60     return _check(a != b, '%s != %s' % (repr(a), repr(b)))
61
62 def WVPASSLT(a, b):
63     ''' Throws an exception unless a < b. '''
64     return _check(a < b, '%s < %s' % (repr(a), repr(b)))
65
66 def WVPASSLE(a, b):
67     ''' Throws an exception unless a <= b. '''
68     return _check(a <= b, '%s <= %s' % (repr(a), repr(b)))
69
70 def WVPASSGT(a, b):
71     ''' Throws an exception unless a > b. '''
72     return _check(a > b, '%s > %s' % (repr(a), repr(b)))
73
74 def WVPASSGE(a, b):
75     ''' Throws an exception unless a >= b. '''
76     return _check(a >= b, '%s >= %s' % (repr(a), repr(b)))
77
78