##// END OF EJS Templates
Add failing test for Audio display object
Thomas Kluyver -
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -1,157 +1,162 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 __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, join as pjoin, dirname
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 from IPython.testing.decorators import skipif_not_numpy
24
25
25 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
26 # Classes and functions
27 # Classes and functions
27 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
28
29
29 #--------------------------
30 #--------------------------
30 # FileLink tests
31 # FileLink tests
31 #--------------------------
32 #--------------------------
32
33
33 def test_instantiation_FileLink():
34 def test_instantiation_FileLink():
34 """FileLink: Test class can be instantiated"""
35 """FileLink: Test class can be instantiated"""
35 fl = display.FileLink('example.txt')
36 fl = display.FileLink('example.txt')
36
37
37 def test_warning_on_non_existant_path_FileLink():
38 def test_warning_on_non_existant_path_FileLink():
38 """FileLink: Calling _repr_html_ on non-existant files returns a warning
39 """FileLink: Calling _repr_html_ on non-existant files returns a warning
39 """
40 """
40 fl = display.FileLink('example.txt')
41 fl = display.FileLink('example.txt')
41 nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
42 nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
42
43
43 def test_existing_path_FileLink():
44 def test_existing_path_FileLink():
44 """FileLink: Calling _repr_html_ functions as expected on existing filepath
45 """FileLink: Calling _repr_html_ functions as expected on existing filepath
45 """
46 """
46 tf = NamedTemporaryFile()
47 tf = NamedTemporaryFile()
47 fl = display.FileLink(tf.name)
48 fl = display.FileLink(tf.name)
48 actual = fl._repr_html_()
49 actual = fl._repr_html_()
49 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
50 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
50 nt.assert_equal(actual,expected)
51 nt.assert_equal(actual,expected)
51
52
52 def test_existing_path_FileLink_repr():
53 def test_existing_path_FileLink_repr():
53 """FileLink: Calling repr() functions as expected on existing filepath
54 """FileLink: Calling repr() functions as expected on existing filepath
54 """
55 """
55 tf = NamedTemporaryFile()
56 tf = NamedTemporaryFile()
56 fl = display.FileLink(tf.name)
57 fl = display.FileLink(tf.name)
57 actual = repr(fl)
58 actual = repr(fl)
58 expected = tf.name
59 expected = tf.name
59 nt.assert_equal(actual,expected)
60 nt.assert_equal(actual,expected)
60
61
61 def test_error_on_directory_to_FileLink():
62 def test_error_on_directory_to_FileLink():
62 """FileLink: Raises error when passed directory
63 """FileLink: Raises error when passed directory
63 """
64 """
64 td = mkdtemp()
65 td = mkdtemp()
65 nt.assert_raises(ValueError,display.FileLink,td)
66 nt.assert_raises(ValueError,display.FileLink,td)
66
67
67 #--------------------------
68 #--------------------------
68 # FileLinks tests
69 # FileLinks tests
69 #--------------------------
70 #--------------------------
70
71
71 def test_instantiation_FileLinks():
72 def test_instantiation_FileLinks():
72 """FileLinks: Test class can be instantiated
73 """FileLinks: Test class can be instantiated
73 """
74 """
74 fls = display.FileLinks('example')
75 fls = display.FileLinks('example')
75
76
76 def test_warning_on_non_existant_path_FileLinks():
77 def test_warning_on_non_existant_path_FileLinks():
77 """FileLinks: Calling _repr_html_ on non-existant files returns a warning
78 """FileLinks: Calling _repr_html_ on non-existant files returns a warning
78 """
79 """
79 fls = display.FileLinks('example')
80 fls = display.FileLinks('example')
80 nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
81 nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
81
82
82 def test_existing_path_FileLinks():
83 def test_existing_path_FileLinks():
83 """FileLinks: Calling _repr_html_ functions as expected on existing dir
84 """FileLinks: Calling _repr_html_ functions as expected on existing dir
84 """
85 """
85 td = mkdtemp()
86 td = mkdtemp()
86 tf1 = NamedTemporaryFile(dir=td)
87 tf1 = NamedTemporaryFile(dir=td)
87 tf2 = NamedTemporaryFile(dir=td)
88 tf2 = NamedTemporaryFile(dir=td)
88 fl = display.FileLinks(td)
89 fl = display.FileLinks(td)
89 actual = fl._repr_html_()
90 actual = fl._repr_html_()
90 actual = actual.split('\n')
91 actual = actual.split('\n')
91 actual.sort()
92 actual.sort()
92 # the links should always have forward slashes, even on windows, so replace
93 # the links should always have forward slashes, even on windows, so replace
93 # backslashes with forward slashes here
94 # backslashes with forward slashes here
94 expected = ["%s/<br>" % td,
95 expected = ["%s/<br>" % td,
95 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" %\
96 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" %\
96 (tf2.name.replace("\\","/"),split(tf2.name)[1]),
97 (tf2.name.replace("\\","/"),split(tf2.name)[1]),
97 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" %\
98 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" %\
98 (tf1.name.replace("\\","/"),split(tf1.name)[1])]
99 (tf1.name.replace("\\","/"),split(tf1.name)[1])]
99 expected.sort()
100 expected.sort()
100 # We compare the sorted list of links here as that's more reliable
101 # We compare the sorted list of links here as that's more reliable
101 nt.assert_equal(actual,expected)
102 nt.assert_equal(actual,expected)
102
103
103 def test_existing_path_FileLinks_alt_formatter():
104 def test_existing_path_FileLinks_alt_formatter():
104 """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter
105 """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter
105 """
106 """
106 td = mkdtemp()
107 td = mkdtemp()
107 tf1 = NamedTemporaryFile(dir=td)
108 tf1 = NamedTemporaryFile(dir=td)
108 tf2 = NamedTemporaryFile(dir=td)
109 tf2 = NamedTemporaryFile(dir=td)
109 def fake_formatter(dirname,fnames,included_suffixes):
110 def fake_formatter(dirname,fnames,included_suffixes):
110 return ["hello","world"]
111 return ["hello","world"]
111 fl = display.FileLinks(td,notebook_display_formatter=fake_formatter)
112 fl = display.FileLinks(td,notebook_display_formatter=fake_formatter)
112 actual = fl._repr_html_()
113 actual = fl._repr_html_()
113 actual = actual.split('\n')
114 actual = actual.split('\n')
114 actual.sort()
115 actual.sort()
115 expected = ["hello","world"]
116 expected = ["hello","world"]
116 expected.sort()
117 expected.sort()
117 # We compare the sorted list of links here as that's more reliable
118 # We compare the sorted list of links here as that's more reliable
118 nt.assert_equal(actual,expected)
119 nt.assert_equal(actual,expected)
119
120
120 def test_existing_path_FileLinks_repr():
121 def test_existing_path_FileLinks_repr():
121 """FileLinks: Calling repr() functions as expected on existing directory """
122 """FileLinks: Calling repr() functions as expected on existing directory """
122 td = mkdtemp()
123 td = mkdtemp()
123 tf1 = NamedTemporaryFile(dir=td)
124 tf1 = NamedTemporaryFile(dir=td)
124 tf2 = NamedTemporaryFile(dir=td)
125 tf2 = NamedTemporaryFile(dir=td)
125 fl = display.FileLinks(td)
126 fl = display.FileLinks(td)
126 actual = repr(fl)
127 actual = repr(fl)
127 actual = actual.split('\n')
128 actual = actual.split('\n')
128 actual.sort()
129 actual.sort()
129 expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]]
130 expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]]
130 expected.sort()
131 expected.sort()
131 # We compare the sorted list of links here as that's more reliable
132 # We compare the sorted list of links here as that's more reliable
132 nt.assert_equal(actual,expected)
133 nt.assert_equal(actual,expected)
133
134
134 def test_existing_path_FileLinks_repr_alt_formatter():
135 def test_existing_path_FileLinks_repr_alt_formatter():
135 """FileLinks: Calling repr() functions as expected w/ alt formatter
136 """FileLinks: Calling repr() functions as expected w/ alt formatter
136 """
137 """
137 td = mkdtemp()
138 td = mkdtemp()
138 tf1 = NamedTemporaryFile(dir=td)
139 tf1 = NamedTemporaryFile(dir=td)
139 tf2 = NamedTemporaryFile(dir=td)
140 tf2 = NamedTemporaryFile(dir=td)
140 def fake_formatter(dirname,fnames,included_suffixes):
141 def fake_formatter(dirname,fnames,included_suffixes):
141 return ["hello","world"]
142 return ["hello","world"]
142 fl = display.FileLinks(td,terminal_display_formatter=fake_formatter)
143 fl = display.FileLinks(td,terminal_display_formatter=fake_formatter)
143 actual = repr(fl)
144 actual = repr(fl)
144 actual = actual.split('\n')
145 actual = actual.split('\n')
145 actual.sort()
146 actual.sort()
146 expected = ["hello","world"]
147 expected = ["hello","world"]
147 expected.sort()
148 expected.sort()
148 # We compare the sorted list of links here as that's more reliable
149 # We compare the sorted list of links here as that's more reliable
149 nt.assert_equal(actual,expected)
150 nt.assert_equal(actual,expected)
150
151
151 def test_error_on_file_to_FileLinks():
152 def test_error_on_file_to_FileLinks():
152 """FileLinks: Raises error when passed file
153 """FileLinks: Raises error when passed file
153 """
154 """
154 td = mkdtemp()
155 td = mkdtemp()
155 tf1 = NamedTemporaryFile(dir=td)
156 tf1 = NamedTemporaryFile(dir=td)
156 nt.assert_raises(ValueError,display.FileLinks,tf1.name)
157 nt.assert_raises(ValueError,display.FileLinks,tf1.name)
157
158
159 @skipif_not_numpy
160 def test_audio_from_file():
161 path = pjoin(dirname(__file__), 'test.wav')
162 display.Audio(filename=path) No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now