Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 65db854

Browse files
committed
Issue #27621: Put query response validation error messages in query box
instead of in separate massagebox. Redo tests to match. Add Mac OSX refinements. Original patch by Mark Roseman.
1 parent 83545f1 commit 65db854

2 files changed

Lines changed: 165 additions & 194 deletions

File tree

Lib/idlelib/idle_test/test_query.py

Lines changed: 93 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,9 @@
1616
from tkinter import Tk
1717
import unittest
1818
from unittest import mock
19-
from idlelib.idle_test.mock_tk import Var, Mbox_func
19+
from idlelib.idle_test.mock_tk import Var
2020
from idlelib import query
2121

22-
# Mock entry.showerror messagebox so don't need click to continue
23-
# when entry_ok and path_ok methods call it to display errors.
24-
25-
orig_showerror = query.showerror
26-
showerror = Mbox_func() # Instance has __call__ method.
27-
28-
def setUpModule():
29-
query.showerror = showerror
30-
31-
def tearDownModule():
32-
query.showerror = orig_showerror
33-
3422

3523
# NON-GUI TESTS
3624

@@ -42,59 +30,49 @@ class Dummy_Query:
4230
entry_ok = query.Query.entry_ok
4331
ok = query.Query.ok
4432
cancel = query.Query.cancel
45-
# Add attributes needed for the tests.
33+
# Add attributes and initialization needed for tests.
4634
entry = Var()
47-
result = None
48-
destroyed = False
35+
entry_error = {}
36+
def __init__(self, dummy_entry):
37+
self.entry.set(dummy_entry)
38+
self.entry_error['text'] = ''
39+
self.result = None
40+
self.destroyed = False
41+
def showerror(self, message):
42+
self.entry_error['text'] = message
4943
def destroy(self):
5044
self.destroyed = True
5145

52-
dialog = Dummy_Query()
53-
54-
def setUp(self):
55-
showerror.title = None
56-
self.dialog.result = None
57-
self.dialog.destroyed = False
58-
5946
def test_entry_ok_blank(self):
60-
dialog = self.dialog
61-
Equal = self.assertEqual
62-
dialog.entry.set(' ')
63-
Equal(dialog.entry_ok(), None)
64-
Equal((dialog.result, dialog.destroyed), (None, False))
65-
Equal(showerror.title, 'Entry Error')
66-
self.assertIn('Blank', showerror.message)
47+
dialog = self.Dummy_Query(' ')
48+
self.assertEqual(dialog.entry_ok(), None)
49+
self.assertEqual((dialog.result, dialog.destroyed), (None, False))
50+
self.assertIn('blank line', dialog.entry_error['text'])
6751

6852
def test_entry_ok_good(self):
69-
dialog = self.dialog
53+
dialog = self.Dummy_Query(' good ')
7054
Equal = self.assertEqual
71-
dialog.entry.set(' good ')
7255
Equal(dialog.entry_ok(), 'good')
7356
Equal((dialog.result, dialog.destroyed), (None, False))
74-
Equal(showerror.title, None)
57+
Equal(dialog.entry_error['text'], '')
7558

7659
def test_ok_blank(self):
77-
dialog = self.dialog
78-
Equal = self.assertEqual
79-
dialog.entry.set('')
60+
dialog = self.Dummy_Query('')
8061
dialog.entry.focus_set = mock.Mock()
81-
Equal(dialog.ok(), None)
62+
self.assertEqual(dialog.ok(), None)
8263
self.assertTrue(dialog.entry.focus_set.called)
8364
del dialog.entry.focus_set
84-
Equal((dialog.result, dialog.destroyed), (None, False))
65+
self.assertEqual((dialog.result, dialog.destroyed), (None, False))
8566

8667
def test_ok_good(self):
87-
dialog = self.dialog
88-
Equal = self.assertEqual
89-
dialog.entry.set('good')
90-
Equal(dialog.ok(), None)
91-
Equal((dialog.result, dialog.destroyed), ('good', True))
68+
dialog = self.Dummy_Query('good')
69+
self.assertEqual(dialog.ok(), None)
70+
self.assertEqual((dialog.result, dialog.destroyed), ('good', True))
9271

