##// END OF EJS Templates
added tests of repr()
Greg Caporaso -
Show More
@@ -1,97 +1,119 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
17
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
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Classes and functions
26 # Classes and functions
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 #--------------------------
29 #--------------------------
30 # FileLink tests
30 # FileLink tests
31 #--------------------------
31 #--------------------------
32
32
33 def test_instantiation_FileLink():
33 def test_instantiation_FileLink():
34 """Test classes can be instantiated"""
34 """Test classes can be instantiated"""
35 fl = display.FileLink('example.txt')
35 fl = display.FileLink('example.txt')
36
36
37 def test_warning_on_non_existant_path_FileLink():
37 def test_warning_on_non_existant_path_FileLink():
38 """Calling _repr_html_ on non-existant files returns a warning"""
38 """Calling _repr_html_ on non-existant files returns a warning"""
39 fl = display.FileLink('example.txt')
39 fl = display.FileLink('example.txt')
40 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>)'))
41
41
42 def test_existing_path_FileLink():
42 def test_existing_path_FileLink():
43 """ Calling _repr_html_ functions as expected on existing filepath """
43 """ Calling _repr_html_ functions as expected on existing filepath """
44 tf = NamedTemporaryFile()
44 tf = NamedTemporaryFile()
45 fl = display.FileLink(tf.name)
45 fl = display.FileLink(tf.name)
46 actual = fl._repr_html_()
46 actual = fl._repr_html_()
47 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
47 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
48 nt.assert_equal(actual,expected)
48 nt.assert_equal(actual,expected)
49
49
50 def test_existing_path_FileLink_repr():
51 """ Calling repr() functions as expected on existing filepath """
52 tf = NamedTemporaryFile()
53 fl = display.FileLink(tf.name)
54 actual = repr(fl)
55 expected = tf.name
56 nt.assert_equal(actual,expected)
57
50 #--------------------------
58 #--------------------------
51 # FileLinks tests
59 # FileLinks tests
52 #--------------------------
60 #--------------------------
53
61
54 def test_instantiation_FileLinks():
62 def test_instantiation_FileLinks():
55 """Test classes can be instantiated"""
63 """Test classes can be instantiated"""
56 fls = display.FileLinks('example')
64 fls = display.FileLinks('example')
57
65
58 def test_warning_on_non_existant_path_FileLinks():
66 def test_warning_on_non_existant_path_FileLinks():
59 """Calling _repr_html_ on non-existant files returns a warning"""
67 """Calling _repr_html_ on non-existant files returns a warning"""
60 fls = display.FileLinks('example')
68 fls = display.FileLinks('example')
61 nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
69 nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
62
70
63 def test_existing_path_FileLinks():
71 def test_existing_path_FileLinks():
64 """ Calling _repr_html_ functions as expected on existing directory """
72 """ Calling _repr_html_ functions as expected on existing directory """
65 td = mkdtemp()
73 td = mkdtemp()
66 tf1 = NamedTemporaryFile(dir=td)
74 tf1 = NamedTemporaryFile(dir=td)
67 tf2 = NamedTemporaryFile(dir=td)
75 tf2 = NamedTemporaryFile(dir=td)
68 fl = display.FileLinks(td)
76 fl = display.FileLinks(td)
69 actual = fl._repr_html_()
77 actual = fl._repr_html_()
70 actual = actual.split('\n')
78 actual = actual.split('\n')
71 actual.sort()
79 actual.sort()
72 expected = ["<a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,tf2.name),
80 expected = ["<a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,tf2.name),
73 "<a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,tf1.name)]
81 "<a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,tf1.name)]
74 expected.sort()
82 expected.sort()
75 # We compare the sorted list of links here as that's more reliable
83 # We compare the sorted list of links here as that's more reliable
76 nt.assert_equal(actual,expected)
84 nt.assert_equal(actual,expected)
77
85
86 def test_existing_path_FileLinks_repr():
87 """ Calling repr() functions as expected on existing directory """
88 td = mkdtemp()
89 tf1 = NamedTemporaryFile(dir=td)
90 tf2 = NamedTemporaryFile(dir=td)
91 fl = display.FileLinks(td)
92 actual = repr(fl)
93 actual = actual.split('\n')
94 actual.sort()
95 expected = [tf1.name,tf2.name]
96 expected.sort()
97 # We compare the sorted list of links here as that's more reliable
98 nt.assert_equal(actual,expected)
99
78 #--------------------------
100 #--------------------------
79 # DirectoryLink tests
101 # DirectoryLink tests
80 #--------------------------
102 #--------------------------
81
103
82 def test_instantiation_DirectoryLink():
104 def test_instantiation_DirectoryLink():
83 """Test classes can be instantiated"""
105 """Test classes can be instantiated"""
84 dl = display.DirectoryLink('example')
106 dl = display.DirectoryLink('example')
85
107
86 def test_warning_on_non_existant_path_DirectoryLink():
108 def test_warning_on_non_existant_path_DirectoryLink():
87 """Calling _repr_html_ on non-existant files returns a warning"""
109 """Calling _repr_html_ on non-existant files returns a warning"""
88 dl = display.DirectoryLink('example')
110 dl = display.DirectoryLink('example')
89 nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)'))
111 nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)'))
90
112
91 def test_existing_path_FileLinks():
113 def test_existing_path_FileLinks():
92 """ Calling _repr_html_ functions as expected on existing directory """
114 """ Calling _repr_html_ functions as expected on existing directory """
93 td = mkdtemp()
115 td = mkdtemp()
94 dl = display.DirectoryLink(td)
116 dl = display.DirectoryLink(td)
95 actual = dl._repr_html_()
117 actual = dl._repr_html_()
96 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (td,td)
118 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (td,td)
97 nt.assert_equal(actual,expected)
119 nt.assert_equal(actual,expected)
General Comments 0
You need to be logged in to leave comments. Login now