1"Test search, coverage 69%."
2
3from idlelib import search
4import unittest
5from test.support import requires
6requires('gui')
7from tkinter import Tk, Text, BooleanVar
8from idlelib import searchengine
9
10# Does not currently test the event handler wrappers.
11# A usage test should simulate clicks and check highlighting.
12# Tests need to be coordinated with SearchDialogBase tests
13# to avoid duplication.
14
15
16class SearchDialogTest(unittest.TestCase):
17
18    @classmethod
19    def setUpClass(cls):
20        cls.root = Tk()
21
22    @classmethod
23    def tearDownClass(cls):
24        cls.root.destroy()
25        del cls.root
26
27    def setUp(self):
28        self.engine = searchengine.SearchEngine(self.root)
29        self.dialog = search.SearchDialog(self.root, self.engine)
30        self.dialog.bell = lambda: None
31        self.text = Text(self.root)
32        self.text.insert('1.0', 'Hello World!')
33
34    def test_find_again(self):
35        # Search for various expressions
36        text = self.text
37
38        self.engine.setpat('')
39        self.assertFalse(self.dialog.find_again(text))
40        self.dialog.bell = lambda: None
41
42        self.engine.setpat('Hello')
43        self.assertTrue(self.dialog.find_again(text))
44
45        self.engine.setpat('Goodbye')
46        self.assertFalse(self.dialog.find_again(text))
47
48        self.engine.setpat('World!')
49        self.assertTrue(self.dialog.find_again(text))
50
51        self.engine.setpat('Hello World!')
52        self.assertTrue(self.dialog.find_again(text))
53
54        # Regular expression
55        self.engine.revar = BooleanVar(self.root, True)
56        self.engine.setpat('W[aeiouy]r')
57        self.assertTrue(self.dialog.find_again(text))
58
59    def test_find_selection(self):
60        # Select some text and make sure it's found
61        text = self.text
62        # Add additional line to find
63        self.text.insert('2.0', 'Hello World!')
64
65        text.tag_add('sel', '1.0', '1.4')       # Select 'Hello'
66        self.assertTrue(self.dialog.find_selection(text))
67
68        text.tag_remove('sel', '1.0', 'end')
69        text.tag_add('sel', '1.6', '1.11')      # Select 'World!'
70        self.assertTrue(self.dialog.find_selection(text))
71
72        text.tag_remove('sel', '1.0', 'end')
73        text.tag_add('sel', '1.0', '1.11')      # Select 'Hello World!'
74        self.assertTrue(self.dialog.find_selection(text))
75
76        # Remove additional line
77        text.delete('2.0', 'end')
78
79if __name__ == '__main__':
80    unittest.main(verbosity=2, exit=2)
81