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