Show More
@@ -1,177 +1,185 | |||||
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 tempfile import NamedTemporaryFile, mkdtemp |
|
15 | from tempfile import NamedTemporaryFile, mkdtemp | |
16 | from os.path import split, join as pjoin, dirname |
|
16 | from os.path import split, join as pjoin, dirname | |
|
17 | import sys | |||
|
18 | try: | |||
|
19 | import pathlib | |||
|
20 | except ImportError: | |||
|
21 | pass | |||
17 |
|
22 | |||
18 | # Third-party imports |
|
23 | # Third-party imports | |
19 | import nose.tools as nt |
|
24 | import nose.tools as nt | |
20 |
|
25 | |||
21 | # Our own imports |
|
26 | # Our own imports | |
22 | from IPython.lib import display |
|
27 | from IPython.lib import display | |
23 | from IPython.testing.decorators import skipif_not_numpy |
|
28 | from IPython.testing.decorators import skipif_not_numpy | |
24 |
|
29 | |||
25 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
26 | # Classes and functions |
|
31 | # Classes and functions | |
27 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
28 |
|
33 | |||
29 | #-------------------------- |
|
34 | #-------------------------- | |
30 | # FileLink tests |
|
35 | # FileLink tests | |
31 | #-------------------------- |
|
36 | #-------------------------- | |
32 |
|
37 | |||
33 | def test_instantiation_FileLink(): |
|
38 | def test_instantiation_FileLink(): | |
34 | """FileLink: Test class can be instantiated""" |
|
39 | """FileLink: Test class can be instantiated""" | |
35 | fl = display.FileLink('example.txt') |
|
40 | fl = display.FileLink('example.txt') | |
|
41 | # TODO: remove if when only Python >= 3.6 is supported | |||
|
42 | if sys.version_info >= (3, 6): | |||
|
43 | fl = display.FileLink(pathlib.PurePath('example.txt')) | |||
36 |
|
44 | |||
37 | def test_warning_on_non_existant_path_FileLink(): |
|
45 | def test_warning_on_non_existant_path_FileLink(): | |
38 | """FileLink: Calling _repr_html_ on non-existant files returns a warning |
|
46 | """FileLink: Calling _repr_html_ on non-existant files returns a warning | |
39 | """ |
|
47 | """ | |
40 | fl = display.FileLink('example.txt') |
|
48 | fl = display.FileLink('example.txt') | |
41 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) |
|
49 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) | |
42 |
|
50 | |||
43 | def test_existing_path_FileLink(): |
|
51 | def test_existing_path_FileLink(): | |
44 | """FileLink: Calling _repr_html_ functions as expected on existing filepath |
|
52 | """FileLink: Calling _repr_html_ functions as expected on existing filepath | |
45 | """ |
|
53 | """ | |
46 | tf = NamedTemporaryFile() |
|
54 | tf = NamedTemporaryFile() | |
47 | fl = display.FileLink(tf.name) |
|
55 | fl = display.FileLink(tf.name) | |
48 | actual = fl._repr_html_() |
|
56 | actual = fl._repr_html_() | |
49 | expected = "<a href='%s' target='_blank'>%s</a><br>" % (tf.name,tf.name) |
|
57 | expected = "<a href='%s' target='_blank'>%s</a><br>" % (tf.name,tf.name) | |
50 | nt.assert_equal(actual,expected) |
|
58 | nt.assert_equal(actual,expected) | |
51 |
|
59 | |||
52 | def test_existing_path_FileLink_repr(): |
|
60 | def test_existing_path_FileLink_repr(): | |
53 | """FileLink: Calling repr() functions as expected on existing filepath |
|
61 | """FileLink: Calling repr() functions as expected on existing filepath | |
54 | """ |
|
62 | """ | |
55 | tf = NamedTemporaryFile() |
|
63 | tf = NamedTemporaryFile() | |
56 | fl = display.FileLink(tf.name) |
|
64 | fl = display.FileLink(tf.name) | |
57 | actual = repr(fl) |
|
65 | actual = repr(fl) | |
58 | expected = tf.name |
|
66 | expected = tf.name | |
59 | nt.assert_equal(actual,expected) |
|
67 | nt.assert_equal(actual,expected) | |
60 |
|
68 | |||
61 | def test_error_on_directory_to_FileLink(): |
|
69 | def test_error_on_directory_to_FileLink(): | |
62 | """FileLink: Raises error when passed directory |
|
70 | """FileLink: Raises error when passed directory | |
63 | """ |
|
71 | """ | |
64 | td = mkdtemp() |
|
72 | td = mkdtemp() | |
65 | nt.assert_raises(ValueError,display.FileLink,td) |
|
73 | nt.assert_raises(ValueError,display.FileLink,td) | |
66 |
|
74 | |||
67 | #-------------------------- |
|
75 | #-------------------------- | |
68 | # FileLinks tests |
|
76 | # FileLinks tests | |
69 | #-------------------------- |
|
77 | #-------------------------- | |
70 |
|
78 | |||
71 | def test_instantiation_FileLinks(): |
|
79 | def test_instantiation_FileLinks(): | |
72 | """FileLinks: Test class can be instantiated |
|
80 | """FileLinks: Test class can be instantiated | |
73 | """ |
|
81 | """ | |
74 | fls = display.FileLinks('example') |
|
82 | fls = display.FileLinks('example') | |
75 |
|
83 | |||
76 | def test_warning_on_non_existant_path_FileLinks(): |
|
84 | def test_warning_on_non_existant_path_FileLinks(): | |
77 | """FileLinks: Calling _repr_html_ on non-existant files returns a warning |
|
85 | """FileLinks: Calling _repr_html_ on non-existant files returns a warning | |
78 | """ |
|
86 | """ | |
79 | fls = display.FileLinks('example') |
|
87 | fls = display.FileLinks('example') | |
80 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
88 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) | |
81 |
|
89 | |||
82 | def test_existing_path_FileLinks(): |
|
90 | def test_existing_path_FileLinks(): | |
83 | """FileLinks: Calling _repr_html_ functions as expected on existing dir |
|
91 | """FileLinks: Calling _repr_html_ functions as expected on existing dir | |
84 | """ |
|
92 | """ | |
85 | td = mkdtemp() |
|
93 | td = mkdtemp() | |
86 | tf1 = NamedTemporaryFile(dir=td) |
|
94 | tf1 = NamedTemporaryFile(dir=td) | |
87 | tf2 = NamedTemporaryFile(dir=td) |
|
95 | tf2 = NamedTemporaryFile(dir=td) | |
88 | fl = display.FileLinks(td) |
|
96 | fl = display.FileLinks(td) | |
89 | actual = fl._repr_html_() |
|
97 | actual = fl._repr_html_() | |
90 | actual = actual.split('\n') |
|
98 | actual = actual.split('\n') | |
91 | actual.sort() |
|
99 | actual.sort() | |
92 | # the links should always have forward slashes, even on windows, so replace |
|
100 | # the links should always have forward slashes, even on windows, so replace | |
93 | # backslashes with forward slashes here |
|
101 | # backslashes with forward slashes here | |
94 | expected = ["%s/<br>" % td, |
|
102 | expected = ["%s/<br>" % td, | |
95 | " <a href='%s' target='_blank'>%s</a><br>" %\ |
|
103 | " <a href='%s' target='_blank'>%s</a><br>" %\ | |
96 | (tf2.name.replace("\\","/"),split(tf2.name)[1]), |
|
104 | (tf2.name.replace("\\","/"),split(tf2.name)[1]), | |
97 | " <a href='%s' target='_blank'>%s</a><br>" %\ |
|
105 | " <a href='%s' target='_blank'>%s</a><br>" %\ | |
98 | (tf1.name.replace("\\","/"),split(tf1.name)[1])] |
|
106 | (tf1.name.replace("\\","/"),split(tf1.name)[1])] | |
99 | expected.sort() |
|
107 | expected.sort() | |
100 | # We compare the sorted list of links here as that's more reliable |
|
108 | # We compare the sorted list of links here as that's more reliable | |
101 | nt.assert_equal(actual,expected) |
|
109 | nt.assert_equal(actual,expected) | |
102 |
|
110 | |||
103 | def test_existing_path_FileLinks_alt_formatter(): |
|
111 | def test_existing_path_FileLinks_alt_formatter(): | |
104 | """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter |
|
112 | """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter | |
105 | """ |
|
113 | """ | |
106 | td = mkdtemp() |
|
114 | td = mkdtemp() | |
107 | tf1 = NamedTemporaryFile(dir=td) |
|
115 | tf1 = NamedTemporaryFile(dir=td) | |
108 | tf2 = NamedTemporaryFile(dir=td) |
|
116 | tf2 = NamedTemporaryFile(dir=td) | |
109 | def fake_formatter(dirname,fnames,included_suffixes): |
|
117 | def fake_formatter(dirname,fnames,included_suffixes): | |
110 | return ["hello","world"] |
|
118 | return ["hello","world"] | |
111 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) |
|
119 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) | |
112 | actual = fl._repr_html_() |
|
120 | actual = fl._repr_html_() | |
113 | actual = actual.split('\n') |
|
121 | actual = actual.split('\n') | |
114 | actual.sort() |
|
122 | actual.sort() | |
115 | expected = ["hello","world"] |
|
123 | expected = ["hello","world"] | |
116 | expected.sort() |
|
124 | expected.sort() | |
117 | # We compare the sorted list of links here as that's more reliable |
|
125 | # We compare the sorted list of links here as that's more reliable | |
118 | nt.assert_equal(actual,expected) |
|
126 | nt.assert_equal(actual,expected) | |
119 |
|
127 | |||
120 | def test_existing_path_FileLinks_repr(): |
|
128 | def test_existing_path_FileLinks_repr(): | |
121 | """FileLinks: Calling repr() functions as expected on existing directory """ |
|
129 | """FileLinks: Calling repr() functions as expected on existing directory """ | |
122 | td = mkdtemp() |
|
130 | td = mkdtemp() | |
123 | tf1 = NamedTemporaryFile(dir=td) |
|
131 | tf1 = NamedTemporaryFile(dir=td) | |
124 | tf2 = NamedTemporaryFile(dir=td) |
|
132 | tf2 = NamedTemporaryFile(dir=td) | |
125 | fl = display.FileLinks(td) |
|
133 | fl = display.FileLinks(td) | |
126 | actual = repr(fl) |
|
134 | actual = repr(fl) | |
127 | actual = actual.split('\n') |
|
135 | actual = actual.split('\n') | |
128 | actual.sort() |
|
136 | actual.sort() | |
129 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] |
|
137 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] | |
130 | expected.sort() |
|
138 | expected.sort() | |
131 | # We compare the sorted list of links here as that's more reliable |
|
139 | # We compare the sorted list of links here as that's more reliable | |
132 | nt.assert_equal(actual,expected) |
|
140 | nt.assert_equal(actual,expected) | |
133 |
|
141 | |||
134 | def test_existing_path_FileLinks_repr_alt_formatter(): |
|
142 | def test_existing_path_FileLinks_repr_alt_formatter(): | |
135 | """FileLinks: Calling repr() functions as expected w/ alt formatter |
|
143 | """FileLinks: Calling repr() functions as expected w/ alt formatter | |
136 | """ |
|
144 | """ | |
137 | td = mkdtemp() |
|
145 | td = mkdtemp() | |
138 | tf1 = NamedTemporaryFile(dir=td) |
|
146 | tf1 = NamedTemporaryFile(dir=td) | |
139 | tf2 = NamedTemporaryFile(dir=td) |
|
147 | tf2 = NamedTemporaryFile(dir=td) | |
140 | def fake_formatter(dirname,fnames,included_suffixes): |
|
148 | def fake_formatter(dirname,fnames,included_suffixes): | |
141 | return ["hello","world"] |
|
149 | return ["hello","world"] | |
142 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) |
|
150 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) | |
143 | actual = repr(fl) |
|
151 | actual = repr(fl) | |
144 | actual = actual.split('\n') |
|
152 | actual = actual.split('\n') | |
145 | actual.sort() |
|
153 | actual.sort() | |
146 | expected = ["hello","world"] |
|
154 | expected = ["hello","world"] | |
147 | expected.sort() |
|
155 | expected.sort() | |
148 | # We compare the sorted list of links here as that's more reliable |
|
156 | # We compare the sorted list of links here as that's more reliable | |
149 | nt.assert_equal(actual,expected) |
|
157 | nt.assert_equal(actual,expected) | |
150 |
|
158 | |||
151 | def test_error_on_file_to_FileLinks(): |
|
159 | def test_error_on_file_to_FileLinks(): | |
152 | """FileLinks: Raises error when passed file |
|
160 | """FileLinks: Raises error when passed file | |
153 | """ |
|
161 | """ | |
154 | td = mkdtemp() |
|
162 | td = mkdtemp() | |
155 | tf1 = NamedTemporaryFile(dir=td) |
|
163 | tf1 = NamedTemporaryFile(dir=td) | |
156 | nt.assert_raises(ValueError,display.FileLinks,tf1.name) |
|
164 | nt.assert_raises(ValueError,display.FileLinks,tf1.name) | |
157 |
|
165 | |||
158 | def test_recursive_FileLinks(): |
|
166 | def test_recursive_FileLinks(): | |
159 | """FileLinks: Does not recurse when recursive=False |
|
167 | """FileLinks: Does not recurse when recursive=False | |
160 | """ |
|
168 | """ | |
161 | td = mkdtemp() |
|
169 | td = mkdtemp() | |
162 | tf = NamedTemporaryFile(dir=td) |
|
170 | tf = NamedTemporaryFile(dir=td) | |
163 | subtd = mkdtemp(dir=td) |
|
171 | subtd = mkdtemp(dir=td) | |
164 | subtf = NamedTemporaryFile(dir=subtd) |
|
172 | subtf = NamedTemporaryFile(dir=subtd) | |
165 | fl = display.FileLinks(td) |
|
173 | fl = display.FileLinks(td) | |
166 | actual = str(fl) |
|
174 | actual = str(fl) | |
167 | actual = actual.split('\n') |
|
175 | actual = actual.split('\n') | |
168 | nt.assert_equal(len(actual), 4, actual) |
|
176 | nt.assert_equal(len(actual), 4, actual) | |
169 | fl = display.FileLinks(td, recursive=False) |
|
177 | fl = display.FileLinks(td, recursive=False) | |
170 | actual = str(fl) |
|
178 | actual = str(fl) | |
171 | actual = actual.split('\n') |
|
179 | actual = actual.split('\n') | |
172 | nt.assert_equal(len(actual), 2, actual) |
|
180 | nt.assert_equal(len(actual), 2, actual) | |
173 |
|
181 | |||
174 | @skipif_not_numpy |
|
182 | @skipif_not_numpy | |
175 | def test_audio_from_file(): |
|
183 | def test_audio_from_file(): | |
176 | path = pjoin(dirname(__file__), 'test.wav') |
|
184 | path = pjoin(dirname(__file__), 'test.wav') | |
177 | display.Audio(filename=path) |
|
185 | display.Audio(filename=path) |
General Comments 0
You need to be logged in to leave comments.
Login now