9372
def test_cancel(self):
94-
dialog = self.dialog
95-
Equal = self.assertEqual
96-
Equal(self.dialog.cancel(), None)
97-
Equal((dialog.result, dialog.destroyed), (None, True))
73+
dialog = self.Dummy_Query('does not matter')
74+
self.assertEqual(dialog.cancel(), None)
75+
self.assertEqual((dialog.result, dialog.destroyed), (None, True))
9876

9977

10078
class SectionNameTest(unittest.TestCase):
@@ -104,42 +82,32 @@ class Dummy_SectionName:
10482
entry_ok = query.SectionName.entry_ok # Function being tested.
10583
used_names = ['used']
10684
entry = Var()
107-
108-
dialog = Dummy_SectionName()
109-
110-
def setUp(self):
111-
showerror.title = None
85+
entry_error = {}
86+
def __init__(self, dummy_entry):
87+
self.entry.set(dummy_entry)
88+
self.entry_error['text'] = ''
89+
def showerror(self, message):
90+
self.entry_error['text'] = message
11291

11392
def test_blank_section_name(self):
114-
dialog = self.dialog
115-
Equal = self.assertEqual
116-
dialog.entry.set(' ')
117-
Equal(dialog.entry_ok(), None)
118-
Equal(showerror.title, 'Name Error')
119-
self.assertIn('No', showerror.message)
93+
dialog = self.Dummy_SectionName(' ')
94+
self.assertEqual(dialog.entry_ok(), None)
95+
self.assertIn('no name', dialog.entry_error['text'])
12096

12197
def test_used_section_name(self):
122-
dialog = self.dialog
123-
Equal = self.assertEqual
124-
dialog.entry.set('used')
125-
Equal(self.dialog.entry_ok(), None)
126-
Equal(showerror.title, 'Name Error')
127-
self.assertIn('use', showerror.message)
98+
dialog = self.Dummy_SectionName('used')
99+
self.assertEqual(dialog.entry_ok(), None)
100+
self.assertIn('use', dialog.entry_error['text'])
128101

129102
def test_long_section_name(self):
130-
dialog = self.dialog
131-
Equal = self.assertEqual
132-
dialog.entry.set('good'*8)
133-
Equal(self.dialog.entry_ok(), None)
134-
Equal(showerror.title, 'Name Error')
135-
self.assertIn('too long', showerror.message)
103+
dialog = self.Dummy_SectionName('good'*8)
104+
self.assertEqual(dialog.entry_ok(), None)
105+
self.assertIn('longer than 30', dialog.entry_error['text'])
136106

137107
def test_good_section_name(self):
138-
dialog = self.dialog
139-
Equal = self.assertEqual
140-
dialog.entry.set(' good ')
141-
Equal(dialog.entry_ok(), 'good')
142-
Equal(showerror.title, None)
108+
dialog = self.Dummy_SectionName(' good ')
109+
self.assertEqual(dialog.entry_ok(), 'good')
110+
self.assertEqual(dialog.entry_error['text'], '')
143111

144112

145113
class ModuleNameTest(unittest.TestCase):
@@ -149,42 +117,32 @@ class Dummy_ModuleName:
149117
entry_ok = query.ModuleName.entry_ok # Function being tested.
150118
text0 = ''
151119
entry = Var()
152-
153-
dialog = Dummy_ModuleName()
154-
155-
def setUp(self):
156-
showerror.title = None
120+
entry_error = {}
121+
def __init__(self, dummy_entry):
122+
self.entry.set(dummy_entry)
123+
self.entry_error['text'] = ''
124+
def showerror(self, message):
125+
self.entry_error['text'] = message
157126

158127
def test_blank_module_name(self):
159-
dialog = self.dialog
160-
Equal = self.assertEqual
161-
dialog.entry.set(' ')
162-
Equal(dialog.entry_ok(), None)
163-
Equal(showerror.title, 'Name Error')
164-
self.assertIn('No', showerror.message)
128+
dialog = self.Dummy_ModuleName(' ')
129+
self.assertEqual(dialog.entry_ok(), None)
130+
self.assertIn('no name', dialog.entry_error['text'])
165131

