1
"""Unit tests for idlelib.AutoExpand"""
3
from test.test_support import requires
4
from Tkinter import Text, Tk
5
#from idlelib.idle_test.mock_tk import Text
6
from idlelib.AutoExpand import AutoExpand
10
# AutoExpand.__init__ only needs .text
11
def __init__(self, text):
14
class AutoExpandTest(unittest.TestCase):
18
if 'Tkinter' in str(Text):
21
cls.text = Text(cls.tk)
24
cls.auto_expand = AutoExpand(Dummy_Editwin(cls.text))
27
def tearDownClass(cls):
28
if hasattr(cls, 'tk'):
31
del cls.text, cls.auto_expand
34
self.text.delete('1.0', 'end')
36
def test_get_prevword(self):
38
previous = self.auto_expand.getprevword
39
equal = self.assertEqual
43
text.insert('insert', 't')
44
equal(previous(), 't')
46
text.insert('insert', 'his')
47
equal(previous(), 'this')
49
text.insert('insert', ' ')
52
text.insert('insert', 'is')
53
equal(previous(), 'is')
55
text.insert('insert', '\nsample\nstring')
56
equal(previous(), 'string')
58
text.delete('3.0', 'insert')
61
text.delete('1.0', 'end')
64
def test_before_only(self):
65
previous = self.auto_expand.getprevword
66
expand = self.auto_expand.expand_word_event
67
equal = self.assertEqual
69
self.text.insert('insert', 'ab ac bx ad ab a')
70
equal(self.auto_expand.getwords(), ['ab', 'ad', 'ac', 'a'])
72
equal(previous(), 'ab')
74
equal(previous(), 'ad')
76
equal(previous(), 'ac')
78
equal(previous(), 'a')
80
def test_after_only(self):
81
# Also add punctuation 'noise' that shoud be ignored.
83
previous = self.auto_expand.getprevword
84
expand = self.auto_expand.expand_word_event
85
equal = self.assertEqual
87
text.insert('insert', 'a, [ab] ac: () bx"" cd ac= ad ya')
88
text.mark_set('insert', '1.1')
89
equal(self.auto_expand.getwords(), ['ab', 'ac', 'ad', 'a'])
91
equal(previous(), 'ab')
93
equal(previous(), 'ac')
95
equal(previous(), 'ad')
97
equal(previous(), 'a')
99
def test_both_before_after(self):
101
previous = self.auto_expand.getprevword
102
expand = self.auto_expand.expand_word_event
103
equal = self.assertEqual
105
text.insert('insert', 'ab xy yz\n')
106
text.insert('insert', 'a ac by ac')
108
text.mark_set('insert', '2.1')
109
equal(self.auto_expand.getwords(), ['ab', 'ac', 'a'])
111
equal(previous(), 'ab')
113
equal(previous(), 'ac')
115
equal(previous(), 'a')
117
def test_other_expand_cases(self):
119
expand = self.auto_expand.expand_word_event
120
equal = self.assertEqual
122
# no expansion candidate found
123
equal(self.auto_expand.getwords(), [])
124
equal(expand('event'), 'break')
126
text.insert('insert', 'bx cy dz a')
127
equal(self.auto_expand.getwords(), [])
129
# reset state by successfully expanding once
130
# move cursor to another position and expand again
131
text.insert('insert', 'ac xy a ac ad a')
132
text.mark_set('insert', '1.7')
134
initial_state = self.auto_expand.state
135
text.mark_set('insert', '1.end')
137
new_state = self.auto_expand.state
138
self.assertNotEqual(initial_state, new_state)
140
if __name__ == '__main__':
141
unittest.main(verbosity=2)