1 | n/a | # Test the functions and main class method of paragraph.py |
---|
2 | n/a | import unittest |
---|
3 | n/a | from idlelib import paragraph as fp |
---|
4 | n/a | from idlelib.editor import EditorWindow |
---|
5 | n/a | from tkinter import Tk, Text |
---|
6 | n/a | from test.support import requires |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | class Is_Get_Test(unittest.TestCase): |
---|
10 | n/a | """Test the is_ and get_ functions""" |
---|
11 | n/a | test_comment = '# This is a comment' |
---|
12 | n/a | test_nocomment = 'This is not a comment' |
---|
13 | n/a | trailingws_comment = '# This is a comment ' |
---|
14 | n/a | leadingws_comment = ' # This is a comment' |
---|
15 | n/a | leadingws_nocomment = ' This is not a comment' |
---|
16 | n/a | |
---|
17 | n/a | def test_is_all_white(self): |
---|
18 | n/a | self.assertTrue(fp.is_all_white('')) |
---|
19 | n/a | self.assertTrue(fp.is_all_white('\t\n\r\f\v')) |
---|
20 | n/a | self.assertFalse(fp.is_all_white(self.test_comment)) |
---|
21 | n/a | |
---|
22 | n/a | def test_get_indent(self): |
---|
23 | n/a | Equal = self.assertEqual |
---|
24 | n/a | Equal(fp.get_indent(self.test_comment), '') |
---|
25 | n/a | Equal(fp.get_indent(self.trailingws_comment), '') |
---|
26 | n/a | Equal(fp.get_indent(self.leadingws_comment), ' ') |
---|
27 | n/a | Equal(fp.get_indent(self.leadingws_nocomment), ' ') |
---|
28 | n/a | |
---|
29 | n/a | def test_get_comment_header(self): |
---|
30 | n/a | Equal = self.assertEqual |
---|
31 | n/a | # Test comment strings |
---|
32 | n/a | Equal(fp.get_comment_header(self.test_comment), '#') |
---|
33 | n/a | Equal(fp.get_comment_header(self.trailingws_comment), '#') |
---|
34 | n/a | Equal(fp.get_comment_header(self.leadingws_comment), ' #') |
---|
35 | n/a | # Test non-comment strings |
---|
36 | n/a | Equal(fp.get_comment_header(self.leadingws_nocomment), ' ') |
---|
37 | n/a | Equal(fp.get_comment_header(self.test_nocomment), '') |
---|
38 | n/a | |
---|
39 | n/a | |
---|
40 | n/a | class FindTest(unittest.TestCase): |
---|
41 | n/a | """Test the find_paragraph function in paragraph module. |
---|
42 | n/a | |
---|
43 | n/a | Using the runcase() function, find_paragraph() is called with 'mark' set at |
---|
44 | n/a | multiple indexes before and inside the test paragraph. |
---|
45 | n/a | |
---|
46 | n/a | It appears that code with the same indentation as a quoted string is grouped |
---|
47 | n/a | as part of the same paragraph, which is probably incorrect behavior. |
---|
48 | n/a | """ |
---|
49 | n/a | |
---|
50 | n/a | @classmethod |
---|
51 | n/a | def setUpClass(cls): |
---|
52 | n/a | from idlelib.idle_test.mock_tk import Text |
---|
53 | n/a | cls.text = Text() |
---|
54 | n/a | |
---|
55 | n/a | def runcase(self, inserttext, stopline, expected): |
---|
56 | n/a | # Check that find_paragraph returns the expected paragraph when |
---|
57 | n/a | # the mark index is set to beginning, middle, end of each line |
---|
58 | n/a | # up to but not including the stop line |
---|
59 | n/a | text = self.text |
---|
60 | n/a | text.insert('1.0', inserttext) |
---|
61 | n/a | for line in range(1, stopline): |
---|
62 | n/a | linelength = int(text.index("%d.end" % line).split('.')[1]) |
---|
63 | n/a | for col in (0, linelength//2, linelength): |
---|
64 | n/a | tempindex = "%d.%d" % (line, col) |
---|
65 | n/a | self.assertEqual(fp.find_paragraph(text, tempindex), expected) |
---|
66 | n/a | text.delete('1.0', 'end') |
---|
67 | n/a | |
---|
68 | n/a | def test_find_comment(self): |
---|
69 | n/a | comment = ( |
---|
70 | n/a | "# Comment block with no blank lines before\n" |
---|
71 | n/a | "# Comment line\n" |
---|
72 | n/a | "\n") |
---|
73 | n/a | self.runcase(comment, 3, ('1.0', '3.0', '#', comment[0:58])) |
---|
74 | n/a | |
---|
75 | n/a | comment = ( |
---|
76 | n/a | "\n" |
---|
77 | n/a | "# Comment block with whitespace line before and after\n" |
---|
78 | n/a | "# Comment line\n" |
---|
79 | n/a | "\n") |
---|
80 | n/a | self.runcase(comment, 4, ('2.0', '4.0', '#', comment[1:70])) |
---|
81 | n/a | |
---|
82 | n/a | comment = ( |
---|
83 | n/a | "\n" |
---|
84 | n/a | " # Indented comment block with whitespace before and after\n" |
---|
85 | n/a | " # Comment line\n" |
---|
86 | n/a | "\n") |
---|
87 | n/a | self.runcase(comment, 4, ('2.0', '4.0', ' #', comment[1:82])) |
---|
88 | n/a | |
---|
89 | n/a | comment = ( |
---|
90 | n/a | "\n" |
---|
91 | n/a | "# Single line comment\n" |
---|
92 | n/a | "\n") |
---|
93 | n/a | self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:23])) |
---|
94 | n/a | |
---|
95 | n/a | comment = ( |
---|
96 | n/a | "\n" |
---|
97 | n/a | " # Single line comment with leading whitespace\n" |
---|
98 | n/a | "\n") |
---|
99 | n/a | self.runcase(comment, 3, ('2.0', '3.0', ' #', comment[1:51])) |
---|
100 | n/a | |
---|
101 | n/a | comment = ( |
---|
102 | n/a | "\n" |
---|
103 | n/a | "# Comment immediately followed by code\n" |
---|
104 | n/a | "x = 42\n" |
---|
105 | n/a | "\n") |
---|
106 | n/a | self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:40])) |
---|
107 | n/a | |
---|
108 | n/a | comment = ( |
---|
109 | n/a | "\n" |
---|
110 | n/a | " # Indented comment immediately followed by code\n" |
---|
111 | n/a | "x = 42\n" |
---|
112 | n/a | "\n") |
---|
113 | n/a | self.runcase(comment, 3, ('2.0', '3.0', ' #', comment[1:53])) |
---|
114 | n/a | |
---|
115 | n/a | comment = ( |
---|
116 | n/a | "\n" |
---|
117 | n/a | "# Comment immediately followed by indented code\n" |
---|
118 | n/a | " x = 42\n" |
---|
119 | n/a | "\n") |
---|
120 | n/a | self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:49])) |
---|
121 | n/a | |
---|
122 | n/a | def test_find_paragraph(self): |
---|
123 | n/a | teststring = ( |
---|
124 | n/a | '"""String with no blank lines before\n' |
---|
125 | n/a | 'String line\n' |
---|
126 | n/a | '"""\n' |
---|
127 | n/a | '\n') |
---|
128 | n/a | self.runcase(teststring, 4, ('1.0', '4.0', '', teststring[0:53])) |
---|
129 | n/a | |
---|
130 | n/a | teststring = ( |
---|
131 | n/a | "\n" |
---|
132 | n/a | '"""String with whitespace line before and after\n' |
---|
133 | n/a | 'String line.\n' |
---|
134 | n/a | '"""\n' |
---|
135 | n/a | '\n') |
---|
136 | n/a | self.runcase(teststring, 5, ('2.0', '5.0', '', teststring[1:66])) |
---|
137 | n/a | |
---|
138 | n/a | teststring = ( |
---|
139 | n/a | '\n' |
---|
140 | n/a | ' """Indented string with whitespace before and after\n' |
---|
141 | n/a | ' Comment string.\n' |
---|
142 | n/a | ' """\n' |
---|
143 | n/a | '\n') |
---|
144 | n/a | self.runcase(teststring, 5, ('2.0', '5.0', ' ', teststring[1:85])) |
---|
145 | n/a | |
---|
146 | n/a | teststring = ( |
---|
147 | n/a | '\n' |
---|
148 | n/a | '"""Single line string."""\n' |
---|
149 | n/a | '\n') |
---|
150 | n/a | self.runcase(teststring, 3, ('2.0', '3.0', '', teststring[1:27])) |
---|
151 | n/a | |
---|
152 | n/a | teststring = ( |
---|
153 | n/a | '\n' |
---|
154 | n/a | ' """Single line string with leading whitespace."""\n' |
---|
155 | n/a | '\n') |
---|
156 | n/a | self.runcase(teststring, 3, ('2.0', '3.0', ' ', teststring[1:55])) |
---|
157 | n/a | |
---|
158 | n/a | |
---|
159 | n/a | class ReformatFunctionTest(unittest.TestCase): |
---|
160 | n/a | """Test the reformat_paragraph function without the editor window.""" |
---|
161 | n/a | |
---|
162 | n/a | def test_reformat_paragraph(self): |
---|
163 | n/a | Equal = self.assertEqual |
---|
164 | n/a | reform = fp.reformat_paragraph |
---|
165 | n/a | hw = "O hello world" |
---|
166 | n/a | Equal(reform(' ', 1), ' ') |
---|
167 | n/a | Equal(reform("Hello world", 20), "Hello world") |
---|
168 | n/a | |
---|
169 | n/a | # Test without leading newline |
---|
170 | n/a | Equal(reform(hw, 1), "O\nhello\nworld") |
---|
171 | n/a | Equal(reform(hw, 6), "O\nhello\nworld") |
---|
172 | n/a | Equal(reform(hw, 7), "O hello\nworld") |
---|
173 | n/a | Equal(reform(hw, 12), "O hello\nworld") |
---|
174 | n/a | Equal(reform(hw, 13), "O hello world") |
---|
175 | n/a | |
---|
176 | n/a | # Test with leading newline |
---|
177 | n/a | hw = "\nO hello world" |
---|
178 | n/a | Equal(reform(hw, 1), "\nO\nhello\nworld") |
---|
179 | n/a | Equal(reform(hw, 6), "\nO\nhello\nworld") |
---|
180 | n/a | Equal(reform(hw, 7), "\nO hello\nworld") |
---|
181 | n/a | Equal(reform(hw, 12), "\nO hello\nworld") |
---|
182 | n/a | Equal(reform(hw, 13), "\nO hello world") |
---|
183 | n/a | |
---|
184 | n/a | |
---|
185 | n/a | class ReformatCommentTest(unittest.TestCase): |
---|
186 | n/a | """Test the reformat_comment function without the editor window.""" |
---|
187 | n/a | |
---|
188 | n/a | def test_reformat_comment(self): |
---|
189 | n/a | Equal = self.assertEqual |
---|
190 | n/a | |
---|
191 | n/a | # reformat_comment formats to a minimum of 20 characters |
---|
192 | n/a | test_string = ( |
---|
193 | n/a | " \"\"\"this is a test of a reformat for a triple quoted string" |
---|
194 | n/a | " will it reformat to less than 70 characters for me?\"\"\"") |
---|
195 | n/a | result = fp.reformat_comment(test_string, 70, " ") |
---|
196 | n/a | expected = ( |
---|
197 | n/a | " \"\"\"this is a test of a reformat for a triple quoted string will it\n" |
---|
198 | n/a | " reformat to less than 70 characters for me?\"\"\"") |
---|
199 | n/a | Equal(result, expected) |
---|
200 | n/a | |
---|
201 | n/a | test_comment = ( |
---|
202 | n/a | "# this is a test of a reformat for a triple quoted string will " |
---|
203 | n/a | "it reformat to less than 70 characters for me?") |
---|
204 | n/a | result = fp.reformat_comment(test_comment, 70, "#") |
---|
205 | n/a | expected = ( |
---|
206 | n/a | "# this is a test of a reformat for a triple quoted string will it\n" |
---|
207 | n/a | "# reformat to less than 70 characters for me?") |
---|
208 | n/a | Equal(result, expected) |
---|
209 | n/a | |
---|
210 | n/a | |
---|
211 | n/a | class FormatClassTest(unittest.TestCase): |
---|
212 | n/a | def test_init_close(self): |
---|
213 | n/a | instance = fp.FormatParagraph('editor') |
---|
214 | n/a | self.assertEqual(instance.editwin, 'editor') |
---|
215 | n/a | instance.close() |
---|
216 | n/a | self.assertEqual(instance.editwin, None) |
---|
217 | n/a | |
---|
218 | n/a | |
---|
219 | n/a | # For testing format_paragraph_event, Initialize FormatParagraph with |
---|
220 | n/a | # a mock Editor with .text and .get_selection_indices. The text must |
---|
221 | n/a | # be a Text wrapper that adds two methods |
---|
222 | n/a | |
---|
223 | n/a | # A real EditorWindow creates unneeded, time-consuming baggage and |
---|
224 | n/a | # sometimes emits shutdown warnings like this: |
---|
225 | n/a | # "warning: callback failed in WindowList <class '_tkinter.TclError'> |
---|
226 | n/a | # : invalid command name ".55131368.windows". |
---|
227 | n/a | # Calling EditorWindow._close in tearDownClass prevents this but causes |
---|
228 | n/a | # other problems (windows left open). |
---|
229 | n/a | |
---|
230 | n/a | class TextWrapper: |
---|
231 | n/a | def __init__(self, master): |
---|
232 | n/a | self.text = Text(master=master) |
---|
233 | n/a | def __getattr__(self, name): |
---|
234 | n/a | return getattr(self.text, name) |
---|
235 | n/a | def undo_block_start(self): pass |
---|
236 | n/a | def undo_block_stop(self): pass |
---|
237 | n/a | |
---|
238 | n/a | class Editor: |
---|
239 | n/a | def __init__(self, root): |
---|
240 | n/a | self.text = TextWrapper(root) |
---|
241 | n/a | get_selection_indices = EditorWindow. get_selection_indices |
---|
242 | n/a | |
---|
243 | n/a | class FormatEventTest(unittest.TestCase): |
---|
244 | n/a | """Test the formatting of text inside a Text widget. |
---|
245 | n/a | |
---|
246 | n/a | This is done with FormatParagraph.format.paragraph_event, |
---|
247 | n/a | which calls functions in the module as appropriate. |
---|
248 | n/a | """ |
---|
249 | n/a | test_string = ( |
---|
250 | n/a | " '''this is a test of a reformat for a triple " |
---|
251 | n/a | "quoted string will it reformat to less than 70 " |
---|
252 | n/a | "characters for me?'''\n") |
---|
253 | n/a | multiline_test_string = ( |
---|
254 | n/a | " '''The first line is under the max width.\n" |
---|
255 | n/a | " The second line's length is way over the max width. It goes " |
---|
256 | n/a | "on and on until it is over 100 characters long.\n" |
---|
257 | n/a | " Same thing with the third line. It is also way over the max " |
---|
258 | n/a | "width, but FormatParagraph will fix it.\n" |
---|
259 | n/a | " '''\n") |
---|
260 | n/a | multiline_test_comment = ( |
---|
261 | n/a | "# The first line is under the max width.\n" |
---|
262 | n/a | "# The second line's length is way over the max width. It goes on " |
---|
263 | n/a | "and on until it is over 100 characters long.\n" |
---|
264 | n/a | "# Same thing with the third line. It is also way over the max " |
---|
265 | n/a | "width, but FormatParagraph will fix it.\n" |
---|
266 | n/a | "# The fourth line is short like the first line.") |
---|
267 | n/a | |
---|
268 | n/a | @classmethod |
---|
269 | n/a | def setUpClass(cls): |
---|
270 | n/a | requires('gui') |
---|
271 | n/a | cls.root = Tk() |
---|
272 | n/a | editor = Editor(root=cls.root) |
---|
273 | n/a | cls.text = editor.text.text # Test code does not need the wrapper. |
---|
274 | n/a | cls.formatter = fp.FormatParagraph(editor).format_paragraph_event |
---|
275 | n/a | # Sets the insert mark just after the re-wrapped and inserted text. |
---|
276 | n/a | |
---|
277 | n/a | @classmethod |
---|
278 | n/a | def tearDownClass(cls): |
---|
279 | n/a | del cls.text, cls.formatter |
---|
280 | n/a | cls.root.destroy() |
---|
281 | n/a | del cls.root |
---|
282 | n/a | |
---|
283 | n/a | def test_short_line(self): |
---|
284 | n/a | self.text.insert('1.0', "Short line\n") |
---|
285 | n/a | self.formatter("Dummy") |
---|
286 | n/a | self.assertEqual(self.text.get('1.0', 'insert'), "Short line\n" ) |
---|
287 | n/a | self.text.delete('1.0', 'end') |
---|
288 | n/a | |
---|
289 | n/a | def test_long_line(self): |
---|
290 | n/a | text = self.text |
---|
291 | n/a | |
---|
292 | n/a | # Set cursor ('insert' mark) to '1.0', within text. |
---|
293 | n/a | text.insert('1.0', self.test_string) |
---|
294 | n/a | text.mark_set('insert', '1.0') |
---|
295 | n/a | self.formatter('ParameterDoesNothing', limit=70) |
---|
296 | n/a | result = text.get('1.0', 'insert') |
---|
297 | n/a | # find function includes \n |
---|
298 | n/a | expected = ( |
---|
299 | n/a | " '''this is a test of a reformat for a triple quoted string will it\n" |
---|
300 | n/a | " reformat to less than 70 characters for me?'''\n") # yes |
---|
301 | n/a | self.assertEqual(result, expected) |
---|
302 | n/a | text.delete('1.0', 'end') |
---|
303 | n/a | |
---|
304 | n/a | # Select from 1.11 to line end. |
---|
305 | n/a | text.insert('1.0', self.test_string) |
---|
306 | n/a | text.tag_add('sel', '1.11', '1.end') |
---|
307 | n/a | self.formatter('ParameterDoesNothing', limit=70) |
---|
308 | n/a | result = text.get('1.0', 'insert') |
---|
309 | n/a | # selection excludes \n |
---|
310 | n/a | expected = ( |
---|
311 | n/a | " '''this is a test of a reformat for a triple quoted string will it reformat\n" |
---|
312 | n/a | " to less than 70 characters for me?'''") # no |
---|
313 | n/a | self.assertEqual(result, expected) |
---|
314 | n/a | text.delete('1.0', 'end') |
---|
315 | n/a | |
---|
316 | n/a | def test_multiple_lines(self): |
---|
317 | n/a | text = self.text |
---|
318 | n/a | # Select 2 long lines. |
---|
319 | n/a | text.insert('1.0', self.multiline_test_string) |
---|
320 | n/a | text.tag_add('sel', '2.0', '4.0') |
---|
321 | n/a | self.formatter('ParameterDoesNothing', limit=70) |
---|
322 | n/a | result = text.get('2.0', 'insert') |
---|
323 | n/a | expected = ( |
---|
324 | n/a | " The second line's length is way over the max width. It goes on and\n" |
---|
325 | n/a | " on until it is over 100 characters long. Same thing with the third\n" |
---|
326 | n/a | " line. It is also way over the max width, but FormatParagraph will\n" |
---|
327 | n/a | " fix it.\n") |
---|
328 | n/a | self.assertEqual(result, expected) |
---|
329 | n/a | text.delete('1.0', 'end') |
---|
330 | n/a | |
---|
331 | n/a | def test_comment_block(self): |
---|
332 | n/a | text = self.text |
---|
333 | n/a | |
---|
334 | n/a | # Set cursor ('insert') to '1.0', within block. |
---|
335 | n/a | text.insert('1.0', self.multiline_test_comment) |
---|
336 | n/a | self.formatter('ParameterDoesNothing', limit=70) |
---|
337 | n/a | result = text.get('1.0', 'insert') |
---|
338 | n/a | expected = ( |
---|
339 | n/a | "# The first line is under the max width. The second line's length is\n" |
---|
340 | n/a | "# way over the max width. It goes on and on until it is over 100\n" |
---|
341 | n/a | "# characters long. Same thing with the third line. It is also way over\n" |
---|
342 | n/a | "# the max width, but FormatParagraph will fix it. The fourth line is\n" |
---|
343 | n/a | "# short like the first line.\n") |
---|
344 | n/a | self.assertEqual(result, expected) |
---|
345 | n/a | text.delete('1.0', 'end') |
---|
346 | n/a | |
---|
347 | n/a | # Select line 2, verify line 1 unaffected. |
---|
348 | n/a | text.insert('1.0', self.multiline_test_comment) |
---|
349 | n/a | text.tag_add('sel', '2.0', '3.0') |
---|
350 | n/a | self.formatter('ParameterDoesNothing', limit=70) |
---|
351 | n/a | result = text.get('1.0', 'insert') |
---|
352 | n/a | expected = ( |
---|
353 | n/a | "# The first line is under the max width.\n" |
---|
354 | n/a | "# The second line's length is way over the max width. It goes on and\n" |
---|
355 | n/a | "# on until it is over 100 characters long.\n") |
---|
356 | n/a | self.assertEqual(result, expected) |
---|
357 | n/a | text.delete('1.0', 'end') |
---|
358 | n/a | |
---|
359 | n/a | # The following block worked with EditorWindow but fails with the mock. |
---|
360 | n/a | # Lines 2 and 3 get pasted together even though the previous block left |
---|
361 | n/a | # the previous line alone. More investigation is needed. |
---|
362 | n/a | ## # Select lines 3 and 4 |
---|
363 | n/a | ## text.insert('1.0', self.multiline_test_comment) |
---|
364 | n/a | ## text.tag_add('sel', '3.0', '5.0') |
---|
365 | n/a | ## self.formatter('ParameterDoesNothing') |
---|
366 | n/a | ## result = text.get('3.0', 'insert') |
---|
367 | n/a | ## expected = ( |
---|
368 | n/a | ##"# Same thing with the third line. It is also way over the max width,\n" |
---|
369 | n/a | ##"# but FormatParagraph will fix it. The fourth line is short like the\n" |
---|
370 | n/a | ##"# first line.\n") |
---|
371 | n/a | ## self.assertEqual(result, expected) |
---|
372 | n/a | ## text.delete('1.0', 'end') |
---|
373 | n/a | |
---|
374 | n/a | |
---|
375 | n/a | if __name__ == '__main__': |
---|
376 | n/a | unittest.main(verbosity=2, exit=2) |
---|