1 | n/a | '''Test functions and SearchEngine class in idlelib.searchengine.py.''' |
---|
2 | n/a | |
---|
3 | n/a | # With mock replacements, the module does not use any gui widgets. |
---|
4 | n/a | # The use of tk.Text is avoided (for now, until mock Text is improved) |
---|
5 | n/a | # by patching instances with an index function returning what is needed. |
---|
6 | n/a | # This works because mock Text.get does not use .index. |
---|
7 | n/a | |
---|
8 | n/a | import re |
---|
9 | n/a | import unittest |
---|
10 | n/a | # from test.support import requires |
---|
11 | n/a | from tkinter import BooleanVar, StringVar, TclError # ,Tk, Text |
---|
12 | n/a | import tkinter.messagebox as tkMessageBox |
---|
13 | n/a | from idlelib import searchengine as se |
---|
14 | n/a | from idlelib.idle_test.mock_tk import Var, Mbox |
---|
15 | n/a | from idlelib.idle_test.mock_tk import Text as mockText |
---|
16 | n/a | |
---|
17 | n/a | def setUpModule(): |
---|
18 | n/a | # Replace s-e module tkinter imports other than non-gui TclError. |
---|
19 | n/a | se.BooleanVar = Var |
---|
20 | n/a | se.StringVar = Var |
---|
21 | n/a | se.tkMessageBox = Mbox |
---|
22 | n/a | |
---|
23 | n/a | def tearDownModule(): |
---|
24 | n/a | # Restore 'just in case', though other tests should also replace. |
---|
25 | n/a | se.BooleanVar = BooleanVar |
---|
26 | n/a | se.StringVar = StringVar |
---|
27 | n/a | se.tkMessageBox = tkMessageBox |
---|
28 | n/a | |
---|
29 | n/a | |
---|
30 | n/a | class Mock: |
---|
31 | n/a | def __init__(self, *args, **kwargs): pass |
---|
32 | n/a | |
---|
33 | n/a | class GetTest(unittest.TestCase): |
---|
34 | n/a | # SearchEngine.get returns singleton created & saved on first call. |
---|
35 | n/a | def test_get(self): |
---|
36 | n/a | saved_Engine = se.SearchEngine |
---|
37 | n/a | se.SearchEngine = Mock # monkey-patch class |
---|
38 | n/a | try: |
---|
39 | n/a | root = Mock() |
---|
40 | n/a | engine = se.get(root) |
---|
41 | n/a | self.assertIsInstance(engine, se.SearchEngine) |
---|
42 | n/a | self.assertIs(root._searchengine, engine) |
---|
43 | n/a | self.assertIs(se.get(root), engine) |
---|
44 | n/a | finally: |
---|
45 | n/a | se.SearchEngine = saved_Engine # restore class to module |
---|
46 | n/a | |
---|
47 | n/a | class GetLineColTest(unittest.TestCase): |
---|
48 | n/a | # Test simple text-independent helper function |
---|
49 | n/a | def test_get_line_col(self): |
---|
50 | n/a | self.assertEqual(se.get_line_col('1.0'), (1, 0)) |
---|
51 | n/a | self.assertEqual(se.get_line_col('1.11'), (1, 11)) |
---|
52 | n/a | |
---|
53 | n/a | self.assertRaises(ValueError, se.get_line_col, ('1.0 lineend')) |
---|
54 | n/a | self.assertRaises(ValueError, se.get_line_col, ('end')) |
---|
55 | n/a | |
---|
56 | n/a | class GetSelectionTest(unittest.TestCase): |
---|
57 | n/a | # Test text-dependent helper function. |
---|
58 | n/a | ## # Need gui for text.index('sel.first/sel.last/insert'). |
---|
59 | n/a | ## @classmethod |
---|
60 | n/a | ## def setUpClass(cls): |
---|
61 | n/a | ## requires('gui') |
---|
62 | n/a | ## cls.root = Tk() |
---|
63 | n/a | ## |
---|
64 | n/a | ## @classmethod |
---|
65 | n/a | ## def tearDownClass(cls): |
---|
66 | n/a | ## cls.root.destroy() |
---|
67 | n/a | ## del cls.root |
---|
68 | n/a | |
---|
69 | n/a | def test_get_selection(self): |
---|
70 | n/a | # text = Text(master=self.root) |
---|
71 | n/a | text = mockText() |
---|
72 | n/a | text.insert('1.0', 'Hello World!') |
---|
73 | n/a | |
---|
74 | n/a | # fix text.index result when called in get_selection |
---|
75 | n/a | def sel(s): |
---|
76 | n/a | # select entire text, cursor irrelevant |
---|
77 | n/a | if s == 'sel.first': return '1.0' |
---|
78 | n/a | if s == 'sel.last': return '1.12' |
---|
79 | n/a | raise TclError |
---|
80 | n/a | text.index = sel # replaces .tag_add('sel', '1.0, '1.12') |
---|
81 | n/a | self.assertEqual(se.get_selection(text), ('1.0', '1.12')) |
---|
82 | n/a | |
---|
83 | n/a | def mark(s): |
---|
84 | n/a | # no selection, cursor after 'Hello' |
---|
85 | n/a | if s == 'insert': return '1.5' |
---|
86 | n/a | raise TclError |
---|
87 | n/a | text.index = mark # replaces .mark_set('insert', '1.5') |
---|
88 | n/a | self.assertEqual(se.get_selection(text), ('1.5', '1.5')) |
---|
89 | n/a | |
---|
90 | n/a | |
---|
91 | n/a | class ReverseSearchTest(unittest.TestCase): |
---|
92 | n/a | # Test helper function that searches backwards within a line. |
---|
93 | n/a | def test_search_reverse(self): |
---|
94 | n/a | Equal = self.assertEqual |
---|
95 | n/a | line = "Here is an 'is' test text." |
---|
96 | n/a | prog = re.compile('is') |
---|
97 | n/a | Equal(se.search_reverse(prog, line, len(line)).span(), (12, 14)) |
---|
98 | n/a | Equal(se.search_reverse(prog, line, 14).span(), (12, 14)) |
---|
99 | n/a | Equal(se.search_reverse(prog, line, 13).span(), (5, 7)) |
---|
100 | n/a | Equal(se.search_reverse(prog, line, 7).span(), (5, 7)) |
---|
101 | n/a | Equal(se.search_reverse(prog, line, 6), None) |
---|
102 | n/a | |
---|
103 | n/a | |
---|
104 | n/a | class SearchEngineTest(unittest.TestCase): |
---|
105 | n/a | # Test class methods that do not use Text widget. |
---|
106 | n/a | |
---|
107 | n/a | def setUp(self): |
---|
108 | n/a | self.engine = se.SearchEngine(root=None) |
---|
109 | n/a | # Engine.root is only used to create error message boxes. |
---|
110 | n/a | # The mock replacement ignores the root argument. |
---|
111 | n/a | |
---|
112 | n/a | def test_is_get(self): |
---|
113 | n/a | engine = self.engine |
---|
114 | n/a | Equal = self.assertEqual |
---|
115 | n/a | |
---|
116 | n/a | Equal(engine.getpat(), '') |
---|
117 | n/a | engine.setpat('hello') |
---|
118 | n/a | Equal(engine.getpat(), 'hello') |
---|
119 | n/a | |
---|
120 | n/a | Equal(engine.isre(), False) |
---|
121 | n/a | engine.revar.set(1) |
---|
122 | n/a | Equal(engine.isre(), True) |
---|
123 | n/a | |
---|
124 | n/a | Equal(engine.iscase(), False) |
---|
125 | n/a | engine.casevar.set(1) |
---|
126 | n/a | Equal(engine.iscase(), True) |
---|
127 | n/a | |
---|
128 | n/a | Equal(engine.isword(), False) |
---|
129 | n/a | engine.wordvar.set(1) |
---|
130 | n/a | Equal(engine.isword(), True) |
---|
131 | n/a | |
---|
132 | n/a | Equal(engine.iswrap(), True) |
---|
133 | n/a | engine.wrapvar.set(0) |
---|
134 | n/a | Equal(engine.iswrap(), False) |
---|
135 | n/a | |
---|
136 | n/a | Equal(engine.isback(), False) |
---|
137 | n/a | engine.backvar.set(1) |
---|
138 | n/a | Equal(engine.isback(), True) |
---|
139 | n/a | |
---|
140 | n/a | def test_setcookedpat(self): |
---|
141 | n/a | engine = self.engine |
---|
142 | n/a | engine.setcookedpat(r'\s') |
---|
143 | n/a | self.assertEqual(engine.getpat(), r'\s') |
---|
144 | n/a | engine.revar.set(1) |
---|
145 | n/a | engine.setcookedpat(r'\s') |
---|
146 | n/a | self.assertEqual(engine.getpat(), r'\\s') |
---|
147 | n/a | |
---|
148 | n/a | def test_getcookedpat(self): |
---|
149 | n/a | engine = self.engine |
---|
150 | n/a | Equal = self.assertEqual |
---|
151 | n/a | |
---|
152 | n/a | Equal(engine.getcookedpat(), '') |
---|
153 | n/a | engine.setpat('hello') |
---|
154 | n/a | Equal(engine.getcookedpat(), 'hello') |
---|
155 | n/a | engine.wordvar.set(True) |
---|
156 | n/a | Equal(engine.getcookedpat(), r'\bhello\b') |
---|
157 | n/a | engine.wordvar.set(False) |
---|
158 | n/a | |
---|
159 | n/a | engine.setpat(r'\s') |
---|
160 | n/a | Equal(engine.getcookedpat(), r'\\s') |
---|
161 | n/a | engine.revar.set(True) |
---|
162 | n/a | Equal(engine.getcookedpat(), r'\s') |
---|
163 | n/a | |
---|
164 | n/a | def test_getprog(self): |
---|
165 | n/a | engine = self.engine |
---|
166 | n/a | Equal = self.assertEqual |
---|
167 | n/a | |
---|
168 | n/a | engine.setpat('Hello') |
---|
169 | n/a | temppat = engine.getprog() |
---|
170 | n/a | Equal(temppat.pattern, re.compile('Hello', re.IGNORECASE).pattern) |
---|
171 | n/a | engine.casevar.set(1) |
---|
172 | n/a | temppat = engine.getprog() |
---|
173 | n/a | Equal(temppat.pattern, re.compile('Hello').pattern, 0) |
---|
174 | n/a | |
---|
175 | n/a | engine.setpat('') |
---|
176 | n/a | Equal(engine.getprog(), None) |
---|
177 | n/a | engine.setpat('+') |
---|
178 | n/a | engine.revar.set(1) |
---|
179 | n/a | Equal(engine.getprog(), None) |
---|
180 | n/a | self.assertEqual(Mbox.showerror.message, |
---|
181 | n/a | 'Error: nothing to repeat at position 0\nPattern: +') |
---|
182 | n/a | |
---|
183 | n/a | def test_report_error(self): |
---|
184 | n/a | showerror = Mbox.showerror |
---|
185 | n/a | Equal = self.assertEqual |
---|
186 | n/a | pat = '[a-z' |
---|
187 | n/a | msg = 'unexpected end of regular expression' |
---|
188 | n/a | |
---|
189 | n/a | Equal(self.engine.report_error(pat, msg), None) |
---|
190 | n/a | Equal(showerror.title, 'Regular expression error') |
---|
191 | n/a | expected_message = ("Error: " + msg + "\nPattern: [a-z") |
---|
192 | n/a | Equal(showerror.message, expected_message) |
---|
193 | n/a | |
---|
194 | n/a | Equal(self.engine.report_error(pat, msg, 5), None) |
---|
195 | n/a | Equal(showerror.title, 'Regular expression error') |
---|
196 | n/a | expected_message += "\nOffset: 5" |
---|
197 | n/a | Equal(showerror.message, expected_message) |
---|
198 | n/a | |
---|
199 | n/a | |
---|
200 | n/a | class SearchTest(unittest.TestCase): |
---|
201 | n/a | # Test that search_text makes right call to right method. |
---|
202 | n/a | |
---|
203 | n/a | @classmethod |
---|
204 | n/a | def setUpClass(cls): |
---|
205 | n/a | ## requires('gui') |
---|
206 | n/a | ## cls.root = Tk() |
---|
207 | n/a | ## cls.text = Text(master=cls.root) |
---|
208 | n/a | cls.text = mockText() |
---|
209 | n/a | test_text = ( |
---|
210 | n/a | 'First line\n' |
---|
211 | n/a | 'Line with target\n' |
---|
212 | n/a | 'Last line\n') |
---|
213 | n/a | cls.text.insert('1.0', test_text) |
---|
214 | n/a | cls.pat = re.compile('target') |
---|
215 | n/a | |
---|
216 | n/a | cls.engine = se.SearchEngine(None) |
---|
217 | n/a | cls.engine.search_forward = lambda *args: ('f', args) |
---|
218 | n/a | cls.engine.search_backward = lambda *args: ('b', args) |
---|
219 | n/a | |
---|
220 | n/a | ## @classmethod |
---|
221 | n/a | ## def tearDownClass(cls): |
---|
222 | n/a | ## cls.root.destroy() |
---|
223 | n/a | ## del cls.root |
---|
224 | n/a | |
---|
225 | n/a | def test_search(self): |
---|
226 | n/a | Equal = self.assertEqual |
---|
227 | n/a | engine = self.engine |
---|
228 | n/a | search = engine.search_text |
---|
229 | n/a | text = self.text |
---|
230 | n/a | pat = self.pat |
---|
231 | n/a | |
---|
232 | n/a | engine.patvar.set(None) |
---|
233 | n/a | #engine.revar.set(pat) |
---|
234 | n/a | Equal(search(text), None) |
---|
235 | n/a | |
---|
236 | n/a | def mark(s): |
---|
237 | n/a | # no selection, cursor after 'Hello' |
---|
238 | n/a | if s == 'insert': return '1.5' |
---|
239 | n/a | raise TclError |
---|
240 | n/a | text.index = mark |
---|
241 | n/a | Equal(search(text, pat), ('f', (text, pat, 1, 5, True, False))) |
---|
242 | n/a | engine.wrapvar.set(False) |
---|
243 | n/a | Equal(search(text, pat), ('f', (text, pat, 1, 5, False, False))) |
---|
244 | n/a | engine.wrapvar.set(True) |
---|
245 | n/a | engine.backvar.set(True) |
---|
246 | n/a | Equal(search(text, pat), ('b', (text, pat, 1, 5, True, False))) |
---|
247 | n/a | engine.backvar.set(False) |
---|
248 | n/a | |
---|
249 | n/a | def sel(s): |
---|
250 | n/a | if s == 'sel.first': return '2.10' |
---|
251 | n/a | if s == 'sel.last': return '2.16' |
---|
252 | n/a | raise TclError |
---|
253 | n/a | text.index = sel |
---|
254 | n/a | Equal(search(text, pat), ('f', (text, pat, 2, 16, True, False))) |
---|
255 | n/a | Equal(search(text, pat, True), ('f', (text, pat, 2, 10, True, True))) |
---|
256 | n/a | engine.backvar.set(True) |
---|
257 | n/a | Equal(search(text, pat), ('b', (text, pat, 2, 10, True, False))) |
---|
258 | n/a | Equal(search(text, pat, True), ('b', (text, pat, 2, 16, True, True))) |
---|
259 | n/a | |
---|
260 | n/a | |
---|
261 | n/a | class ForwardBackwardTest(unittest.TestCase): |
---|
262 | n/a | # Test that search_forward method finds the target. |
---|
263 | n/a | ## @classmethod |
---|
264 | n/a | ## def tearDownClass(cls): |
---|
265 | n/a | ## cls.root.destroy() |
---|
266 | n/a | ## del cls.root |
---|
267 | n/a | |
---|
268 | n/a | @classmethod |
---|
269 | n/a | def setUpClass(cls): |
---|
270 | n/a | cls.engine = se.SearchEngine(None) |
---|
271 | n/a | ## requires('gui') |
---|
272 | n/a | ## cls.root = Tk() |
---|
273 | n/a | ## cls.text = Text(master=cls.root) |
---|
274 | n/a | cls.text = mockText() |
---|
275 | n/a | # search_backward calls index('end-1c') |
---|
276 | n/a | cls.text.index = lambda index: '4.0' |
---|
277 | n/a | test_text = ( |
---|
278 | n/a | 'First line\n' |
---|
279 | n/a | 'Line with target\n' |
---|
280 | n/a | 'Last line\n') |
---|
281 | n/a | cls.text.insert('1.0', test_text) |
---|
282 | n/a | cls.pat = re.compile('target') |
---|
283 | n/a | cls.res = (2, (10, 16)) # line, slice indexes of 'target' |
---|
284 | n/a | cls.failpat = re.compile('xyz') # not in text |
---|
285 | n/a | cls.emptypat = re.compile(r'\w*') # empty match possible |
---|
286 | n/a | |
---|
287 | n/a | def make_search(self, func): |
---|
288 | n/a | def search(pat, line, col, wrap, ok=0): |
---|
289 | n/a | res = func(self.text, pat, line, col, wrap, ok) |
---|
290 | n/a | # res is (line, matchobject) or None |
---|
291 | n/a | return (res[0], res[1].span()) if res else res |
---|
292 | n/a | return search |
---|
293 | n/a | |
---|
294 | n/a | def test_search_forward(self): |
---|
295 | n/a | # search for non-empty match |
---|
296 | n/a | Equal = self.assertEqual |
---|
297 | n/a | forward = self.make_search(self.engine.search_forward) |
---|
298 | n/a | pat = self.pat |
---|
299 | n/a | Equal(forward(pat, 1, 0, True), self.res) |
---|
300 | n/a | Equal(forward(pat, 3, 0, True), self.res) # wrap |
---|
301 | n/a | Equal(forward(pat, 3, 0, False), None) # no wrap |
---|
302 | n/a | Equal(forward(pat, 2, 10, False), self.res) |
---|
303 | n/a | |
---|
304 | n/a | Equal(forward(self.failpat, 1, 0, True), None) |
---|
305 | n/a | Equal(forward(self.emptypat, 2, 9, True, ok=True), (2, (9, 9))) |
---|
306 | n/a | #Equal(forward(self.emptypat, 2, 9, True), self.res) |
---|
307 | n/a | # While the initial empty match is correctly ignored, skipping |
---|
308 | n/a | # the rest of the line and returning (3, (0,4)) seems buggy - tjr. |
---|
309 | n/a | Equal(forward(self.emptypat, 2, 10, True), self.res) |
---|
310 | n/a | |
---|
311 | n/a | def test_search_backward(self): |
---|
312 | n/a | # search for non-empty match |
---|
313 | n/a | Equal = self.assertEqual |
---|
314 | n/a | backward = self.make_search(self.engine.search_backward) |
---|
315 | n/a | pat = self.pat |
---|
316 | n/a | Equal(backward(pat, 3, 5, True), self.res) |
---|
317 | n/a | Equal(backward(pat, 2, 0, True), self.res) # wrap |
---|
318 | n/a | Equal(backward(pat, 2, 0, False), None) # no wrap |
---|
319 | n/a | Equal(backward(pat, 2, 16, False), self.res) |
---|
320 | n/a | |
---|
321 | n/a | Equal(backward(self.failpat, 3, 9, True), None) |
---|
322 | n/a | Equal(backward(self.emptypat, 2, 10, True, ok=True), (2, (9,9))) |
---|
323 | n/a | # Accepted because 9 < 10, not because ok=True. |
---|
324 | n/a | # It is not clear that ok=True is useful going back - tjr |
---|
325 | n/a | Equal(backward(self.emptypat, 2, 9, True), (2, (5, 9))) |
---|
326 | n/a | |
---|
327 | n/a | |
---|
328 | n/a | if __name__ == '__main__': |
---|
329 | n/a | unittest.main(verbosity=2, exit=2) |
---|