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