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

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

#countcontent
1n/a"""Unittest for UndoDelegator in idlelib.undo.py.
2n/a
3n/aCoverage about 80% (retest).
4n/a"""
5n/afrom test.support import requires
6n/arequires('gui')
7n/a
8n/aimport unittest
9n/afrom unittest.mock import Mock
10n/afrom tkinter import Text, Tk
11n/afrom idlelib.undo import UndoDelegator
12n/afrom idlelib.percolator import Percolator
13n/a
14n/a
15n/aclass UndoDelegatorTest(unittest.TestCase):
16n/a
17n/a @classmethod
18n/a def setUpClass(cls):
19n/a cls.root = Tk()
20n/a cls.text = Text(cls.root)
21n/a cls.percolator = Percolator(cls.text)
22n/a
23n/a @classmethod
24n/a def tearDownClass(cls):
25n/a cls.percolator.redir.close()
26n/a del cls.percolator, cls.text
27n/a cls.root.destroy()
28n/a del cls.root
29n/a
30n/a def setUp(self):
31n/a self.delegator = UndoDelegator()
32n/a self.delegator.bell = Mock()
33n/a self.percolator.insertfilter(self.delegator)
34n/a
35n/a def tearDown(self):
36n/a self.percolator.removefilter(self.delegator)
37n/a self.text.delete('1.0', 'end')
38n/a self.delegator.resetcache()
39n/a
40n/a def test_undo_event(self):
41n/a text = self.text
42n/a
43n/a text.insert('insert', 'foobar')
44n/a text.insert('insert', 'h')
45n/a text.event_generate('<<undo>>')
46n/a self.assertEqual(text.get('1.0', 'end'), '\n')
47n/a
48n/a text.insert('insert', 'foo')
49n/a text.insert('insert', 'bar')
50n/a text.delete('1.2', '1.4')
51n/a text.insert('insert', 'hello')
52n/a text.event_generate('<<undo>>')
53n/a self.assertEqual(text.get('1.0', '1.4'), 'foar')
54n/a text.event_generate('<<undo>>')
55n/a self.assertEqual(text.get('1.0', '1.6'), 'foobar')
56n/a text.event_generate('<<undo>>')
57n/a self.assertEqual(text.get('1.0', '1.3'), 'foo')
58n/a text.event_generate('<<undo>>')
59n/a self.delegator.undo_event('event')
60n/a self.assertTrue(self.delegator.bell.called)
61n/a
62n/a def test_redo_event(self):
63n/a text = self.text
64n/a
65n/a text.insert('insert', 'foo')
66n/a text.insert('insert', 'bar')
67n/a text.delete('1.0', '1.3')
68n/a text.event_generate('<<undo>>')
69n/a text.event_generate('<<redo>>')
70n/a self.assertEqual(text.get('1.0', '1.3'), 'bar')
71n/a text.event_generate('<<redo>>')
72n/a self.assertTrue(self.delegator.bell.called)
73n/a
74n/a def test_dump_event(self):
75n/a """
76n/a Dump_event cannot be tested directly without changing
77n/a environment variables. So, test statements in dump_event
78n/a indirectly
79n/a """
80n/a text = self.text
81n/a d = self.delegator
82n/a
83n/a text.insert('insert', 'foo')
84n/a text.insert('insert', 'bar')
85n/a text.delete('1.2', '1.4')
86n/a self.assertTupleEqual((d.pointer, d.can_merge), (3, True))
87n/a text.event_generate('<<undo>>')
88n/a self.assertTupleEqual((d.pointer, d.can_merge), (2, False))
89n/a
90n/a def test_get_set_saved(self):
91n/a # test the getter method get_saved
92n/a # test the setter method set_saved
93n/a # indirectly test check_saved
94n/a d = self.delegator
95n/a
96n/a self.assertTrue(d.get_saved())
97n/a self.text.insert('insert', 'a')
98n/a self.assertFalse(d.get_saved())
99n/a d.saved_change_hook = Mock()
100n/a
101n/a d.set_saved(True)
102n/a self.assertEqual(d.pointer, d.saved)
103n/a self.assertTrue(d.saved_change_hook.called)
104n/a
105n/a d.set_saved(False)
106n/a self.assertEqual(d.saved, -1)
107n/a self.assertTrue(d.saved_change_hook.called)
108n/a
109n/a def test_undo_start_stop(self):
110n/a # test the undo_block_start and undo_block_stop methods
111n/a text = self.text
112n/a
113n/a text.insert('insert', 'foo')
114n/a self.delegator.undo_block_start()
115n/a text.insert('insert', 'bar')
116n/a text.insert('insert', 'bar')
117n/a self.delegator.undo_block_stop()
118n/a self.assertEqual(text.get('1.0', '1.3'), 'foo')
119n/a
120n/a # test another code path
121n/a self.delegator.undo_block_start()
122n/a text.insert('insert', 'bar')
123n/a self.delegator.undo_block_stop()
124n/a self.assertEqual(text.get('1.0', '1.3'), 'foo')
125n/a
126n/a def test_addcmd(self):
127n/a text = self.text
128n/a # when number of undo operations exceeds max_undo
129n/a self.delegator.max_undo = max_undo = 10
130n/a for i in range(max_undo + 10):
131n/a text.insert('insert', 'foo')
132n/a self.assertLessEqual(len(self.delegator.undolist), max_undo)
133n/a
134n/aif __name__ == '__main__':
135n/a unittest.main(verbosity=2, exit=False)