Show More
@@ -1,141 +1,148 | |||||
1 | """ |
|
1 | """ | |
2 | Module with tests for Strings |
|
2 | Module with tests for Strings | |
3 | """ |
|
3 | """ | |
4 |
|
4 | |||
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | import os |
|
16 | import os | |
17 |
|
17 | |||
18 | from ...tests.base import TestsBase |
|
18 | from ...tests.base import TestsBase | |
19 | from ..strings import (wrap_text, html2text, add_anchor, strip_dollars, |
|
19 | from ..strings import (wrap_text, html2text, add_anchor, strip_dollars, | |
20 | strip_files_prefix, get_lines, comment_lines, ipython2python, posix_path, |
|
20 | strip_files_prefix, get_lines, comment_lines, ipython2python, posix_path, | |
21 | add_prompts |
|
21 | add_prompts | |
22 | ) |
|
22 | ) | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | # Class |
|
26 | # Class | |
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 |
|
28 | |||
29 | class TestStrings(TestsBase): |
|
29 | class TestStrings(TestsBase): | |
30 |
|
30 | |||
31 | def test_wrap_text(self): |
|
31 | def test_wrap_text(self): | |
32 | """wrap_text test""" |
|
32 | """wrap_text test""" | |
33 | test_text = """ |
|
33 | test_text = """ | |
34 | Tush! never tell me; I take it much unkindly |
|
34 | Tush! never tell me; I take it much unkindly | |
35 | That thou, Iago, who hast had my purse |
|
35 | That thou, Iago, who hast had my purse | |
36 | As if the strings were thine, shouldst know of this. |
|
36 | As if the strings were thine, shouldst know of this. | |
37 | """ |
|
37 | """ | |
38 | for length in [30,5,1]: |
|
38 | for length in [30,5,1]: | |
39 | self._confirm_wrap_text(test_text, length) |
|
39 | self._confirm_wrap_text(test_text, length) | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | def _confirm_wrap_text(self, text, length): |
|
42 | def _confirm_wrap_text(self, text, length): | |
43 | for line in wrap_text(text, length).split('\n'): |
|
43 | for line in wrap_text(text, length).split('\n'): | |
44 | assert len(line) <= length |
|
44 | assert len(line) <= length | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | def test_html2text(self): |
|
47 | def test_html2text(self): | |
48 | """html2text test""" |
|
48 | """html2text test""" | |
49 | #TODO: More tests |
|
49 | #TODO: More tests | |
50 | self.assertEqual(html2text('<name>joe</name>'), 'joe') |
|
50 | self.assertEqual(html2text('<name>joe</name>'), 'joe') | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | def test_add_anchor(self): |
|
53 | def test_add_anchor(self): | |
54 | """add_anchor test""" |
|
54 | """add_anchor test""" | |
55 | #TODO: More tests |
|
55 | #TODO: More tests | |
56 | results = add_anchor('<b>Hello World!</b>') |
|
56 | results = add_anchor('<b>Hello World!</b>') | |
57 | assert 'Hello World!' in results |
|
57 | assert 'Hello World!' in results | |
58 | assert 'id="' in results |
|
58 | assert 'id="' in results | |
59 | assert 'class="anchor-link"' in results |
|
59 | assert 'class="anchor-link"' in results | |
60 | assert '<b' in results |
|
60 | assert '<b' in results | |
61 | assert '</b>' in results |
|
61 | assert '</b>' in results | |
62 |
|
62 | |||
63 |
|
63 | |||
64 | def test_strip_dollars(self): |
|
64 | def test_strip_dollars(self): | |
65 | """strip_dollars test""" |
|
65 | """strip_dollars test""" | |
66 | tests = [ |
|
66 | tests = [ | |
67 | ('', ''), |
|
67 | ('', ''), | |
68 | ('$$', ''), |
|
68 | ('$$', ''), | |
69 | ('$H$', 'H'), |
|
69 | ('$H$', 'H'), | |
70 | ('$He', 'He'), |
|
70 | ('$He', 'He'), | |
71 | ('H$el', 'H$el'), |
|
71 | ('H$el', 'H$el'), | |
72 | ('Hell$', 'Hell'), |
|
72 | ('Hell$', 'Hell'), | |
73 | ('Hello', 'Hello'), |
|
73 | ('Hello', 'Hello'), | |
74 | ('W$o$rld', 'W$o$rld')] |
|
74 | ('W$o$rld', 'W$o$rld')] | |
75 | for test in tests: |
|
75 | for test in tests: | |
76 | self._try_strip_dollars(test[0], test[1]) |
|
76 | self._try_strip_dollars(test[0], test[1]) | |
77 |
|
77 | |||
78 |
|
78 | |||
79 | def _try_strip_dollars(self, test, result): |
|
79 | def _try_strip_dollars(self, test, result): | |
80 | self.assertEqual(strip_dollars(test), result) |
|
80 | self.assertEqual(strip_dollars(test), result) | |
81 |
|
81 | |||
82 |
|
82 | |||
83 | def test_strip_files_prefix(self): |
|
83 | def test_strip_files_prefix(self): | |
84 | """strip_files_prefix test""" |
|
84 | """strip_files_prefix test""" | |
85 | tests = [ |
|
85 | tests = [ | |
86 | ('', ''), |
|
86 | ('', ''), | |
87 | ('/files', '/files'), |
|
87 | ('/files', '/files'), | |
88 | ('test="/files"', 'test="/files"'), |
|
88 | ('test="/files"', 'test="/files"'), | |
89 | ('My files are in `files/`', 'My files are in `files/`'), |
|
89 | ('My files are in `files/`', 'My files are in `files/`'), | |
90 | ('<a href="files/test.html">files/test.html</a>', '<a href="test.html">files/test.html</a>'), |
|
90 | ('<a href="files/test.html">files/test.html</a>', '<a href="test.html">files/test.html</a>'), | |
|
91 | ('<a href="/files/test.html">files/test.html</a>', '<a href="test.html">files/test.html</a>'), | |||
|
92 | ("<a href='files/test.html'>files/test.html</a>", "<a href='test.html'>files/test.html</a>"), | |||
|
93 | ('<img src="files/url/location.gif">', '<img src="url/location.gif">'), | |||
|
94 | ('<img src="/files/url/location.gif">', '<img src="url/location.gif">'), | |||
91 | ('hello![caption]', 'hello![caption]'), |
|
95 | ('hello![caption]', 'hello![caption]'), | |
92 | ('hello![caption](/url/location.gif)', 'hello![caption](/url/location.gif)'), |
|
96 | ('hello![caption](/url/location.gif)', 'hello![caption](/url/location.gif)'), | |
93 | ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'), |
|
97 | ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'), | |
94 | ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'), |
|
98 | ('hello![caption](url/location.gif)', 'hello![caption](url/location.gif)'), | |
95 | ('hello![caption](files/url/location.gif)', 'hello![caption](url/location.gif)'), |
|
99 | ('hello![caption](files/url/location.gif)', 'hello![caption](url/location.gif)'), | |
96 |
('hello![caption](/files/url/location.gif)', 'hello![caption](url/location.gif)'), |
|
100 | ('hello![caption](/files/url/location.gif)', 'hello![caption](url/location.gif)'), | |
|
101 | ('hello [text](/files/url/location.gif)', 'hello [text](url/location.gif)'), | |||
|
102 | ('hello [text space](files/url/location.gif)', 'hello [text space](url/location.gif)'), | |||
|
103 | ] | |||
97 | for test in tests: |
|
104 | for test in tests: | |
98 | self._try_files_prefix(test[0], test[1]) |
|
105 | self._try_files_prefix(test[0], test[1]) | |
99 |
|
106 | |||
100 |
|
107 | |||
101 | def _try_files_prefix(self, test, result): |
|
108 | def _try_files_prefix(self, test, result): | |
102 | self.assertEqual(strip_files_prefix(test), result) |
|
109 | self.assertEqual(strip_files_prefix(test), result) | |
103 |
|
110 | |||
104 |
|
111 | |||
105 | def test_comment_lines(self): |
|
112 | def test_comment_lines(self): | |
106 | """comment_lines test""" |
|
113 | """comment_lines test""" | |
107 | for line in comment_lines('hello\nworld\n!').split('\n'): |
|
114 | for line in comment_lines('hello\nworld\n!').split('\n'): | |
108 | assert line.startswith('# ') |
|
115 | assert line.startswith('# ') | |
109 | for line in comment_lines('hello\nworld\n!', 'beep').split('\n'): |
|
116 | for line in comment_lines('hello\nworld\n!', 'beep').split('\n'): | |
110 | assert line.startswith('beep') |
|
117 | assert line.startswith('beep') | |
111 |
|
118 | |||
112 |
|
119 | |||
113 | def test_get_lines(self): |
|
120 | def test_get_lines(self): | |
114 | """get_lines test""" |
|
121 | """get_lines test""" | |
115 | text = "hello\nworld\n!" |
|
122 | text = "hello\nworld\n!" | |
116 | self.assertEqual(get_lines(text, start=1), "world\n!") |
|
123 | self.assertEqual(get_lines(text, start=1), "world\n!") | |
117 | self.assertEqual(get_lines(text, end=2), "hello\nworld") |
|
124 | self.assertEqual(get_lines(text, end=2), "hello\nworld") | |
118 | self.assertEqual(get_lines(text, start=2, end=5), "!") |
|
125 | self.assertEqual(get_lines(text, start=2, end=5), "!") | |
119 | self.assertEqual(get_lines(text, start=-2), "world\n!") |
|
126 | self.assertEqual(get_lines(text, start=-2), "world\n!") | |
120 |
|
127 | |||
121 |
|
128 | |||
122 | def test_ipython2python(self): |
|
129 | def test_ipython2python(self): | |
123 | """ipython2python test""" |
|
130 | """ipython2python test""" | |
124 | #TODO: More tests |
|
131 | #TODO: More tests | |
125 | results = ipython2python(u'%%pylab\nprint("Hello-World")').replace("u'", "'") |
|
132 | results = ipython2python(u'%%pylab\nprint("Hello-World")').replace("u'", "'") | |
126 | self.fuzzy_compare(results, u"get_ipython().run_cell_magic('pylab', '', 'print(\"Hello-World\")')", |
|
133 | self.fuzzy_compare(results, u"get_ipython().run_cell_magic('pylab', '', 'print(\"Hello-World\")')", | |
127 | ignore_spaces=True, ignore_newlines=True) |
|
134 | ignore_spaces=True, ignore_newlines=True) | |
128 |
|
135 | |||
129 | def test_posix_path(self): |
|
136 | def test_posix_path(self): | |
130 | """posix_path test""" |
|
137 | """posix_path test""" | |
131 | path_list = ['foo', 'bar'] |
|
138 | path_list = ['foo', 'bar'] | |
132 | expected = '/'.join(path_list) |
|
139 | expected = '/'.join(path_list) | |
133 | native = os.path.join(*path_list) |
|
140 | native = os.path.join(*path_list) | |
134 | filtered = posix_path(native) |
|
141 | filtered = posix_path(native) | |
135 | self.assertEqual(filtered, expected) |
|
142 | self.assertEqual(filtered, expected) | |
136 |
|
143 | |||
137 | def test_add_prompts(self): |
|
144 | def test_add_prompts(self): | |
138 | """add_prompts test""" |
|
145 | """add_prompts test""" | |
139 | text1 = """for i in range(10):\n i += 1\n print i""" |
|
146 | text1 = """for i in range(10):\n i += 1\n print i""" | |
140 | text2 = """>>> for i in range(10):\n... i += 1\n... print i""" |
|
147 | text2 = """>>> for i in range(10):\n... i += 1\n... print i""" | |
141 | self.assertEqual(text2, add_prompts(text1)) |
|
148 | self.assertEqual(text2, add_prompts(text1)) |
General Comments 0
You need to be logged in to leave comments.
Login now