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