|
| 1 | +'''Unittests for idlelib/SearchDialogBase.py |
| 2 | +
|
| 3 | +Coverage: 99%. The only thing not covered is inconsequential -- |
| 4 | +testing skipping of suite when self.needwrapbutton is false. |
| 5 | +
|
| 6 | +''' |
| 7 | +import unittest |
| 8 | +from test.support import requires |
| 9 | +from tkinter import Tk, Toplevel, Frame, Label, BooleanVar, StringVar |
| 10 | +from idlelib import SearchEngine as se |
| 11 | +from idlelib import SearchDialogBase as sdb |
| 12 | +from idlelib.idle_test.mock_idle import Func |
| 13 | +from idlelib.idle_test.mock_tk import Var, Mbox |
| 14 | + |
| 15 | +# The following could help make some tests gui-free. |
| 16 | +# However, they currently make radiobutton tests fail. |
| 17 | +##def setUpModule(): |
| 18 | +## # Replace tk objects used to initialize se.SearchEngine. |
| 19 | +## se.BooleanVar = Var |
| 20 | +## se.StringVar = Var |
| 21 | +## |
| 22 | +##def tearDownModule(): |
| 23 | +## se.BooleanVar = BooleanVar |
| 24 | +## se.StringVar = StringVar |
| 25 | + |
| 26 | +class SearchDialogBaseTest(unittest.TestCase): |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def setUpClass(cls): |
| 30 | + requires('gui') |
| 31 | + cls.root = Tk() |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def tearDownClass(cls): |
| 35 | + cls.root.destroy() |
| 36 | + del cls.root |
| 37 | + |
| 38 | + def setUp(self): |
| 39 | + self.engine = se.SearchEngine(self.root) # None also seems to work |
| 40 | + self.dialog = sdb.SearchDialogBase(root=self.root, engine=self.engine) |
| 41 | + |
| 42 | + def tearDown(self): |
| 43 | + self.dialog.close() |
| 44 | + |
| 45 | + def test_open_and_close(self): |
| 46 | + # open calls create_widgets, which needs default_command |
| 47 | + self.dialog.default_command = None |
| 48 | + |
| 49 | + # Since text parameter of .open is not used in base class, |
| 50 | + # pass dummy 'text' instead of tk.Text(). |
| 51 | + self.dialog.open('text') |
| 52 | + self.assertEqual(self.dialog.top.state(), 'normal') |
| 53 | + self.dialog.close() |
| 54 | + self.assertEqual(self.dialog.top.state(), 'withdrawn') |
| 55 | + |
| 56 | + self.dialog.open('text', searchphrase="hello") |
| 57 | + self.assertEqual(self.dialog.ent.get(), 'hello') |
| 58 | + self.dialog.close() |
| 59 | + |
| 60 | + def test_create_widgets(self): |
| 61 | + self.dialog.create_entries = Func() |
| 62 | + self.dialog.create_option_buttons = Func() |
| 63 | + self.dialog.create_other_buttons = Func() |
| 64 | + self.dialog.create_command_buttons = Func() |
| 65 | + |
| 66 | + self.dialog.default_command = None |
| 67 | + self.dialog.create_widgets() |
| 68 | + |
| 69 | + self.assertTrue(self.dialog.create_entries.called) |
| 70 | + self.assertTrue(self.dialog.create_option_buttons.called) |
| 71 | + self.assertTrue(self.dialog.create_other_buttons.called) |
| 72 | + self.assertTrue(self.dialog.create_command_buttons.called) |
| 73 | + |
| 74 | + def test_make_entry(self): |
| 75 | + equal = self.assertEqual |
| 76 | + self.dialog.row = 0 |
| 77 | + self.dialog.top = Toplevel(self.root) |
| 78 | + label, entry = self.dialog.make_entry("Test:", 'hello') |
| 79 | + equal(label.cget('text'), 'Test:') |
| 80 | + |
| 81 | + self.assertIn(entry.get(), 'hello') |
| 82 | + egi = entry.grid_info() |
| 83 | + equal(egi['row'], 0) |
| 84 | + equal(egi['column'], 1) |
| 85 | + equal(egi['rowspan'], 1) |
| 86 | + equal(egi['columnspan'], 1) |
| 87 | + equal(self.dialog.row, 1) |
| 88 | + |
| 89 | + def test_create_entries(self): |
| 90 | + self.dialog.row = 0 |
| 91 | + self.engine.setpat('hello') |
| 92 | + self.dialog.create_entries() |
| 93 | + self.assertIn(self.dialog.ent.get(), 'hello') |
| 94 | + |
| 95 | + def test_make_frame(self): |
| 96 | + self.dialog.row = 0 |
| 97 | + self.dialog.top = Toplevel(self.root) |
| 98 | + label, frame = self.dialog.make_frame() |
| 99 | + self.assertEqual(label, '') |
| 100 | + self.assertIsInstance(frame, Frame) |
| 101 | + |
| 102 | + label, labelledframe = self.dialog.make_frame('testlabel') |
| 103 | + self.assertEqual(label.cget('text'), 'testlabel') |
| 104 | + self.assertIsInstance(labelledframe, Frame) |
| 105 | + |
| 106 | + def btn_test_setup(self, which): |
| 107 | + self.dialog.row = 0 |
| 108 | + self.dialog.top = Toplevel(self.root) |
| 109 | + if which == 'option': |
| 110 | + self.dialog.create_option_buttons() |
| 111 | + elif which == 'other': |
| 112 | + self.dialog.create_other_buttons() |
| 113 | + else: |
| 114 | + raise ValueError('bad which arg %s' % which) |
| 115 | + |
| 116 | + def test_create_option_buttons(self): |
| 117 | + self.btn_test_setup('option') |
| 118 | + self.checkboxtests() |
| 119 | + |
| 120 | + def test_create_option_buttons_flipped(self): |
| 121 | + for var in ('revar', 'casevar', 'wordvar', 'wrapvar'): |
| 122 | + Var = getattr(self.engine, var) |
| 123 | + Var.set(not Var.get()) |
| 124 | + self.btn_test_setup('option') |
| 125 | + self.checkboxtests(flip=1) |
| 126 | + |
| 127 | + def checkboxtests(self, flip=0): |
| 128 | + """Tests the four checkboxes in the search dialog window.""" |
| 129 | + engine = self.engine |
| 130 | + for child in self.dialog.top.winfo_children(): |
| 131 | + for grandchild in child.winfo_children(): |
| 132 | + text = grandchild.config()['text'][-1] |
| 133 | + if text == ('Regular', 'expression'): |
| 134 | + self.btnstatetest(grandchild, engine.revar, flip) |
| 135 | + elif text == ('Match', 'case'): |
| 136 | + self.btnstatetest(grandchild, engine.casevar, flip) |
| 137 | + elif text == ('Whole', 'word'): |
| 138 | + self.btnstatetest(grandchild, engine.wordvar, flip) |
| 139 | + elif text == ('Wrap', 'around'): |
| 140 | + self.btnstatetest(grandchild, engine.wrapvar, not flip) |
| 141 | + |
| 142 | + def btnstatetest(self, button, var, defaultstate): |
| 143 | + self.assertEqual(var.get(), defaultstate) |
| 144 | + if defaultstate == 1: |
| 145 | + button.deselect() |
| 146 | + else: |
| 147 | + button.select() |
| 148 | + self.assertEqual(var.get(), 1 - defaultstate) |
| 149 | + |
| 150 | + def test_create_other_buttons(self): |
| 151 | + self.btn_test_setup('other') |
| 152 | + self.radiobuttontests() |
| 153 | + |
| 154 | + def test_create_other_buttons_flipped(self): |
| 155 | + self.engine.backvar.set(1) |
| 156 | + self.btn_test_setup('other') |
| 157 | + self.radiobuttontests(back=1) |
| 158 | + |
| 159 | + def radiobuttontests(self, back=0): |
| 160 | + searchupbtn = None |
| 161 | + searchdownbtn = None |
| 162 | + |
| 163 | + for child in self.dialog.top.winfo_children(): |
| 164 | + for grandchild in child.children.values(): |
| 165 | + text = grandchild.config()['text'][-1] |
| 166 | + if text == 'Up': |
| 167 | + searchupbtn = grandchild |
| 168 | + elif text == 'Down': |
| 169 | + searchdownbtn = grandchild |
| 170 | + |
| 171 | + # Defaults to searching downward |
| 172 | + self.assertEqual(self.engine.backvar.get(), back) |
| 173 | + if back: |
| 174 | + searchdownbtn.select() |
| 175 | + else: |
| 176 | + searchupbtn.select() |
| 177 | + self.assertEqual(self.engine.backvar.get(), not back) |
| 178 | + searchdownbtn.select() |
| 179 | + |
| 180 | + def test_make_button(self): |
| 181 | + self.dialog.top = Toplevel(self.root) |
| 182 | + self.dialog.buttonframe = Frame(self.dialog.top) |
| 183 | + btn = self.dialog.make_button('Test', self.dialog.close) |
| 184 | + self.assertEqual(btn.cget('text'), 'Test') |
| 185 | + |
| 186 | + def test_create_command_buttons(self): |
| 187 | + self.dialog.create_command_buttons() |
| 188 | + # Look for close button command in buttonframe |
| 189 | + closebuttoncommand = '' |
| 190 | + for child in self.dialog.buttonframe.winfo_children(): |
| 191 | + if child.config()['text'][-1] == 'close': |
| 192 | + closebuttoncommand = child.config()['command'][-1] |
| 193 | + self.assertIn('close', closebuttoncommand) |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +if __name__ == '__main__': |
| 198 | + unittest.main(verbosity=2, exit=2) |
0 commit comments