Home | History | Annotate | Download | only in idle_test
      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.test_support import requires
      9 from Tkinter import Tk, Toplevel, Frame ## 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
     14 
     15 # The ## imports above & following could help make some tests gui-free.# 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 class SearchDialogBaseTest(unittest.TestCase):
     26 
     27     @classmethod
     28     def setUpClass(cls):
     29         requires('gui')
     30         cls.root = Tk()
     31 
     32     @classmethod
     33     def tearDownClass(cls):
     34         cls.root.destroy()
     35         del cls.root
     36 
     37     def setUp(self):
     38         self.engine = se.SearchEngine(self.root)  # None also seems to work
     39         self.dialog = sdb.SearchDialogBase(root=self.root, engine=self.engine)
     40 
     41     def tearDown(self):
     42         self.dialog.close()
     43 
     44     def test_open_and_close(self):
     45         # open calls create_widgets, which needs default_command
     46         self.dialog.default_command = None
     47 
     48         # Since text parameter of .open is not used in base class,
     49         # pass dummy 'text' instead of tk.Text().
     50         self.dialog.open('text')
     51         self.assertEqual(self.dialog.top.state(), 'normal')
     52         self.dialog.close()
     53         self.assertEqual(self.dialog.top.state(), 'withdrawn')
     54 
     55         self.dialog.open('text', searchphrase="hello")
     56         self.assertEqual(self.dialog.ent.get(), 'hello')
     57         self.dialog.close()
     58 
     59     def test_create_widgets(self):
     60         self.dialog.create_entries = Func()
     61         self.dialog.create_option_buttons = Func()
     62         self.dialog.create_other_buttons = Func()
     63         self.dialog.create_command_buttons = Func()
     64 
     65         self.dialog.default_command = None
     66         self.dialog.create_widgets()
     67 
     68         self.assertTrue(self.dialog.create_entries.called)
     69         self.assertTrue(self.dialog.create_option_buttons.called)
     70         self.assertTrue(self.dialog.create_other_buttons.called)
     71         self.assertTrue(self.dialog.create_command_buttons.called)
     72 
     73     def test_make_entry(self):
     74         equal = self.assertEqual
     75         self.dialog.row = 0
     76         self.dialog.top = Toplevel(self.root)
     77         entry, label = self.dialog.make_entry("Test:", 'hello')
     78         equal(label['text'], 'Test:')
     79 
     80         self.assertIn(entry.get(), 'hello')
     81         egi = entry.grid_info()
     82         equal(int(egi['row']), 0)
     83         equal(int(egi['column']), 1)
     84         equal(int(egi['rowspan']), 1)
     85         equal(int(egi['columnspan']), 1)
     86         equal(self.dialog.row, 1)
     87 
     88     def test_create_entries(self):
     89         self.dialog.row = 0
     90         self.engine.setpat('hello')
     91         self.dialog.create_entries()
     92         self.assertIn(self.dialog.ent.get(), 'hello')
     93 
     94     def test_make_frame(self):
     95         self.dialog.row = 0
     96         self.dialog.top = Toplevel(self.root)
     97         frame, label = self.dialog.make_frame()
     98         self.assertEqual(label, '')
     99         self.assertIsInstance(frame, Frame)
    100 
    101         frame, label = self.dialog.make_frame('testlabel')
    102         self.assertEqual(label['text'], 'testlabel')
    103         self.assertIsInstance(frame, Frame)
    104 
    105     def btn_test_setup(self, meth):
    106         self.dialog.top = Toplevel(self.root)
    107         self.dialog.row = 0
    108         return meth()
    109 
    110     def test_create_option_buttons(self):
    111         e = self.engine
    112         for state in (0, 1):
    113             for var in (e.revar, e.casevar, e.wordvar, e.wrapvar):
    114                 var.set(state)
    115             frame, options = self.btn_test_setup(
    116                     self.dialog.create_option_buttons)
    117             for spec, button in zip (options, frame.pack_slaves()):
    118                 var, label = spec
    119                 self.assertEqual(button['text'], label)
    120                 self.assertEqual(var.get(), state)
    121                 if state == 1:
    122                     button.deselect()
    123                 else:
    124                     button.select()
    125                 self.assertEqual(var.get(), 1 - 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                     buttons[val].select()
    142                     self.assertEqual(var.get(), 1 - state)
    143                     buttons[1-val].select()
    144                     self.assertEqual(var.get(), state)
    145 
    146     def test_make_button(self):
    147         self.dialog.top = Toplevel(self.root)
    148         self.dialog.buttonframe = Frame(self.dialog.top)
    149         btn = self.dialog.make_button('Test', self.dialog.close)
    150         self.assertEqual(btn['text'], 'Test')
    151 
    152     def test_create_command_buttons(self):
    153         self.dialog.create_command_buttons()
    154         # Look for close button command in buttonframe
    155         closebuttoncommand = ''
    156         for child in self.dialog.buttonframe.winfo_children():
    157             if child['text'] == 'close':
    158                 closebuttoncommand = child['command']
    159         self.assertIn('close', closebuttoncommand)
    160 
    161 
    162 
    163 if __name__ == '__main__':
    164     unittest.main(verbosity=2, exit=2)
    165