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