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