1 | n/a | import unittest |
---|
2 | n/a | from test.support import requires |
---|
3 | n/a | |
---|
4 | n/a | import tkinter as tk |
---|
5 | n/a | from tkinter import Text as tkText |
---|
6 | n/a | from idlelib.idle_test.mock_tk import Text as mkText |
---|
7 | n/a | from idlelib.history import History |
---|
8 | n/a | from idlelib.config import idleConf |
---|
9 | n/a | |
---|
10 | n/a | line1 = 'a = 7' |
---|
11 | n/a | line2 = 'b = a' |
---|
12 | n/a | |
---|
13 | n/a | class StoreTest(unittest.TestCase): |
---|
14 | n/a | '''Tests History.__init__ and History.store with mock Text''' |
---|
15 | n/a | |
---|
16 | n/a | @classmethod |
---|
17 | n/a | def setUpClass(cls): |
---|
18 | n/a | cls.text = mkText() |
---|
19 | n/a | cls.history = History(cls.text) |
---|
20 | n/a | |
---|
21 | n/a | def tearDown(self): |
---|
22 | n/a | self.text.delete('1.0', 'end') |
---|
23 | n/a | self.history.history = [] |
---|
24 | n/a | |
---|
25 | n/a | def test_init(self): |
---|
26 | n/a | self.assertIs(self.history.text, self.text) |
---|
27 | n/a | self.assertEqual(self.history.history, []) |
---|
28 | n/a | self.assertIsNone(self.history.prefix) |
---|
29 | n/a | self.assertIsNone(self.history.pointer) |
---|
30 | n/a | self.assertEqual(self.history.cyclic, |
---|
31 | n/a | idleConf.GetOption("main", "History", "cyclic", 1, "bool")) |
---|
32 | n/a | |
---|
33 | n/a | def test_store_short(self): |
---|
34 | n/a | self.history.store('a') |
---|
35 | n/a | self.assertEqual(self.history.history, []) |
---|
36 | n/a | self.history.store(' a ') |
---|
37 | n/a | self.assertEqual(self.history.history, []) |
---|
38 | n/a | |
---|
39 | n/a | def test_store_dup(self): |
---|
40 | n/a | self.history.store(line1) |
---|
41 | n/a | self.assertEqual(self.history.history, [line1]) |
---|
42 | n/a | self.history.store(line2) |
---|
43 | n/a | self.assertEqual(self.history.history, [line1, line2]) |
---|
44 | n/a | self.history.store(line1) |
---|
45 | n/a | self.assertEqual(self.history.history, [line2, line1]) |
---|
46 | n/a | |
---|
47 | n/a | def test_store_reset(self): |
---|
48 | n/a | self.history.prefix = line1 |
---|
49 | n/a | self.history.pointer = 0 |
---|
50 | n/a | self.history.store(line2) |
---|
51 | n/a | self.assertIsNone(self.history.prefix) |
---|
52 | n/a | self.assertIsNone(self.history.pointer) |
---|
53 | n/a | |
---|
54 | n/a | |
---|
55 | n/a | class TextWrapper: |
---|
56 | n/a | def __init__(self, master): |
---|
57 | n/a | self.text = tkText(master=master) |
---|
58 | n/a | self._bell = False |
---|
59 | n/a | def __getattr__(self, name): |
---|
60 | n/a | return getattr(self.text, name) |
---|
61 | n/a | def bell(self): |
---|
62 | n/a | self._bell = True |
---|
63 | n/a | |
---|
64 | n/a | class FetchTest(unittest.TestCase): |
---|
65 | n/a | '''Test History.fetch with wrapped tk.Text. |
---|
66 | n/a | ''' |
---|
67 | n/a | @classmethod |
---|
68 | n/a | def setUpClass(cls): |
---|
69 | n/a | requires('gui') |
---|
70 | n/a | cls.root = tk.Tk() |
---|
71 | n/a | cls.root.withdraw() |
---|
72 | n/a | |
---|
73 | n/a | def setUp(self): |
---|
74 | n/a | self.text = text = TextWrapper(self.root) |
---|
75 | n/a | text.insert('1.0', ">>> ") |
---|
76 | n/a | text.mark_set('iomark', '1.4') |
---|
77 | n/a | text.mark_gravity('iomark', 'left') |
---|
78 | n/a | self.history = History(text) |
---|
79 | n/a | self.history.history = [line1, line2] |
---|
80 | n/a | |
---|
81 | n/a | @classmethod |
---|
82 | n/a | def tearDownClass(cls): |
---|
83 | n/a | cls.root.destroy() |
---|
84 | n/a | del cls.root |
---|
85 | n/a | |
---|
86 | n/a | def fetch_test(self, reverse, line, prefix, index, *, bell=False): |
---|
87 | n/a | # Perform one fetch as invoked by Alt-N or Alt-P |
---|
88 | n/a | # Test the result. The line test is the most important. |
---|
89 | n/a | # The last two are diagnostic of fetch internals. |
---|
90 | n/a | History = self.history |
---|
91 | n/a | History.fetch(reverse) |
---|
92 | n/a | |
---|
93 | n/a | Equal = self.assertEqual |
---|
94 | n/a | Equal(self.text.get('iomark', 'end-1c'), line) |
---|
95 | n/a | Equal(self.text._bell, bell) |
---|
96 | n/a | if bell: |
---|
97 | n/a | self.text._bell = False |
---|
98 | n/a | Equal(History.prefix, prefix) |
---|
99 | n/a | Equal(History.pointer, index) |
---|
100 | n/a | Equal(self.text.compare("insert", '==', "end-1c"), 1) |
---|
101 | n/a | |
---|
102 | n/a | def test_fetch_prev_cyclic(self): |
---|
103 | n/a | prefix = '' |
---|
104 | n/a | test = self.fetch_test |
---|
105 | n/a | test(True, line2, prefix, 1) |
---|
106 | n/a | test(True, line1, prefix, 0) |
---|
107 | n/a | test(True, prefix, None, None, bell=True) |
---|
108 | n/a | |
---|
109 | n/a | def test_fetch_next_cyclic(self): |
---|
110 | n/a | prefix = '' |
---|
111 | n/a | test = self.fetch_test |
---|
112 | n/a | test(False, line1, prefix, 0) |
---|
113 | n/a | test(False, line2, prefix, 1) |
---|
114 | n/a | test(False, prefix, None, None, bell=True) |
---|
115 | n/a | |
---|
116 | n/a | # Prefix 'a' tests skip line2, which starts with 'b' |
---|
117 | n/a | def test_fetch_prev_prefix(self): |
---|
118 | n/a | prefix = 'a' |
---|
119 | n/a | self.text.insert('iomark', prefix) |
---|
120 | n/a | self.fetch_test(True, line1, prefix, 0) |
---|
121 | n/a | self.fetch_test(True, prefix, None, None, bell=True) |
---|
122 | n/a | |
---|
123 | n/a | def test_fetch_next_prefix(self): |
---|
124 | n/a | prefix = 'a' |
---|
125 | n/a | self.text.insert('iomark', prefix) |
---|
126 | n/a | self.fetch_test(False, line1, prefix, 0) |
---|
127 | n/a | self.fetch_test(False, prefix, None, None, bell=True) |
---|
128 | n/a | |
---|
129 | n/a | def test_fetch_prev_noncyclic(self): |
---|
130 | n/a | prefix = '' |
---|
131 | n/a | self.history.cyclic = False |
---|
132 | n/a | test = self.fetch_test |
---|
133 | n/a | test(True, line2, prefix, 1) |
---|
134 | n/a | test(True, line1, prefix, 0) |
---|
135 | n/a | test(True, line1, prefix, 0, bell=True) |
---|
136 | n/a | |
---|
137 | n/a | def test_fetch_next_noncyclic(self): |
---|
138 | n/a | prefix = '' |
---|
139 | n/a | self.history.cyclic = False |
---|
140 | n/a | test = self.fetch_test |
---|
141 | n/a | test(False, prefix, None, None, bell=True) |
---|
142 | n/a | test(True, line2, prefix, 1) |
---|
143 | n/a | test(False, prefix, None, None, bell=True) |
---|
144 | n/a | test(False, prefix, None, None, bell=True) |
---|
145 | n/a | |
---|
146 | n/a | def test_fetch_cursor_move(self): |
---|
147 | n/a | # Move cursor after fetch |
---|
148 | n/a | self.history.fetch(reverse=True) # initialization |
---|
149 | n/a | self.text.mark_set('insert', 'iomark') |
---|
150 | n/a | self.fetch_test(True, line2, None, None, bell=True) |
---|
151 | n/a | |
---|
152 | n/a | def test_fetch_edit(self): |
---|
153 | n/a | # Edit after fetch |
---|
154 | n/a | self.history.fetch(reverse=True) # initialization |
---|
155 | n/a | self.text.delete('iomark', 'insert', ) |
---|
156 | n/a | self.text.insert('iomark', 'a =') |
---|
157 | n/a | self.fetch_test(True, line1, 'a =', 0) # prefix is reset |
---|
158 | n/a | |
---|
159 | n/a | def test_history_prev_next(self): |
---|
160 | n/a | # Minimally test functions bound to events |
---|
161 | n/a | self.history.history_prev('dummy event') |
---|
162 | n/a | self.assertEqual(self.history.pointer, 1) |
---|
163 | n/a | self.history.history_next('dummy event') |
---|
164 | n/a | self.assertEqual(self.history.pointer, None) |
---|
165 | n/a | |
---|
166 | n/a | |
---|
167 | n/a | if __name__ == '__main__': |
---|
168 | n/a | unittest.main(verbosity=2, exit=2) |
---|