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

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

#countcontent
1n/a'''tests idlelib.searchbase.
2n/a
3n/aCoverage: 99%. The only thing not covered is inconsequential --
4n/atesting skipping of suite when self.needwrapbutton is false.
5n/a'''
6n/aimport unittest
7n/afrom test.support import requires
8n/afrom tkinter import Tk, Toplevel, Frame ##, BooleanVar, StringVar
9n/afrom idlelib import searchengine as se
10n/afrom idlelib import searchbase as sdb
11n/afrom idlelib.idle_test.mock_idle import Func
12n/a## from idlelib.idle_test.mock_tk import Var
13n/a
14n/a# The ## imports above & following could help make some tests gui-free.
15n/a# However, they currently make radiobutton tests fail.
16n/a##def setUpModule():
17n/a## # Replace tk objects used to initialize se.SearchEngine.
18n/a## se.BooleanVar = Var
19n/a## se.StringVar = Var
20n/a##
21n/a##def tearDownModule():
22n/a## se.BooleanVar = BooleanVar
23n/a## se.StringVar = StringVar
24n/a
25n/aclass SearchDialogBaseTest(unittest.TestCase):
26n/a
27n/a @classmethod
28n/a def setUpClass(cls):
29n/a requires('gui')
30n/a cls.root = Tk()
31n/a
32n/a @classmethod
33n/a def tearDownClass(cls):
34n/a cls.root.destroy()
35n/a del cls.root
36n/a
37n/a def setUp(self):
38n/a self.engine = se.SearchEngine(self.root) # None also seems to work
39n/a self.dialog = sdb.SearchDialogBase(root=self.root, engine=self.engine)
40n/a
41n/a def tearDown(self):
42n/a self.dialog.close()
43n/a
44n/a def test_open_and_close(self):
45n/a # open calls create_widgets, which needs default_command
46n/a self.dialog.default_command = None
47n/a
48n/a # Since text parameter of .open is not used in base class,
49n/a # pass dummy 'text' instead of tk.Text().
50n/a self.dialog.open('text')
51n/a self.assertEqual(self.dialog.top.state(), 'normal')
52n/a self.dialog.close()
53n/a self.assertEqual(self.dialog.top.state(), 'withdrawn')
54n/a
55n/a self.dialog.open('text', searchphrase="hello")
56n/a self.assertEqual(self.dialog.ent.get(), 'hello')
57n/a self.dialog.close()
58n/a
59n/a def test_create_widgets(self):
60n/a self.dialog.create_entries = Func()
61n/a self.dialog.create_option_buttons = Func()
62n/a self.dialog.create_other_buttons = Func()
63n/a self.dialog.create_command_buttons = Func()
64n/a
65n/a self.dialog.default_command = None
66n/a self.dialog.create_widgets()
67n/a
68n/a self.assertTrue(self.dialog.create_entries.called)
69n/a self.assertTrue(self.dialog.create_option_buttons.called)
70n/a self.assertTrue(self.dialog.create_other_buttons.called)
71n/a self.assertTrue(self.dialog.create_command_buttons.called)
72n/a
73n/a def test_make_entry(self):
74n/a equal = self.assertEqual
75n/a self.dialog.row = 0
76n/a self.dialog.top = self.root
77n/a entry, label = self.dialog.make_entry("Test:", 'hello')
78n/a equal(label['text'], 'Test:')
79n/a
80n/a self.assertIn(entry.get(), 'hello')
81n/a egi = entry.grid_info()
82n/a equal(int(egi['row']), 0)
83n/a equal(int(egi['column']), 1)
84n/a equal(int(egi['rowspan']), 1)
85n/a equal(int(egi['columnspan']), 1)
86n/a equal(self.dialog.row, 1)
87n/a
88n/a def test_create_entries(self):
89n/a self.dialog.top = self.root
90n/a self.dialog.row = 0
91n/a self.engine.setpat('hello')
92n/a self.dialog.create_entries()
93n/a self.assertIn(self.dialog.ent.get(), 'hello')
94n/a
95n/a def test_make_frame(self):
96n/a self.dialog.row = 0
97n/a self.dialog.top = self.root
98n/a frame, label = self.dialog.make_frame()
99n/a self.assertEqual(label, '')
100n/a self.assertIsInstance(frame, Frame)
101n/a
102n/a frame, label = self.dialog.make_frame('testlabel')
103n/a self.assertEqual(label['text'], 'testlabel')
104n/a self.assertIsInstance(frame, Frame)
105n/a
106n/a def btn_test_setup(self, meth):
107n/a self.dialog.top = self.root
108n/a self.dialog.row = 0
109n/a return meth()
110n/a
111n/a def test_create_option_buttons(self):
112n/a e = self.engine
113n/a for state in (0, 1):
114n/a for var in (e.revar, e.casevar, e.wordvar, e.wrapvar):
115n/a var.set(state)
116n/a frame, options = self.btn_test_setup(
117n/a self.dialog.create_option_buttons)
118n/a for spec, button in zip (options, frame.pack_slaves()):
119n/a var, label = spec
120n/a self.assertEqual(button['text'], label)
121n/a self.assertEqual(var.get(), state)
122n/a
123n/a def test_create_other_buttons(self):
124n/a for state in (False, True):
125n/a var = self.engine.backvar
126n/a var.set(state)
127n/a frame, others = self.btn_test_setup(
128n/a self.dialog.create_other_buttons)
129n/a buttons = frame.pack_slaves()
130n/a for spec, button in zip(others, buttons):
131n/a val, label = spec
132n/a self.assertEqual(button['text'], label)
133n/a if val == state:
134n/a # hit other button, then this one
135n/a # indexes depend on button order
136n/a self.assertEqual(var.get(), state)
137n/a
138n/a def test_make_button(self):
139n/a self.dialog.top = self.root
140n/a self.dialog.buttonframe = Frame(self.dialog.top)
141n/a btn = self.dialog.make_button('Test', self.dialog.close)
142n/a self.assertEqual(btn['text'], 'Test')
143n/a
144n/a def test_create_command_buttons(self):
145n/a self.dialog.top = self.root
146n/a self.dialog.create_command_buttons()
147n/a # Look for close button command in buttonframe
148n/a closebuttoncommand = ''
149n/a for child in self.dialog.buttonframe.winfo_children():
150n/a if child['text'] == 'close':
151n/a closebuttoncommand = child['command']
152n/a self.assertIn('close', closebuttoncommand)
153n/a
154n/a
155n/aif __name__ == '__main__':
156n/a unittest.main(verbosity=2, exit=2)