ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_query.py

Python code coverage for Lib/idlelib/idle_test/test_query.py

#countcontent
1n/a"""Test idlelib.query.
2n/a
3n/aNon-gui tests for Query, SectionName, ModuleName, and HelpSource use
4n/adummy versions that extract the non-gui methods and add other needed
5n/aattributes. GUI tests create an instance of each class and simulate
6n/aentries and button clicks. Subclass tests only target the new code in
7n/athe subclass definition.
8n/a
9n/aThe appearance of the widgets is checked by the Query and
10n/aHelpSource htests. These are run by running query.py.
11n/a
12n/aCoverage: 94% (100% for Query and SectionName).
13n/a6 of 8 missing are ModuleName exceptions I don't know how to trigger.
14n/a"""
15n/afrom test.support import requires
16n/aimport sys
17n/afrom tkinter import Tk
18n/aimport unittest
19n/afrom unittest import mock
20n/afrom idlelib.idle_test.mock_tk import Var
21n/afrom idlelib import query
22n/a
23n/a
24n/a# NON-GUI TESTS
25n/a
26n/aclass QueryTest(unittest.TestCase):
27n/a "Test Query base class."
28n/a
29n/a class Dummy_Query:
30n/a # Test the following Query methods.
31n/a entry_ok = query.Query.entry_ok
32n/a ok = query.Query.ok
33n/a cancel = query.Query.cancel
34n/a # Add attributes and initialization needed for tests.
35n/a entry = Var()
36n/a entry_error = {}
37n/a def __init__(self, dummy_entry):
38n/a self.entry.set(dummy_entry)
39n/a self.entry_error['text'] = ''
40n/a self.result = None
41n/a self.destroyed = False
42n/a def showerror(self, message):
43n/a self.entry_error['text'] = message
44n/a def destroy(self):
45n/a self.destroyed = True
46n/a
47n/a def test_entry_ok_blank(self):
48n/a dialog = self.Dummy_Query(' ')
49n/a self.assertEqual(dialog.entry_ok(), None)
50n/a self.assertEqual((dialog.result, dialog.destroyed), (None, False))
51n/a self.assertIn('blank line', dialog.entry_error['text'])
52n/a
53n/a def test_entry_ok_good(self):
54n/a dialog = self.Dummy_Query(' good ')
55n/a Equal = self.assertEqual
56n/a Equal(dialog.entry_ok(), 'good')
57n/a Equal((dialog.result, dialog.destroyed), (None, False))
58n/a Equal(dialog.entry_error['text'], '')
59n/a
60n/a def test_ok_blank(self):
61n/a dialog = self.Dummy_Query('')
62n/a dialog.entry.focus_set = mock.Mock()
63n/a self.assertEqual(dialog.ok(), None)
64n/a self.assertTrue(dialog.entry.focus_set.called)
65n/a del dialog.entry.focus_set
66n/a self.assertEqual((dialog.result, dialog.destroyed), (None, False))
67n/a
68n/a def test_ok_good(self):
69n/a dialog = self.Dummy_Query('good')
70n/a self.assertEqual(dialog.ok(), None)
71n/a self.assertEqual((dialog.result, dialog.destroyed), ('good', True))
72n/a
73n/a def test_cancel(self):
74n/a dialog = self.Dummy_Query('does not matter')
75n/a self.assertEqual(dialog.cancel(), None)
76n/a self.assertEqual((dialog.result, dialog.destroyed), (None, True))
77n/a
78n/a
79n/aclass SectionNameTest(unittest.TestCase):
80n/a "Test SectionName subclass of Query."
81n/a
82n/a class Dummy_SectionName:
83n/a entry_ok = query.SectionName.entry_ok # Function being tested.
84n/a used_names = ['used']
85n/a entry = Var()
86n/a entry_error = {}
87n/a def __init__(self, dummy_entry):
88n/a self.entry.set(dummy_entry)
89n/a self.entry_error['text'] = ''
90n/a def showerror(self, message):
91n/a self.entry_error['text'] = message
92n/a
93n/a def test_blank_section_name(self):
94n/a dialog = self.Dummy_SectionName(' ')
95n/a self.assertEqual(dialog.entry_ok(), None)
96n/a self.assertIn('no name', dialog.entry_error['text'])
97n/a
98n/a def test_used_section_name(self):
99n/a dialog = self.Dummy_SectionName('used')
100n/a self.assertEqual(dialog.entry_ok(), None)
101n/a self.assertIn('use', dialog.entry_error['text'])
102n/a
103n/a def test_long_section_name(self):
104n/a dialog = self.Dummy_SectionName('good'*8)
105n/a self.assertEqual(dialog.entry_ok(), None)
106n/a self.assertIn('longer than 30', dialog.entry_error['text'])
107n/a
108n/a def test_good_section_name(self):
109n/a dialog = self.Dummy_SectionName(' good ')
110n/a self.assertEqual(dialog.entry_ok(), 'good')
111n/a self.assertEqual(dialog.entry_error['text'], '')
112n/a
113n/a
114n/aclass ModuleNameTest(unittest.TestCase):
115n/a "Test ModuleName subclass of Query."
116n/a
117n/a class Dummy_ModuleName:
118n/a entry_ok = query.ModuleName.entry_ok # Function being tested.
119n/a text0 = ''
120n/a entry = Var()
121n/a entry_error = {}
122n/a def __init__(self, dummy_entry):
123n/a self.entry.set(dummy_entry)
124n/a self.entry_error['text'] = ''
125n/a def showerror(self, message):
126n/a self.entry_error['text'] = message
127n/a
128n/a def test_blank_module_name(self):
129n/a dialog = self.Dummy_ModuleName(' ')
130n/a self.assertEqual(dialog.entry_ok(), None)
131n/a self.assertIn('no name', dialog.entry_error['text'])
132n/a
133n/a def test_bogus_module_name(self):
134n/a dialog = self.Dummy_ModuleName('__name_xyz123_should_not_exist__')
135n/a self.assertEqual(dialog.entry_ok(), None)
136n/a self.assertIn('not found', dialog.entry_error['text'])
137n/a
138n/a def test_c_source_name(self):
139n/a dialog = self.Dummy_ModuleName('itertools')
140n/a self.assertEqual(dialog.entry_ok(), None)
141n/a self.assertIn('source-based', dialog.entry_error['text'])
142n/a
143n/a def test_good_module_name(self):
144n/a dialog = self.Dummy_ModuleName('idlelib')
145n/a self.assertTrue(dialog.entry_ok().endswith('__init__.py'))
146n/a self.assertEqual(dialog.entry_error['text'], '')
147n/a
148n/a
149n/a# 3 HelpSource test classes each test one function.
150n/a
151n/aorig_platform = query.platform
152n/a
153n/aclass HelpsourceBrowsefileTest(unittest.TestCase):
154n/a "Test browse_file method of ModuleName subclass of Query."
155n/a
156n/a class Dummy_HelpSource:
157n/a browse_file = query.HelpSource.browse_file
158n/a pathvar = Var()
159n/a
160n/a def test_file_replaces_path(self):
161n/a dialog = self.Dummy_HelpSource()
162n/a # Path is widget entry, either '' or something.
163n/a # Func return is file dialog return, either '' or something.
164n/a # Func return should override widget entry.
165n/a # We need all 4 combination to test all (most) code paths.
166n/a for path, func, result in (
167n/a ('', lambda a,b,c:'', ''),
168n/a ('', lambda a,b,c: __file__, __file__),
169n/a ('htest', lambda a,b,c:'', 'htest'),
170n/a ('htest', lambda a,b,c: __file__, __file__)):
171n/a with self.subTest():
172n/a dialog.pathvar.set(path)
173n/a dialog.askfilename = func
174n/a dialog.browse_file()
175n/a self.assertEqual(dialog.pathvar.get(), result)
176n/a
177n/a
178n/aclass HelpsourcePathokTest(unittest.TestCase):
179n/a "Test path_ok method of HelpSource subclass of Query."
180n/a
181n/a class Dummy_HelpSource:
182n/a path_ok = query.HelpSource.path_ok
183n/a path = Var()
184n/a path_error = {}
185n/a def __init__(self, dummy_path):
186n/a self.path.set(dummy_path)
187n/a self.path_error['text'] = ''
188n/a def showerror(self, message, widget=None):
189n/a self.path_error['text'] = message
190n/a
191n/a @classmethod
192n/a def tearDownClass(cls):
193n/a query.platform = orig_platform
194n/a
195n/a def test_path_ok_blank(self):
196n/a dialog = self.Dummy_HelpSource(' ')
197n/a self.assertEqual(dialog.path_ok(), None)
198n/a self.assertIn('no help file', dialog.path_error['text'])
199n/a
200n/a def test_path_ok_bad(self):
201n/a dialog = self.Dummy_HelpSource(__file__ + 'bad-bad-bad')
202n/a self.assertEqual(dialog.path_ok(), None)
203n/a self.assertIn('not exist', dialog.path_error['text'])
204n/a
205n/a def test_path_ok_web(self):
206n/a dialog = self.Dummy_HelpSource('')
207n/a Equal = self.assertEqual
208n/a for url in 'www.py.org', 'http://py.org':
209n/a with self.subTest():
210n/a dialog.path.set(url)
211n/a self.assertEqual(dialog.path_ok(), url)
212n/a self.assertEqual(dialog.path_error['text'], '')
213n/a
214n/a def test_path_ok_file(self):
215n/a dialog = self.Dummy_HelpSource('')
216n/a for platform, prefix in ('darwin', 'file://'), ('other', ''):
217n/a with self.subTest():
218n/a query.platform = platform
219n/a dialog.path.set(__file__)
220n/a self.assertEqual(dialog.path_ok(), prefix + __file__)
221n/a self.assertEqual(dialog.path_error['text'], '')
222n/a
223n/a
224n/aclass HelpsourceEntryokTest(unittest.TestCase):
225n/a "Test entry_ok method of HelpSource subclass of Query."
226n/a
227n/a class Dummy_HelpSource:
228n/a entry_ok = query.HelpSource.entry_ok
229n/a entry_error = {}
230n/a path_error = {}
231n/a def item_ok(self):
232n/a return self.name
233n/a def path_ok(self):
234n/a return self.path
235n/a
236n/a def test_entry_ok_helpsource(self):
237n/a dialog = self.Dummy_HelpSource()
238n/a for name, path, result in ((None, None, None),
239n/a (None, 'doc.txt', None),
240n/a ('doc', None, None),
241n/a ('doc', 'doc.txt', ('doc', 'doc.txt'))):
242n/a with self.subTest():
243n/a dialog.name, dialog.path = name, path
244n/a self.assertEqual(dialog.entry_ok(), result)
245n/a
246n/a
247n/a# GUI TESTS
248n/a
249n/aclass QueryGuiTest(unittest.TestCase):
250n/a
251n/a @classmethod
252n/a def setUpClass(cls):
253n/a requires('gui')
254n/a cls.root = root = Tk()
255n/a cls.root.withdraw()
256n/a cls.dialog = query.Query(root, 'TEST', 'test', _utest=True)
257n/a cls.dialog.destroy = mock.Mock()
258n/a
259n/a @classmethod
260n/a def tearDownClass(cls):
261n/a del cls.dialog
262n/a cls.root.destroy()
263n/a del cls.root
264n/a
265n/a def setUp(self):
266n/a self.dialog.entry.delete(0, 'end')
267n/a self.dialog.result = None
268n/a self.dialog.destroy.reset_mock()
269n/a
270n/a def test_click_ok(self):
271n/a dialog = self.dialog
272n/a dialog.entry.insert(0, 'abc')
273n/a dialog.button_ok.invoke()
274n/a self.assertEqual(dialog.result, 'abc')
275n/a self.assertTrue(dialog.destroy.called)
276n/a
277n/a def test_click_blank(self):
278n/a dialog = self.dialog
279n/a dialog.button_ok.invoke()
280n/a self.assertEqual(dialog.result, None)
281n/a self.assertFalse(dialog.destroy.called)
282n/a
283n/a def test_click_cancel(self):
284n/a dialog = self.dialog
285n/a dialog.entry.insert(0, 'abc')
286n/a dialog.button_cancel.invoke()
287n/a self.assertEqual(dialog.result, None)
288n/a self.assertTrue(dialog.destroy.called)
289n/a
290n/a
291n/aclass SectionnameGuiTest(unittest.TestCase):
292n/a
293n/a @classmethod
294n/a def setUpClass(cls):
295n/a requires('gui')
296n/a
297n/a def test_click_section_name(self):
298n/a root = Tk()
299n/a root.withdraw()
300n/a dialog = query.SectionName(root, 'T', 't', {'abc'}, _utest=True)
301n/a Equal = self.assertEqual
302n/a self.assertEqual(dialog.used_names, {'abc'})
303n/a dialog.entry.insert(0, 'okay')
304n/a dialog.button_ok.invoke()
305n/a self.assertEqual(dialog.result, 'okay')
306n/a del dialog
307n/a root.destroy()
308n/a del root
309n/a
310n/a
311n/aclass ModulenameGuiTest(unittest.TestCase):
312n/a
313n/a @classmethod
314n/a def setUpClass(cls):
315n/a requires('gui')
316n/a
317n/a def test_click_module_name(self):
318n/a root = Tk()
319n/a root.withdraw()
320n/a dialog = query.ModuleName(root, 'T', 't', 'idlelib', _utest=True)
321n/a self.assertEqual(dialog.text0, 'idlelib')
322n/a self.assertEqual(dialog.entry.get(), 'idlelib')
323n/a dialog.button_ok.invoke()
324n/a self.assertTrue(dialog.result.endswith('__init__.py'))
325n/a del dialog
326n/a root.destroy()
327n/a del root
328n/a
329n/a
330n/aclass HelpsourceGuiTest(unittest.TestCase):
331n/a
332n/a @classmethod
333n/a def setUpClass(cls):
334n/a requires('gui')
335n/a
336n/a def test_click_help_source(self):
337n/a root = Tk()
338n/a root.withdraw()
339n/a dialog = query.HelpSource(root, 'T', menuitem='__test__',
340n/a filepath=__file__, _utest=True)
341n/a Equal = self.assertEqual
342n/a Equal(dialog.entry.get(), '__test__')
343n/a Equal(dialog.path.get(), __file__)
344n/a dialog.button_ok.invoke()
345n/a prefix = "file://" if sys.platform == 'darwin' else ''
346n/a Equal(dialog.result, ('__test__', prefix + __file__))
347n/a del dialog
348n/a root.destroy()
349n/a del root
350n/a
351n/a
352n/aif __name__ == '__main__':
353n/a unittest.main(verbosity=2, exit=False)