2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
import py
|
|
|
|
from py.__.code import safe_repr
|
|
|
|
|
|
|
|
def test_simple_repr():
|
|
|
|
assert safe_repr._repr(1) == '1'
|
|
|
|
assert safe_repr._repr(None) == 'None'
|
|
|
|
|
|
|
|
class BrokenRepr:
|
|
|
|
def __init__(self, ex):
|
|
|
|
self.ex = ex
|
|
|
|
foo = 0
|
|
|
|
def __repr__(self):
|
|
|
|
raise self.ex
|
|
|
|
|
|
|
|
def test_exception():
|
|
|
|
assert 'Exception' in safe_repr._repr(BrokenRepr(Exception("broken")))
|
|
|
|
|
|
|
|
class BrokenReprException(Exception):
|
|
|
|
__str__ = None
|
|
|
|
__repr__ = None
|
|
|
|
|
|
|
|
def test_broken_exception():
|
|
|
|
assert 'Exception' in safe_repr._repr(BrokenRepr(BrokenReprException("really broken")))
|
|
|
|
|
|
|
|
def test_string_exception():
|
2008-08-19 01:33:31 +08:00
|
|
|
if py.std.sys.version_info < (2,6):
|
|
|
|
assert 'unknown' in safe_repr._repr(BrokenRepr("string"))
|
|
|
|
else:
|
|
|
|
assert 'TypeError' in safe_repr._repr(BrokenRepr("string"))
|
|
|
|
|
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def test_big_repr():
|
|
|
|
assert len(safe_repr._repr(range(1000))) <= \
|
|
|
|
len('[' + safe_repr.SafeRepr().maxlist * "1000" + ']')
|
|
|
|
|
|
|
|
|
|
|
|
|