blob: ae8186cdc49f7b5abbaacc22996794a94187b0b8 [file] [log] [blame]
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04001"""Unit tests for idlelib.autoexpand"""
Terry Jan Reedy7936e6f2014-06-04 20:50:59 -04002import unittest
3from test.support import requires
4from tkinter import Text, Tk
5#from idlelib.idle_test.mock_tk import Text
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04006from idlelib.autoexpand import AutoExpand
Terry Jan Reedy7936e6f2014-06-04 20:50:59 -04007
8
9class Dummy_Editwin:
10 # AutoExpand.__init__ only needs .text
11 def __init__(self, text):
12 self.text = text
13
14class AutoExpandTest(unittest.TestCase):
15
16 @classmethod
17 def setUpClass(cls):
18 if 'tkinter' in str(Text):
19 requires('gui')
20 cls.tk = Tk()
21 cls.text = Text(cls.tk)
22 else:
23 cls.text = Text()
24 cls.auto_expand = AutoExpand(Dummy_Editwin(cls.text))
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040025 cls.auto_expand.bell = lambda: None
Terry Jan Reedy7936e6f2014-06-04 20:50:59 -040026
27 @classmethod
28 def tearDownClass(cls):
Terry Jan Reedy75cbeb52016-06-03 22:19:17 -040029 del cls.text, cls.auto_expand
Terry Jan Reedy7936e6f2014-06-04 20:50:59 -040030 if hasattr(cls, 'tk'):
31 cls.tk.destroy()
32 del cls.tk
Terry Jan Reedy7936e6f2014-06-04 20:50:59 -040033
34 def tearDown(self):
35 self.text.delete('1.0', 'end')
36
37 def test_get_prevword(self):
38 text = self.text
39 previous = self.auto_expand.getprevword
40 equal = self.assertEqual
41
42 equal(previous(), '')
43
44 text.insert('insert', 't')
45 equal(previous(), 't')
46
47 text.insert('insert', 'his')
48 equal(previous(), 'this')
49
50 text.insert('insert', ' ')
51 equal(previous(), '')
52
53 text.insert('insert', 'is')
54 equal(previous(), 'is')
55
56 text.insert('insert', '\nsample\nstring')
57 equal(previous(), 'string')
58
59 text.delete('3.0', 'insert')
60 equal(previous(), '')
61
62 text.delete('1.0', 'end')
63 equal(previous(), '')
64
65 def test_before_only(self):
66 previous = self.auto_expand.getprevword
67 expand = self.auto_expand.expand_word_event
68 equal = self.assertEqual
69
70 self.text.insert('insert', 'ab ac bx ad ab a')
71 equal(self.auto_expand.getwords(), ['ab', 'ad', 'ac', 'a'])
72 expand('event')
73 equal(previous(), 'ab')
74 expand('event')
75 equal(previous(), 'ad')
76 expand('event')
77 equal(previous(), 'ac')
78 expand('event')
79 equal(previous(), 'a')
80
81 def test_after_only(self):
Berker Peksagf23530f2014-10-19 18:04:38 +030082 # Also add punctuation 'noise' that should be ignored.
Terry Jan Reedy7936e6f2014-06-04 20:50:59 -040083 text = self.text
84 previous = self.auto_expand.getprevword
85 expand = self.auto_expand.expand_word_event
86 equal = self.assertEqual
87
88 text.insert('insert', 'a, [ab] ac: () bx"" cd ac= ad ya')
89 text.mark_set('insert', '1.1')
90 equal(self.auto_expand.getwords(), ['ab', 'ac', 'ad', 'a'])
91 expand('event')
92 equal(previous(), 'ab')
93 expand('event')
94 equal(previous(), 'ac')
95 expand('event')
96 equal(previous(), 'ad')
97 expand('event')
98 equal(previous(), 'a')
99
100 def test_both_before_after(self):
101 text = self.text
102 previous = self.auto_expand.getprevword
103 expand = self.auto_expand.expand_word_event
104 equal = self.assertEqual
105
106 text.insert('insert', 'ab xy yz\n')
107 text.insert('insert', 'a ac by ac')
108
109 text.mark_set('insert', '2.1')
110 equal(self.auto_expand.getwords(), ['ab', 'ac', 'a'])
111 expand('event')
112 equal(previous(), 'ab')
113 expand('event')
114 equal(previous(), 'ac')
115 expand('event')
116 equal(previous(), 'a')
117
118 def test_other_expand_cases(self):
119 text = self.text
120 expand = self.auto_expand.expand_word_event
121 equal = self.assertEqual
122
123 # no expansion candidate found
124 equal(self.auto_expand.getwords(), [])
125 equal(expand('event'), 'break')
126
127 text.insert('insert', 'bx cy dz a')
128 equal(self.auto_expand.getwords(), [])
129
130 # reset state by successfully expanding once
131 # move cursor to another position and expand again
132 text.insert('insert', 'ac xy a ac ad a')
133 text.mark_set('insert', '1.7')
134 expand('event')
135 initial_state = self.auto_expand.state
136 text.mark_set('insert', '1.end')
137 expand('event')
138 new_state = self.auto_expand.state
139 self.assertNotEqual(initial_state, new_state)
140
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -0400141
Terry Jan Reedy7936e6f2014-06-04 20:50:59 -0400142if __name__ == '__main__':
143 unittest.main(verbosity=2)