Show More
@@ -1,36 +1,34 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Test suite for the deepreload module.""" |
|
2 | """Test suite for the deepreload module.""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | from pathlib import Path |
|
7 | from pathlib import Path | |
8 |
|
8 | |||
9 | import nose.tools as nt |
|
|||
10 |
|
||||
11 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
9 | from IPython.utils.syspathcontext import prepended_to_syspath | |
12 | from IPython.utils.tempdir import TemporaryDirectory |
|
10 | from IPython.utils.tempdir import TemporaryDirectory | |
13 | from IPython.lib.deepreload import reload as dreload |
|
11 | from IPython.lib.deepreload import reload as dreload | |
14 |
|
12 | |||
15 |
|
13 | |||
16 | def test_deepreload(): |
|
14 | def test_deepreload(): | |
17 | "Test that dreload does deep reloads and skips excluded modules." |
|
15 | "Test that dreload does deep reloads and skips excluded modules." | |
18 | with TemporaryDirectory() as tmpdir: |
|
16 | with TemporaryDirectory() as tmpdir: | |
19 | with prepended_to_syspath(tmpdir): |
|
17 | with prepended_to_syspath(tmpdir): | |
20 | tmpdirpath = Path(tmpdir) |
|
18 | tmpdirpath = Path(tmpdir) | |
21 | with open(tmpdirpath / "A.py", "w") as f: |
|
19 | with open(tmpdirpath / "A.py", "w") as f: | |
22 | f.write("class Object(object):\n pass\n") |
|
20 | f.write("class Object(object):\n pass\n") | |
23 | with open(tmpdirpath / "B.py", "w") as f: |
|
21 | with open(tmpdirpath / "B.py", "w") as f: | |
24 | f.write("import A\n") |
|
22 | f.write("import A\n") | |
25 | import A |
|
23 | import A | |
26 | import B |
|
24 | import B | |
27 |
|
25 | |||
28 | # Test that A is not reloaded. |
|
26 | # Test that A is not reloaded. | |
29 | obj = A.Object() |
|
27 | obj = A.Object() | |
30 | dreload(B, exclude=["A"]) |
|
28 | dreload(B, exclude=["A"]) | |
31 |
|
|
29 | assert isinstance(obj, A.Object) is True | |
32 |
|
30 | |||
33 | # Test that A is reloaded. |
|
31 | # Test that A is reloaded. | |
34 | obj = A.Object() |
|
32 | obj = A.Object() | |
35 | dreload(B) |
|
33 | dreload(B) | |
36 |
|
|
34 | assert isinstance(obj, A.Object) is False |
@@ -1,272 +1,272 b'' | |||||
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 pathlib |
|
17 | import pathlib | |
18 | from unittest import TestCase, mock |
|
18 | from unittest import TestCase, mock | |
19 | import struct |
|
19 | import struct | |
20 | import wave |
|
20 | import wave | |
21 | from io import BytesIO |
|
21 | from io import BytesIO | |
22 |
|
22 | |||
23 | # Third-party imports |
|
23 | # Third-party imports | |
24 | import nose.tools as nt |
|
24 | import pytest | |
25 |
|
25 | |||
26 | try: |
|
26 | try: | |
27 | import numpy |
|
27 | import numpy | |
28 | except ImportError: |
|
28 | except ImportError: | |
29 | pass |
|
29 | pass | |
30 |
|
30 | |||
31 | # Our own imports |
|
31 | # Our own imports | |
32 | from IPython.lib import display |
|
32 | from IPython.lib import display | |
33 |
|
33 | |||
34 | from IPython.testing.decorators import skipif_not_numpy |
|
34 | from IPython.testing.decorators import skipif_not_numpy | |
35 |
|
35 | |||
36 | #----------------------------------------------------------------------------- |
|
36 | #----------------------------------------------------------------------------- | |
37 | # Classes and functions |
|
37 | # Classes and functions | |
38 | #----------------------------------------------------------------------------- |
|
38 | #----------------------------------------------------------------------------- | |
39 |
|
39 | |||
40 | #-------------------------- |
|
40 | #-------------------------- | |
41 | # FileLink tests |
|
41 | # FileLink tests | |
42 | #-------------------------- |
|
42 | #-------------------------- | |
43 |
|
43 | |||
44 | def test_instantiation_FileLink(): |
|
44 | def test_instantiation_FileLink(): | |
45 | """FileLink: Test class can be instantiated""" |
|
45 | """FileLink: Test class can be instantiated""" | |
46 | fl = display.FileLink('example.txt') |
|
46 | fl = display.FileLink('example.txt') | |
47 | # TODO: remove if when only Python >= 3.6 is supported |
|
47 | # TODO: remove if when only Python >= 3.6 is supported | |
48 | fl = display.FileLink(pathlib.PurePath('example.txt')) |
|
48 | fl = display.FileLink(pathlib.PurePath('example.txt')) | |
49 |
|
49 | |||
50 | def test_warning_on_non_existent_path_FileLink(): |
|
50 | def test_warning_on_non_existent_path_FileLink(): | |
51 | """FileLink: Calling _repr_html_ on non-existent files returns a warning |
|
51 | """FileLink: Calling _repr_html_ on non-existent files returns a warning""" | |
52 | """ |
|
52 | fl = display.FileLink("example.txt") | |
53 | fl = display.FileLink('example.txt') |
|
53 | assert fl._repr_html_().startswith("Path (<tt>example.txt</tt>)") | |
54 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) |
|
54 | ||
55 |
|
55 | |||
56 | def test_existing_path_FileLink(): |
|
56 | def test_existing_path_FileLink(): | |
57 | """FileLink: Calling _repr_html_ functions as expected on existing filepath |
|
57 | """FileLink: Calling _repr_html_ functions as expected on existing filepath | |
58 | """ |
|
58 | """ | |
59 | tf = NamedTemporaryFile() |
|
59 | tf = NamedTemporaryFile() | |
60 | fl = display.FileLink(tf.name) |
|
60 | fl = display.FileLink(tf.name) | |
61 | actual = fl._repr_html_() |
|
61 | actual = fl._repr_html_() | |
62 | expected = "<a href='%s' target='_blank'>%s</a><br>" % (tf.name, tf.name) |
|
62 | expected = "<a href='%s' target='_blank'>%s</a><br>" % (tf.name, tf.name) | |
63 | assert actual == expected |
|
63 | assert actual == expected | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | def test_existing_path_FileLink_repr(): |
|
66 | def test_existing_path_FileLink_repr(): | |
67 | """FileLink: Calling repr() functions as expected on existing filepath |
|
67 | """FileLink: Calling repr() functions as expected on existing filepath | |
68 | """ |
|
68 | """ | |
69 | tf = NamedTemporaryFile() |
|
69 | tf = NamedTemporaryFile() | |
70 | fl = display.FileLink(tf.name) |
|
70 | fl = display.FileLink(tf.name) | |
71 | actual = repr(fl) |
|
71 | actual = repr(fl) | |
72 | expected = tf.name |
|
72 | expected = tf.name | |
73 | assert actual == expected |
|
73 | assert actual == expected | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | def test_error_on_directory_to_FileLink(): |
|
76 | def test_error_on_directory_to_FileLink(): | |
77 | """FileLink: Raises error when passed directory |
|
77 | """FileLink: Raises error when passed directory | |
78 | """ |
|
78 | """ | |
79 | td = mkdtemp() |
|
79 | td = mkdtemp() | |
80 |
|
|
80 | pytest.raises(ValueError, display.FileLink, td) | |
81 |
|
81 | |||
82 | #-------------------------- |
|
82 | #-------------------------- | |
83 | # FileLinks tests |
|
83 | # FileLinks tests | |
84 | #-------------------------- |
|
84 | #-------------------------- | |
85 |
|
85 | |||
86 | def test_instantiation_FileLinks(): |
|
86 | def test_instantiation_FileLinks(): | |
87 | """FileLinks: Test class can be instantiated |
|
87 | """FileLinks: Test class can be instantiated | |
88 | """ |
|
88 | """ | |
89 | fls = display.FileLinks('example') |
|
89 | fls = display.FileLinks('example') | |
90 |
|
90 | |||
91 | def test_warning_on_non_existent_path_FileLinks(): |
|
91 | def test_warning_on_non_existent_path_FileLinks(): | |
92 | """FileLinks: Calling _repr_html_ on non-existent files returns a warning |
|
92 | """FileLinks: Calling _repr_html_ on non-existent files returns a warning""" | |
93 | """ |
|
93 | fls = display.FileLinks("example") | |
94 | fls = display.FileLinks('example') |
|
94 | assert fls._repr_html_().startswith("Path (<tt>example</tt>)") | |
95 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
95 | ||
96 |
|
96 | |||
97 | def test_existing_path_FileLinks(): |
|
97 | def test_existing_path_FileLinks(): | |
98 | """FileLinks: Calling _repr_html_ functions as expected on existing dir |
|
98 | """FileLinks: Calling _repr_html_ functions as expected on existing dir | |
99 | """ |
|
99 | """ | |
100 | td = mkdtemp() |
|
100 | td = mkdtemp() | |
101 | tf1 = NamedTemporaryFile(dir=td) |
|
101 | tf1 = NamedTemporaryFile(dir=td) | |
102 | tf2 = NamedTemporaryFile(dir=td) |
|
102 | tf2 = NamedTemporaryFile(dir=td) | |
103 | fl = display.FileLinks(td) |
|
103 | fl = display.FileLinks(td) | |
104 | actual = fl._repr_html_() |
|
104 | actual = fl._repr_html_() | |
105 | actual = actual.split('\n') |
|
105 | actual = actual.split('\n') | |
106 | actual.sort() |
|
106 | actual.sort() | |
107 | # the links should always have forward slashes, even on windows, so replace |
|
107 | # the links should always have forward slashes, even on windows, so replace | |
108 | # backslashes with forward slashes here |
|
108 | # backslashes with forward slashes here | |
109 | expected = ["%s/<br>" % td, |
|
109 | expected = ["%s/<br>" % td, | |
110 | " <a href='%s' target='_blank'>%s</a><br>" %\ |
|
110 | " <a href='%s' target='_blank'>%s</a><br>" %\ | |
111 | (tf2.name.replace("\\","/"),split(tf2.name)[1]), |
|
111 | (tf2.name.replace("\\","/"),split(tf2.name)[1]), | |
112 | " <a href='%s' target='_blank'>%s</a><br>" %\ |
|
112 | " <a href='%s' target='_blank'>%s</a><br>" %\ | |
113 | (tf1.name.replace("\\","/"),split(tf1.name)[1])] |
|
113 | (tf1.name.replace("\\","/"),split(tf1.name)[1])] | |
114 | expected.sort() |
|
114 | expected.sort() | |
115 | # We compare the sorted list of links here as that's more reliable |
|
115 | # We compare the sorted list of links here as that's more reliable | |
116 | assert actual == expected |
|
116 | assert actual == expected | |
117 |
|
117 | |||
118 |
|
118 | |||
119 | def test_existing_path_FileLinks_alt_formatter(): |
|
119 | def test_existing_path_FileLinks_alt_formatter(): | |
120 | """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter |
|
120 | """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter | |
121 | """ |
|
121 | """ | |
122 | td = mkdtemp() |
|
122 | td = mkdtemp() | |
123 | tf1 = NamedTemporaryFile(dir=td) |
|
123 | tf1 = NamedTemporaryFile(dir=td) | |
124 | tf2 = NamedTemporaryFile(dir=td) |
|
124 | tf2 = NamedTemporaryFile(dir=td) | |
125 | def fake_formatter(dirname,fnames,included_suffixes): |
|
125 | def fake_formatter(dirname,fnames,included_suffixes): | |
126 | return ["hello","world"] |
|
126 | return ["hello","world"] | |
127 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) |
|
127 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) | |
128 | actual = fl._repr_html_() |
|
128 | actual = fl._repr_html_() | |
129 | actual = actual.split('\n') |
|
129 | actual = actual.split('\n') | |
130 | actual.sort() |
|
130 | actual.sort() | |
131 | expected = ["hello","world"] |
|
131 | expected = ["hello","world"] | |
132 | expected.sort() |
|
132 | expected.sort() | |
133 | # We compare the sorted list of links here as that's more reliable |
|
133 | # We compare the sorted list of links here as that's more reliable | |
134 | assert actual == expected |
|
134 | assert actual == expected | |
135 |
|
135 | |||
136 |
|
136 | |||
137 | def test_existing_path_FileLinks_repr(): |
|
137 | def test_existing_path_FileLinks_repr(): | |
138 | """FileLinks: Calling repr() functions as expected on existing directory """ |
|
138 | """FileLinks: Calling repr() functions as expected on existing directory """ | |
139 | td = mkdtemp() |
|
139 | td = mkdtemp() | |
140 | tf1 = NamedTemporaryFile(dir=td) |
|
140 | tf1 = NamedTemporaryFile(dir=td) | |
141 | tf2 = NamedTemporaryFile(dir=td) |
|
141 | tf2 = NamedTemporaryFile(dir=td) | |
142 | fl = display.FileLinks(td) |
|
142 | fl = display.FileLinks(td) | |
143 | actual = repr(fl) |
|
143 | actual = repr(fl) | |
144 | actual = actual.split('\n') |
|
144 | actual = actual.split('\n') | |
145 | actual.sort() |
|
145 | actual.sort() | |
146 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] |
|
146 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] | |
147 | expected.sort() |
|
147 | expected.sort() | |
148 | # We compare the sorted list of links here as that's more reliable |
|
148 | # We compare the sorted list of links here as that's more reliable | |
149 | assert actual == expected |
|
149 | assert actual == expected | |
150 |
|
150 | |||
151 |
|
151 | |||
152 | def test_existing_path_FileLinks_repr_alt_formatter(): |
|
152 | def test_existing_path_FileLinks_repr_alt_formatter(): | |
153 | """FileLinks: Calling repr() functions as expected w/ alt formatter |
|
153 | """FileLinks: Calling repr() functions as expected w/ alt formatter | |
154 | """ |
|
154 | """ | |
155 | td = mkdtemp() |
|
155 | td = mkdtemp() | |
156 | tf1 = NamedTemporaryFile(dir=td) |
|
156 | tf1 = NamedTemporaryFile(dir=td) | |
157 | tf2 = NamedTemporaryFile(dir=td) |
|
157 | tf2 = NamedTemporaryFile(dir=td) | |
158 | def fake_formatter(dirname,fnames,included_suffixes): |
|
158 | def fake_formatter(dirname,fnames,included_suffixes): | |
159 | return ["hello","world"] |
|
159 | return ["hello","world"] | |
160 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) |
|
160 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) | |
161 | actual = repr(fl) |
|
161 | actual = repr(fl) | |
162 | actual = actual.split('\n') |
|
162 | actual = actual.split('\n') | |
163 | actual.sort() |
|
163 | actual.sort() | |
164 | expected = ["hello","world"] |
|
164 | expected = ["hello","world"] | |
165 | expected.sort() |
|
165 | expected.sort() | |
166 | # We compare the sorted list of links here as that's more reliable |
|
166 | # We compare the sorted list of links here as that's more reliable | |
167 | assert actual == expected |
|
167 | assert actual == expected | |
168 |
|
168 | |||
169 |
|
169 | |||
170 | def test_error_on_file_to_FileLinks(): |
|
170 | def test_error_on_file_to_FileLinks(): | |
171 | """FileLinks: Raises error when passed file |
|
171 | """FileLinks: Raises error when passed file | |
172 | """ |
|
172 | """ | |
173 | td = mkdtemp() |
|
173 | td = mkdtemp() | |
174 | tf1 = NamedTemporaryFile(dir=td) |
|
174 | tf1 = NamedTemporaryFile(dir=td) | |
175 |
|
|
175 | pytest.raises(ValueError, display.FileLinks, tf1.name) | |
|
176 | ||||
176 |
|
177 | |||
177 | def test_recursive_FileLinks(): |
|
178 | def test_recursive_FileLinks(): | |
178 | """FileLinks: Does not recurse when recursive=False |
|
179 | """FileLinks: Does not recurse when recursive=False | |
179 | """ |
|
180 | """ | |
180 | td = mkdtemp() |
|
181 | td = mkdtemp() | |
181 | tf = NamedTemporaryFile(dir=td) |
|
182 | tf = NamedTemporaryFile(dir=td) | |
182 | subtd = mkdtemp(dir=td) |
|
183 | subtd = mkdtemp(dir=td) | |
183 | subtf = NamedTemporaryFile(dir=subtd) |
|
184 | subtf = NamedTemporaryFile(dir=subtd) | |
184 | fl = display.FileLinks(td) |
|
185 | fl = display.FileLinks(td) | |
185 | actual = str(fl) |
|
186 | actual = str(fl) | |
186 | actual = actual.split('\n') |
|
187 | actual = actual.split('\n') | |
187 | assert len(actual) == 4, actual |
|
188 | assert len(actual) == 4, actual | |
188 | fl = display.FileLinks(td, recursive=False) |
|
189 | fl = display.FileLinks(td, recursive=False) | |
189 | actual = str(fl) |
|
190 | actual = str(fl) | |
190 | actual = actual.split('\n') |
|
191 | actual = actual.split('\n') | |
191 | assert len(actual) == 2, actual |
|
192 | assert len(actual) == 2, actual | |
192 |
|
193 | |||
193 | def test_audio_from_file(): |
|
194 | def test_audio_from_file(): | |
194 | path = pjoin(dirname(__file__), 'test.wav') |
|
195 | path = pjoin(dirname(__file__), 'test.wav') | |
195 | display.Audio(filename=path) |
|
196 | display.Audio(filename=path) | |
196 |
|
197 | |||
197 | class TestAudioDataWithNumpy(TestCase): |
|
198 | class TestAudioDataWithNumpy(TestCase): | |
198 |
|
199 | |||
199 | @skipif_not_numpy |
|
200 | @skipif_not_numpy | |
200 | def test_audio_from_numpy_array(self): |
|
201 | def test_audio_from_numpy_array(self): | |
201 | test_tone = get_test_tone() |
|
202 | test_tone = get_test_tone() | |
202 | audio = display.Audio(test_tone, rate=44100) |
|
203 | audio = display.Audio(test_tone, rate=44100) | |
203 | assert len(read_wav(audio.data)) == len(test_tone) |
|
204 | assert len(read_wav(audio.data)) == len(test_tone) | |
204 |
|
205 | |||
205 | @skipif_not_numpy |
|
206 | @skipif_not_numpy | |
206 | def test_audio_from_list(self): |
|
207 | def test_audio_from_list(self): | |
207 | test_tone = get_test_tone() |
|
208 | test_tone = get_test_tone() | |
208 | audio = display.Audio(list(test_tone), rate=44100) |
|
209 | audio = display.Audio(list(test_tone), rate=44100) | |
209 | assert len(read_wav(audio.data)) == len(test_tone) |
|
210 | assert len(read_wav(audio.data)) == len(test_tone) | |
210 |
|
211 | |||
211 | @skipif_not_numpy |
|
212 | @skipif_not_numpy | |
212 | def test_audio_from_numpy_array_without_rate_raises(self): |
|
213 | def test_audio_from_numpy_array_without_rate_raises(self): | |
213 |
|
|
214 | self.assertRaises(ValueError, display.Audio, get_test_tone()) | |
214 |
|
215 | |||
215 | @skipif_not_numpy |
|
216 | @skipif_not_numpy | |
216 | def test_audio_data_normalization(self): |
|
217 | def test_audio_data_normalization(self): | |
217 | expected_max_value = numpy.iinfo(numpy.int16).max |
|
218 | expected_max_value = numpy.iinfo(numpy.int16).max | |
218 | for scale in [1, 0.5, 2]: |
|
219 | for scale in [1, 0.5, 2]: | |
219 | audio = display.Audio(get_test_tone(scale), rate=44100) |
|
220 | audio = display.Audio(get_test_tone(scale), rate=44100) | |
220 | actual_max_value = numpy.max(numpy.abs(read_wav(audio.data))) |
|
221 | actual_max_value = numpy.max(numpy.abs(read_wav(audio.data))) | |
221 | assert actual_max_value == expected_max_value |
|
222 | assert actual_max_value == expected_max_value | |
222 |
|
223 | |||
223 | @skipif_not_numpy |
|
224 | @skipif_not_numpy | |
224 | def test_audio_data_without_normalization(self): |
|
225 | def test_audio_data_without_normalization(self): | |
225 | max_int16 = numpy.iinfo(numpy.int16).max |
|
226 | max_int16 = numpy.iinfo(numpy.int16).max | |
226 | for scale in [1, 0.5, 0.2]: |
|
227 | for scale in [1, 0.5, 0.2]: | |
227 | test_tone = get_test_tone(scale) |
|
228 | test_tone = get_test_tone(scale) | |
228 | test_tone_max_abs = numpy.max(numpy.abs(test_tone)) |
|
229 | test_tone_max_abs = numpy.max(numpy.abs(test_tone)) | |
229 | expected_max_value = int(max_int16 * test_tone_max_abs) |
|
230 | expected_max_value = int(max_int16 * test_tone_max_abs) | |
230 | audio = display.Audio(test_tone, rate=44100, normalize=False) |
|
231 | audio = display.Audio(test_tone, rate=44100, normalize=False) | |
231 | actual_max_value = numpy.max(numpy.abs(read_wav(audio.data))) |
|
232 | actual_max_value = numpy.max(numpy.abs(read_wav(audio.data))) | |
232 | assert actual_max_value == expected_max_value |
|
233 | assert actual_max_value == expected_max_value | |
233 |
|
234 | |||
234 | def test_audio_data_without_normalization_raises_for_invalid_data(self): |
|
235 | def test_audio_data_without_normalization_raises_for_invalid_data(self): | |
235 |
|
|
236 | self.assertRaises( | |
236 | ValueError, |
|
237 | ValueError, | |
237 | lambda: display.Audio([1.001], rate=44100, normalize=False)) |
|
238 | lambda: display.Audio([1.001], rate=44100, normalize=False)) | |
238 |
|
|
239 | self.assertRaises( | |
239 | ValueError, |
|
240 | ValueError, | |
240 | lambda: display.Audio([-1.001], rate=44100, normalize=False)) |
|
241 | lambda: display.Audio([-1.001], rate=44100, normalize=False)) | |
241 |
|
242 | |||
242 | def simulate_numpy_not_installed(): |
|
243 | def simulate_numpy_not_installed(): | |
243 | try: |
|
244 | try: | |
244 | import numpy |
|
245 | import numpy | |
245 | return mock.patch('numpy.array', mock.MagicMock(side_effect=ImportError)) |
|
246 | return mock.patch('numpy.array', mock.MagicMock(side_effect=ImportError)) | |
246 | except ModuleNotFoundError: |
|
247 | except ModuleNotFoundError: | |
247 | return lambda x:x |
|
248 | return lambda x:x | |
248 |
|
249 | |||
249 | @simulate_numpy_not_installed() |
|
250 | @simulate_numpy_not_installed() | |
250 | class TestAudioDataWithoutNumpy(TestAudioDataWithNumpy): |
|
251 | class TestAudioDataWithoutNumpy(TestAudioDataWithNumpy): | |
251 | # All tests from `TestAudioDataWithNumpy` are inherited. |
|
252 | # All tests from `TestAudioDataWithNumpy` are inherited. | |
252 |
|
253 | |||
253 | @skipif_not_numpy |
|
254 | @skipif_not_numpy | |
254 | def test_audio_raises_for_nested_list(self): |
|
255 | def test_audio_raises_for_nested_list(self): | |
255 | stereo_signal = [list(get_test_tone())] * 2 |
|
256 | stereo_signal = [list(get_test_tone())] * 2 | |
256 | nt.assert_raises( |
|
257 | self.assertRaises(TypeError, lambda: display.Audio(stereo_signal, rate=44100)) | |
257 | TypeError, |
|
258 | ||
258 | lambda: display.Audio(stereo_signal, rate=44100)) |
|
|||
259 |
|
259 | |||
260 | @skipif_not_numpy |
|
260 | @skipif_not_numpy | |
261 | def get_test_tone(scale=1): |
|
261 | def get_test_tone(scale=1): | |
262 | return numpy.sin(2 * numpy.pi * 440 * numpy.linspace(0, 1, 44100)) * scale |
|
262 | return numpy.sin(2 * numpy.pi * 440 * numpy.linspace(0, 1, 44100)) * scale | |
263 |
|
263 | |||
264 | def read_wav(data): |
|
264 | def read_wav(data): | |
265 | with wave.open(BytesIO(data)) as wave_file: |
|
265 | with wave.open(BytesIO(data)) as wave_file: | |
266 | wave_data = wave_file.readframes(wave_file.getnframes()) |
|
266 | wave_data = wave_file.readframes(wave_file.getnframes()) | |
267 | num_samples = wave_file.getnframes() * wave_file.getnchannels() |
|
267 | num_samples = wave_file.getnframes() * wave_file.getnchannels() | |
268 | return struct.unpack('<%sh' % num_samples, wave_data) |
|
268 | return struct.unpack('<%sh' % num_samples, wave_data) | |
269 |
|
269 | |||
270 | def test_code_from_file(): |
|
270 | def test_code_from_file(): | |
271 | c = display.Code(filename=__file__) |
|
271 | c = display.Code(filename=__file__) | |
272 | assert c._repr_html_().startswith('<style>') |
|
272 | assert c._repr_html_().startswith('<style>') |
General Comments 0
You need to be logged in to leave comments.
Login now