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