Show More
@@ -1,97 +1,101 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | Simple tests for :mod:`IPython.extensions.pretty`. |
|
4 | Simple tests for :mod:`IPython.extensions.pretty`. | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (C) 2008-2009 The IPython Development Team |
|
8 | # Copyright (C) 2008-2009 The IPython Development Team | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
11 | # the file COPYING, distributed as part of this software. |
|
11 | # the file COPYING, distributed as part of this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | from unittest import TestCase |
|
18 | from unittest import TestCase | |
19 |
|
19 | |||
20 | from IPython.core.component import Component, masquerade_as |
|
20 | from IPython.core.component import Component, masquerade_as | |
21 | from IPython.core.iplib import InteractiveShell |
|
21 | from IPython.core.iplib import InteractiveShell | |
22 | from IPython.extensions import pretty as pretty_ext |
|
22 | from IPython.extensions import pretty as pretty_ext | |
23 | from IPython.external import pretty |
|
23 | from IPython.external import pretty | |
|
24 | from IPython.testing import decorators as dec | |||
24 | from IPython.testing import tools as tt |
|
25 | from IPython.testing import tools as tt | |
25 | from IPython.utils.traitlets import Bool |
|
26 | from IPython.utils.traitlets import Bool | |
26 |
|
27 | |||
27 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
28 | # Tests |
|
29 | # Tests | |
29 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
30 |
|
31 | |||
31 |
|
||||
32 | class InteractiveShellStub(Component): |
|
32 | class InteractiveShellStub(Component): | |
33 | pprint = Bool(True) |
|
33 | pprint = Bool(True) | |
34 |
|
34 | |||
35 | class A(object): |
|
35 | class A(object): | |
36 | pass |
|
36 | pass | |
37 |
|
37 | |||
38 | def a_pprinter(o, p, c): |
|
38 | def a_pprinter(o, p, c): | |
39 | return p.text("<A>") |
|
39 | return p.text("<A>") | |
40 |
|
40 | |||
41 | class TestPrettyResultDisplay(TestCase): |
|
41 | class TestPrettyResultDisplay(TestCase): | |
42 |
|
42 | |||
43 | def setUp(self): |
|
43 | def setUp(self): | |
44 | self.ip = InteractiveShellStub(None) |
|
44 | self.ip = InteractiveShellStub(None) | |
45 | # This allows our stub to be retrieved instead of the real |
|
45 | # This allows our stub to be retrieved instead of the real | |
46 | # InteractiveShell |
|
46 | # InteractiveShell | |
47 | masquerade_as(self.ip, InteractiveShell) |
|
47 | masquerade_as(self.ip, InteractiveShell) | |
48 | self.prd = pretty_ext.PrettyResultDisplay(self.ip, |
|
48 | self.prd = pretty_ext.PrettyResultDisplay(self.ip, | |
49 | name='pretty_result_display') |
|
49 | name='pretty_result_display') | |
50 |
|
50 | |||
51 | def test_for_type(self): |
|
51 | def test_for_type(self): | |
52 | self.prd.for_type(A, a_pprinter) |
|
52 | self.prd.for_type(A, a_pprinter) | |
53 | a = A() |
|
53 | a = A() | |
54 | result = pretty.pretty(a) |
|
54 | result = pretty.pretty(a) | |
55 | self.assertEquals(result, "<A>") |
|
55 | self.assertEquals(result, "<A>") | |
56 |
|
56 | |||
57 | ipy_src = """ |
|
57 | ipy_src = """ | |
58 | class A(object): |
|
58 | class A(object): | |
59 | def __repr__(self): |
|
59 | def __repr__(self): | |
60 | return 'A()' |
|
60 | return 'A()' | |
61 |
|
61 | |||
62 | class B(object): |
|
62 | class B(object): | |
63 | def __repr__(self): |
|
63 | def __repr__(self): | |
64 | return 'B()' |
|
64 | return 'B()' | |
65 |
|
65 | |||
66 | a = A() |
|
66 | a = A() | |
67 | b = B() |
|
67 | b = B() | |
68 |
|
68 | |||
69 | def a_pretty_printer(obj, p, cycle): |
|
69 | def a_pretty_printer(obj, p, cycle): | |
70 | p.text('<A>') |
|
70 | p.text('<A>') | |
71 |
|
71 | |||
72 | def b_pretty_printer(obj, p, cycle): |
|
72 | def b_pretty_printer(obj, p, cycle): | |
73 | p.text('<B>') |
|
73 | p.text('<B>') | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | a |
|
76 | a | |
77 | b |
|
77 | b | |
78 |
|
78 | |||
79 | ip = get_ipython() |
|
79 | ip = get_ipython() | |
80 | prd = ip.load_extension('pretty') |
|
80 | prd = ip.load_extension('pretty') | |
81 | prd.for_type(A, a_pretty_printer) |
|
81 | prd.for_type(A, a_pretty_printer) | |
82 | prd.for_type_by_name(B.__module__, B.__name__, b_pretty_printer) |
|
82 | prd.for_type_by_name(B.__module__, B.__name__, b_pretty_printer) | |
83 |
|
83 | |||
84 | a |
|
84 | a | |
85 | b |
|
85 | b | |
86 | """ |
|
86 | """ | |
87 | ipy_out = """ |
|
87 | ipy_out = """ | |
88 | A() |
|
88 | A() | |
89 | B() |
|
89 | B() | |
90 | <A> |
|
90 | <A> | |
91 | <B> |
|
91 | <B> | |
92 | """ |
|
92 | """ | |
93 |
|
93 | |||
94 | class TestPrettyInteractively(tt.TempFileMixin): |
|
94 | class TestPrettyInteractively(tt.TempFileMixin): | |
|
95 | ||||
|
96 | # XXX Unfortunately, ipexec_validate fails under win32. If someone helps | |||
|
97 | # us write a win32-compatible version, we can reactivate this test. | |||
|
98 | @dec.skip_win32 | |||
95 | def test_printers(self): |
|
99 | def test_printers(self): | |
96 | self.mktmp(ipy_src, '.ipy') |
|
100 | self.mktmp(ipy_src, '.ipy') | |
97 | tt.ipexec_validate(self.fname, ipy_out) |
|
101 | tt.ipexec_validate(self.fname, ipy_out) |
General Comments 0
You need to be logged in to leave comments.
Login now