mirror of
https://github.com/python/cpython.git
synced 2025-07-25 04:04:13 +00:00
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.
This commit is contained in:
parent
91116b629e
commit
889bca0df1
1 changed files with 48 additions and 19 deletions
|
@ -2,21 +2,21 @@
|
||||||
TestCases for python DB Btree key comparison function.
|
TestCases for python DB Btree key comparison function.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys, os
|
import sys, os, re
|
||||||
import test_all
|
import test_all
|
||||||
|
from cStringIO import StringIO
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from bsddb3 import db
|
from bsddb3 import db
|
||||||
|
|
||||||
def lexical_cmp (db, left, right):
|
lexical_cmp = cmp
|
||||||
return cmp (left, right)
|
|
||||||
|
|
||||||
def lowercase_cmp(db, left, right):
|
def lowercase_cmp(left, right):
|
||||||
return cmp (left.lower(), right.lower())
|
return cmp (left.lower(), right.lower())
|
||||||
|
|
||||||
def make_reverse_comparator (cmp):
|
def make_reverse_comparator (cmp):
|
||||||
def reverse (db, left, right, delegate=cmp):
|
def reverse (left, right, delegate=cmp):
|
||||||
return - delegate (db, left, right)
|
return - delegate (left, right)
|
||||||
return reverse
|
return reverse
|
||||||
|
|
||||||
_expected_lexical_test_data = ['', 'CCCP', 'a', 'aaa', 'b', 'c', 'cccce', 'ccccf']
|
_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):
|
class ComparatorTests (unittest.TestCase):
|
||||||
def comparator_test_helper (self, comparator, expected_data):
|
def comparator_test_helper (self, comparator, expected_data):
|
||||||
data = expected_data[:]
|
data = expected_data[:]
|
||||||
data.sort (lambda l, r, cmp=comparator: cmp (None, l, r))
|
data.sort (comparator)
|
||||||
self.failUnless (data == expected_data,
|
self.failUnless (data == expected_data,
|
||||||
"comparator `%s' is not right: %s vs. %s"
|
"comparator `%s' is not right: %s vs. %s"
|
||||||
% (comparator, expected_data, data))
|
% (comparator, expected_data, data))
|
||||||
|
@ -131,7 +131,7 @@ class BtreeKeyCompareTestCase (AbstractBtreeKeyCompareTestCase):
|
||||||
|
|
||||||
def test_compare_function_useless (self):
|
def test_compare_function_useless (self):
|
||||||
self.startTest ()
|
self.startTest ()
|
||||||
def socialist_comparator (db, l, r):
|
def socialist_comparator (l, r):
|
||||||
return 0
|
return 0
|
||||||
self.createDB (socialist_comparator)
|
self.createDB (socialist_comparator)
|
||||||
self.addDataToDB (['b', 'a', 'd'])
|
self.addDataToDB (['b', 'a', 'd'])
|
||||||
|
@ -157,40 +157,69 @@ class BtreeExceptionsTestCase (AbstractBtreeKeyCompareTestCase):
|
||||||
|
|
||||||
def test_compare_function_incorrect (self):
|
def test_compare_function_incorrect (self):
|
||||||
self.startTest ()
|
self.startTest ()
|
||||||
def bad_comparator (db, l, r):
|
def bad_comparator (l, r):
|
||||||
return 1
|
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.assertRaises (TypeError, self.createDB, bad_comparator)
|
||||||
self.finishTest ()
|
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 ()
|
self.startTest ()
|
||||||
def bad_comparator (db, l, r):
|
def bad_comparator (l, r):
|
||||||
if l == r:
|
if l == r:
|
||||||
# pass the set_bt_compare test
|
# pass the set_bt_compare test
|
||||||
return 0
|
return 0
|
||||||
raise RuntimeError, "i'm a naughty comparison function"
|
raise RuntimeError, "i'm a naughty comparison function"
|
||||||
self.createDB (bad_comparator)
|
self.createDB (bad_comparator)
|
||||||
print "\n*** this test should print 2 uncatchable tracebacks ***"
|
#print "\n*** test should print 2 uncatchable tracebacks ***"
|
||||||
self.addDataToDB (['a', 'b', 'c']) # this should raise, but...
|
self.addDataToDB (['a', 'b', 'c']) # this should raise, but...
|
||||||
self.finishTest ()
|
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 ()
|
self.startTest ()
|
||||||
def bad_comparator (db, l, r):
|
def bad_comparator (l, r):
|
||||||
if l == r:
|
if l == r:
|
||||||
# pass the set_bt_compare test
|
# pass the set_bt_compare test
|
||||||
return 0
|
return 0
|
||||||
return l
|
return l
|
||||||
self.createDB (bad_comparator)
|
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.addDataToDB (['a', 'b', 'c']) # this should raise, but...
|
||||||
self.finishTest ()
|
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 test_cannot_assign_twice (self):
|
||||||
|
|
||||||
def my_compare (db, a, b):
|
def my_compare (a, b):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
self.startTest ()
|
self.startTest ()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue