##// END OF EJS Templates
added tests of passing alternative formatter functions
Greg Caporaso -
Show More
@@ -1,120 +1,156
1 """Tests for IPython.lib.display.
1 """Tests for IPython.lib.display.
2
2
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2012, the IPython Development Team.
5 # Copyright (c) 2012, the IPython Development Team.
6 #
6 #
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8 #
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 from __future__ import print_function
15 from __future__ import print_function
16 from tempfile import NamedTemporaryFile, mkdtemp
16 from tempfile import NamedTemporaryFile, mkdtemp
17 from os.path import split
17 from os.path import split
18
18
19 # Third-party imports
19 # Third-party imports
20 import nose.tools as nt
20 import nose.tools as nt
21
21
22 # Our own imports
22 # Our own imports
23 from IPython.lib import display
23 from IPython.lib import display
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Classes and functions
26 # Classes and functions
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 #--------------------------
29 #--------------------------
30 # FileLink tests
30 # FileLink tests
31 #--------------------------
31 #--------------------------
32
32
33 def test_instantiation_FileLink():
33 def test_instantiation_FileLink():
34 """Test classes can be instantiated"""
34 """Test classes can be instantiated"""
35 fl = display.FileLink('example.txt')
35 fl = display.FileLink('example.txt')
36
36
37 def test_warning_on_non_existant_path_FileLink():
37 def test_warning_on_non_existant_path_FileLink():
38 """Calling _repr_html_ on non-existant files returns a warning"""
38 """Calling _repr_html_ on non-existant files returns a warning"""
39 fl = display.FileLink('example.txt')
39 fl = display.FileLink('example.txt')
40 nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
40 nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
41
41
42 def test_existing_path_FileLink():
42 def test_existing_path_FileLink():
43 """ Calling _repr_html_ functions as expected on existing filepath """
43 """ Calling _repr_html_ functions as expected on existing filepath """
44 tf = NamedTemporaryFile()
44 tf = NamedTemporaryFile()
45 fl = display.FileLink(tf.name)
45 fl = display.FileLink(tf.name)
46 actual = fl._repr_html_()
46 actual = fl._repr_html_()
47 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
47 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
48 nt.assert_equal(actual,expected)
48 nt.assert_equal(actual,expected)
49
49
50 def test_existing_path_FileLink_repr():
50 def test_existing_path_FileLink_repr():
51 """ Calling repr() functions as expected on existing filepath """
51 """ Calling repr() functions as expected on existing filepath """
52 tf = NamedTemporaryFile()
52 tf = NamedTemporaryFile()
53 fl = display.FileLink(tf.name)
53 fl = display.FileLink(tf.name)
54 actual = repr(fl)
54 actual = repr(fl)
55 expected = tf.name
55 expected = tf.name
56 nt.assert_equal(actual,expected)
56 nt.assert_equal(actual,expected)
57
57
58 #--------------------------
58 #--------------------------
59 # FileLinks tests
59 # FileLinks tests
60 #--------------------------
60 #--------------------------
61
61
62 def test_instantiation_FileLinks():
62 def test_instantiation_FileLinks():
63 """Test classes can be instantiated"""
63 """Test classes can be instantiated"""
64 fls = display.FileLinks('example')
64 fls = display.FileLinks('example')
65
65
66 def test_warning_on_non_existant_path_FileLinks():
66 def test_warning_on_non_existant_path_FileLinks():
67 """Calling _repr_html_ on non-existant files returns a warning"""
67 """Calling _repr_html_ on non-existant files returns a warning"""
68 fls = display.FileLinks('example')
68 fls = display.FileLinks('example')
69 nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
69 nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
70
70
71 def test_existing_path_FileLinks():
71 def test_existing_path_FileLinks():
72 """ Calling _repr_html_ functions as expected on existing directory """
72 """ Calling _repr_html_ functions as expected on existing directory """
73 td = mkdtemp()
73 td = mkdtemp()
74 tf1 = NamedTemporaryFile(dir=td)
74 tf1 = NamedTemporaryFile(dir=td)
75 tf2 = NamedTemporaryFile(dir=td)
75 tf2 = NamedTemporaryFile(dir=td)
76 fl = display.FileLinks(td)
76 fl = display.FileLinks(td)
77 actual = fl._repr_html_()
77 actual = fl._repr_html_()
78 actual = actual.split('\n')
78 actual = actual.split('\n')
79 actual.sort()
79 actual.sort()
80 expected = ["%s/<br>" % td,
80 expected = ["%s/<br>" % td,
81 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,split(tf2.name)[1]),
81 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,split(tf2.name)[1]),
82 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,split(tf1.name)[1])]
82 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,split(tf1.name)[1])]
83 expected.sort()
83 expected.sort()
84 # We compare the sorted list of links here as that's more reliable
84 # We compare the sorted list of links here as that's more reliable
85 nt.assert_equal(actual,expected)
85 nt.assert_equal(actual,expected)
86
86
87 def test_existing_path_FileLinks_alt_formatter():
88 """ Calling _repr_html_ functions as expected with an alternative formatter
89 """
90 td = mkdtemp()
91 tf1 = NamedTemporaryFile(dir=td)
92 tf2 = NamedTemporaryFile(dir=td)
93 def fake_formatter(output_lines,dirname,fnames):
94 output_lines.extend(["hello","world"])
95 return
96 fl = display.FileLinks(td,notebook_display_formatter=fake_formatter)
97 actual = fl._repr_html_()
98 actual = actual.split('\n')
99 actual.sort()
100 expected = ["hello","world"]
101 expected.sort()
102 # We compare the sorted list of links here as that's more reliable
103 nt.assert_equal(actual,expected)
104
87 def test_existing_path_FileLinks_repr():
105 def test_existing_path_FileLinks_repr():
88 """ Calling repr() functions as expected on existing directory """
106 """ Calling repr() functions as expected on existing directory """
89 td = mkdtemp()
107 td = mkdtemp()
90 tf1 = NamedTemporaryFile(dir=td)
108 tf1 = NamedTemporaryFile(dir=td)
91 tf2 = NamedTemporaryFile(dir=td)
109 tf2 = NamedTemporaryFile(dir=td)
92 fl = display.FileLinks(td)
110 fl = display.FileLinks(td)
93 actual = repr(fl)
111 actual = repr(fl)
94 actual = actual.split('\n')
112 actual = actual.split('\n')
95 actual.sort()
113 actual.sort()
96 expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]]
114 expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]]
97 expected.sort()
115 expected.sort()
98 # We compare the sorted list of links here as that's more reliable
116 # We compare the sorted list of links here as that's more reliable
99 nt.assert_equal(actual,expected)
117 nt.assert_equal(actual,expected)
100
118
119 def test_existing_path_FileLinks_repr_alt_formatter():
120 """ Calling repr() functions as expected with an alternative formatter
121 """
122 td = mkdtemp()
123 tf1 = NamedTemporaryFile(dir=td)
124 tf2 = NamedTemporaryFile(dir=td)
125 def fake_formatter(output_lines,dirname,fnames):
126 output_lines.extend(["hello","world"])
127 return
128 fl = display.FileLinks(td,terminal_display_formatter=fake_formatter)
129 actual = repr(fl)
130 actual = actual.split('\n')
131 actual.sort()
132 expected = ["hello","world"]
133 expected.sort()
134 # We compare the sorted list of links here as that's more reliable
135 nt.assert_equal(actual,expected)
136
101 #--------------------------
137 #--------------------------
102 # DirectoryLink tests
138 # DirectoryLink tests
103 #--------------------------
139 #--------------------------
104
140
105 def test_instantiation_DirectoryLink():
141 def test_instantiation_DirectoryLink():
106 """Test classes can be instantiated"""
142 """Test classes can be instantiated"""
107 dl = display.DirectoryLink('example')
143 dl = display.DirectoryLink('example')
108
144
109 def test_warning_on_non_existant_path_DirectoryLink():
145 def test_warning_on_non_existant_path_DirectoryLink():
110 """Calling _repr_html_ on non-existant files returns a warning"""
146 """Calling _repr_html_ on non-existant files returns a warning"""
111 dl = display.DirectoryLink('example')
147 dl = display.DirectoryLink('example')
112 nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)'))
148 nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)'))
113
149
114 def test_existing_path_DirectoryLink():
150 def test_existing_path_DirectoryLink():
115 """ Calling _repr_html_ functions as expected on existing directory """
151 """ Calling _repr_html_ functions as expected on existing directory """
116 td = mkdtemp()
152 td = mkdtemp()
117 dl = display.DirectoryLink(td)
153 dl = display.DirectoryLink(td)
118 actual = dl._repr_html_()
154 actual = dl._repr_html_()
119 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (td,td)
155 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (td,td)
120 nt.assert_equal(actual,expected)
156 nt.assert_equal(actual,expected)
General Comments 0
You need to be logged in to leave comments. Login now