Issue #18592: Refactor 2 SearchDialogBase.create_(option/other)_buttons methods

to remove duplication and return info for tests.  Rewrite corresponding tests.
Test_create_option_buttons was not testing anything because of buggy
comparisons.  Use Python subscripting to get widget options.
This commit is contained in:
Terry Jan Reedy 2014-06-30 23:52:14 -04:00
parent 1530a82223
commit 6a0fe8db19
2 changed files with 70 additions and 124 deletions

View File

@ -105,65 +105,44 @@ class SearchDialogBase:
def make_frame(self,labeltext=None): def make_frame(self,labeltext=None):
"Return gridded labeled Frame for option or other buttons." "Return gridded labeled Frame for option or other buttons."
if labeltext: if labeltext:
l = Label(self.top, text=labeltext) label = Label(self.top, text=labeltext)
l.grid(row=self.row, column=0, sticky="nw") label.grid(row=self.row, column=0, sticky="nw")
else: else:
l = '' label = ''
f = Frame(self.top) frame = Frame(self.top)
f.grid(row=self.row, column=1, columnspan=1, sticky="nwe") frame.grid(row=self.row, column=1, columnspan=1, sticky="nwe")
self.row = self.row + 1 self.row = self.row + 1
return l, f return frame, label # label for test
def create_option_buttons(self): def create_option_buttons(self):
"Fill frame with Checkbuttons bound to SearchEngine booleanvars." "Fill frame with Checkbuttons bound to SearchEngine booleanvars."
f = self.make_frame("Options")[1] frame = self.make_frame("Options")[0]
engine = self.engine
btn = Checkbutton(f, anchor="w", options = [(engine.revar, "Regular expression"),
variable=self.engine.revar, (engine.casevar, "Match case"),
text="Regular expression") (engine.wordvar, "Whole word")]
btn.pack(side="left", fill="both")
if self.engine.isre():
btn.select()
btn = Checkbutton(f, anchor="w",
variable=self.engine.casevar,
text="Match case")
btn.pack(side="left", fill="both")
if self.engine.iscase():
btn.select()
btn = Checkbutton(f, anchor="w",
variable=self.engine.wordvar,
text="Whole word")
btn.pack(side="left", fill="both")
if self.engine.isword():
btn.select()
if self.needwrapbutton: if self.needwrapbutton:
btn = Checkbutton(f, anchor="w", options.append((engine.wrapvar, "Wrap around"))
variable=self.engine.wrapvar, for var, label in options:
text="Wrap around") btn = Checkbutton(frame, anchor="w", variable=var, text=label)
btn.pack(side="left", fill="both") btn.pack(side="left", fill="both")
if self.engine.iswrap(): if var.get():
btn.select() btn.select()
return frame, options # for test
def create_other_buttons(self): def create_other_buttons(self):
"Fill frame with buttons tied to other options." "Fill frame with buttons tied to other options."
f = self.make_frame("Direction")[1] frame = self.make_frame("Direction")[0]
var = self.engine.backvar
btn = Radiobutton(f, anchor="w", others = [(1, 'Up'), (0, 'Down')]
variable=self.engine.backvar, value=1, for val, label in others:
text="Up") btn = Radiobutton(frame, anchor="w",
variable=var, value=val, text=label)
btn.pack(side="left", fill="both") btn.pack(side="left", fill="both")
if self.engine.isback(): #print(var.get(), val, label)
btn.select() if var.get() == val:
btn = Radiobutton(f, anchor="w",
variable=self.engine.backvar, value=0,
text="Down")
btn.pack(side="left", fill="both")
if not self.engine.isback():
btn.select() btn.select()
return frame, others # for test
def make_button(self, label, command, isdef=0): def make_button(self, label, command, isdef=0):
"Return command button gridded in command frame." "Return command button gridded in command frame."

View File

