1import unittest
2from test.test_support import requires
3from Tkinter import Tk, Text
4
5import idlelib.AutoComplete as ac
6import idlelib.AutoCompleteWindow as acw
7from idlelib.idle_test.mock_idle import Func
8from idlelib.idle_test.mock_tk import Event
9
10class AutoCompleteWindow:
11    def complete():
12        return
13
14class DummyEditwin:
15    def __init__(self, root, text):
16        self.root = root
17        self.text = text
18        self.indentwidth = 8
19        self.tabwidth = 8
20        self.context_use_ps1 = True
21
22
23class AutoCompleteTest(unittest.TestCase):
24
25    @classmethod
26    def setUpClass(cls):
27        requires('gui')
28        cls.root = Tk()
29        cls.text = Text(cls.root)
30        cls.editor = DummyEditwin(cls.root, cls.text)
31
32    @classmethod
33    def tearDownClass(cls):
34        del cls.editor, cls.text
35        cls.root.destroy()
36        del cls.root
37
38    def setUp(self):
39        self.editor.text.delete('1.0', 'end')
40        self.autocomplete = ac.AutoComplete(self.editor)
41
42    def test_init(self):
43        self.assertEqual(self.autocomplete.editwin, self.editor)
44
45    def test_make_autocomplete_window(self):
46        testwin = self.autocomplete._make_autocomplete_window()
47        self.assertIsInstance(testwin, acw.AutoCompleteWindow)
48
49    def test_remove_autocomplete_window(self):
50        self.autocomplete.autocompletewindow = (
51            self.autocomplete._make_autocomplete_window())
52        self.autocomplete._remove_autocomplete_window()
53        self.assertIsNone(self.autocomplete.autocompletewindow)
54
55    def test_force_open_completions_event(self):
56        # Test that force_open_completions_event calls _open_completions
57        o_cs = Func()
58        self.autocomplete.open_completions = o_cs
59        self.autocomplete.force_open_completions_event('event')
60        self.assertEqual(o_cs.args, (True, False, True))
61
62    def test_try_open_completions_event(self):
63        Equal = self.assertEqual
64        autocomplete = self.autocomplete
65        trycompletions = self.autocomplete.try_open_completions_event
66        o_c_l = Func()
67        autocomplete._open_completions_later = o_c_l
68
69        # _open_completions_later should not be called with no text in editor
70        trycompletions('event')
71        Equal(o_c_l.args, None)
72
73        # _open_completions_later should be called with COMPLETE_ATTRIBUTES (1)
74        self.text.insert('1.0', 're.')
75        trycompletions('event')
76        Equal(o_c_l.args, (False, False, False, 1))
77
78        # _open_completions_later should be called with COMPLETE_FILES (2)
79        self.text.delete('1.0', 'end')
80        self.text.insert('1.0', '"./Lib/')
81        trycompletions('event')
82        Equal(o_c_l.args, (False, False, False, 2))
83
84    def test_autocomplete_event(self):
85        Equal = self.assertEqual
86        autocomplete = self.autocomplete
87
88        # Test that the autocomplete event is ignored if user is pressing a
89        # modifier key in addition to the tab key
90        ev = Event(mc_state=True)
91        self.assertIsNone(autocomplete.autocomplete_event(ev))
92        del ev.mc_state
93
94        # If autocomplete window is open, complete() method is called
95        self.text.insert('1.0', 're.')
96        # This must call autocomplete._make_autocomplete_window()
97        Equal(self.autocomplete.autocomplete_event(ev), 'break')
98
99        # If autocomplete window is not active or does not exist,
100        # open_completions is called. Return depends on its return.
101        autocomplete._remove_autocomplete_window()
102        o_cs = Func()  # .result = None
103        autocomplete.open_completions = o_cs
104        Equal(self.autocomplete.autocomplete_event(ev), None)
105        Equal(o_cs.args, (False, True, True))
106        o_cs.result = True
107        Equal(self.autocomplete.autocomplete_event(ev), 'break')
108        Equal(o_cs.args, (False, True, True))
109
110    def test_open_completions_later(self):
111        # Test that autocomplete._delayed_completion_id is set
112        pass
113
114    def test_delayed_open_completions(self):
115        # Test that autocomplete._delayed_completion_id set to None and that
116        # open_completions only called if insertion index is the same as
117        # _delayed_completion_index
118        pass
119
120    def test_open_completions(self):
121        # Test completions of files and attributes as well as non-completion
122        # of errors
123        pass
124
125    def test_fetch_completions(self):
126        # Test that fetch_completions returns 2 lists:
127        # For attribute completion, a large list containing all variables, and
128        # a small list containing non-private variables.
129        # For file completion, a large list containing all files in the path,
130        # and a small list containing files that do not start with '.'
131        pass
132
133    def test_get_entity(self):
134        # Test that a name is in the namespace of sys.modules and
135        # __main__.__dict__
136        pass
137
138
139if __name__ == '__main__':
140    unittest.main(verbosity=2)
141