Show More
@@ -1,120 +1,156 | |||
|
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 | 17 | from os.path import split |
|
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 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Classes and functions |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | #-------------------------- |
|
30 | 30 | # FileLink tests |
|
31 | 31 | #-------------------------- |
|
32 | 32 | |
|
33 | 33 | def test_instantiation_FileLink(): |
|
34 | 34 | """Test classes can be instantiated""" |
|
35 | 35 | fl = display.FileLink('example.txt') |
|
36 | 36 | |
|
37 | 37 | def test_warning_on_non_existant_path_FileLink(): |
|
38 | 38 | """Calling _repr_html_ on non-existant files returns a warning""" |
|
39 | 39 | fl = display.FileLink('example.txt') |
|
40 | 40 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) |
|
41 | 41 | |
|
42 | 42 | def test_existing_path_FileLink(): |
|
43 | 43 | """ Calling _repr_html_ functions as expected on existing filepath """ |
|
44 | 44 | tf = NamedTemporaryFile() |
|
45 | 45 | fl = display.FileLink(tf.name) |
|
46 | 46 | actual = fl._repr_html_() |
|
47 | 47 | expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name) |
|
48 | 48 | nt.assert_equal(actual,expected) |
|
49 | 49 | |
|
50 | 50 | def test_existing_path_FileLink_repr(): |
|
51 | 51 | """ Calling repr() functions as expected on existing filepath """ |
|
52 | 52 | tf = NamedTemporaryFile() |
|
53 | 53 | fl = display.FileLink(tf.name) |
|
54 | 54 | actual = repr(fl) |
|
55 | 55 | expected = tf.name |
|
56 | 56 | nt.assert_equal(actual,expected) |
|
57 | 57 | |
|
58 | 58 | #-------------------------- |
|
59 | 59 | # FileLinks tests |
|
60 | 60 | #-------------------------- |
|
61 | 61 | |
|
62 | 62 | def test_instantiation_FileLinks(): |
|
63 | 63 | """Test classes can be instantiated""" |
|
64 | 64 | fls = display.FileLinks('example') |
|
65 | 65 | |
|
66 | 66 | def test_warning_on_non_existant_path_FileLinks(): |
|
67 | 67 | """Calling _repr_html_ on non-existant files returns a warning""" |
|
68 | 68 | fls = display.FileLinks('example') |
|
69 | 69 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
70 | 70 | |
|
71 | 71 | def test_existing_path_FileLinks(): |
|
72 | 72 | """ Calling _repr_html_ functions as expected on existing directory """ |
|
73 | 73 | td = mkdtemp() |
|
74 | 74 | tf1 = NamedTemporaryFile(dir=td) |
|
75 | 75 | tf2 = NamedTemporaryFile(dir=td) |
|
76 | 76 | fl = display.FileLinks(td) |
|
77 | 77 | actual = fl._repr_html_() |
|
78 | 78 | actual = actual.split('\n') |
|
79 | 79 | actual.sort() |
|
80 | 80 | expected = ["%s/<br>" % td, |
|
81 | 81 | " <a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,split(tf2.name)[1]), |
|
82 | 82 | " <a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,split(tf1.name)[1])] |
|
83 | 83 | expected.sort() |
|
84 | 84 | # We compare the sorted list of links here as that's more reliable |
|
85 | 85 | nt.assert_equal(actual,expected) |
|
86 | 86 | |
|
87 | def test_existing_path_FileLinks_alt_formatter(): | |
|
88 | """ Calling _repr_html_ functions as expected with an alternative formatter | |
|
89 | """ | |
|
90 | td = mkdtemp() | |
|
91 | tf1 = NamedTemporaryFile(dir=td) | |
|
92 | tf2 = NamedTemporaryFile(dir=td) | |
|
93 | def fake_formatter(output_lines,dirname,fnames): | |
|
94 | output_lines.extend(["hello","world"]) | |
|
95 | return | |
|
96 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) | |
|
97 | actual = fl._repr_html_() | |
|
98 | actual = actual.split('\n') | |
|
99 | actual.sort() | |
|
100 | expected = ["hello","world"] | |
|
101 | expected.sort() | |
|
102 | # We compare the sorted list of links here as that's more reliable | |
|
103 | nt.assert_equal(actual,expected) | |
|
104 | ||
|
87 | 105 | def test_existing_path_FileLinks_repr(): |
|
88 | 106 | """ Calling repr() functions as expected on existing directory """ |
|
89 | 107 | td = mkdtemp() |
|
90 | 108 | tf1 = NamedTemporaryFile(dir=td) |
|
91 | 109 | tf2 = NamedTemporaryFile(dir=td) |
|
92 | 110 | fl = display.FileLinks(td) |
|
93 | 111 | actual = repr(fl) |
|
94 | 112 | actual = actual.split('\n') |
|
95 | 113 | actual.sort() |
|
96 | 114 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] |
|
97 | 115 | expected.sort() |
|
98 | 116 | # We compare the sorted list of links here as that's more reliable |
|
99 | 117 | nt.assert_equal(actual,expected) |
|
100 | 118 | |
|
119 | def test_existing_path_FileLinks_repr_alt_formatter(): | |
|
120 | """ Calling repr() functions as expected with an alternative formatter | |
|
121 | """ | |
|
122 | td = mkdtemp() | |
|
123 | tf1 = NamedTemporaryFile(dir=td) | |
|
124 | tf2 = NamedTemporaryFile(dir=td) | |
|
125 | def fake_formatter(output_lines,dirname,fnames): | |
|
126 | output_lines.extend(["hello","world"]) | |
|
127 | return | |
|
128 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) | |
|
129 | actual = repr(fl) | |
|
130 | actual = actual.split('\n') | |
|
131 | actual.sort() | |
|
132 | expected = ["hello","world"] | |
|
133 | expected.sort() | |
|
134 | # We compare the sorted list of links here as that's more reliable | |
|
135 | nt.assert_equal(actual,expected) | |
|
136 | ||
|
101 | 137 | #-------------------------- |
|
102 | 138 | # DirectoryLink tests |
|
103 | 139 | #-------------------------- |
|
104 | 140 | |
|
105 | 141 | def test_instantiation_DirectoryLink(): |
|
106 | 142 | """Test classes can be instantiated""" |
|
107 | 143 | dl = display.DirectoryLink('example') |
|
108 | 144 | |
|
109 | 145 | def test_warning_on_non_existant_path_DirectoryLink(): |
|
110 | 146 | """Calling _repr_html_ on non-existant files returns a warning""" |
|
111 | 147 | dl = display.DirectoryLink('example') |
|
112 | 148 | nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
113 | 149 | |
|
114 | 150 | def test_existing_path_DirectoryLink(): |
|
115 | 151 | """ Calling _repr_html_ functions as expected on existing directory """ |
|
116 | 152 | td = mkdtemp() |
|
117 | 153 | dl = display.DirectoryLink(td) |
|
118 | 154 | actual = dl._repr_html_() |
|
119 | 155 | expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (td,td) |
|
120 | 156 | nt.assert_equal(actual,expected) |
General Comments 0
You need to be logged in to leave comments.
Login now