166132
def test_bogus_module_name(self):
167-
dialog = self.dialog
168-
Equal = self.assertEqual
169-
dialog.entry.set('__name_xyz123_should_not_exist__')
170-
Equal(self.dialog.entry_ok(), None)
171-
Equal(showerror.title, 'Import Error')
172-
self.assertIn('not found', showerror.message)
133+
dialog = self.Dummy_ModuleName('__name_xyz123_should_not_exist__')
134+
self.assertEqual(dialog.entry_ok(), None)
135+
self.assertIn('not found', dialog.entry_error['text'])
173136

174137
def test_c_source_name(self):
175-
dialog = self.dialog
176-
Equal = self.assertEqual
177-
dialog.entry.set('itertools')
178-
Equal(self.dialog.entry_ok(), None)
179-
Equal(showerror.title, 'Import Error')
180-
self.assertIn('source-based', showerror.message)
138+
dialog = self.Dummy_ModuleName('itertools')
139+
self.assertEqual(dialog.entry_ok(), None)
140+
self.assertIn('source-based', dialog.entry_error['text'])
181141

182142
def test_good_module_name(self):
183-
dialog = self.dialog
184-
Equal = self.assertEqual
185-
dialog.entry.set('idlelib')
143+
dialog = self.Dummy_ModuleName('idlelib')
186144
self.assertTrue(dialog.entry_ok().endswith('__init__.py'))
187-
Equal(showerror.title, None)
145+
self.assertEqual(dialog.entry_error['text'], '')
188146

189147

190148
# 3 HelpSource test classes each test one function.
@@ -198,13 +156,13 @@ class Dummy_HelpSource:
198156
browse_file = query.HelpSource.browse_file
199157
pathvar = Var()
200158

