- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathtest_config_key.py
356 lines (287 loc) · 11.2 KB
/
test_config_key.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"""Test config_key, coverage 98%.
Coverage is effectively 100%. Tkinter dialog is mocked, Mac-only line
may be skipped, and dummy function in bind test should not be called.
Not tested: exit with 'self.advanced or self.keys_ok(keys) ...' False.
"""
fromidlelibimportconfig_key
fromtest.supportimportrequires
importunittest
fromunittestimportmock
fromtkinterimportTk, TclError
fromidlelib.idle_test.mock_idleimportFunc
fromidlelib.idle_test.mock_tkimportMbox_func
classValidationTest(unittest.TestCase):
"Test validation methods: ok, keys_ok, bind_ok."
classValidator(config_key.GetKeysFrame):
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
classlist_keys_final:
get=Func()
self.list_keys_final=list_keys_final
get_modifiers=Func()
showerror=Mbox_func()
@classmethod
defsetUpClass(cls):
requires('gui')
cls.root=Tk()
cls.root.withdraw()
keylist= [['<Key-F12>'], ['<Control-Key-x>', '<Control-Key-X>']]
cls.dialog=cls.Validator(cls.root, '<<Test>>', keylist)
@classmethod
deftearDownClass(cls):
delcls.dialog
cls.root.update_idletasks()
cls.root.destroy()
delcls.root
defsetUp(self):
self.dialog.showerror.message=''
# A test that needs a particular final key value should set it.
# A test that sets a non-blank modifier list should reset it to [].
deftest_ok_empty(self):
self.dialog.key_string.set(' ')
self.dialog.ok()
self.assertEqual(self.dialog.result, '')
self.assertEqual(self.dialog.showerror.message, 'No key specified.')
deftest_ok_good(self):
self.dialog.key_string.set('<Key-F11>')
self.dialog.list_keys_final.get.result='F11'
self.dialog.ok()
self.assertEqual(self.dialog.result, '<Key-F11>')
self.assertEqual(self.dialog.showerror.message, '')
deftest_keys_no_ending(self):
self.assertFalse(self.dialog.keys_ok('<Control-Shift'))
self.assertIn('Missing the final', self.dialog.showerror.message)
deftest_keys_no_modifier_bad(self):
self.dialog.list_keys_final.get.result='A'
self.assertFalse(self.dialog.keys_ok('<Key-A>'))
self.assertIn('No modifier', self.dialog.showerror.message)
deftest_keys_no_modifier_ok(self):
self.dialog.list_keys_final.get.result='F11'
self.assertTrue(self.dialog.keys_ok('<Key-F11>'))
self.assertEqual(self.dialog.showerror.message, '')
deftest_keys_shift_bad(self):
self.dialog.list_keys_final.get.result='a'
self.dialog.get_modifiers.result= ['Shift']
self.assertFalse(self.dialog.keys_ok('<a>'))
self.assertIn('shift modifier', self.dialog.showerror.message)
self.dialog.get_modifiers.result= []
deftest_keys_dup(self):
formods, final, seqin (([], 'F12', '<Key-F12>'),
(['Control'], 'x', '<Control-Key-x>'),
(['Control'], 'X', '<Control-Key-X>')):
withself.subTest(m=mods, f=final, s=seq):
self.dialog.list_keys_final.get.result=final
self.dialog.get_modifiers.result=mods
self.assertFalse(self.dialog.keys_ok(seq))
self.assertIn('already in use', self.dialog.showerror.message)
self.dialog.get_modifiers.result= []
deftest_bind_ok(self):
self.assertTrue(self.dialog.bind_ok('<Control-Shift-Key-a>'))
self.assertEqual(self.dialog.showerror.message, '')
deftest_bind_not_ok(self):
self.assertFalse(self.dialog.bind_ok('<Control-Shift>'))
self.assertIn('not accepted', self.dialog.showerror.message)
classToggleLevelTest(unittest.TestCase):
"Test toggle between Basic and Advanced frames."
@classmethod
defsetUpClass(cls):
requires('gui')
cls.root=Tk()
cls.root.withdraw()
cls.dialog=config_key.GetKeysFrame(cls.root, '<<Test>>', [])
@classmethod
deftearDownClass(cls):
delcls.dialog
cls.root.update_idletasks()
cls.root.destroy()
delcls.root
deftest_toggle_level(self):
dialog=self.dialog
defstackorder():
"""Get the stack order of the children of the frame.
winfo_children() stores the children in stack order, so
this can be used to check whether a frame is above or
below another one.
"""
forindex, childinenumerate(dialog.winfo_children()):
ifchild._name=='keyseq_basic':
basic=index
ifchild._name=='keyseq_advanced':
advanced=index
returnbasic, advanced
# New window starts at basic level.
self.assertFalse(dialog.advanced)
self.assertIn('Advanced', dialog.button_level['text'])
basic, advanced=stackorder()
self.assertGreater(basic, advanced)
# Toggle to advanced.
dialog.toggle_level()
self.assertTrue(dialog.advanced)
self.assertIn('Basic', dialog.button_level['text'])
basic, advanced=stackorder()
self.assertGreater(advanced, basic)
# Toggle to basic.
dialog.button_level.invoke()
self.assertFalse(dialog.advanced)
self.assertIn('Advanced', dialog.button_level['text'])
basic, advanced=stackorder()
self.assertGreater(basic, advanced)
classKeySelectionTest(unittest.TestCase):
"Test selecting key on Basic frames."
classBasic(config_key.GetKeysFrame):
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
classlist_keys_final:
get=Func()
select_clear=Func()
yview=Func()
self.list_keys_final=list_keys_final
defset_modifiers_for_platform(self):
self.modifiers= ['foo', 'bar', 'BAZ']
self.modifier_label= {'BAZ': 'ZZZ'}
showerror=Mbox_func()
@classmethod
defsetUpClass(cls):
requires('gui')
cls.root=Tk()
cls.root.withdraw()
cls.dialog=cls.Basic(cls.root, '<<Test>>', [])
@classmethod
deftearDownClass(cls):
delcls.dialog
cls.root.update_idletasks()
cls.root.destroy()
delcls.root
defsetUp(self):
self.dialog.clear_key_seq()
deftest_get_modifiers(self):
dialog=self.dialog
gm=dialog.get_modifiers
eq=self.assertEqual
# Modifiers are set on/off by invoking the checkbutton.
dialog.modifier_checkbuttons['foo'].invoke()
eq(gm(), ['foo'])
dialog.modifier_checkbuttons['BAZ'].invoke()
eq(gm(), ['foo', 'BAZ'])
dialog.modifier_checkbuttons['foo'].invoke()
eq(gm(), ['BAZ'])
@mock.patch.object(config_key.GetKeysFrame, 'get_modifiers')
deftest_build_key_string(self, mock_modifiers):
dialog=self.dialog
key=dialog.list_keys_final
string=dialog.key_string.get
eq=self.assertEqual
key.get.result='a'
mock_modifiers.return_value= []
dialog.build_key_string()
eq(string(), '<Key-a>')
mock_modifiers.return_value= ['mymod']
dialog.build_key_string()
eq(string(), '<mymod-Key-a>')
key.get.result=''
mock_modifiers.return_value= ['mymod', 'test']
dialog.build_key_string()
eq(string(), '<mymod-test>')
@mock.patch.object(config_key.GetKeysFrame, 'get_modifiers')
deftest_final_key_selected(self, mock_modifiers):
dialog=self.dialog
key=dialog.list_keys_final
string=dialog.key_string.get
eq=self.assertEqual
mock_modifiers.return_value= ['Shift']
key.get.result='{'
dialog.final_key_selected()
eq(string(), '<Shift-Key-braceleft>')
classCancelWindowTest(unittest.TestCase):
"Simulate user clicking [Cancel] button."
@classmethod
defsetUpClass(cls):
requires('gui')
cls.root=Tk()
cls.root.withdraw()
cls.dialog=config_key.GetKeysWindow(
cls.root, 'Title', '<<Test>>', [], _utest=True)
@classmethod
deftearDownClass(cls):
cls.dialog.cancel()
delcls.dialog
cls.root.update_idletasks()
cls.root.destroy()
delcls.root
@mock.patch.object(config_key.GetKeysFrame, 'ok')
deftest_cancel(self, mock_frame_ok):
self.assertEqual(self.dialog.winfo_class(), 'Toplevel')
self.dialog.button_cancel.invoke()
withself.assertRaises(TclError):
self.dialog.winfo_class()
self.assertEqual(self.dialog.result, '')
mock_frame_ok.assert_not_called()
classOKWindowTest(unittest.TestCase):
"Simulate user clicking [OK] button."
@classmethod
defsetUpClass(cls):
requires('gui')
cls.root=Tk()
cls.root.withdraw()
cls.dialog=config_key.GetKeysWindow(
cls.root, 'Title', '<<Test>>', [], _utest=True)
@classmethod
deftearDownClass(cls):
cls.dialog.cancel()
delcls.dialog
cls.root.update_idletasks()
cls.root.destroy()
delcls.root
@mock.patch.object(config_key.GetKeysFrame, 'ok')
deftest_ok(self, mock_frame_ok):
self.assertEqual(self.dialog.winfo_class(), 'Toplevel')
self.dialog.button_ok.invoke()
withself.assertRaises(TclError):
self.dialog.winfo_class()
mock_frame_ok.assert_called()
classWindowResultTest(unittest.TestCase):
"Test window result get and set."
@classmethod
defsetUpClass(cls):
requires('gui')
cls.root=Tk()
cls.root.withdraw()
cls.dialog=config_key.GetKeysWindow(
cls.root, 'Title', '<<Test>>', [], _utest=True)
@classmethod
deftearDownClass(cls):
cls.dialog.cancel()
delcls.dialog
cls.root.update_idletasks()
cls.root.destroy()
delcls.root
deftest_result(self):
dialog=self.dialog
eq=self.assertEqual
dialog.result=''
eq(dialog.result, '')
eq(dialog.frame.result,'')
dialog.result='bar'
eq(dialog.result,'bar')
eq(dialog.frame.result,'bar')
dialog.frame.result='foo'
eq(dialog.result, 'foo')
eq(dialog.frame.result,'foo')
classHelperTest(unittest.TestCase):
"Test module level helper functions."
deftest_translate_key(self):
tr=config_key.translate_key
eq=self.assertEqual
# Letters return unchanged with no 'Shift'.
eq(tr('q', []), 'Key-q')
eq(tr('q', ['Control', 'Alt']), 'Key-q')
# 'Shift' uppercases single lowercase letters.
eq(tr('q', ['Shift']), 'Key-Q')
eq(tr('q', ['Control', 'Shift']), 'Key-Q')
eq(tr('q', ['Control', 'Alt', 'Shift']), 'Key-Q')
# Convert key name to keysym.
eq(tr('Page Up', []), 'Key-Prior')
# 'Shift' doesn't change case when it's not a single char.
eq(tr('*', ['Shift']), 'Key-asterisk')
if__name__=='__main__':
unittest.main(verbosity=2)