1"Test editor, coverage 35%." 2 3from idlelib import editor 4import unittest 5from test.support import requires 6from tkinter import Tk 7 8Editor = editor.EditorWindow 9 10 11class EditorWindowTest(unittest.TestCase): 12 13 @classmethod 14 def setUpClass(cls): 15 requires('gui') 16 cls.root = Tk() 17 cls.root.withdraw() 18 19 @classmethod 20 def tearDownClass(cls): 21 cls.root.update_idletasks() 22 for id in cls.root.tk.call('after', 'info'): 23 cls.root.after_cancel(id) 24 cls.root.destroy() 25 del cls.root 26 27 def test_init(self): 28 e = Editor(root=self.root) 29 self.assertEqual(e.root, self.root) 30 e._close() 31 32 33class TestGetLineIndent(unittest.TestCase): 34 def test_empty_lines(self): 35 for tabwidth in [1, 2, 4, 6, 8]: 36 for line in ['', '\n']: 37 with self.subTest(line=line, tabwidth=tabwidth): 38 self.assertEqual( 39 editor.get_line_indent(line, tabwidth=tabwidth), 40 (0, 0), 41 ) 42 43 def test_tabwidth_4(self): 44 # (line, (raw, effective)) 45 tests = (('no spaces', (0, 0)), 46 # Internal space isn't counted. 47 (' space test', (4, 4)), 48 ('\ttab test', (1, 4)), 49 ('\t\tdouble tabs test', (2, 8)), 50 # Different results when mixing tabs and spaces. 51 (' \tmixed test', (5, 8)), 52 (' \t mixed test', (5, 6)), 53 ('\t mixed test', (5, 8)), 54 # Spaces not divisible by tabwidth. 55 (' \tmixed test', (3, 4)), 56 (' \t mixed test', (3, 5)), 57 ('\t mixed test', (3, 6)), 58 # Only checks spaces and tabs. 59 ('\nnewline test', (0, 0))) 60 61 for line, expected in tests: 62 with self.subTest(line=line): 63 self.assertEqual( 64 editor.get_line_indent(line, tabwidth=4), 65 expected, 66 ) 67 68 def test_tabwidth_8(self): 69 # (line, (raw, effective)) 70 tests = (('no spaces', (0, 0)), 71 # Internal space isn't counted. 72 (' space test', (8, 8)), 73 ('\ttab test', (1, 8)), 74 ('\t\tdouble tabs test', (2, 16)), 75 # Different results when mixing tabs and spaces. 76 (' \tmixed test', (9, 16)), 77 (' \t mixed test', (9, 10)), 78 ('\t mixed test', (9, 16)), 79 # Spaces not divisible by tabwidth. 80 (' \tmixed test', (3, 8)), 81 (' \t mixed test', (3, 9)), 82 ('\t mixed test', (3, 10)), 83 # Only checks spaces and tabs. 84 ('\nnewline test', (0, 0))) 85 86 for line, expected in tests: 87 with self.subTest(line=line): 88 self.assertEqual( 89 editor.get_line_indent(line, tabwidth=8), 90 expected, 91 ) 92 93 94if __name__ == '__main__': 95 unittest.main(verbosity=2) 96