201-
dialog = Dummy_HelpSource()
202-
203159
def test_file_replaces_path(self):
204-
# Path is widget entry, file is file dialog return.
205-
dialog = self.dialog
160+
dialog = self.Dummy_HelpSource()
161+
# Path is widget entry, either '' or something.
162+
# Func return is file dialog return, either '' or something.
163+
# Func return should override widget entry.
164+
# We need all 4 combination to test all (most) code paths.
206165
for path, func, result in (
207-
# We need all combination to test all (most) code paths.
208166
('', lambda a,b,c:'', ''),
209167
('', lambda a,b,c: __file__, __file__),
210168
('htest', lambda a,b,c:'', 'htest'),
@@ -217,78 +175,72 @@ def test_file_replaces_path(self):
217175

218176

219177
class HelpsourcePathokTest(unittest.TestCase):
220-
"Test path_ok method of ModuleName subclass of Query."
178+
"Test path_ok method of HelpSource subclass of Query."
221179

222180
class Dummy_HelpSource:
223181
path_ok = query.HelpSource.path_ok
224182
path = Var()
225-
226-
dialog = Dummy_HelpSource()
183+
path_error = {}
184+
def __init__(self, dummy_path):
185+
self.path.set(dummy_path)
186+
self.path_error['text'] = ''
187+
def showerror(self, message, widget=None):
188+
self.path_error['text'] = message
227189

228190
@classmethod
229191
def tearDownClass(cls):
230192
query.platform = orig_platform
231193

232-
def setUp(self):
233-
showerror.title = None
234-
235194
def test_path_ok_blank(self):
236-
dialog = self.dialog
237-
Equal = self.assertEqual
238-
dialog.path.set(' ')
239-
Equal(dialog.path_ok(), None)
240-
Equal(showerror.title, 'File Path Error')
241-
self.assertIn('No help', showerror.message)
195+
dialog = self.Dummy_HelpSource(' ')
196+
self.assertEqual(dialog.path_ok(), None)
197+
self.assertIn('no help file', dialog.path_error['text'])
242198

243199
def test_path_ok_bad(self):
244-
dialog = self.dialog
245-
Equal = self.assertEqual
246-
dialog.path.set(__file__ + 'bad-bad-bad')
247-
Equal(dialog.path_ok(), None)
248-
Equal(showerror.title, 'File Path Error')
249-
self.assertIn('not exist', showerror.message)
200+
dialog = self.Dummy_HelpSource(__file__ + 'bad-bad-bad')
201+
self.assertEqual(dialog.path_ok(), None)
202+
self.assertIn('not exist', dialog.path_error['text'])
250203

251204
def test_path_ok_web(self):
252-
dialog = self.dialog
205+
dialog = self.Dummy_HelpSource('')
253206
Equal = self.assertEqual
254207
for url in 'www.py.org', 'http://py.org':
255208
with self.subTest():
256209
dialog.path.set(url)
257-
Equal(dialog.path_ok(), url)
258-
Equal(showerror.title, None)
210+
self.assertEqual(dialog.path_ok(), url)
211+
self.assertEqual(dialog.path_error['text'], '')
259212

260213
def test_path_ok_file(self):
261-
dialog = self.dialog
262-
Equal = self.assertEqual
214+
dialog = self.Dummy_HelpSource('')
263215
for platform, prefix in ('darwin', 'file://'), ('other', ''):
264216
with self.subTest():
265217
query.platform = platform
266218
dialog.path.set(__file__)
267-
Equal(dialog.path_ok(), prefix + __file__)
268-
Equal(showerror.title, None)
219+
self.assertEqual(dialog.path_ok(), prefix + __file__)
220+
self.assertEqual(dialog.path_error['text'], '')
269221

270222

271223
class HelpsourceEntryokTest(unittest.TestCase):
272-
"Test entry_ok method of ModuleName subclass of Query."
224+
"Test entry_ok method of HelpSource subclass of Query."
273225

274226
class Dummy_HelpSource:
275227
entry_ok = query.HelpSource.entry_ok
228+
entry_error = {}
229+
path_error = {}
276230
def item_ok(self):
277231
return self.name
278232
def path_ok(self):
279233
return self.path
280234

281-
dialog = Dummy_HelpSource()
282-
283235
def test_entry_ok_helpsource(self):
284-
dialog = self.dialog
236+
dialog = self.Dummy_HelpSource()
285237
for name, path, result in ((None, None, None),
286238
(None, 'doc.txt', None),
287239
('doc', None, None),
288240
('doc', 'doc.txt', ('doc', 'doc.txt'))):
289241
with self.subTest():
290242
dialog.name, dialog.path = name, path
291-
self.assertEqual(self.dialog.entry_ok(), result)
243+
self.assertEqual(dialog.entry_ok(), result)
292244

293245

294246
# GUI TESTS
@@ -344,10 +296,10 @@ def test_click_section_name(self):
344296
root = Tk()
345297
dialog = query.SectionName(root, 'T', 't', {'abc'}, _utest=True)
346298
Equal = self.assertEqual
347-
Equal(dialog.used_names, {'abc'})
299+
self.assertEqual(dialog.used_names, {'abc'})
348300
dialog.entry.insert(0, 'okay')
349301
dialog.button_ok.invoke()
350-
Equal(dialog.result, 'okay')
302+
self.assertEqual(dialog.result, 'okay')
351303
del dialog
352304
root.destroy()
353305
del root
@@ -362,9 +314,8 @@ def setUpClass(cls):
362314
def test_click_module_name(self):
363315
root = Tk()
364316
dialog = query.ModuleName(root, 'T', 't', 'idlelib', _utest=True)
365-
Equal = self.assertEqual
366-
Equal(dialog.text0, 'idlelib')
367-
Equal(dialog.entry.get(), 'idlelib')
317+
self.assertEqual(dialog.text0, 'idlelib')
318+
self.assertEqual(dialog.entry.get(), 'idlelib')
368319
dialog.button_ok.invoke()
369320
self.assertTrue(dialog.result.endswith('__init__.py'))
370321
del dialog

0 commit comments

Comments
 (0)