cpython: a0e8f2d882a3 (original) (raw)
Mercurial > cpython
changeset 91491:a0e8f2d882a3 3.4
Issue #18592: Add unittests for SearchDialogBase. Patch by Phil Webster. [#18592]
Terry Jan Reedy tjreedy@udel.edu | |
---|---|
date | Mon, 30 Jun 2014 20:00:03 -0400 |
parents | fcfa9c5a00fd |
children | b3f4616b9a94 e0561df131aa |
files | Lib/idlelib/SearchDialogBase.py Lib/idlelib/idle_test/test_searchdialogbase.py |
diffstat | 2 files changed, 213 insertions(+), 10 deletions(-)[+] [-] Lib/idlelib/SearchDialogBase.py 25 Lib/idlelib/idle_test/test_searchdialogbase.py 198 |
line wrap: on
line diff
--- a/Lib/idlelib/SearchDialogBase.py +++ b/Lib/idlelib/SearchDialogBase.py @@ -16,10 +16,12 @@ class SearchDialogBase: (make_button, create_command_buttons). These are bound to functions that execute the command.
- Except for command buttons, this base class is not limited to
- items common to all three subclasses. Rather, it is the Find dialog
- minus the "Find Next" command and its execution function.
- The other dialogs override methods to replace and add widgets.
- Except for command buttons, this base class is not limited to items
- common to all three subclasses. Rather, it is the Find dialog minus
- the "Find Next" command, its execution function, and the
- default_command attribute needed in create_widgets. The other
- dialogs override attributes and methods, the latter to replace and
- add widgets. ''' title = "Search Dialog" # replace in subclasses @@ -30,9 +32,10 @@ class SearchDialogBase: '''Initialize root, engine, and top attributes.
top (level widget): set in create_widgets() called from open().
text (Text being searched): set in open(), only used in subclasses().[](#l1.24)
text (Text searched): set in open(), only used in subclasses().[](#l1.25) ent (ry): created in make_entry() called from create_entry().[](#l1.26) row (of grid): 0 in create_widgets(), +1 in make_entry/frame().[](#l1.27)
default_command: set in subclasses, used in create_widgers().[](#l1.28)
title (of dialog): class attribute, override in subclasses. icon (of dialog): ditto, use unclear if cannot minimize dialog. @@ -93,25 +96,27 @@ class SearchDialogBase: e = Entry(self.top, textvariable=var, exportselection=0) e.grid(row=self.row, column=1, sticky="nwe") self.row = self.row + 1
return e[](#l1.36)
return l, e # return label for testing[](#l1.37)
def create_entries(self): "Create one or more entry lines with make_entry."
self.ent = self.make_entry("Find:", self.engine.patvar)[](#l1.41)
self.ent = self.make_entry("Find:", self.engine.patvar)[1][](#l1.42)
def make_frame(self,labeltext=None): "Return gridded labeled Frame for option or other buttons." if labeltext: l = Label(self.top, text=labeltext) l.grid(row=self.row, column=0, sticky="nw")
else:[](#l1.49)
l = ''[](#l1.50) f = Frame(self.top)[](#l1.51) f.grid(row=self.row, column=1, columnspan=1, sticky="nwe")[](#l1.52) self.row = self.row + 1[](#l1.53)
return f[](#l1.54)
return l, f[](#l1.55)
def create_option_buttons(self): "Fill frame with Checkbuttons bound to SearchEngine booleanvars."
f = self.make_frame("Options")[](#l1.59)
f = self.make_frame("Options")[1][](#l1.60)
btn = Checkbutton(f, anchor="w", variable=self.engine.revar, @@ -144,7 +149,7 @@ class SearchDialogBase: def create_other_buttons(self): "Fill frame with buttons tied to other options."
f = self.make_frame("Direction")[](#l1.68)
f = self.make_frame("Direction")[1][](#l1.69)
btn = Radiobutton(f, anchor="w", variable=self.engine.backvar, value=1,
new file mode 100644 --- /dev/null +++ b/Lib/idlelib/idle_test/test_searchdialogbase.py @@ -0,0 +1,198 @@ +'''Unittests for idlelib/SearchDialogBase.py + +Coverage: 99%. The only thing not covered is inconsequential -- +testing skipping of suite when self.needwrapbutton is false. + +''' +import unittest +from test.support import requires +from tkinter import Tk, Toplevel, Frame, Label, BooleanVar, StringVar +from idlelib import SearchEngine as se +from idlelib import SearchDialogBase as sdb +from idlelib.idle_test.mock_idle import Func +from idlelib.idle_test.mock_tk import Var, Mbox + +# The following could help make some tests gui-free. +# However, they currently make radiobutton tests fail. +##def setUpModule(): +## # Replace tk objects used to initialize se.SearchEngine. +## se.BooleanVar = Var +## se.StringVar = Var +## +##def tearDownModule(): +## se.BooleanVar = BooleanVar +## se.StringVar = StringVar + +class SearchDialogBaseTest(unittest.TestCase): +
- def setUp(self):
self.engine = se.SearchEngine(self.root) # None also seems to work[](#l2.43)
self.dialog = sdb.SearchDialogBase(root=self.root, engine=self.engine)[](#l2.44)
- def test_open_and_close(self):
# open calls create_widgets, which needs default_command[](#l2.50)
self.dialog.default_command = None[](#l2.51)
# Since text parameter of .open is not used in base class,[](#l2.53)
# pass dummy 'text' instead of tk.Text().[](#l2.54)
self.dialog.open('text')[](#l2.55)
self.assertEqual(self.dialog.top.state(), 'normal')[](#l2.56)
self.dialog.close()[](#l2.57)
self.assertEqual(self.dialog.top.state(), 'withdrawn')[](#l2.58)
self.dialog.open('text', searchphrase="hello")[](#l2.60)
self.assertEqual(self.dialog.ent.get(), 'hello')[](#l2.61)
self.dialog.close()[](#l2.62)
- def test_create_widgets(self):
self.dialog.create_entries = Func()[](#l2.65)
self.dialog.create_option_buttons = Func()[](#l2.66)
self.dialog.create_other_buttons = Func()[](#l2.67)
self.dialog.create_command_buttons = Func()[](#l2.68)
self.dialog.default_command = None[](#l2.70)
self.dialog.create_widgets()[](#l2.71)
self.assertTrue(self.dialog.create_entries.called)[](#l2.73)
self.assertTrue(self.dialog.create_option_buttons.called)[](#l2.74)
self.assertTrue(self.dialog.create_other_buttons.called)[](#l2.75)
self.assertTrue(self.dialog.create_command_buttons.called)[](#l2.76)
- def test_make_entry(self):
equal = self.assertEqual[](#l2.79)
self.dialog.row = 0[](#l2.80)
self.dialog.top = Toplevel(self.root)[](#l2.81)
label, entry = self.dialog.make_entry("Test:", 'hello')[](#l2.82)
equal(label.cget('text'), 'Test:')[](#l2.83)
self.assertIn(entry.get(), 'hello')[](#l2.85)
egi = entry.grid_info()[](#l2.86)
equal(egi['row'], 0)[](#l2.87)
equal(egi['column'], 1)[](#l2.88)
equal(egi['rowspan'], 1)[](#l2.89)
equal(egi['columnspan'], 1)[](#l2.90)
equal(self.dialog.row, 1)[](#l2.91)
- def test_create_entries(self):
self.dialog.row = 0[](#l2.94)
self.engine.setpat('hello')[](#l2.95)
self.dialog.create_entries()[](#l2.96)
self.assertIn(self.dialog.ent.get(), 'hello')[](#l2.97)
- def test_make_frame(self):
self.dialog.row = 0[](#l2.100)
self.dialog.top = Toplevel(self.root)[](#l2.101)
label, frame = self.dialog.make_frame()[](#l2.102)
self.assertEqual(label, '')[](#l2.103)
self.assertIsInstance(frame, Frame)[](#l2.104)
label, labelledframe = self.dialog.make_frame('testlabel')[](#l2.106)
self.assertEqual(label.cget('text'), 'testlabel')[](#l2.107)
self.assertIsInstance(labelledframe, Frame)[](#l2.108)
- def btn_test_setup(self, which):
self.dialog.row = 0[](#l2.111)
self.dialog.top = Toplevel(self.root)[](#l2.112)
if which == 'option':[](#l2.113)
self.dialog.create_option_buttons()[](#l2.114)
elif which == 'other':[](#l2.115)
self.dialog.create_other_buttons()[](#l2.116)
else:[](#l2.117)
raise ValueError('bad which arg %s' % which)[](#l2.118)
- def test_create_option_buttons(self):
self.btn_test_setup('option')[](#l2.121)
self.checkboxtests()[](#l2.122)
- def test_create_option_buttons_flipped(self):
for var in ('revar', 'casevar', 'wordvar', 'wrapvar'):[](#l2.125)
Var = getattr(self.engine, var)[](#l2.126)
Var.set(not Var.get())[](#l2.127)
self.btn_test_setup('option')[](#l2.128)
self.checkboxtests(flip=1)[](#l2.129)
- def checkboxtests(self, flip=0):
"""Tests the four checkboxes in the search dialog window."""[](#l2.132)
engine = self.engine[](#l2.133)
for child in self.dialog.top.winfo_children():[](#l2.134)
for grandchild in child.winfo_children():[](#l2.135)
text = grandchild.config()['text'][-1][](#l2.136)
if text == ('Regular', 'expression'):[](#l2.137)
self.btnstatetest(grandchild, engine.revar, flip)[](#l2.138)
elif text == ('Match', 'case'):[](#l2.139)
self.btnstatetest(grandchild, engine.casevar, flip)[](#l2.140)
elif text == ('Whole', 'word'):[](#l2.141)
self.btnstatetest(grandchild, engine.wordvar, flip)[](#l2.142)
elif text == ('Wrap', 'around'):[](#l2.143)
self.btnstatetest(grandchild, engine.wrapvar, not flip)[](#l2.144)
- def btnstatetest(self, button, var, defaultstate):
self.assertEqual(var.get(), defaultstate)[](#l2.147)
if defaultstate == 1:[](#l2.148)
button.deselect()[](#l2.149)
else:[](#l2.150)
button.select()[](#l2.151)
self.assertEqual(var.get(), 1 - defaultstate)[](#l2.152)
- def test_create_other_buttons(self):
self.btn_test_setup('other')[](#l2.155)
self.radiobuttontests()[](#l2.156)
- def test_create_other_buttons_flipped(self):
self.engine.backvar.set(1)[](#l2.159)
self.btn_test_setup('other')[](#l2.160)
self.radiobuttontests(back=1)[](#l2.161)
for child in self.dialog.top.winfo_children():[](#l2.167)
for grandchild in child.children.values():[](#l2.168)
text = grandchild.config()['text'][-1][](#l2.169)
if text == 'Up':[](#l2.170)
searchupbtn = grandchild[](#l2.171)
elif text == 'Down':[](#l2.172)
searchdownbtn = grandchild[](#l2.173)
# Defaults to searching downward[](#l2.175)
self.assertEqual(self.engine.backvar.get(), back)[](#l2.176)
if back:[](#l2.177)
searchdownbtn.select()[](#l2.178)
else:[](#l2.179)
searchupbtn.select()[](#l2.180)
self.assertEqual(self.engine.backvar.get(), not back)[](#l2.181)
searchdownbtn.select()[](#l2.182)
- def test_make_button(self):
self.dialog.top = Toplevel(self.root)[](#l2.185)
self.dialog.buttonframe = Frame(self.dialog.top)[](#l2.186)
btn = self.dialog.make_button('Test', self.dialog.close)[](#l2.187)
self.assertEqual(btn.cget('text'), 'Test')[](#l2.188)
- def test_create_command_buttons(self):
self.dialog.create_command_buttons()[](#l2.191)
# Look for close button command in buttonframe[](#l2.192)
closebuttoncommand = ''[](#l2.193)
for child in self.dialog.buttonframe.winfo_children():[](#l2.194)
if child.config()['text'][-1] == 'close':[](#l2.195)
closebuttoncommand = child.config()['command'][-1][](#l2.196)
self.assertIn('close', closebuttoncommand)[](#l2.197)