test_format.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. "Test format, coverage 99%."
  2. from idlelib import format as ft
  3. import unittest
  4. from unittest import mock
  5. from test.support import requires
  6. from tkinter import Tk, Text
  7. from idlelib.editor import EditorWindow
  8. from idlelib.idle_test.mock_idle import Editor as MockEditor
  9. class 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. def test_is_all_white(self):
  17. self.assertTrue(ft.is_all_white(''))
  18. self.assertTrue(ft.is_all_white('\t\n\r\f\v'))
  19. self.assertFalse(ft.is_all_white(self.test_comment))
  20. def test_get_indent(self):
  21. Equal = self.assertEqual
  22. Equal(ft.get_indent(self.test_comment), '')
  23. Equal(ft.get_indent(self.trailingws_comment), '')
  24. Equal(ft.get_indent(self.leadingws_comment), ' ')
  25. Equal(ft.get_indent(self.leadingws_nocomment), ' ')
  26. def test_get_comment_header(self):
  27. Equal = self.assertEqual
  28. # Test comment strings
  29. Equal(ft.get_comment_header(self.test_comment), '#')
  30. Equal(ft.get_comment_header(self.trailingws_comment), '#')
  31. Equal(ft.get_comment_header(self.leadingws_comment), ' #')
  32. # Test non-comment strings
  33. Equal(ft.get_comment_header(self.leadingws_nocomment), ' ')
  34. Equal(ft.get_comment_header(self.test_nocomment), '')
  35. class FindTest(unittest.TestCase):
  36. """Test the find_paragraph function in paragraph module.
  37. Using the runcase() function, find_paragraph() is called with 'mark' set at
  38. multiple indexes before and inside the test paragraph.
  39. It appears that code with the same indentation as a quoted string is grouped
  40. as part of the same paragraph, which is probably incorrect behavior.
  41. """
  42. @classmethod
  43. def setUpClass(cls):
  44. from idlelib.idle_test.mock_tk import Text
  45. cls.text = Text()
  46. def runcase(self, inserttext, stopline, expected):
  47. # Check that find_paragraph returns the expected paragraph when
  48. # the mark index is set to beginning, middle, end of each line
  49. # up to but not including the stop line
  50. text = self.text
  51. text.insert('1.0', inserttext)
  52. for line in range(1, stopline):
  53. linelength = int(text.index("%d.end" % line).split('.')[1])
  54. for col in (0, linelength//2, linelength):
  55. tempindex = "%d.%d" % (line, col)
  56. self.assertEqual(ft.find_paragraph(text, tempindex), expected)
  57. text.delete('1.0', 'end')
  58. def test_find_comment(self):
  59. comment = (
  60. "# Comment block with no blank lines before\n"
  61. "# Comment line\n"
  62. "\n")
  63. self.runcase(comment, 3, ('1.0', '3.0', '#', comment[0:58]))
  64. comment = (
  65. "\n"
  66. "# Comment block with whitespace line before and after\n"
  67. "# Comment line\n"
  68. "\n")
  69. self.runcase(comment, 4, ('2.0', '4.0', '#', comment[1:70]))
  70. comment = (
  71. "\n"
  72. " # Indented comment block with whitespace before and after\n"
  73. " # Comment line\n"
  74. "\n")
  75. self.runcase(comment, 4, ('2.0', '4.0', ' #', comment[1:82]))
  76. comment = (
  77. "\n"
  78. "# Single line comment\n"
  79. "\n")
  80. self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:23]))
  81. comment = (
  82. "\n"
  83. " # Single line comment with leading whitespace\n"
  84. "\n")
  85. self.runcase(comment, 3, ('2.0', '3.0', ' #', comment[1:51]))
  86. comment = (
  87. "\n"
  88. "# Comment immediately followed by code\n"
  89. "x = 42\n"
  90. "\n")
  91. self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:40]))
  92. comment = (
  93. "\n"
  94. " # Indented comment immediately followed by code\n"
  95. "x = 42\n"
  96. "\n")
  97. self.runcase(comment, 3, ('2.0', '3.0', ' #', comment[1:53]))
  98. comment = (
  99. "\n"
  100. "# Comment immediately followed by indented code\n"
  101. " x = 42\n"
  102. "\n")
  103. self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:49]))
  104. def test_find_paragraph(self):
  105. teststring = (
  106. '"""String with no blank lines before\n'
  107. 'String line\n'
  108. '"""\n'
  109. '\n')
  110. self.runcase(teststring, 4, ('1.0', '4.0', '', teststring[0:53]))
  111. teststring = (
  112. "\n"
  113. '"""String with whitespace line before and after\n'
  114. 'String line.\n'
  115. '"""\n'
  116. '\n')
  117. self.runcase(teststring, 5, ('2.0', '5.0', '', teststring[1:66]))
  118. teststring = (
  119. '\n'
  120. ' """Indented string with whitespace before and after\n'
  121. ' Comment string.\n'
  122. ' """\n'
  123. '\n')
  124. self.runcase(teststring, 5, ('2.0', '5.0', ' ', teststring[1:85]))
  125. teststring = (
  126. '\n'
  127. '"""Single line string."""\n'
  128. '\n')
  129. self.runcase(teststring, 3, ('2.0', '3.0', '', teststring[1:27]))
  130. teststring = (
  131. '\n'
  132. ' """Single line string with leading whitespace."""\n'
  133. '\n')
  134. self.runcase(teststring, 3, ('2.0', '3.0', ' ', teststring[1:55]))
  135. class ReformatFunctionTest(unittest.TestCase):
  136. """Test the reformat_paragraph function without the editor window."""
  137. def test_reformat_paragraph(self):
  138. Equal = self.assertEqual
  139. reform = ft.reformat_paragraph
  140. hw = "O hello world"
  141. Equal(reform(' ', 1), ' ')
  142. Equal(reform("Hello world", 20), "Hello world")
  143. # Test without leading newline
  144. Equal(reform(hw, 1), "O\nhello\nworld")
  145. Equal(reform(hw, 6), "O\nhello\nworld")
  146. Equal(reform(hw, 7), "O hello\nworld")
  147. Equal(reform(hw, 12), "O hello\nworld")
  148. Equal(reform(hw, 13), "O hello world")
  149. # Test with leading newline
  150. hw = "\nO hello world"
  151. Equal(reform(hw, 1), "\nO\nhello\nworld")
  152. Equal(reform(hw, 6), "\nO\nhello\nworld")
  153. Equal(reform(hw, 7), "\nO hello\nworld")
  154. Equal(reform(hw, 12), "\nO hello\nworld")
  155. Equal(reform(hw, 13), "\nO hello world")
  156. class ReformatCommentTest(unittest.TestCase):
  157. """Test the reformat_comment function without the editor window."""
  158. def test_reformat_comment(self):
  159. Equal = self.assertEqual
  160. # reformat_comment formats to a minimum of 20 characters
  161. test_string = (
  162. " \"\"\"this is a test of a reformat for a triple quoted string"
  163. " will it reformat to less than 70 characters for me?\"\"\"")
  164. result = ft.reformat_comment(test_string, 70, " ")
  165. expected = (
  166. " \"\"\"this is a test of a reformat for a triple quoted string will it\n"
  167. " reformat to less than 70 characters for me?\"\"\"")
  168. Equal(result, expected)
  169. test_comment = (
  170. "# this is a test of a reformat for a triple quoted string will "
  171. "it reformat to less than 70 characters for me?")
  172. result = ft.reformat_comment(test_comment, 70, "#")
  173. expected = (
  174. "# this is a test of a reformat for a triple quoted string will it\n"
  175. "# reformat to less than 70 characters for me?")
  176. Equal(result, expected)
  177. class FormatClassTest(unittest.TestCase):
  178. def test_init_close(self):
  179. instance = ft.FormatParagraph('editor')
  180. self.assertEqual(instance.editwin, 'editor')
  181. instance.close()
  182. self.assertEqual(instance.editwin, None)
  183. # For testing format_paragraph_event, Initialize FormatParagraph with
  184. # a mock Editor with .text and .get_selection_indices. The text must
  185. # be a Text wrapper that adds two methods
  186. # A real EditorWindow creates unneeded, time-consuming baggage and
  187. # sometimes emits shutdown warnings like this:
  188. # "warning: callback failed in WindowList <class '_tkinter.TclError'>
  189. # : invalid command name ".55131368.windows".
  190. # Calling EditorWindow._close in tearDownClass prevents this but causes
  191. # other problems (windows left open).
  192. class TextWrapper:
  193. def __init__(self, master):
  194. self.text = Text(master=master)
  195. def __getattr__(self, name):
  196. return getattr(self.text, name)
  197. def undo_block_start(self): pass
  198. def undo_block_stop(self): pass
  199. class Editor:
  200. def __init__(self, root):
  201. self.text = TextWrapper(root)
  202. get_selection_indices = EditorWindow. get_selection_indices
  203. class FormatEventTest(unittest.TestCase):
  204. """Test the formatting of text inside a Text widget.
  205. This is done with FormatParagraph.format.paragraph_event,
  206. which calls functions in the module as appropriate.
  207. """
  208. test_string = (
  209. " '''this is a test of a reformat for a triple "
  210. "quoted string will it reformat to less than 70 "
  211. "characters for me?'''\n")
  212. multiline_test_string = (
  213. " '''The first line is under the max width.\n"
  214. " The second line's length is way over the max width. It goes "
  215. "on and on until it is over 100 characters long.\n"
  216. " Same thing with the third line. It is also way over the max "
  217. "width, but FormatParagraph will fix it.\n"
  218. " '''\n")
  219. multiline_test_comment = (
  220. "# The first line is under the max width.\n"
  221. "# The second line's length is way over the max width. It goes on "
  222. "and on until it is over 100 characters long.\n"
  223. "# Same thing with the third line. It is also way over the max "
  224. "width, but FormatParagraph will fix it.\n"
  225. "# The fourth line is short like the first line.")
  226. @classmethod
  227. def setUpClass(cls):
  228. requires('gui')
  229. cls.root = Tk()
  230. cls.root.withdraw()
  231. editor = Editor(root=cls.root)
  232. cls.text = editor.text.text # Test code does not need the wrapper.
  233. cls.formatter = ft.FormatParagraph(editor).format_paragraph_event
  234. # Sets the insert mark just after the re-wrapped and inserted text.
  235. @classmethod
  236. def tearDownClass(cls):
  237. del cls.text, cls.formatter
  238. cls.root.update_idletasks()
  239. cls.root.destroy()
  240. del cls.root
  241. def test_short_line(self):
  242. self.text.insert('1.0', "Short line\n")
  243. self.formatter("Dummy")
  244. self.assertEqual(self.text.get('1.0', 'insert'), "Short line\n" )
  245. self.text.delete('1.0', 'end')
  246. def test_long_line(self):
  247. text = self.text
  248. # Set cursor ('insert' mark) to '1.0', within text.
  249. text.insert('1.0', self.test_string)
  250. text.mark_set('insert', '1.0')
  251. self.formatter('ParameterDoesNothing', limit=70)
  252. result = text.get('1.0', 'insert')
  253. # find function includes \n
  254. expected = (
  255. " '''this is a test of a reformat for a triple quoted string will it\n"
  256. " reformat to less than 70 characters for me?'''\n") # yes
  257. self.assertEqual(result, expected)
  258. text.delete('1.0', 'end')
  259. # Select from 1.11 to line end.
  260. text.insert('1.0', self.test_string)
  261. text.tag_add('sel', '1.11', '1.end')
  262. self.formatter('ParameterDoesNothing', limit=70)
  263. result = text.get('1.0', 'insert')
  264. # selection excludes \n
  265. expected = (
  266. " '''this is a test of a reformat for a triple quoted string will it reformat\n"
  267. " to less than 70 characters for me?'''") # no
  268. self.assertEqual(result, expected)
  269. text.delete('1.0', 'end')
  270. def test_multiple_lines(self):
  271. text = self.text
  272. # Select 2 long lines.
  273. text.insert('1.0', self.multiline_test_string)
  274. text.tag_add('sel', '2.0', '4.0')
  275. self.formatter('ParameterDoesNothing', limit=70)
  276. result = text.get('2.0', 'insert')
  277. expected = (
  278. " The second line's length is way over the max width. It goes on and\n"
  279. " on until it is over 100 characters long. Same thing with the third\n"
  280. " line. It is also way over the max width, but FormatParagraph will\n"
  281. " fix it.\n")
  282. self.assertEqual(result, expected)
  283. text.delete('1.0', 'end')
  284. def test_comment_block(self):
  285. text = self.text
  286. # Set cursor ('insert') to '1.0', within block.
  287. text.insert('1.0', self.multiline_test_comment)
  288. self.formatter('ParameterDoesNothing', limit=70)
  289. result = text.get('1.0', 'insert')
  290. expected = (
  291. "# The first line is under the max width. The second line's length is\n"
  292. "# way over the max width. It goes on and on until it is over 100\n"
  293. "# characters long. Same thing with the third line. It is also way over\n"
  294. "# the max width, but FormatParagraph will fix it. The fourth line is\n"
  295. "# short like the first line.\n")
  296. self.assertEqual(result, expected)
  297. text.delete('1.0', 'end')
  298. # Select line 2, verify line 1 unaffected.
  299. text.insert('1.0', self.multiline_test_comment)
  300. text.tag_add('sel', '2.0', '3.0')
  301. self.formatter('ParameterDoesNothing', limit=70)
  302. result = text.get('1.0', 'insert')
  303. expected = (
  304. "# The first line is under the max width.\n"
  305. "# The second line's length is way over the max width. It goes on and\n"
  306. "# on until it is over 100 characters long.\n")
  307. self.assertEqual(result, expected)
  308. text.delete('1.0', 'end')
  309. # The following block worked with EditorWindow but fails with the mock.
  310. # Lines 2 and 3 get pasted together even though the previous block left
  311. # the previous line alone. More investigation is needed.
  312. ## # Select lines 3 and 4
  313. ## text.insert('1.0', self.multiline_test_comment)
  314. ## text.tag_add('sel', '3.0', '5.0')
  315. ## self.formatter('ParameterDoesNothing')
  316. ## result = text.get('3.0', 'insert')
  317. ## expected = (
  318. ##"# Same thing with the third line. It is also way over the max width,\n"
  319. ##"# but FormatParagraph will fix it. The fourth line is short like the\n"
  320. ##"# first line.\n")
  321. ## self.assertEqual(result, expected)
  322. ## text.delete('1.0', 'end')
  323. class DummyEditwin:
  324. def __init__(self, root, text):
  325. self.root = root
  326. self.text = text
  327. self.indentwidth = 4
  328. self.tabwidth = 4
  329. self.usetabs = False
  330. self.context_use_ps1 = True
  331. _make_blanks = EditorWindow._make_blanks
  332. get_selection_indices = EditorWindow.get_selection_indices
  333. class FormatRegionTest(unittest.TestCase):
  334. @classmethod
  335. def setUpClass(cls):
  336. requires('gui')
  337. cls.root = Tk()
  338. cls.root.withdraw()
  339. cls.text = Text(cls.root)
  340. cls.text.undo_block_start = mock.Mock()
  341. cls.text.undo_block_stop = mock.Mock()
  342. cls.editor = DummyEditwin(cls.root, cls.text)
  343. cls.formatter = ft.FormatRegion(cls.editor)
  344. @classmethod
  345. def tearDownClass(cls):
  346. del cls.text, cls.formatter, cls.editor
  347. cls.root.update_idletasks()
  348. cls.root.destroy()
  349. del cls.root
  350. def setUp(self):
  351. self.text.insert('1.0', self.code_sample)
  352. def tearDown(self):
  353. self.text.delete('1.0', 'end')
  354. code_sample = """\
  355. # WS line needed for test.
  356. class C1():
  357. # Class comment.
  358. def __init__(self, a, b):
  359. self.a = a
  360. self.b = b
  361. def compare(self):
  362. if a > b:
  363. return a
  364. elif a < b:
  365. return b
  366. else:
  367. return None
  368. """
  369. def test_get_region(self):
  370. get = self.formatter.get_region
  371. text = self.text
  372. eq = self.assertEqual
  373. # Add selection.
  374. text.tag_add('sel', '7.0', '10.0')
  375. expected_lines = ['',
  376. ' def compare(self):',
  377. ' if a > b:',
  378. '']
  379. eq(get(), ('7.0', '10.0', '\n'.join(expected_lines), expected_lines))
  380. # Remove selection.
  381. text.tag_remove('sel', '1.0', 'end')
  382. eq(get(), ('15.0', '16.0', '\n', ['', '']))
  383. def test_set_region(self):
  384. set_ = self.formatter.set_region
  385. text = self.text
  386. eq = self.assertEqual
  387. save_bell = text.bell
  388. text.bell = mock.Mock()
  389. line6 = self.code_sample.splitlines()[5]
  390. line10 = self.code_sample.splitlines()[9]
  391. text.tag_add('sel', '6.0', '11.0')
  392. head, tail, chars, lines = self.formatter.get_region()
  393. # No changes.
  394. set_(head, tail, chars, lines)
  395. text.bell.assert_called_once()
  396. eq(text.get('6.0', '11.0'), chars)
  397. eq(text.get('sel.first', 'sel.last'), chars)
  398. text.tag_remove('sel', '1.0', 'end')
  399. # Alter selected lines by changing lines and adding a newline.
  400. newstring = 'added line 1\n\n\n\n'
  401. newlines = newstring.split('\n')
  402. set_('7.0', '10.0', chars, newlines)
  403. # Selection changed.
  404. eq(text.get('sel.first', 'sel.last'), newstring)
  405. # Additional line added, so last index is changed.
  406. eq(text.get('7.0', '11.0'), newstring)
  407. # Before and after lines unchanged.
  408. eq(text.get('6.0', '7.0-1c'), line6)
  409. eq(text.get('11.0', '12.0-1c'), line10)
  410. text.tag_remove('sel', '1.0', 'end')
  411. text.bell = save_bell
  412. def test_indent_region_event(self):
  413. indent = self.formatter.indent_region_event
  414. text = self.text
  415. eq = self.assertEqual
  416. text.tag_add('sel', '7.0', '10.0')
  417. indent()
  418. # Blank lines aren't affected by indent.
  419. eq(text.get('7.0', '10.0'), ('\n def compare(self):\n if a > b:\n'))
  420. def test_dedent_region_event(self):
  421. dedent = self.formatter.dedent_region_event
  422. text = self.text
  423. eq = self.assertEqual
  424. text.tag_add('sel', '7.0', '10.0')
  425. dedent()
  426. # Blank lines aren't affected by dedent.
  427. eq(text.get('7.0', '10.0'), ('\ndef compare(self):\n if a > b:\n'))
  428. def test_comment_region_event(self):
  429. comment = self.formatter.comment_region_event
  430. text = self.text
  431. eq = self.assertEqual
  432. text.tag_add('sel', '7.0', '10.0')
  433. comment()
  434. eq(text.get('7.0', '10.0'), ('##\n## def compare(self):\n## if a > b:\n'))
  435. def test_uncomment_region_event(self):
  436. comment = self.formatter.comment_region_event
  437. uncomment = self.formatter.uncomment_region_event
  438. text = self.text
  439. eq = self.assertEqual
  440. text.tag_add('sel', '7.0', '10.0')
  441. comment()
  442. uncomment()
  443. eq(text.get('7.0', '10.0'), ('\n def compare(self):\n if a > b:\n'))
  444. # Only remove comments at the beginning of a line.
  445. text.tag_remove('sel', '1.0', 'end')
  446. text.tag_add('sel', '3.0', '4.0')
  447. uncomment()
  448. eq(text.get('3.0', '3.end'), (' # Class comment.'))
  449. self.formatter.set_region('3.0', '4.0', '', ['# Class comment.', ''])
  450. uncomment()
  451. eq(text.get('3.0', '3.end'), (' Class comment.'))
  452. @mock.patch.object(ft.FormatRegion, "_asktabwidth")
  453. def test_tabify_region_event(self, _asktabwidth):
  454. tabify = self.formatter.tabify_region_event
  455. text = self.text
  456. eq = self.assertEqual
  457. text.tag_add('sel', '7.0', '10.0')
  458. # No tabwidth selected.
  459. _asktabwidth.return_value = None
  460. self.assertIsNone(tabify())
  461. _asktabwidth.return_value = 3
  462. self.assertIsNotNone(tabify())
  463. eq(text.get('7.0', '10.0'), ('\n\t def compare(self):\n\t\t if a > b:\n'))
  464. @mock.patch.object(ft.FormatRegion, "_asktabwidth")
  465. def test_untabify_region_event(self, _asktabwidth):
  466. untabify = self.formatter.untabify_region_event
  467. text = self.text
  468. eq = self.assertEqual
  469. text.tag_add('sel', '7.0', '10.0')
  470. # No tabwidth selected.
  471. _asktabwidth.return_value = None
  472. self.assertIsNone(untabify())
  473. _asktabwidth.return_value = 2
  474. self.formatter.tabify_region_event()
  475. _asktabwidth.return_value = 3
  476. self.assertIsNotNone(untabify())
  477. eq(text.get('7.0', '10.0'), ('\n def compare(self):\n if a > b:\n'))
  478. @mock.patch.object(ft, "askinteger")
  479. def test_ask_tabwidth(self, askinteger):
  480. ask = self.formatter._asktabwidth
  481. askinteger.return_value = 10
  482. self.assertEqual(ask(), 10)
  483. class IndentsTest(unittest.TestCase):
  484. @mock.patch.object(ft, "askyesno")
  485. def test_toggle_tabs(self, askyesno):
  486. editor = DummyEditwin(None, None) # usetabs == False.
  487. indents = ft.Indents(editor)
  488. askyesno.return_value = True
  489. indents.toggle_tabs_event(None)
  490. self.assertEqual(editor.usetabs, True)
  491. self.assertEqual(editor.indentwidth, 8)
  492. indents.toggle_tabs_event(None)
  493. self.assertEqual(editor.usetabs, False)
  494. self.assertEqual(editor.indentwidth, 8)
  495. @mock.patch.object(ft, "askinteger")
  496. def test_change_indentwidth(self, askinteger):
  497. editor = DummyEditwin(None, None) # indentwidth == 4.
  498. indents = ft.Indents(editor)
  499. askinteger.return_value = None
  500. indents.change_indentwidth_event(None)
  501. self.assertEqual(editor.indentwidth, 4)
  502. askinteger.return_value = 3
  503. indents.change_indentwidth_event(None)
  504. self.assertEqual(editor.indentwidth, 3)
  505. askinteger.return_value = 5
  506. editor.usetabs = True
  507. indents.change_indentwidth_event(None)
  508. self.assertEqual(editor.indentwidth, 3)
  509. class RstripTest(unittest.TestCase):
  510. @classmethod
  511. def setUpClass(cls):
  512. requires('gui')
  513. cls.root = Tk()
  514. cls.root.withdraw()
  515. cls.text = Text(cls.root)
  516. cls.editor = MockEditor(text=cls.text)
  517. cls.do_rstrip = ft.Rstrip(cls.editor).do_rstrip
  518. @classmethod
  519. def tearDownClass(cls):
  520. del cls.text, cls.do_rstrip, cls.editor
  521. cls.root.update_idletasks()
  522. cls.root.destroy()
  523. del cls.root
  524. def tearDown(self):
  525. self.text.delete('1.0', 'end-1c')
  526. def test_rstrip_lines(self):
  527. original = (
  528. "Line with an ending tab \n"
  529. "Line ending in 5 spaces \n"
  530. "Linewithnospaces\n"
  531. " indented line\n"
  532. " indented line with trailing space \n"
  533. " \n")
  534. stripped = (
  535. "Line with an ending tab\n"
  536. "Line ending in 5 spaces\n"
  537. "Linewithnospaces\n"
  538. " indented line\n"
  539. " indented line with trailing space\n")
  540. self.text.insert('1.0', original)
  541. self.do_rstrip()
  542. self.assertEqual(self.text.get('1.0', 'insert'), stripped)
  543. def test_rstrip_end(self):
  544. text = self.text
  545. for code in ('', '\n', '\n\n\n'):
  546. with self.subTest(code=code):
  547. text.insert('1.0', code)
  548. self.do_rstrip()
  549. self.assertEqual(text.get('1.0','end-1c'), '')
  550. for code in ('a\n', 'a\n\n', 'a\n\n\n'):
  551. with self.subTest(code=code):
  552. text.delete('1.0', 'end-1c')
  553. text.insert('1.0', code)
  554. self.do_rstrip()
  555. self.assertEqual(text.get('1.0','end-1c'), 'a\n')
  556. if __name__ == '__main__':
  557. unittest.main(verbosity=2, exit=2)