source: titan/mediathek/localhoster/lib/python2.7/idlelib/idle_test/test_formatparagraph.py @ 40094

Last change on this file since 40094 was 40094, checked in by obi, 7 years ago

tithek add yoztube-dl support

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