ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_paragraph.py

Python code coverage for Lib/idlelib/idle_test/test_paragraph.py

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