1 | n/a | """Unittest for idlelib.replace.py""" |
---|
2 | n/a | from test.support import requires |
---|
3 | n/a | requires('gui') |
---|
4 | n/a | |
---|
5 | n/a | import unittest |
---|
6 | n/a | from unittest.mock import Mock |
---|
7 | n/a | from tkinter import Tk, Text |
---|
8 | n/a | from idlelib.idle_test.mock_tk import Mbox |
---|
9 | n/a | import idlelib.searchengine as se |
---|
10 | n/a | from idlelib.replace import ReplaceDialog |
---|
11 | n/a | |
---|
12 | n/a | orig_mbox = se.tkMessageBox |
---|
13 | n/a | showerror = Mbox.showerror |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | class ReplaceDialogTest(unittest.TestCase): |
---|
17 | n/a | |
---|
18 | n/a | @classmethod |
---|
19 | n/a | def setUpClass(cls): |
---|
20 | n/a | cls.root = Tk() |
---|
21 | n/a | cls.root.withdraw() |
---|
22 | n/a | se.tkMessageBox = Mbox |
---|
23 | n/a | cls.engine = se.SearchEngine(cls.root) |
---|
24 | n/a | cls.dialog = ReplaceDialog(cls.root, cls.engine) |
---|
25 | n/a | cls.dialog.bell = lambda: None |
---|
26 | n/a | cls.dialog.ok = Mock() |
---|
27 | n/a | cls.text = Text(cls.root) |
---|
28 | n/a | cls.text.undo_block_start = Mock() |
---|
29 | n/a | cls.text.undo_block_stop = Mock() |
---|
30 | n/a | cls.dialog.text = cls.text |
---|
31 | n/a | |
---|
32 | n/a | @classmethod |
---|
33 | n/a | def tearDownClass(cls): |
---|
34 | n/a | se.tkMessageBox = orig_mbox |
---|
35 | n/a | del cls.text, cls.dialog, cls.engine |
---|
36 | n/a | cls.root.destroy() |
---|
37 | n/a | del cls.root |
---|
38 | n/a | |
---|
39 | n/a | def setUp(self): |
---|
40 | n/a | self.text.insert('insert', 'This is a sample sTring') |
---|
41 | n/a | |
---|
42 | n/a | def tearDown(self): |
---|
43 | n/a | self.engine.patvar.set('') |
---|
44 | n/a | self.dialog.replvar.set('') |
---|
45 | n/a | self.engine.wordvar.set(False) |
---|
46 | n/a | self.engine.casevar.set(False) |
---|
47 | n/a | self.engine.revar.set(False) |
---|
48 | n/a | self.engine.wrapvar.set(True) |
---|
49 | n/a | self.engine.backvar.set(False) |
---|
50 | n/a | showerror.title = '' |
---|
51 | n/a | showerror.message = '' |
---|
52 | n/a | self.text.delete('1.0', 'end') |
---|
53 | n/a | |
---|
54 | n/a | def test_replace_simple(self): |
---|
55 | n/a | # Test replace function with all options at default setting. |
---|
56 | n/a | # Wrap around - True |
---|
57 | n/a | # Regular Expression - False |
---|
58 | n/a | # Match case - False |
---|
59 | n/a | # Match word - False |
---|
60 | n/a | # Direction - Forwards |
---|
61 | n/a | text = self.text |
---|
62 | n/a | equal = self.assertEqual |
---|
63 | n/a | pv = self.engine.patvar |
---|
64 | n/a | rv = self.dialog.replvar |
---|
65 | n/a | replace = self.dialog.replace_it |
---|
66 | n/a | |
---|
67 | n/a | # test accessor method |
---|
68 | n/a | self.engine.setpat('asdf') |
---|
69 | n/a | equal(self.engine.getpat(), pv.get()) |
---|
70 | n/a | |
---|
71 | n/a | # text found and replaced |
---|
72 | n/a | pv.set('a') |
---|
73 | n/a | rv.set('asdf') |
---|
74 | n/a | replace() |
---|
75 | n/a | equal(text.get('1.8', '1.12'), 'asdf') |
---|
76 | n/a | |
---|
77 | n/a | # dont "match word" case |
---|
78 | n/a | text.mark_set('insert', '1.0') |
---|
79 | n/a | pv.set('is') |
---|
80 | n/a | rv.set('hello') |
---|
81 | n/a | replace() |
---|
82 | n/a | equal(text.get('1.2', '1.7'), 'hello') |
---|
83 | n/a | |
---|
84 | n/a | # dont "match case" case |
---|
85 | n/a | pv.set('string') |
---|
86 | n/a | rv.set('world') |
---|
87 | n/a | replace() |
---|
88 | n/a | equal(text.get('1.23', '1.28'), 'world') |
---|
89 | n/a | |
---|
90 | n/a | # without "regular expression" case |
---|
91 | n/a | text.mark_set('insert', 'end') |
---|
92 | n/a | text.insert('insert', '\nline42:') |
---|
93 | n/a | before_text = text.get('1.0', 'end') |
---|
94 | n/a | pv.set(r'[a-z][\d]+') |
---|
95 | n/a | replace() |
---|
96 | n/a | after_text = text.get('1.0', 'end') |
---|
97 | n/a | equal(before_text, after_text) |
---|
98 | n/a | |
---|
99 | n/a | # test with wrap around selected and complete a cycle |
---|
100 | n/a | text.mark_set('insert', '1.9') |
---|
101 | n/a | pv.set('i') |
---|
102 | n/a | rv.set('j') |
---|
103 | n/a | replace() |
---|
104 | n/a | equal(text.get('1.8'), 'i') |
---|
105 | n/a | equal(text.get('2.1'), 'j') |
---|
106 | n/a | replace() |
---|
107 | n/a | equal(text.get('2.1'), 'j') |
---|
108 | n/a | equal(text.get('1.8'), 'j') |
---|
109 | n/a | before_text = text.get('1.0', 'end') |
---|
110 | n/a | replace() |
---|
111 | n/a | after_text = text.get('1.0', 'end') |
---|
112 | n/a | equal(before_text, after_text) |
---|
113 | n/a | |
---|
114 | n/a | # text not found |
---|
115 | n/a | before_text = text.get('1.0', 'end') |
---|
116 | n/a | pv.set('foobar') |
---|
117 | n/a | replace() |
---|
118 | n/a | after_text = text.get('1.0', 'end') |
---|
119 | n/a | equal(before_text, after_text) |
---|
120 | n/a | |
---|
121 | n/a | # test access method |
---|
122 | n/a | self.dialog.find_it(0) |
---|
123 | n/a | |
---|
124 | n/a | def test_replace_wrap_around(self): |
---|
125 | n/a | text = self.text |
---|
126 | n/a | equal = self.assertEqual |
---|
127 | n/a | pv = self.engine.patvar |
---|
128 | n/a | rv = self.dialog.replvar |
---|
129 | n/a | replace = self.dialog.replace_it |
---|
130 | n/a | self.engine.wrapvar.set(False) |
---|
131 | n/a | |
---|
132 | n/a | # replace candidate found both after and before 'insert' |
---|
133 | n/a | text.mark_set('insert', '1.4') |
---|
134 | n/a | pv.set('i') |
---|
135 | n/a | rv.set('j') |
---|
136 | n/a | replace() |
---|
137 | n/a | equal(text.get('1.2'), 'i') |
---|
138 | n/a | equal(text.get('1.5'), 'j') |
---|
139 | n/a | replace() |
---|
140 | n/a | equal(text.get('1.2'), 'i') |
---|
141 | n/a | equal(text.get('1.20'), 'j') |
---|
142 | n/a | replace() |
---|
143 | n/a | equal(text.get('1.2'), 'i') |
---|
144 | n/a | |
---|
145 | n/a | # replace candidate found only before 'insert' |
---|
146 | n/a | text.mark_set('insert', '1.8') |
---|
147 | n/a | pv.set('is') |
---|
148 | n/a | before_text = text.get('1.0', 'end') |
---|
149 | n/a | replace() |
---|
150 | n/a | after_text = text.get('1.0', 'end') |
---|
151 | n/a | equal(before_text, after_text) |
---|
152 | n/a | |
---|
153 | n/a | def test_replace_whole_word(self): |
---|
154 | n/a | text = self.text |
---|
155 | n/a | equal = self.assertEqual |
---|
156 | n/a | pv = self.engine.patvar |
---|
157 | n/a | rv = self.dialog.replvar |
---|
158 | n/a | replace = self.dialog.replace_it |
---|
159 | n/a | self.engine.wordvar.set(True) |
---|
160 | n/a | |
---|
161 | n/a | pv.set('is') |
---|
162 | n/a | rv.set('hello') |
---|
163 | n/a | replace() |
---|
164 | n/a | equal(text.get('1.0', '1.4'), 'This') |
---|
165 | n/a | equal(text.get('1.5', '1.10'), 'hello') |
---|
166 | n/a | |
---|
167 | n/a | def test_replace_match_case(self): |
---|
168 | n/a | equal = self.assertEqual |
---|
169 | n/a | text = self.text |
---|
170 | n/a | pv = self.engine.patvar |
---|
171 | n/a | rv = self.dialog.replvar |
---|
172 | n/a | replace = self.dialog.replace_it |
---|
173 | n/a | self.engine.casevar.set(True) |
---|
174 | n/a | |
---|
175 | n/a | before_text = self.text.get('1.0', 'end') |
---|
176 | n/a | pv.set('this') |
---|
177 | n/a | rv.set('that') |
---|
178 | n/a | replace() |
---|
179 | n/a | after_text = self.text.get('1.0', 'end') |
---|
180 | n/a | equal(before_text, after_text) |
---|
181 | n/a | |
---|
182 | n/a | pv.set('This') |
---|
183 | n/a | replace() |
---|
184 | n/a | equal(text.get('1.0', '1.4'), 'that') |
---|
185 | n/a | |
---|
186 | n/a | def test_replace_regex(self): |
---|
187 | n/a | equal = self.assertEqual |
---|
188 | n/a | text = self.text |
---|
189 | n/a | pv = self.engine.patvar |
---|
190 | n/a | rv = self.dialog.replvar |
---|
191 | n/a | replace = self.dialog.replace_it |
---|
192 | n/a | self.engine.revar.set(True) |
---|
193 | n/a | |
---|
194 | n/a | before_text = text.get('1.0', 'end') |
---|
195 | n/a | pv.set(r'[a-z][\d]+') |
---|
196 | n/a | rv.set('hello') |
---|
197 | n/a | replace() |
---|
198 | n/a | after_text = text.get('1.0', 'end') |
---|
199 | n/a | equal(before_text, after_text) |
---|
200 | n/a | |
---|
201 | n/a | text.insert('insert', '\nline42') |
---|
202 | n/a | replace() |
---|
203 | n/a | equal(text.get('2.0', '2.8'), 'linhello') |
---|
204 | n/a | |
---|
205 | n/a | pv.set('') |
---|
206 | n/a | replace() |
---|
207 | n/a | self.assertIn('error', showerror.title) |
---|
208 | n/a | self.assertIn('Empty', showerror.message) |
---|
209 | n/a | |
---|
210 | n/a | pv.set(r'[\d') |
---|
211 | n/a | replace() |
---|
212 | n/a | self.assertIn('error', showerror.title) |
---|
213 | n/a | self.assertIn('Pattern', showerror.message) |
---|
214 | n/a | |
---|
215 | n/a | showerror.title = '' |
---|
216 | n/a | showerror.message = '' |
---|
217 | n/a | pv.set('[a]') |
---|
218 | n/a | rv.set('test\\') |
---|
219 | n/a | replace() |
---|
220 | n/a | self.assertIn('error', showerror.title) |
---|
221 | n/a | self.assertIn('Invalid Replace Expression', showerror.message) |
---|
222 | n/a | |
---|
223 | n/a | # test access method |
---|
224 | n/a | self.engine.setcookedpat("\'") |
---|
225 | n/a | equal(pv.get(), "\\'") |
---|
226 | n/a | |
---|
227 | n/a | def test_replace_backwards(self): |
---|
228 | n/a | equal = self.assertEqual |
---|
229 | n/a | text = self.text |
---|
230 | n/a | pv = self.engine.patvar |
---|
231 | n/a | rv = self.dialog.replvar |
---|
232 | n/a | replace = self.dialog.replace_it |
---|
233 | n/a | self.engine.backvar.set(True) |
---|
234 | n/a | |
---|
235 | n/a | text.insert('insert', '\nis as ') |
---|
236 | n/a | |
---|
237 | n/a | pv.set('is') |
---|
238 | n/a | rv.set('was') |
---|
239 | n/a | replace() |
---|
240 | n/a | equal(text.get('1.2', '1.4'), 'is') |
---|
241 | n/a | equal(text.get('2.0', '2.3'), 'was') |
---|
242 | n/a | replace() |
---|
243 | n/a | equal(text.get('1.5', '1.8'), 'was') |
---|
244 | n/a | replace() |
---|
245 | n/a | equal(text.get('1.2', '1.5'), 'was') |
---|
246 | n/a | |
---|
247 | n/a | def test_replace_all(self): |
---|
248 | n/a | text = self.text |
---|
249 | n/a | pv = self.engine.patvar |
---|
250 | n/a | rv = self.dialog.replvar |
---|
251 | n/a | replace_all = self.dialog.replace_all |
---|
252 | n/a | |
---|
253 | n/a | text.insert('insert', '\n') |
---|
254 | n/a | text.insert('insert', text.get('1.0', 'end')*100) |
---|
255 | n/a | pv.set('is') |
---|
256 | n/a | rv.set('was') |
---|
257 | n/a | replace_all() |
---|
258 | n/a | self.assertNotIn('is', text.get('1.0', 'end')) |
---|
259 | n/a | |
---|
260 | n/a | self.engine.revar.set(True) |
---|
261 | n/a | pv.set('') |
---|
262 | n/a | replace_all() |
---|
263 | n/a | self.assertIn('error', showerror.title) |
---|
264 | n/a | self.assertIn('Empty', showerror.message) |
---|
265 | n/a | |
---|
266 | n/a | pv.set('[s][T]') |
---|
267 | n/a | rv.set('\\') |
---|
268 | n/a | replace_all() |
---|
269 | n/a | |
---|
270 | n/a | self.engine.revar.set(False) |
---|
271 | n/a | pv.set('text which is not present') |
---|
272 | n/a | rv.set('foobar') |
---|
273 | n/a | replace_all() |
---|
274 | n/a | |
---|
275 | n/a | def test_default_command(self): |
---|
276 | n/a | text = self.text |
---|
277 | n/a | pv = self.engine.patvar |
---|
278 | n/a | rv = self.dialog.replvar |
---|
279 | n/a | replace_find = self.dialog.default_command |
---|
280 | n/a | equal = self.assertEqual |
---|
281 | n/a | |
---|
282 | n/a | pv.set('This') |
---|
283 | n/a | rv.set('was') |
---|
284 | n/a | replace_find() |
---|
285 | n/a | equal(text.get('sel.first', 'sel.last'), 'was') |
---|
286 | n/a | |
---|
287 | n/a | self.engine.revar.set(True) |
---|
288 | n/a | pv.set('') |
---|
289 | n/a | replace_find() |
---|
290 | n/a | |
---|
291 | n/a | |
---|
292 | n/a | if __name__ == '__main__': |
---|
293 | n/a | unittest.main(verbosity=2) |
---|