@ -76,7 +76,7 @@ class SearchDialogBaseTest(unittest.TestCase):
self.dialog.row = 0 self.dialog.row = 0
self.dialog.top = Toplevel(self.root) self.dialog.top = Toplevel(self.root)
label, entry = self.dialog.make_entry("Test:", 'hello') label, entry = self.dialog.make_entry("Test:", 'hello')
equal(label.cget('text'), 'Test:') equal(label['text'], 'Test:')
self.assertIn(entry.get(), 'hello') self.assertIn(entry.get(), 'hello')
egi = entry.grid_info() egi = entry.grid_info()
@ -95,101 +95,68 @@ class SearchDialogBaseTest(unittest.TestCase):
def test_make_frame(self): def test_make_frame(self):
self.dialog.row = 0 self.dialog.row = 0
self.dialog.top = Toplevel(self.root) self.dialog.top = Toplevel(self.root)
label, frame = self.dialog.make_frame() frame, label = self.dialog.make_frame()
self.assertEqual(label, '') self.assertEqual(label, '')
self.assertIsInstance(frame, Frame) self.assertIsInstance(frame, Frame)
label, labelledframe = self.dialog.make_frame('testlabel') frame, label = self.dialog.make_frame('testlabel')
self.assertEqual(label.cget('text'), 'testlabel') self.assertEqual(label['text'], 'testlabel')
self.assertIsInstance(labelledframe, Frame) self.assertIsInstance(frame, Frame)
def btn_test_setup(self, which): def btn_test_setup(self, meth):
self.dialog.row = 0
self.dialog.top = Toplevel(self.root) self.dialog.top = Toplevel(self.root)
if which == 'option': self.dialog.row = 0
self.dialog.create_option_buttons() return meth()
elif which == 'other':
self.dialog.create_other_buttons()
else:
raise ValueError('bad which arg %s' % which)
def test_create_option_buttons(self): def test_create_option_buttons(self):
self.btn_test_setup('option') e = self.engine
self.checkboxtests() for state in (0, 1):
for var in (e.revar, e.casevar, e.wordvar, e.wrapvar):
def test_create_option_buttons_flipped(self): var.set(state)
for var in ('revar', 'casevar', 'wordvar', 'wrapvar'): frame, options = self.btn_test_setup(
Var = getattr(self.engine, var) self.dialog.create_option_buttons)
Var.set(not Var.get()) for spec, button in zip (options, frame.pack_slaves()):
self.btn_test_setup('option') var, label = spec
self.checkboxtests(flip=1) self.assertEqual(button['text'], label)
self.assertEqual(var.get(), state)
def checkboxtests(self, flip=0): if state == 1:
"""Tests the four checkboxes in the search dialog window."""
engine = self.engine
for child in self.dialog.top.winfo_children():
for grandchild in child.winfo_children():
text = grandchild.config()['text'][-1]
if text == ('Regular', 'expression'):
self.btnstatetest(grandchild, engine.revar, flip)
elif text == ('Match', 'case'):
self.btnstatetest(grandchild, engine.casevar, flip)
elif text == ('Whole', 'word'):
self.btnstatetest(grandchild, engine.wordvar, flip)
elif text == ('Wrap', 'around'):
self.btnstatetest(grandchild, engine.wrapvar, not flip)
def btnstatetest(self, button, var, defaultstate):
self.assertEqual(var.get(), defaultstate)
if defaultstate == 1:
button.deselect() button.deselect()
else: else:
button.select() button.select()
self.assertEqual(var.get(), 1 - defaultstate) self.assertEqual(var.get(), 1 - state)
def test_create_other_buttons(self): def test_create_other_buttons(self):
self.btn_test_setup('other') for state in (False, True):
self.radiobuttontests() var = self.engine.backvar
var.set(state)
def test_create_other_buttons_flipped(self): frame, others = self.btn_test_setup(
self.engine.backvar.set(1) self.dialog.create_other_buttons)
self.btn_test_setup('other') buttons = frame.pack_slaves()
self.radiobuttontests(back=1) for spec, button in zip(others, buttons):
val, label = spec
def radiobuttontests(self, back=0): self.assertEqual(button['text'], label)
searchupbtn = None if val == state:
searchdownbtn = None # hit other button, then this one
# indexes depend on button order
for child in self.dialog.top.winfo_children(): self.assertEqual(var.get(), state)
for grandchild in child.children.values(): buttons[val].select()
text = grandchild.config()['text'][-1] self.assertEqual(var.get(), 1 - state)
if text == 'Up': buttons[1-val].select()
searchupbtn = grandchild self.assertEqual(var.get(), state)
elif text == 'Down':
searchdownbtn = grandchild
# Defaults to searching downward
self.assertEqual(self.engine.backvar.get(), back)
if back:
searchdownbtn.select()
else:
searchupbtn.select()
self.assertEqual(self.engine.backvar.get(), not back)
searchdownbtn.select()
def test_make_button(self): def test_make_button(self):
self.dialog.top = Toplevel(self.root) self.dialog.top = Toplevel(self.root)
self.dialog.buttonframe = Frame(self.dialog.top) self.dialog.buttonframe = Frame(self.dialog.top)
btn = self.dialog.make_button('Test', self.dialog.close) btn = self.dialog.make_button('Test', self.dialog.close)
self.assertEqual(btn.cget('text'), 'Test') self.assertEqual(btn['text'], 'Test')
def test_create_command_buttons(self): def test_create_command_buttons(self):
self.dialog.create_command_buttons() self.dialog.create_command_buttons()
# Look for close button command in buttonframe # Look for close button command in buttonframe
closebuttoncommand = '' closebuttoncommand = ''
for child in self.dialog.buttonframe.winfo_children(): for child in self.dialog.buttonframe.winfo_children():
if child.config()['text'][-1] == 'close': if child['text'] == 'close':
closebuttoncommand = child.config()['command'][-1] closebuttoncommand = child['command']
self.assertIn('close', closebuttoncommand) self.assertIn('close', closebuttoncommand)