def test_numbers(self): eq = self.assertEquals eq(r(123), repr(123)) eq(r(123L), repr(123L)) eq(r(1.0/3), repr(1.0/3)) n = 10L**100 expected = repr(n)[:18] + "..." + repr(n)[-19:] eq(r(n), expected)
def test_tuple(self): eq = self.assertEquals eq(r((1,)), "(1,)") t3 = (1, 2, 3) eq(r(t3), "(1, 2, 3)") r2 = Repr() r2.maxtuple = 2 expected = repr(t3)[:-2] + "...)" eq(r2.repr(t3), expected)
def test_string(self): eq = self.assertEquals eq(r("abc"), "'abc'") eq(r("abcdefghijklmnop"),"'abcdefghijklmnop'") s = "a"*30+"b"*30 expected = repr(s)[:13] + "..." + repr(s)[-14:] eq(r(s), expected) eq(r("\"'"), repr("\"'")) s = "\""*30+"'"*100 expected = repr(s)[:13] + "..." + repr(s)[-14:] eq(r(s), expected)
def test_instance(self): eq = self.assertEquals i1 = ClassWithRepr("a") eq(r(i1), repr(i1)) i2 = ClassWithRepr("x"*1000) expected = repr(i2)[:13] + "..." + repr(i2)[-14:] eq(r(i2), expected) i3 = ClassWithFailingRepr() eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3))) s = r(ClassWithFailingRepr) self.failUnless(s.startswith("<class ")) self.failUnless(s.endswith(">")) self.failUnless(s.find("...") == 8)
def test_instance(self): eq = self.assertEquals i1 = ClassWithRepr("a") eq(r(i1), repr(i1)) i2 = ClassWithRepr("x"*1000) expected = `i2`[:13] + "..." + `i2`[-14:] eq(r(i2), expected) i3 = ClassWithFailingRepr() # On some systems (RH10) id() can be a negative number. # work around this. MAX = 2L*sys.maxint+1 eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%(id(i3)&MAX))) s = r(ClassWithFailingRepr) self.failUnless(s.startswith("<class ")) self.failUnless(s.endswith(">")) self.failUnless(s.find("...") == 8)
def test_instance(self): # Disabled for PyPy because it relies on oldstyle class behaviour. # Running the test under oldstyle results in many more other problems # though. eq = self.assertEquals i1 = ClassWithRepr("a") eq(r(i1), repr(i1)) i2 = ClassWithRepr("x"*1000) expected = repr(i2)[:13] + "..." + repr(i2)[-14:] eq(r(i2), expected) i3 = ClassWithFailingRepr() eq(r(i3), ("<ClassWithFailingRepr instance at %x>"%id(i3))) s = r(ClassWithFailingRepr) self.failUnless(s.startswith("<class ")) self.failUnless(s.endswith(">")) self.failUnless(s.find("...") == 8)
def test_cell(self): def get_cell(): x = 42 def inner(): return x return inner x = get_cell().__closure__[0] self.assertRegexpMatches(repr(x), r'<cell at 0x[0-9A-Fa-f]+: ' r'int object at 0x[0-9A-Fa-f]+>') self.assertRegexpMatches(r(x), r'<cell at.*\.\.\..*>')
def test_nesting(self): eq = self.assertEqual # everything is meant to give up after 6 levels. eq(r([[[[[[[]]]]]]]), "[[[[[[[]]]]]]]") eq(r([[[[[[[[]]]]]]]]), "[[[[[[[...]]]]]]]") eq(r(nestedTuple(6)), "(((((((),),),),),),)") eq(r(nestedTuple(7)), "(((((((...),),),),),),)") eq(r({nestedTuple(5): nestedTuple(5)}), "{((((((),),),),),): ((((((),),),),),)}") eq(r({nestedTuple(6): nestedTuple(6)}), "{((((((...),),),),),): ((((((...),),),),),)}") eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]") eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")
def test_unsortable(self): # Repr.repr() used to call sorted() on sets, frozensets and dicts # without taking into account that not all objects are comparable x = set([1j, 2j, 3j]) y = frozenset(x) z = {1j: 1, 2j: 2} r(x) r(y) r(z)
def test_cell_repr(self): import re from repr import repr as r # Don't shadow builtin repr def get_cell(): x = 42 def inner(): return x return inner x = get_cell().__closure__[0] assert re.match(r'<cell at 0x[0-9A-Fa-f]+: int object at 0x[0-9A-Fa-f]+>', repr(x)) assert re.match(r'<cell at.*\.\.\..*>', r(x)) def get_cell(): if False: x = 42 def inner(): return x return inner x = get_cell().__closure__[0] assert re.match(r'<cell at 0x[0-9A-Fa-f]+: empty>', repr(x))
def test_cell_repr(): import re from repr import repr as r # Don't shadow builtin repr def get_cell(): x = 42 def inner(): return x return inner x = get_cell().__closure__[0] assert re.match(r'<cell at 0x[0-9A-Fa-f]+: int object at 0x[0-9A-Fa-f]+>', repr(x)) assert re.match(r'<cell at.*\.\.\..*>', r(x)) def get_cell(): if False: x = 42 def inner(): return x return inner x = get_cell().__closure__[0] assert re.match(r'<cell at 0x[0-9A-Fa-f]+: empty>', repr(x))
def test_container(self): from array import array from collections import deque eq = self.assertEquals # Tuples give up after 6 elements eq(r(()), "()") eq(r((1,)), "(1,)") eq(r((1, 2, 3)), "(1, 2, 3)") eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)") eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)") # Lists give up after 6 as well eq(r([]), "[]") eq(r([1]), "[1]") eq(r([1, 2, 3]), "[1, 2, 3]") eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]") eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]") # Sets give up after 6 as well eq(r(set([])), "set([])") eq(r(set([1])), "set([1])") eq(r(set([1, 2, 3])), "set([1, 2, 3])") eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])") eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])") # Frozensets give up after 6 as well eq(r(frozenset([])), "frozenset([])") eq(r(frozenset([1])), "frozenset([1])") eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])") eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])") eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])") # collections.deque after 6 eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])") # Dictionaries give up after 4. eq(r({}), "{}") d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4} eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}") d['arthur'] = 1 eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}") # array.array after 5. eq(r(array('i')), "array('i', [])") eq(r(array('i', [1])), "array('i', [1])") eq(r(array('i', [1, 2])), "array('i', [1, 2])") eq(r(array('i', [1, 2, 3])), "array('i', [1, 2, 3])") eq(r(array('i', [1, 2, 3, 4])), "array('i', [1, 2, 3, 4])") eq(r(array('i', [1, 2, 3, 4, 5])), "array('i', [1, 2, 3, 4, 5])") eq(r(array('i', [1, 2, 3, 4, 5, 6])), "array('i', [1, 2, 3, 4, 5, ...])")
"""
def test_container(self): eq = self.assertEquals # Tuples give up after 6 elements eq(r(()), "()") eq(r((1,)), "(1,)") eq(r((1, 2, 3)), "(1, 2, 3)") eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)") eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)") # Lists give up after 6 as well eq(r([]), "[]") eq(r([1]), "[1]") eq(r([1, 2, 3]), "[1, 2, 3]") eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]") eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]") # Dictionaries give up after 4. eq(r({}), "{}") d = {"alice": 1, "bob": 2, "charles": 3, "dave": 4} eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}") d["arthur"] = 1 eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")
def test_container(self): eq = self.assertEquals # Tuples give up after 6 elements eq(r(()), "()") eq(r((1,)), "(1,)") eq(r((1, 2, 3)), "(1, 2, 3)") eq(r((1, 2, 3, 4, 5, 6)), "(1, 2, 3, 4, 5, 6)") eq(r((1, 2, 3, 4, 5, 6, 7)), "(1, 2, 3, 4, 5, 6, ...)") # Lists give up after 6 as well eq(r([]), "[]") eq(r([1]), "[1]") eq(r([1, 2, 3]), "[1, 2, 3]") eq(r([1, 2, 3, 4, 5, 6]), "[1, 2, 3, 4, 5, 6]") eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]") # Dictionaries give up after 4. eq(r({}), "{}") d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4} eq(r(d), "{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}") d['arthur'] = 1 eq(r(d), "{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}")