PageRenderTime 52ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/python/Lib/idlelib/idle_test/test_formatparagraph.py

https://gitlab.com/pmuontains/Odoo
Python | 377 lines | 306 code | 32 blank | 39 comment | 5 complexity | c2674f8879f68f3dda0ae504cfc32acf MD5 | raw file
  1. # Test the functions and main class method of FormatParagraph.py
  2. import unittest
  3. from idlelib import FormatParagraph as fp
  4. from idlelib.EditorWindow import EditorWindow
  5. from Tkinter import Tk, Text
  6. from test.test_support import requires
  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(fp.is_all_white(''))
  16. self.assertTrue(fp.is_all_white('\t\n\r\f\v'))
  17. self.assertFalse(fp.is_all_white(self.test_comment))
  18. def test_get_indent(self):
  19. Equal = self.assertEqual
  20. Equal(fp.get_indent(self.test_comment), '')
  21. Equal(fp.get_indent(self.trailingws_comment), '')
  22. Equal(fp.get_indent(self.leadingws_comment), ' ')
  23. Equal(fp.get_indent(self.leadingws_nocomment), ' ')
  24. def test_get_comment_header(self):
  25. Equal = self.assertEqual
  26. # Test comment strings
  27. Equal(fp.get_comment_header(self.test_comment), '#')
  28. Equal(fp.get_comment_header(self.trailingws_comment), '#')
  29. Equal(fp.get_comment_header(self.leadingws_comment), ' #')
  30. # Test non-comment strings
  31. Equal(fp.get_comment_header(self.leadingws_nocomment), ' ')
  32. Equal(fp.get_comment_header(self.test_nocomment), '')
  33. class FindTest(unittest.TestCase):
  34. """Test the find_paragraph function in FormatParagraph.
  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(fp.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_paragrah(self):
  136. Equal = self.assertEqual
  137. reform = fp.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 = fp.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 = fp.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 = fp.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.im_func
  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. editor = Editor(root=cls.root)
  229. cls.text = editor.text.text # Test code does not need the wrapper.
  230. cls.formatter = fp.FormatParagraph(editor).format_paragraph_event
  231. # Sets the insert mark just after the re-wrapped and inserted text.
  232. @classmethod
  233. def tearDownClass(cls):
  234. cls.root.destroy()
  235. del cls.root
  236. del cls.text
  237. del cls.formatter
  238. def test_short_line(self):
  239. self.text.insert('1.0', "Short line\n")
  240. self.formatter("Dummy")
  241. self.assertEqual(self.text.get('1.0', 'insert'), "Short line\n" )
  242. self.text.delete('1.0', 'end')
  243. def test_long_line(self):
  244. text = self.text
  245. # Set cursor ('insert' mark) to '1.0', within text.
  246. text.insert('1.0', self.test_string)
  247. text.mark_set('insert', '1.0')
  248. self.formatter('ParameterDoesNothing', limit=70)
  249. result = text.get('1.0', 'insert')
  250. # find function includes \n
  251. expected = (
  252. " '''this is a test of a reformat for a triple quoted string will it\n"
  253. " reformat to less than 70 characters for me?'''\n") # yes
  254. self.assertEqual(result, expected)
  255. text.delete('1.0', 'end')
  256. # Select from 1.11 to line end.
  257. text.insert('1.0', self.test_string)
  258. text.tag_add('sel', '1.11', '1.end')
  259. self.formatter('ParameterDoesNothing', limit=70)
  260. result = text.get('1.0', 'insert')
  261. # selection excludes \n
  262. expected = (
  263. " '''this is a test of a reformat for a triple quoted string will it reformat\n"
  264. " to less than 70 characters for me?'''") # no
  265. self.assertEqual(result, expected)
  266. text.delete('1.0', 'end')
  267. def test_multiple_lines(self):
  268. text = self.text
  269. # Select 2 long lines.
  270. text.insert('1.0', self.multiline_test_string)
  271. text.tag_add('sel', '2.0', '4.0')
  272. self.formatter('ParameterDoesNothing', limit=70)
  273. result = text.get('2.0', 'insert')
  274. expected = (
  275. " The second line's length is way over the max width. It goes on and\n"
  276. " on until it is over 100 characters long. Same thing with the third\n"
  277. " line. It is also way over the max width, but FormatParagraph will\n"
  278. " fix it.\n")
  279. self.assertEqual(result, expected)
  280. text.delete('1.0', 'end')
  281. def test_comment_block(self):
  282. text = self.text
  283. # Set cursor ('insert') to '1.0', within block.
  284. text.insert('1.0', self.multiline_test_comment)
  285. self.formatter('ParameterDoesNothing', limit=70)
  286. result = text.get('1.0', 'insert')
  287. expected = (
  288. "# The first line is under the max width. The second line's length is\n"
  289. "# way over the max width. It goes on and on until it is over 100\n"
  290. "# characters long. Same thing with the third line. It is also way over\n"
  291. "# the max width, but FormatParagraph will fix it. The fourth line is\n"
  292. "# short like the first line.\n")
  293. self.assertEqual(result, expected)
  294. text.delete('1.0', 'end')
  295. # Select line 2, verify line 1 unaffected.
  296. text.insert('1.0', self.multiline_test_comment)
  297. text.tag_add('sel', '2.0', '3.0')
  298. self.formatter('ParameterDoesNothing', limit=70)
  299. result = text.get('1.0', 'insert')
  300. expected = (
  301. "# The first line is under the max width.\n"
  302. "# The second line's length is way over the max width. It goes on and\n"
  303. "# on until it is over 100 characters long.\n")
  304. self.assertEqual(result, expected)
  305. text.delete('1.0', 'end')
  306. # The following block worked with EditorWindow but fails with the mock.
  307. # Lines 2 and 3 get pasted together even though the previous block left
  308. # the previous line alone. More investigation is needed.
  309. ## # Select lines 3 and 4
  310. ## text.insert('1.0', self.multiline_test_comment)
  311. ## text.tag_add('sel', '3.0', '5.0')
  312. ## self.formatter('ParameterDoesNothing')
  313. ## result = text.get('3.0', 'insert')
  314. ## expected = (
  315. ##"# Same thing with the third line. It is also way over the max width,\n"
  316. ##"# but FormatParagraph will fix it. The fourth line is short like the\n"
  317. ##"# first line.\n")
  318. ## self.assertEqual(result, expected)
  319. ## text.delete('1.0', 'end')
  320. if __name__ == '__main__':
  321. unittest.main(verbosity=2, exit=2)