(root)/
Python-3.12.0/
Lib/
test/
test_tkinter/
test_text.py
       1  import unittest
       2  import tkinter
       3  from test.support import requires
       4  from test.test_tkinter.support import AbstractTkTest
       5  
       6  requires('gui')
       7  
       8  class ESC[4;38;5;81mTextTest(ESC[4;38;5;149mAbstractTkTest, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
       9  
      10      def setUp(self):
      11          super().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      def test_count(self):
      44          # XXX Some assertions do not check against the intended result,
      45          # but instead check the current result to prevent regression.
      46          text = self.text
      47          text.insert('1.0',
      48              'Lorem ipsum dolor sit amet,\n'
      49              'consectetur adipiscing elit,\n'
      50              'sed do eiusmod tempor incididunt\n'
      51              'ut labore et dolore magna aliqua.')
      52  
      53          options = ('chars', 'indices', 'lines',
      54                     'displaychars', 'displayindices', 'displaylines',
      55                     'xpixels', 'ypixels')
      56          if self.wantobjects:
      57              self.assertEqual(len(text.count('1.0', 'end', *options)), 8)
      58          else:
      59              text.count('1.0', 'end', *options)
      60          self.assertEqual(text.count('1.0', 'end', 'chars', 'lines'), (124, 4)
      61                           if self.wantobjects else '124 4')
      62          self.assertEqual(text.count('1.3', '4.5', 'chars', 'lines'), (92, 3)
      63                           if self.wantobjects else '92 3')
      64          self.assertEqual(text.count('4.5', '1.3', 'chars', 'lines'), (-92, -3)
      65                           if self.wantobjects else '-92 -3')
      66          self.assertEqual(text.count('1.3', '1.3', 'chars', 'lines'), (0, 0)
      67                           if self.wantobjects else '0 0')
      68          self.assertEqual(text.count('1.0', 'end', 'lines'), (4,)
      69                           if self.wantobjects else ('4',))
      70          self.assertEqual(text.count('end', '1.0', 'lines'), (-4,)
      71                           if self.wantobjects else ('-4',))
      72          self.assertEqual(text.count('1.3', '1.5', 'lines'), None
      73                           if self.wantobjects else ('0',))
      74          self.assertEqual(text.count('1.3', '1.3', 'lines'), None
      75                           if self.wantobjects else ('0',))
      76          self.assertEqual(text.count('1.0', 'end'), (124,)  # 'indices' by default
      77                           if self.wantobjects else ('124',))
      78          self.assertRaises(tkinter.TclError, text.count, '1.0', 'end', 'spam')
      79          self.assertRaises(tkinter.TclError, text.count, '1.0', 'end', '-lines')
      80  
      81          self.assertIsInstance(text.count('1.3', '1.5', 'ypixels'), tuple)
      82          self.assertIsInstance(text.count('1.3', '1.5', 'update', 'ypixels'), int
      83                                if self.wantobjects else str)
      84          self.assertEqual(text.count('1.3', '1.3', 'update', 'ypixels'), None
      85                           if self.wantobjects else '0')
      86          self.assertEqual(text.count('1.3', '1.5', 'update', 'indices'), 2
      87                           if self.wantobjects else '2')
      88          self.assertEqual(text.count('1.3', '1.3', 'update', 'indices'), None
      89                           if self.wantobjects else '0')
      90          self.assertEqual(text.count('1.3', '1.5', 'update'), (2,)
      91                           if self.wantobjects else ('2',))
      92          self.assertEqual(text.count('1.3', '1.3', 'update'), None
      93                           if self.wantobjects else ('0',))
      94  
      95  
      96  if __name__ == "__main__":
      97      unittest.main()