Show More
@@ -1,64 +1,112 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 | |||
|
17 | ||||
16 |
|
18 | |||
17 | # Third-party imports |
|
19 | # Third-party imports | |
18 | import nose.tools as nt |
|
20 | import nose.tools as nt | |
19 |
|
21 | |||
20 | # Our own imports |
|
22 | # Our own imports | |
21 | from IPython.lib import display |
|
23 | from IPython.lib import display | |
22 |
|
24 | |||
23 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
24 | # Classes and functions |
|
26 | # Classes and functions | |
25 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
26 |
|
28 | |||
27 | #-------------------------- |
|
29 | #-------------------------- | |
28 | # FileLink tests |
|
30 | # FileLink tests | |
29 | #-------------------------- |
|
31 | #-------------------------- | |
30 |
|
32 | |||
31 | def test_instantiation_FileLink(): |
|
33 | def test_instantiation_FileLink(): | |
32 | """Test classes can be instantiated""" |
|
34 | """Test classes can be instantiated""" | |
33 | fl = display.FileLink('example.txt') |
|
35 | fl = display.FileLink('example.txt') | |
34 |
|
36 | |||
35 | def test_warning_on_non_existant_path_FileLink(): |
|
37 | def test_warning_on_non_existant_path_FileLink(): | |
36 | """Calling _repr_html_ on non-existant files returns a warning""" |
|
38 | """Calling _repr_html_ on non-existant files returns a warning""" | |
37 | fl = display.FileLink('example.txt') |
|
39 | fl = display.FileLink('example.txt') | |
38 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) |
|
40 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) | |
39 |
|
41 | |||
|
42 | def test_existing_path_FileLink(): | |||
|
43 | """ Calling _repr_html_ functions as expected on existing filepath """ | |||
|
44 | tf = NamedTemporaryFile() | |||
|
45 | fl = display.FileLink(tf.name) | |||
|
46 | actual = fl._repr_html_() | |||
|
47 | # Note: this is a bit awkward since tf.name has to be a full path | |||
|
48 | # for it to exist, but that won't happen in our applications. While | |||
|
49 | # in practice "files/" would be prepended to the href path, os.join | |||
|
50 | # doesn't do that if the second value is an absolute path. So, the | |||
|
51 | # expected here is slightly different than what we get in practice. | |||
|
52 | expected = "<a href='%s' target='_blank'>%s</a><br>" % (tf.name,tf.name) | |||
|
53 | nt.assert_equal(actual,expected) | |||
|
54 | ||||
40 | #-------------------------- |
|
55 | #-------------------------- | |
41 | # FileLinks tests |
|
56 | # FileLinks tests | |
42 | #-------------------------- |
|
57 | #-------------------------- | |
43 |
|
58 | |||
44 | def test_instantiation_FileLinks(): |
|
59 | def test_instantiation_FileLinks(): | |
45 | """Test classes can be instantiated""" |
|
60 | """Test classes can be instantiated""" | |
46 |
fls = display.FileLinks( |
|
61 | fls = display.FileLinks('example') | |
47 |
|
62 | |||
48 | def test_warning_on_non_existant_path_FileLinks(): |
|
63 | def test_warning_on_non_existant_path_FileLinks(): | |
49 | """Calling _repr_html_ on non-existant files returns a warning""" |
|
64 | """Calling _repr_html_ on non-existant files returns a warning""" | |
50 | fls = display.FileLinks('example') |
|
65 | fls = display.FileLinks('example') | |
51 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
66 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) | |
52 |
|
67 | |||
|
68 | def test_existing_path_FileLinks(): | |||
|
69 | """ Calling _repr_html_ functions as expected on existing directory """ | |||
|
70 | td = mkdtemp() | |||
|
71 | tf1 = NamedTemporaryFile(dir=td) | |||
|
72 | tf2 = NamedTemporaryFile(dir=td) | |||
|
73 | fl = display.FileLinks(td) | |||
|
74 | actual = fl._repr_html_() | |||
|
75 | actual = actual.split('\n') | |||
|
76 | actual.sort() | |||
|
77 | # Note: this is a bit awkward since tf.name has to be a full path | |||
|
78 | # for it to exist, but that won't happen in our applications. While | |||
|
79 | # in practice "files/" would be prepended to the href path, os.join | |||
|
80 | # doesn't do that if the second value is an absolute path. So, the | |||
|
81 | # expected here is slightly different than what we get in practice. | |||
|
82 | expected = ["<a href='%s' target='_blank'>%s</a><br>" % (tf2.name,tf2.name.split('/')[-1]), | |||
|
83 | "<a href='%s' target='_blank'>%s</a><br>" % (tf1.name,tf1.name.split('/')[-1])] | |||
|
84 | expected.sort() | |||
|
85 | # We compare the sorted list of links here as that's more reliable | |||
|
86 | nt.assert_equal(actual,expected) | |||
|
87 | ||||
53 | #-------------------------- |
|
88 | #-------------------------- | |
54 | # DirectoryLink tests |
|
89 | # DirectoryLink tests | |
55 | #-------------------------- |
|
90 | #-------------------------- | |
56 |
|
91 | |||
57 | def test_instantiation_DirectoryLink(): |
|
92 | def test_instantiation_DirectoryLink(): | |
58 | """Test classes can be instantiated""" |
|
93 | """Test classes can be instantiated""" | |
59 | dl = display.DirectoryLink('example') |
|
94 | dl = display.DirectoryLink('example') | |
60 |
|
95 | |||
61 | def test_warning_on_non_existant_path_DirectoryLink(): |
|
96 | def test_warning_on_non_existant_path_DirectoryLink(): | |
62 | """Calling _repr_html_ on non-existant files returns a warning""" |
|
97 | """Calling _repr_html_ on non-existant files returns a warning""" | |
63 | dl = display.DirectoryLink('example') |
|
98 | dl = display.DirectoryLink('example') | |
64 | nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
99 | nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)')) | |
|
100 | ||||
|
101 | def test_existing_path_FileLinks(): | |||
|
102 | """ Calling _repr_html_ functions as expected on existing directory """ | |||
|
103 | td = mkdtemp() | |||
|
104 | dl = display.DirectoryLink(td) | |||
|
105 | actual = dl._repr_html_() | |||
|
106 | # Note: this is a bit awkward since tf.name has to be a full path | |||
|
107 | # for it to exist, but that won't happen in our applications. While | |||
|
108 | # in practice "files/" would be prepended to the href path, os.join | |||
|
109 | # doesn't do that if the second value is an absolute path. So, the | |||
|
110 | # expected here is slightly different than what we get in practice. | |||
|
111 | expected = "<a href='%s' target='_blank'>%s</a><br>" % (td,td) | |||
|
112 | nt.assert_equal(actual,expected) |
General Comments 0
You need to be logged in to leave comments.
Login now