Show More
@@ -1,84 +1,94 b'' | |||
|
1 | 1 | """Tests for IPython.lib.pretty. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2011, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | from __future__ import print_function |
|
15 | 15 | |
|
16 | 16 | # Third-party imports |
|
17 | 17 | import nose.tools as nt |
|
18 | 18 | |
|
19 | 19 | # Our own imports |
|
20 | 20 | from IPython.lib import pretty |
|
21 | from IPython.testing.decorators import skip_without | |
|
21 | 22 | |
|
22 | 23 | #----------------------------------------------------------------------------- |
|
23 | 24 | # Classes and functions |
|
24 | 25 | #----------------------------------------------------------------------------- |
|
25 | 26 | |
|
26 | 27 | class MyList(object): |
|
27 | 28 | def __init__(self, content): |
|
28 | 29 | self.content = content |
|
29 | 30 | def _repr_pretty_(self, p, cycle): |
|
30 | 31 | if cycle: |
|
31 | 32 | p.text("MyList(...)") |
|
32 | 33 | else: |
|
33 | 34 | with p.group(3, "MyList(", ")"): |
|
34 | 35 | for (i, child) in enumerate(self.content): |
|
35 | 36 | if i: |
|
36 | 37 | p.text(",") |
|
37 | 38 | p.breakable() |
|
38 | 39 | else: |
|
39 | 40 | p.breakable("") |
|
40 | 41 | p.pretty(child) |
|
41 | 42 | |
|
42 | 43 | |
|
43 | 44 | class MyDict(dict): |
|
44 | 45 | def _repr_pretty_(self, p, cycle): |
|
45 | 46 | p.text("MyDict(...)") |
|
46 | 47 | |
|
47 | 48 | |
|
48 | 49 | class Dummy1(object): |
|
49 | 50 | def _repr_pretty_(self, p, cycle): |
|
50 | 51 | p.text("Dummy1(...)") |
|
51 | 52 | |
|
52 | 53 | class Dummy2(Dummy1): |
|
53 | 54 | _repr_pretty_ = None |
|
54 | 55 | |
|
55 | 56 | |
|
56 | 57 | def test_indentation(): |
|
57 | 58 | """Test correct indentation in groups""" |
|
58 | 59 | count = 40 |
|
59 | 60 | gotoutput = pretty.pretty(MyList(range(count))) |
|
60 | 61 | expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")" |
|
61 | 62 | |
|
62 | 63 | nt.assert_equal(gotoutput, expectedoutput) |
|
63 | 64 | |
|
64 | 65 | |
|
65 | 66 | def test_dispatch(): |
|
66 | 67 | """ |
|
67 | 68 | Test correct dispatching: The _repr_pretty_ method for MyDict |
|
68 | 69 | must be found before the registered printer for dict. |
|
69 | 70 | """ |
|
70 | 71 | gotoutput = pretty.pretty(MyDict()) |
|
71 | 72 | expectedoutput = "MyDict(...)" |
|
72 | 73 | |
|
73 | 74 | nt.assert_equal(gotoutput, expectedoutput) |
|
74 | 75 | |
|
75 | 76 | |
|
76 | 77 | def test_callability_checking(): |
|
77 | 78 | """ |
|
78 | 79 | Test that the _repr_pretty_ method is tested for callability and skipped if |
|
79 | 80 | not. |
|
80 | 81 | """ |
|
81 | 82 | gotoutput = pretty.pretty(Dummy2()) |
|
82 | 83 | expectedoutput = "Dummy1(...)" |
|
83 | 84 | |
|
84 | 85 | nt.assert_equal(gotoutput, expectedoutput) |
|
86 | ||
|
87 | @skip_without('xxlimited') | |
|
88 | def test_pprint_heap_allocated_type(): | |
|
89 | """ | |
|
90 | Test that pprint works for heap allocated types. | |
|
91 | """ | |
|
92 | import xxlimited | |
|
93 | output = pretty.pretty(xxlimited.Null) | |
|
94 | nt.assert_equal(output, 'xxlimited.Null') |
General Comments 0
You need to be logged in to leave comments.
Login now