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