Home | History | Annotate | Download | only in idle_test
      1 '''tests idlelib.searchbase.
      2 
      3 Coverage: 99%. The only thing not covered is inconsequential --
      4 testing skipping of suite when self.needwrapbutton is false.
      5 '''
      6 import unittest
      7 from test.support import requires
      8 from tkinter import Tk, Toplevel, Frame ##, BooleanVar, StringVar
      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 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 = 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.top = self.root
     90         self.dialog.row = 0
     91         self.engine.setpat('hello')
     92         self.dialog.create_entries()
     93         self.assertIn(self.dialog.ent.get(), 'hello')
     94 
     95     def test_make_frame(self):
     96         self.dialog.row = 0
     97         self.dialog.top = self.root
     98         frame, label = self.dialog.make_frame()
     99         self.assertEqual(label, '')
    100         self.assertIsInstance(frame, Frame)
    101 
    102         frame, label = self.dialog.make_frame('testlabel')
    103         self.assertEqual(label['text'], 'testlabel')
    104         self.assertIsInstance(frame, Frame)
    105 
    106     def btn_test_setup(self, meth):
    107         self.dialog.top = self.root
    108         self.dialog.row = 0
    109         return meth()
    110 
    111     def test_create_option_buttons(self):
    112         e = self.engine
    113         for state in (0, 1):
    114             for var in (e.revar, e.casevar, e.wordvar, e.wrapvar):
    115                 var.set(state)
    116             frame, options = self.btn_test_setup(
    117                     self.dialog.create_option_buttons)
    118             for spec, button in zip (options, frame.pack_slaves()):
    119                 var, label = spec
    120                 self.assertEqual(button['text'], label)
    121                 self.assertEqual(var.get(), state)
    122 
    123     def test_create_other_buttons(self):
    124         for state in (False, True):
    125             var = self.engine.backvar
    126             var.set(state)
    127             frame, others = self.btn_test_setup(
    128                 self.dialog.create_other_buttons)
    129             buttons = frame.pack_slaves()
    130             for spec, button in zip(others, buttons):
    131                 val, label = spec
    132                 self.assertEqual(button['text'], label)
    133                 if val == state:
    134                     # hit other button, then this one
    135                     # indexes depend on button order
    136                     self.assertEqual(var.get(), state)
    137 
    138     def test_make_button(self):
    139         self.dialog.top = self.root
    140         self.dialog.buttonframe = Frame(self.dialog.top)
    141         btn = self.dialog.make_button('Test', self.dialog.close)
    142         self.assertEqual(btn['text'], 'Test')
    143 
    144     def test_create_command_buttons(self):
    145         self.dialog.top = self.root
    146         self.dialog.create_command_buttons()
    147         # Look for close button command in buttonframe
    148         closebuttoncommand = ''
    149         for child in self.dialog.buttonframe.winfo_children():
    150             if child['text'] == 'close':
    151                 closebuttoncommand = child['command']
    152         self.assertIn('close', closebuttoncommand)
    153 
    154 
    155 if __name__ == '__main__':
    156     unittest.main(verbosity=2, exit=2)
    157