diff -r 6bee7f1061e0 Lib/idlelib/SearchDialogBase.py --- a/Lib/idlelib/SearchDialogBase.py Sat Aug 10 20:01:43 2013 +0300 +++ b/Lib/idlelib/SearchDialogBase.py Sat Aug 10 17:33:49 2013 -0400 @@ -1,3 +1,5 @@ +# Provides base for GrepDialog, SearchDialog, and ReplaceDialog + from tkinter import * class SearchDialogBase: @@ -138,3 +140,6 @@ b = self.make_button("close", self.close) b.lower() + +if __name__ == '__main__': + unittest.main(verbosity=2, exit=False) diff -r 6bee7f1061e0 Lib/idlelib/idle_test/test_searchdialogbase.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/idlelib/idle_test/test_searchdialogbase.py Sat Aug 10 17:33:49 2013 -0400 @@ -0,0 +1,162 @@ +import unittest +from test.support import requires +import idlelib.SearchDialogBase as sdb +import idlelib.SearchEngine as se +from tkinter import * + +class SearchDialogBaseTest(unittest.TestCase): + + @classmethod + def setUpClass(cls): + requires('gui') + cls.root = Tk() + + @classmethod + def tearDownClass(cls): + cls.root.destroy() + + def setUp(self): + self.text = Text() + self.searchengine = se.SearchEngine(root=self.root) + self.searchdialog = sdb.SearchDialogBase(root=self.root, engine=self.searchengine) + + def tearDown(self): + self.searchdialog.close() + + def test_open(self): + self.searchdialog.default_command = None + + self.searchdialog.open(self.text) + self.assertEqual(self.searchdialog.top.state(), 'normal') + self.searchdialog.close() + + self.searchdialog.open(text=self.text, searchphrase="hello") + self.assertEqual(self.searchdialog.ent.get(), 'hello') + self.searchdialog.close() + + def test_close(self): + self.searchdialog.default_command = None + self.searchdialog.open(self.text) + self.searchdialog.close() + self.assertEqual(self.searchdialog.top.state(), 'withdrawn') + + def test_create_widgets(self): + self.searchdialog.default_command = None + self.searchdialog.create_widgets() + + # create_widgets mainly calls functions that are already tested here, + # so I have duplicated the tests here for now + + # Test create_entries + self.searchengine.setpat('hello') + self.assertIn(self.searchdialog.ent.get(), 'hello') + + # Test create_option_buttons + self.checkboxtests() + self.radiobuttontests() + + # Test create_command_buttons + closebuttoncommand = '' + for child in self.searchdialog.buttonframe.winfo_children(): + if child.config()['text'][-1] == 'close': + closebuttoncommand = child.config()['command'][-1] + self.assertIn('close', closebuttoncommand) + + def test_make_entry(self): + self.searchdialog.row = 0 + entry = self.searchdialog.make_entry("Test Label:", 'hello') + # Should probably test label name as well + self.assertIn(entry.get(), 'hello') + self.assertEqual(entry.grid_info()['row'], '0') + self.assertEqual(entry.grid_info()['column'], '1') + self.assertEqual(entry.grid_info()['rowspan'], '1') + self.assertEqual(entry.grid_info()['columnspan'], '1') + self.assertEqual(self.searchdialog.row, 1) + + def test_make_frame(self): + self.searchdialog.row = 0 + self.searchdialog.top = Toplevel(self.root) + frame = self.searchdialog.make_frame() + self.assertIsInstance(frame, Frame) + + labelledframe = self.searchdialog.make_frame('test label') + self.assertIsInstance(labelledframe, Frame) + # Need to test for existence of label + + def test_make_button(self): + self.searchdialog.top = Toplevel(self.root) + self.searchdialog.buttonframe = Frame(self.searchdialog.top) + btn = self.searchdialog.make_button('Test', self.searchdialog.close) + self.assertEqual(btn.config()['text'][-1], 'Test') + + def test_create_entries(self): + self.searchdialog.row = 0 + self.searchengine.setpat('hello') + self.searchdialog.create_entries() + self.assertIn(self.searchdialog.ent.get(), 'hello') + + def test_create_option_buttons(self): + self.searchdialog.row = 0 + self.searchdialog.top = Toplevel(self.root) + self.searchdialog.create_option_buttons() + self.checkboxtests() + + def test_create_other_buttons(self): + self.searchdialog.row = 0 + self.searchdialog.top = Toplevel(self.root) + self.searchdialog.create_other_buttons() + self.radiobuttontests() + + def test_create_command_buttons(self): + self.searchdialog.create_command_buttons() + # Look for close button command in buttonframe + closebuttoncommand = '' + for child in self.searchdialog.buttonframe.winfo_children(): + if child.config()['text'][-1] == 'close': + closebuttoncommand = child.config()['command'][-1] + self.assertIn('close', closebuttoncommand) + + def checkboxtests(self): + """Tests the four checkboxes in the search dialog window.""" + engine = self.searchengine + for child in self.searchdialog.top.winfo_children(): + for grandchild in child.winfo_children(): + text = grandchild.config()['text'][-1] + if text == ('Regular', 'expression'): + self.btnstatetest(grandchild, engine.revar, 0) + elif text == ('Match', 'case'): + self.btnstatetest(grandchild, engine.casevar, 0) + elif text == ('Whole', 'word'): + self.btnstatetest(grandchild, engine.wordvar, 0) + elif text == ('Wrap', 'around'): + self.btnstatetest(grandchild, engine.wrapvar, 1) + + def btnstatetest(self, button, var, defaultstate): + self.assertEqual(var.get(), defaultstate) + if defaultstate == 1: + button.deselect() + else: + button.select() + self.assertEqual(var.get(), 1 - defaultstate) + + def radiobuttontests(self): + searchupbtn = None + searchdownbtn = None + + for child in self.searchdialog.top.winfo_children(): + for grandchild in child.children.values(): + text = grandchild.config()['text'][-1] + if text == 'Up': + searchupbtn = grandchild + elif text == 'Down': + searchdownbtn = grandchild + + # Defaults to searching downward + self.assertEqual(self.searchengine.backvar.get(), 0) + searchupbtn.select() + self.assertEqual(self.searchengine.backvar.get(), 1) + searchdownbtn.select() + + +if __name__ == '__main__': + unittest.main(verbosity=2, exit=2)