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