1 | n/a | '''Test idlelib.parenmatch. |
---|
2 | n/a | |
---|
3 | n/a | This must currently be a gui test because ParenMatch methods use |
---|
4 | n/a | several text methods not defined on idlelib.idle_test.mock_tk.Text. |
---|
5 | n/a | ''' |
---|
6 | n/a | from test.support import requires |
---|
7 | n/a | requires('gui') |
---|
8 | n/a | |
---|
9 | n/a | import unittest |
---|
10 | n/a | from unittest.mock import Mock |
---|
11 | n/a | from tkinter import Tk, Text |
---|
12 | n/a | from idlelib.parenmatch import ParenMatch |
---|
13 | n/a | |
---|
14 | n/a | class DummyEditwin: |
---|
15 | n/a | def __init__(self, text): |
---|
16 | n/a | self.text = text |
---|
17 | n/a | self.indentwidth = 8 |
---|
18 | n/a | self.tabwidth = 8 |
---|
19 | n/a | self.context_use_ps1 = True |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | class ParenMatchTest(unittest.TestCase): |
---|
23 | n/a | |
---|
24 | n/a | @classmethod |
---|
25 | n/a | def setUpClass(cls): |
---|
26 | n/a | cls.root = Tk() |
---|
27 | n/a | cls.text = Text(cls.root) |
---|
28 | n/a | cls.editwin = DummyEditwin(cls.text) |
---|
29 | n/a | cls.editwin.text_frame = Mock() |
---|
30 | n/a | |
---|
31 | n/a | @classmethod |
---|
32 | n/a | def tearDownClass(cls): |
---|
33 | n/a | del cls.text, cls.editwin |
---|
34 | n/a | cls.root.update_idletasks() |
---|
35 | n/a | cls.root.destroy() |
---|
36 | n/a | del cls.root |
---|
37 | n/a | |
---|
38 | n/a | def tearDown(self): |
---|
39 | n/a | self.text.delete('1.0', 'end') |
---|
40 | n/a | |
---|
41 | n/a | def get_parenmatch(self): |
---|
42 | n/a | pm = ParenMatch(self.editwin) |
---|
43 | n/a | pm.bell = lambda: None |
---|
44 | n/a | return pm |
---|
45 | n/a | |
---|
46 | n/a | def test_paren_expression(self): |
---|
47 | n/a | """ |
---|
48 | n/a | Test ParenMatch with 'expression' style. |
---|
49 | n/a | """ |
---|
50 | n/a | text = self.text |
---|
51 | n/a | pm = self.get_parenmatch() |
---|
52 | n/a | pm.set_style('expression') |
---|
53 | n/a | |
---|
54 | n/a | text.insert('insert', 'def foobar(a, b') |
---|
55 | n/a | pm.flash_paren_event('event') |
---|
56 | n/a | self.assertIn('<<parenmatch-check-restore>>', text.event_info()) |
---|
57 | n/a | self.assertTupleEqual(text.tag_prevrange('paren', 'end'), |
---|
58 | n/a | ('1.10', '1.15')) |
---|
59 | n/a | text.insert('insert', ')') |
---|
60 | n/a | pm.restore_event() |
---|
61 | n/a | self.assertNotIn('<<parenmatch-check-restore>>', text.event_info()) |
---|
62 | n/a | self.assertEqual(text.tag_prevrange('paren', 'end'), ()) |
---|
63 | n/a | |
---|
64 | n/a | # paren_closed_event can only be tested as below |
---|
65 | n/a | pm.paren_closed_event('event') |
---|
66 | n/a | self.assertTupleEqual(text.tag_prevrange('paren', 'end'), |
---|
67 | n/a | ('1.10', '1.16')) |
---|
68 | n/a | |
---|
69 | n/a | def test_paren_default(self): |
---|
70 | n/a | """ |
---|
71 | n/a | Test ParenMatch with 'default' style. |
---|
72 | n/a | """ |
---|
73 | n/a | text = self.text |
---|
74 | n/a | pm = self.get_parenmatch() |
---|
75 | n/a | pm.set_style('default') |
---|
76 | n/a | |
---|
77 | n/a | text.insert('insert', 'def foobar(a, b') |
---|
78 | n/a | pm.flash_paren_event('event') |
---|
79 | n/a | self.assertIn('<<parenmatch-check-restore>>', text.event_info()) |
---|
80 | n/a | self.assertTupleEqual(text.tag_prevrange('paren', 'end'), |
---|
81 | n/a | ('1.10', '1.11')) |
---|
82 | n/a | text.insert('insert', ')') |
---|
83 | n/a | pm.restore_event() |
---|
84 | n/a | self.assertNotIn('<<parenmatch-check-restore>>', text.event_info()) |
---|
85 | n/a | self.assertEqual(text.tag_prevrange('paren', 'end'), ()) |
---|
86 | n/a | |
---|
87 | n/a | def test_paren_corner(self): |
---|
88 | n/a | """ |
---|
89 | n/a | Test corner cases in flash_paren_event and paren_closed_event. |
---|
90 | n/a | |
---|
91 | n/a | These cases force conditional expression and alternate paths. |
---|
92 | n/a | """ |
---|
93 | n/a | text = self.text |
---|
94 | n/a | pm = self.get_parenmatch() |
---|
95 | n/a | |
---|
96 | n/a | text.insert('insert', '# this is a commen)') |
---|
97 | n/a | self.assertIsNone(pm.paren_closed_event('event')) |
---|
98 | n/a | |
---|
99 | n/a | text.insert('insert', '\ndef') |
---|
100 | n/a | self.assertIsNone(pm.flash_paren_event('event')) |
---|
101 | n/a | self.assertIsNone(pm.paren_closed_event('event')) |
---|
102 | n/a | |
---|
103 | n/a | text.insert('insert', ' a, *arg)') |
---|
104 | n/a | self.assertIsNone(pm.paren_closed_event('event')) |
---|
105 | n/a | |
---|
106 | n/a | def test_handle_restore_timer(self): |
---|
107 | n/a | pm = self.get_parenmatch() |
---|
108 | n/a | pm.restore_event = Mock() |
---|
109 | n/a | pm.handle_restore_timer(0) |
---|
110 | n/a | self.assertTrue(pm.restore_event.called) |
---|
111 | n/a | pm.restore_event.reset_mock() |
---|
112 | n/a | pm.handle_restore_timer(1) |
---|
113 | n/a | self.assertFalse(pm.restore_event.called) |
---|
114 | n/a | |
---|
115 | n/a | |
---|
116 | n/a | if __name__ == '__main__': |
---|
117 | n/a | unittest.main(verbosity=2) |
---|