Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d3e4b4

Browse files
committed
Merge heads.
2 parents adaddde + 7936e6f commit 0d3e4b4

3 files changed

Lines changed: 164 additions & 0 deletions

File tree

Lib/idlelib/AutoExpand.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
'''Complete the current word before the cursor with words in the editor.
2+
3+
Each menu selection or shortcut key selection replaces the word with a
4+
different word with the same prefix. The search for matches begins
5+
before the target and moves toward the top of the editor. It then starts
6+
after the cursor and moves down. It then returns to the original word and
7+
the cycle starts again.
8+
9+
Changing the current text line or leaving the cursor in a different
10+
place before requesting the next selection causes AutoExpand to reset
11+
its state.
12+
13+
This is an extension file and there is only one instance of AutoExpand.
14+
'''
115
import string
216
import re
317

@@ -20,6 +34,7 @@ def __init__(self, editwin):
2034
self.state = None
2135

2236
def expand_word_event(self, event):
37+
"Replace the current word with the next expansion."
2338
curinsert = self.text.index("insert")
2439
curline = self.text.get("insert linestart", "insert lineend")
2540
if not self.state:
@@ -46,6 +61,7 @@ def expand_word_event(self, event):
4661
return "break"
4762

4863
def getwords(self):
64+
"Return a list of words that match the prefix before the cursor."
4965
word = self.getprevword()
5066
if not word:
5167
return []
@@ -76,8 +92,13 @@ def getwords(self):
7692
return words
7793

7894
def getprevword(self):
95+
"Return the word prefix before the cursor."
7996
line = self.text.get("insert linestart", "insert")
8097
i = len(line)
8198
while i > 0 and line[i-1] in self.wordchars:
8299
i = i-1
83100
return line[i:]
101+
102+
if __name__ == '__main__':
103+
import unittest
104+
unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2)
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
"""Unit tests for idlelib.AutoExpand"""
2+
import unittest
3+
from 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
7+
8+
9+
class Dummy_Editwin:
10+
# AutoExpand.__init__ only needs .text
11+
def __init__(self, text):
12+
self.text = text
13+
14+
class 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))
25+
26+
@classmethod
27+
def tearDownClass(cls):
28+
if hasattr(cls, 'tk'):
29+
cls.tk.destroy()
30+
del cls.tk
31+
del cls.text, cls.auto_expand
32+
33+
def tearDown(self):
34+
self.text.delete('1.0', 'end')
35+
36+
def test_get_prevword(self):
37+
text = self.text
38+
previous = self.auto_expand.getprevword
39+
equal = self.assertEqual
40+
41+
equal(previous(), '')
42+
43+
text.insert('insert', 't')
44+
equal(previous(), 't')
45+
46+
text.insert('insert', 'his')
47+
equal(previous(), 'this')
48+
49+
text.insert('insert', ' ')
50+
equal(previous(), '')
51+
52+
text.insert('insert', 'is')
53+
equal(previous(), 'is')
54+
55+
text.insert('insert', '\nsample\nstring')
56+
equal(previous(), 'string')
57+
58+
text.delete('3.0', 'insert')
59+
equal(previous(), '')
60+
61+
text.delete('1.0', 'end')
62+
equal(previous(), '')
63+
64+
def test_before_only(self):
65+
previous = self.auto_expand.getprevword
66+
expand = self.auto_expand.expand_word_event
67+
equal = self.assertEqual
68+
69+
self.text.insert('insert', 'ab ac bx ad ab a')
70+
equal(self.auto_expand.getwords(), ['ab', 'ad', 'ac', 'a'])
71+
expand('event')
72+
equal(previous(), 'ab')
73+
expand('event')
74+
equal(previous(), 'ad')
75+
expand('event')
76+
equal(previous(), 'ac')
77+
expand('event')
78+
equal(previous(), 'a')
79+
80+
def test_after_only(self):
81+
# Also add punctuation 'noise' that shoud be ignored.
82+
text = self.text
83+
previous = self.auto_expand.getprevword
84+
expand = self.auto_expand.expand_word_event
85+
equal = self.assertEqual
86+
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'])
90+
expand('event')
91+
equal(previous(), 'ab')
92+
expand('event')
93+
equal(previous(), 'ac')
94+
expand('event')
95+
equal(previous(), 'ad')
96+
expand('event')
97+
equal(previous(), 'a')
98+
99+
def test_both_before_after(self):
100+
text = self.text
101+
previous = self.auto_expand.getprevword
102+
expand = self.auto_expand.expand_word_event
103+
equal = self.assertEqual
104+
105+
text.insert('insert', 'ab xy yz\n')
106+
text.insert('insert', 'a ac by ac')
107+
108+
text.mark_set('insert', '2.1')
109+
equal(self.auto_expand.getwords(), ['ab', 'ac', 'a'])
110+
expand('event')
111+
equal(previous(), 'ab')
112+
expand('event')
113+
equal(previous(), 'ac')
114+
expand('event')
115+
equal(previous(), 'a')
116+
117+
def test_other_expand_cases(self):
118+
text = self.text
119+
expand = self.auto_expand.expand_word_event
120+
equal = self.assertEqual
121+
122+
# no expansion candidate found
123+
equal(self.auto_expand.getwords(), [])
124+
equal(expand('event'), 'break')
125+
126+
text.insert('insert', 'bx cy dz a')
127+
equal(self.auto_expand.getwords(), [])
128+
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')
133+
expand('event')
134+
initial_state = self.auto_expand.state
135+
text.mark_set('insert', '1.end')
136+
expand('event')
137+
new_state = self.auto_expand.state
138+
self.assertNotEqual(initial_state, new_state)
139+
140+
if __name__ == '__main__':
141+
unittest.main(verbosity=2)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Build
6666
IDLE
6767
----
6868

69+
- Issue #18292: Add unittest for AutoExpand. Patch by Saihadhav Heblikar.
70+
6971
- Issue #18409: Add unittest for AutoComplete. Patch by Phil Webster.
7072

7173
Tests

0 commit comments

Comments
 (0)