1import unittest
2import Tkinter as tkinter
3from test.test_support import requires, run_unittest
4from test_ttk.support import AbstractTkTest
5
6requires('gui')
7
8class TextTest(AbstractTkTest, unittest.TestCase):
9
10    def setUp(self):
11        super(TextTest, self).setUp()
12        self.text = tkinter.Text(self.root)
13
14    def test_debug(self):
15        text = self.text
16        olddebug = text.debug()
17        try:
18            text.debug(0)
19            self.assertEqual(text.debug(), 0)
20            text.debug(1)
21            self.assertEqual(text.debug(), 1)
22        finally:
23            text.debug(olddebug)
24            self.assertEqual(text.debug(), olddebug)
25
26    def test_search(self):
27        text = self.text
28
29        # pattern and index are obligatory arguments.
30        self.assertRaises(tkinter.TclError, text.search, None, '1.0')
31        self.assertRaises(tkinter.TclError, text.search, 'a', None)
32        self.assertRaises(tkinter.TclError, text.search, None, None)
33
34        # Invalid text index.
35        self.assertRaises(tkinter.TclError, text.search, '', 0)
36
37        # Check if we are getting the indices as strings -- you are likely
38        # to get Tcl_Obj under Tk 8.5 if Tkinter doesn't convert it.
39        text.insert('1.0', 'hi-test')
40        self.assertEqual(text.search('-test', '1.0', 'end'), '1.2')
41        self.assertEqual(text.search('test', '1.0', 'end'), '1.3')
42
43
44tests_gui = (TextTest, )
45
46if __name__ == "__main__":
47    run_unittest(*tests_gui)
48