PageRenderTime 85ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/idlelib/idle_test/test_paragraph.py

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