diff options
author | Gregory P. Smith <greg@mad-scientist.com> | 2005-06-06 17:30:22 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@mad-scientist.com> | 2005-06-06 17:30:22 (GMT) |
commit | 889bca0df11207fa94e618942bb5f1fcf32140fd (patch) | |
tree | a8f4a8f2fe9180fd57a4c4c6d66ac60b195594b6 | |
parent | 91116b629eaae56cd8302aee8c1940733c29d7a4 (diff) | |
download | cpython-889bca0df11207fa94e618942bb5f1fcf32140fd.zip cpython-889bca0df11207fa94e618942bb5f1fcf32140fd.tar.gz cpython-889bca0df11207fa94e618942bb5f1fcf32140fd.tar.bz2 |
make the tests that expect uncatchable exceptions from a callback test
for them in a roundabout way (catching and parsing stderr)
keeps test output clean.
-rw-r--r-- | Lib/bsddb/test/test_compare.py | 67 |
1 files changed, 48 insertions, 19 deletions
diff --git a/Lib/bsddb/test/test_compare.py b/Lib/bsddb/test/test_compare.py index 53b14ba..225959c 100644 --- a/Lib/bsddb/test/test_compare.py +++ b/Lib/bsddb/test/test_compare.py @@ -2,21 +2,21 @@ TestCases for python DB Btree key comparison function. """ -import sys, os +import sys, os, re import test_all +from cStringIO import StringIO import unittest from bsddb3 import db -def lexical_cmp (db, left, right): - return cmp (left, right) +lexical_cmp = cmp -def lowercase_cmp(db, left, right): +def lowercase_cmp(left, right): return cmp (left.lower(), right.lower()) def make_reverse_comparator (cmp): - def reverse (db, left, right, delegate=cmp): - return - delegate (db, left, right) + def reverse (left, right, delegate=cmp): + return - delegate (left, right) return reverse _expected_lexical_test_data = ['', 'CCCP', 'a', 'aaa', 'b', 'c', 'cccce', 'ccccf'] @@ -25,7 +25,7 @@ _expected_lowercase_test_data = ['', 'a', 'aaa', 'b', 'c', 'CC', 'cccce', 'ccccf class ComparatorTests (unittest.TestCase): def comparator_test_helper (self, comparator, expected_data): data = expected_data[:] - data.sort (lambda l, r, cmp=comparator: cmp (None, l, r)) + data.sort (comparator) self.failUnless (data == expected_data, "comparator `%s' is not right: %s vs. %s" % (comparator, expected_data, data)) @@ -131,7 +131,7 @@ class BtreeKeyCompareTestCase (AbstractBtreeKeyCompareTestCase): def test_compare_function_useless (self): self.startTest () - def socialist_comparator (db, l, r): + def socialist_comparator (l, r): return 0 self.createDB (socialist_comparator) self.addDataToDB (['b', 'a', 'd']) @@ -157,40 +157,69 @@ class BtreeExceptionsTestCase (AbstractBtreeKeyCompareTestCase): def test_compare_function_incorrect (self): self.startTest () - def bad_comparator (db, l, r): + def bad_comparator (l, r): return 1 - # verify that set_bt_compare checks that comparator(db, '', '') == 0 + # verify that set_bt_compare checks that comparator('', '') == 0 self.assertRaises (TypeError, self.createDB, bad_comparator) self.finishTest () - def test_compare_function_exception (self): + def verifyStderr(self, method, successRe): + """ + Call method() while capturing sys.stderr output internally and + call self.fail() if successRe.search() does not match the stderr + output. This is used to test for uncatchable exceptions. + """ + stdErr = sys.stderr + sys.stderr = StringIO() + try: + method() + finally: + temp = sys.stderr + sys.stderr = stdErr + errorOut = temp.getvalue() + if not successRe.search(errorOut): + self.fail("unexpected stderr output:\n"+errorOut) + + def _test_compare_function_exception (self): self.startTest () - def bad_comparator (db, l, r): + def bad_comparator (l, r): if l == r: # pass the set_bt_compare test return 0 raise RuntimeError, "i'm a naughty comparison function" self.createDB (bad_comparator) - print "\n*** this test should print 2 uncatchable tracebacks ***" - self.addDataToDB (['a', 'b', 'c']) # this should raise, but... - self.finishTest () + #print "\n*** test should print 2 uncatchable tracebacks ***" + self.addDataToDB (['a', 'b', 'c']) # this should raise, but... + self.finishTest () - def test_compare_function_bad_return (self): + def test_compare_function_exception(self): + self.verifyStderr( + self._test_compare_function_exception, + re.compile('(^RuntimeError:.* naughty.*){2}', re.M|re.S) + ) + + def _test_compare_function_bad_return (self): self.startTest () - def bad_comparator (db, l, r): + def bad_comparator (l, r): if l == r: # pass the set_bt_compare test return 0 return l self.createDB (bad_comparator) - print "\n*** this test should print 2 errors about returning an int ***" + #print "\n*** test should print 2 errors about returning an int ***" self.addDataToDB (['a', 'b', 'c']) # this should raise, but... self.finishTest () + def test_compare_function_bad_return(self): + self.verifyStderr( + self._test_compare_function_bad_return, + re.compile('(^TypeError:.* return an int.*){2}', re.M|re.S) + ) + def test_cannot_assign_twice (self): - def my_compare (db, a, b): + def my_compare (a, b): return 0 self.startTest () |