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