]> rtime.felk.cvut.cz Git - wvtest.git/commitdiff
Import wvtest for python from eqldata project.
authorAvery Pennarun <apenwarr@gmail.com>
Fri, 1 May 2009 00:46:08 +0000 (20:46 -0400)
committerAvery Pennarun <apenwarr@gmail.com>
Fri, 1 May 2009 01:09:44 +0000 (21:09 -0400)
Commit 5db96d703ab5a0c253d09d0cbcfd56646eaffbfe.

python/t/twvtest.py [new file with mode: 0644]
python/wvtest.py [new file with mode: 0644]
python/wvtestmain.py [new file with mode: 0755]

diff --git a/python/t/twvtest.py b/python/t/twvtest.py
new file mode 100644 (file)
index 0000000..8a092e3
--- /dev/null
@@ -0,0 +1,30 @@
+from wvtest import *
+
+last=None
+
+@wvtest
+def test1():
+    WVPASSLT(1, 2)
+    WVPASSLE(1, 1)
+    WVPASSGT(2, 1)
+    WVPASSGE(2, 2)
+    
+    # ensure tests run in the order they were declared
+    global last
+    WVPASSEQ(last, None)
+    last='test1'
+
+@wvtest
+def booga2():
+    # ensure tests run in the order they were declared
+    global last
+    WVPASSEQ(last, 'test1')
+    last='booga2'
+    
+@wvtest
+def booga1():
+    # ensure tests run in the order they were declared
+    global last
+    WVPASSEQ(last, 'booga2')
+    last='booga1'
+
diff --git a/python/wvtest.py b/python/wvtest.py
new file mode 100644 (file)
index 0000000..7f632fd
--- /dev/null
@@ -0,0 +1,78 @@
+import traceback
+import os.path
+import re
+import sys
+
+_registered = []
+_tests = 0
+_fails = 0
+
+def wvtest(func):
+    ''' Use this decorator (@wvtest) in front of any function you want to run
+        as part of the unit test suite.  Then run:
+           python wvtestmain.py path/to/yourtest.py
+       to run all the @wvtest functions in that file.
+    '''
+    _registered.append(func)
+    return func
+
+def _result(msg, tb, code):
+    global _tests, _fails
+    _tests += 1
+    if code != 'ok':
+       _fails += 1
+    (filename, line, func, text) = tb
+    filename = os.path.basename(filename)
+    msg = re.sub(r'\s+', ' ', str(msg))
+    sys.stderr.flush()
+    print '! %-70s %s' % ('%s:%-4d %s' % (filename, line, msg),
+                         code)
+    sys.stdout.flush()
+    
+
+def _check(cond, msg = 'unknown', tb = None):
+    if tb == None: tb = traceback.extract_stack()[-3]
+    if cond:
+       _result(msg, tb, 'ok')
+    else:
+       _result(msg, tb, 'FAILED')
+    return cond
+
+def _code():
+    (filename, line, func, text) = traceback.extract_stack()[-3]
+    text = re.sub(r'^\w+\((.*)\)$', r'\1', text);
+    return text
+
+def WVPASS(cond = True):
+    ''' Throws an exception unless cond is true. '''
+    return _check(cond, _code())
+
+def WVFAIL(cond = True):
+    ''' Throws an exception unless cond is false. '''
+    return _check(not cond, 'NOT(%s)' % _code())
+
+def WVPASSEQ(a, b):
+    ''' Throws an exception unless a == b. '''
+    return _check(a == b, '%s == %s' % (repr(a), repr(b)))
+
+def WVPASSNE(a, b):
+    ''' Throws an exception unless a != b. '''
+    return _check(a != b, '%s != %s' % (repr(a), repr(b)))
+
+def WVPASSLT(a, b):
+    ''' Throws an exception unless a < b. '''
+    return _check(a < b, '%s < %s' % (repr(a), repr(b)))
+
+def WVPASSLE(a, b):
+    ''' Throws an exception unless a <= b. '''
+    return _check(a <= b, '%s <= %s' % (repr(a), repr(b)))
+
+def WVPASSGT(a, b):
+    ''' Throws an exception unless a > b. '''
+    return _check(a > b, '%s > %s' % (repr(a), repr(b)))
+
+def WVPASSGE(a, b):
+    ''' Throws an exception unless a >= b. '''
+    return _check(a >= b, '%s >= %s' % (repr(a), repr(b)))
+
+
diff --git a/python/wvtestmain.py b/python/wvtestmain.py
new file mode 100755 (executable)
index 0000000..62adfce
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/python
+import wvtest
+import sys, imp, types, os, os.path
+import traceback
+
+def runtest(modname, fname, f):
+    print
+    print 'Testing "%s" in %s.py:' % (fname, modname)
+    try:
+        f()
+    except Exception, e:
+        print
+        print traceback.format_exc()
+        tb = sys.exc_info()[2]
+        wvtest._result(e, traceback.extract_tb(tb)[-1],
+                       'EXCEPTION')
+
+for modname in sys.argv[1:]:
+    if modname.endswith('.py'):
+        modname = modname[:-3]
+    print
+    print 'Importing: %s' % modname
+    wvtest._registered = []
+    mod = __import__(modname, None, None, [])
+
+    for t in wvtest._registered:
+       runtest(modname, t.func_name, t)
+                        
+print
+print 'WvTest: %d tests, %d failures.' % (wvtest._tests, wvtest._fails)