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