ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_parenmatch.py

Python code coverage for Lib/idlelib/idle_test/test_parenmatch.py

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