• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"Test searchbase, coverage 98%."
2# The only thing not covered is inconsequential --
3# testing skipping of suite when self.needwrapbutton is false.
4
5import unittest
6from test.support import requires
7from tkinter import Tk
8from tkinter.ttk import Frame
9from idlelib import searchengine as se
10from idlelib import searchbase as sdb
11from 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
26class SearchDialogBaseTest(unittest.TestCase):
27
28    @classmethod
29    def setUpClass(cls):
30        requires('gui')
31        cls.root = Tk()
32
33    @classmethod
34    def tearDownClass(cls):
35        cls.root.destroy()
36        del cls.root
37
38    def setUp(self):
39        self.engine = se.SearchEngine(self.root)  # None also seems to work
40        self.dialog = sdb.SearchDialogBase(root=self.root, engine=self.engine)
41
42    def tearDown(self):
43        self.dialog.close()
44
45    def test_open_and_close(self):
46        # open calls create_widgets, which needs default_command
47        self.dialog.default_command = None
48
49        # Since text parameter of .open is not used in base class,
50        # pass dummy 'text' instead of tk.Text().
51        self.dialog.open('text')
52        self.assertEqual(self.dialog.top.state(), 'normal')
53        self.dialog.close()
54        self.assertEqual(self.dialog.top.state(), 'withdrawn')
55
56        self.dialog.open('text', searchphrase="hello")
57        self.assertEqual(self.dialog.ent.get(), 'hello')
58        self.dialog.close()
59
60    def test_create_widgets(self):
61        self.dialog.create_entries = Func()
62        self.dialog.create_option_buttons = Func()
63        self.dialog.create_other_buttons = Func()
64        self.dialog.create_command_buttons = Func()
65
66        self.dialog.default_command = None
67        self.dialog.create_widgets()
68
69        self.assertTrue(self.dialog.create_entries.called)
70        self.assertTrue(self.dialog.create_option_buttons.called)
71        self.assertTrue(self.dialog.create_other_buttons.called)
72        self.assertTrue(self.dialog.create_command_buttons.called)
73
74    def test_make_entry(self):
75        equal = self.assertEqual
76        self.dialog.row = 0
77        self.dialog.top = self.root
78        entry, label = self.dialog.make_entry("Test:", 'hello')
79        equal(label['text'], 'Test:')
80
81        self.assertIn(entry.get(), 'hello')
82        egi = entry.grid_info()
83        equal(int(egi['row']), 0)
84        equal(int(egi['column']), 1)
85        equal(int(egi['rowspan']), 1)
86        equal(int(egi['columnspan']), 1)
87        equal(self.dialog.row, 1)
88
89    def test_create_entries(self):
90        self.dialog.top = self.root
91        self.dialog.row = 0
92        self.engine.setpat('hello')
93        self.dialog.create_entries()
94        self.assertIn(self.dialog.ent.get(), 'hello')
95
96    def test_make_frame(self):
97        self.dialog.row = 0
98        self.dialog.top = self.root
99        frame, label = self.dialog.make_frame()
100        self.assertEqual(label, '')
101        self.assertEqual(str(type(frame)), "<class 'tkinter.ttk.Frame'>")
102        # self.assertIsInstance(frame, Frame) fails when test is run by
103        # test_idle not run from IDLE editor.  See issue 33987 PR.
104
105        frame, label = self.dialog.make_frame('testlabel')
106        self.assertEqual(label['text'], 'testlabel')
107
108    def btn_test_setup(self, meth):
109        self.dialog.top = self.root
110        self.dialog.row = 0
111        return meth()
112
113    def test_create_option_buttons(self):
114        e = self.engine
115        for state in (0, 1):
116            for var in (e.revar, e.casevar, e.wordvar, e.wrapvar):
117                var.set(state)
118            frame, options = self.btn_test_setup(
119                    self.dialog.create_option_buttons)
120            for spec, button in zip (options, frame.pack_slaves()):
121                var, label = spec
122                self.assertEqual(button['text'], label)
123                self.assertEqual(var.get(), state)
124
125    def test_create_other_buttons(self):
126        for state in (False, True):
127            var = self.engine.backvar
128            var.set(state)
129            frame, others = self.btn_test_setup(
130                self.dialog.create_other_buttons)
131            buttons = frame.pack_slaves()
132            for spec, button in zip(others, buttons):
133                val, label = spec
134                self.assertEqual(button['text'], label)
135                if val == state:
136                    # hit other button, then this one
137                    # indexes depend on button order
138                    self.assertEqual(var.get(), state)
139
140    def test_make_button(self):
141        self.dialog.top = self.root
142        self.dialog.buttonframe = Frame(self.dialog.top)
143        btn = self.dialog.make_button('Test', self.dialog.close)
144        self.assertEqual(btn['text'], 'Test')
145
146    def test_create_command_buttons(self):
147        self.dialog.top = self.root
148        self.dialog.create_command_buttons()
149        # Look for close button command in buttonframe
150        closebuttoncommand = ''
151        for child in self.dialog.buttonframe.winfo_children():
152            if child['text'] == 'close':
153                closebuttoncommand = child['command']
154        self.assertIn('close', closebuttoncommand)
155
156
157if __name__ == '__main__':
158    unittest.main(verbosity=2, exit=2)
159