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