##// END OF EJS Templates
Download as always starts downloads in new window/tab...
Download as always starts downloads in new window/tab This is a slightly worse user experience if it succeeds, because the new tab flashes up before closing again, but it will let us display an informative error page if it fails, without navigating the user away from the interactive notebook view.

File last commit:

r13434:5a80f8ab
r13838:19c6ee99
Show More
test_pretty.py
183 lines | 4.9 KiB | text/x-python | PythonLexer
Walter Doerwald
Add test for the indentation fix.
r6295 """Tests for IPython.lib.pretty.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2011, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function
# Third-party imports
import nose.tools as nt
# Our own imports
from IPython.lib import pretty
Bradley M. Froehle
Add test for gh-2684.
r8888 from IPython.testing.decorators import skip_without
Walter Doerwald
Add test for the indentation fix.
r6295
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
class MyList(object):
def __init__(self, content):
self.content = content
def _repr_pretty_(self, p, cycle):
if cycle:
p.text("MyList(...)")
else:
with p.group(3, "MyList(", ")"):
for (i, child) in enumerate(self.content):
if i:
p.text(",")
p.breakable()
else:
p.breakable("")
p.pretty(child)
Walter Doerwald
Fix dispatching in the pretty printing module....
r6313 class MyDict(dict):
def _repr_pretty_(self, p, cycle):
p.text("MyDict(...)")
Robert Kern
BUG: Look up the `_repr_pretty_` method on the class within the MRO rather than the original leaf class....
r7831 class Dummy1(object):
def _repr_pretty_(self, p, cycle):
p.text("Dummy1(...)")
class Dummy2(Dummy1):
_repr_pretty_ = None
MinRK
test pretty print when __module__ is None
r10789 class NoModule(object):
pass
NoModule.__module__ = None
Alex Rudy
Adds p.break_ for explicit newlines in lib.pretty...
r11875 class Breaking(object):
def _repr_pretty_(self, p, cycle):
with p.group(4,"TG: ",":"):
p.text("Breaking(")
p.break_()
p.text(")")
Alex Rudy
Uses p.break_() when repr() is used for printing...
r11876 class BreakingRepr(object):
def __repr__(self):
return "Breaking(\n)"
class BreakingReprParent(object):
def _repr_pretty_(self, p, cycle):
with p.group(4,"TG: ",":"):
p.pretty(BreakingRepr())
MinRK
test bad repr
r13425 class BadRepr(object):
def __repr__(self):
return 1/0
Alex Rudy
Uses p.break_() when repr() is used for printing...
r11876
Robert Kern
BUG: Look up the `_repr_pretty_` method on the class within the MRO rather than the original leaf class....
r7831
Walter Doerwald
Add test for the indentation fix.
r6295 def test_indentation():
"""Test correct indentation in groups"""
count = 40
gotoutput = pretty.pretty(MyList(range(count)))
expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")"
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(gotoutput, expectedoutput)
Walter Doerwald
Fix dispatching in the pretty printing module....
r6313
def test_dispatch():
"""
Test correct dispatching: The _repr_pretty_ method for MyDict
must be found before the registered printer for dict.
"""
gotoutput = pretty.pretty(MyDict())
expectedoutput = "MyDict(...)"
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(gotoutput, expectedoutput)
Robert Kern
BUG: Look up the `_repr_pretty_` method on the class within the MRO rather than the original leaf class....
r7831
def test_callability_checking():
"""
Test that the _repr_pretty_ method is tested for callability and skipped if
not.
"""
gotoutput = pretty.pretty(Dummy2())
expectedoutput = "Dummy1(...)"
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(gotoutput, expectedoutput)
Bradley M. Froehle
Add test for gh-2684.
r8888
Robert Kern
ENH: Add tests for set and frozenset pretty-printing.
r10234
def test_sets():
"""
Test that set and frozenset use Python 3 formatting.
"""
Robert Kern
ENH: Make the set pretty-printer sort the elements if possible, similar to dict keys.
r10235 objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
frozenset([1, 2]), set([-1, -2, -3])]
Robert Kern
ENH: Add tests for set and frozenset pretty-printing.
r10234 expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
Robert Kern
ENH: Make the set pretty-printer sort the elements if possible, similar to dict keys.
r10235 'frozenset({1, 2})', '{-3, -2, -1}']
Robert Kern
ENH: Add tests for set and frozenset pretty-printing.
r10234 for obj, expected_output in zip(objects, expected):
got_output = pretty.pretty(obj)
yield nt.assert_equal, got_output, expected_output
Bradley M. Froehle
Add test for gh-2684.
r8888 @skip_without('xxlimited')
def test_pprint_heap_allocated_type():
"""
Test that pprint works for heap allocated types.
"""
import xxlimited
output = pretty.pretty(xxlimited.Null)
nt.assert_equal(output, 'xxlimited.Null')
MinRK
test pretty print when __module__ is None
r10789
def test_pprint_nomod():
"""
Test that pprint works for classes with no __module__.
"""
output = pretty.pretty(NoModule)
nt.assert_equal(output, 'NoModule')
Alex Rudy
Adds p.break_ for explicit newlines in lib.pretty...
r11875
def test_pprint_break():
"""
Test that p.break_ produces expected output
"""
output = pretty.pretty(Breaking())
expected = "TG: Breaking(\n ):"
nt.assert_equal(output, expected)
Alex Rudy
Uses p.break_() when repr() is used for printing...
r11876
def test_pprint_break_repr():
"""
Test that p.break_ is used in repr
"""
output = pretty.pretty(BreakingReprParent())
expected = "TG: Breaking(\n ):"
MinRK
test bad repr
r13425 nt.assert_equal(output, expected)
def test_bad_repr():
"""Don't raise, even when repr fails"""
output = pretty.pretty(BadRepr())
nt.assert_in("failed", output)
MinRK
test really bad repr
r13434 nt.assert_in("at 0x", output)
nt.assert_in("test_pretty", output)
class BadException(Exception):
def __str__(self):
return -1
class ReallyBadRepr(object):
__module__ = 1
@property
def __class__(self):
raise ValueError("I am horrible")
def __repr__(self):
raise BadException()
def test_really_bad_repr():
output = pretty.pretty(ReallyBadRepr())
nt.assert_in("failed", output)
nt.assert_in("BadException: unknown", output)
nt.assert_in("unknown type", output)