Skip to content

Commit 2f47ecb

Browse files
committed
Add test for ZzDummy being loaded in extensions list
1 parent c032cf8 commit 2f47ecb

2 files changed

Lines changed: 178 additions & 0 deletions

File tree

Lib/idlelib/idle_test/test_zzdummy.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def checklines(self, text, value):
8282
actual.append(txt.startswith(value))
8383
return actual
8484

85+
def test_exists(self):
86+
self.assertIn("ZzDummy", zzdummy.idleConf.GetExtensions())
87+
8588
def test_init(self):
8689
zz = self.zz
8790
self.assertEqual(zz.editwin, self.editor)
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
"Test zzdummy, coverage 100%."
2+
3+
from idlelib import zzdummy
4+
import unittest
5+
from test.support import requires
6+
from tkinter import Tk, Text
7+
from unittest import mock
8+
from idlelib import config
9+
from idlelib import editor
10+
from idlelib import format
11+
12+
13+
real_usercfg = zzdummy.idleConf.userCfg
14+
test_usercfg = {
15+
'main': config.IdleUserConfParser(''),
16+
'highlight': config.IdleUserConfParser(''),
17+
'keys': config.IdleUserConfParser(''),
18+
'extensions': config.IdleUserConfParser(''),
19+
}
20+
test_usercfg["extensions"].read_dict(
21+
{"ZzDummy": {'enable': 'False', 'enable_shell': 'False', 'enable_editor': 'True', 'z-text': 'Z'}}
22+
)
23+
real_defaultcfg = zzdummy.idleConf.defaultCfg
24+
test_defaultcfg = {
25+
'main': config.IdleUserConfParser(''),
26+
'highlight': config.IdleUserConfParser(''),
27+
'keys': config.IdleUserConfParser(''),
28+
'extensions': config.IdleUserConfParser(''),
29+
}
30+
test_defaultcfg["extensions"].read_dict(
31+
{
32+
"AutoComplete": {'popupwait': '2000'},
33+
"CodeContext": {'maxlines': '15'},
34+
"FormatParagraph": {'max-width': '72'},
35+
"ParenMatch": {'style': 'expression', 'flash-delay': '500', 'bell': 'True'},
36+
}
37+
)
38+
code_sample = """\
39+
40+
class C1:
41+
# Class comment.
42+
def __init__(self, a, b):
43+
self.a = a
44+
self.b = b
45+
"""
46+
47+
48+
class DummyEditwin:
49+
get_selection_indices = editor.EditorWindow.get_selection_indices
50+
def __init__(self, root, text):
51+
self.root = root
52+
self.top = root
53+
self.text = text
54+
self.fregion = format.FormatRegion(self)
55+
self.text.undo_block_start = mock.Mock()
56+
self.text.undo_block_stop = mock.Mock()
57+
58+
59+
class ZZDummyTest(unittest.TestCase):
60+
61+
@classmethod
62+
def setUpClass(cls):
63+
requires('gui')
64+
root = cls.root = Tk()
65+
root.withdraw()
66+
text = cls.text = Text(cls.root)
67+
cls.editor = DummyEditwin(root, text)
68+
zzdummy.idleConf.userCfg = test_usercfg
69+
zzdummy.idleConf.defaultCfg = test_defaultcfg
70+
71+
@classmethod
72+
def tearDownClass(cls):
73+
zzdummy.idleConf.defaultCfg = real_defaultcfg
74+
zzdummy.idleConf.userCfg = real_usercfg
75+
del cls.editor, cls.text
76+
cls.root.update_idletasks()
77+
for id in cls.root.tk.call('after', 'info'):
78+
cls.root.after_cancel(id) # Need for EditorWindow.
79+
cls.root.destroy()
80+
del cls.root
81+
82+
def setUp(self):
83+
text = self.text
84+
text.insert('1.0', code_sample)
85+
text.undo_block_start.reset_mock()
86+
text.undo_block_stop.reset_mock()
87+
zz = self.zz = zzdummy.ZzDummy(self.editor)
88+
zzdummy.ZzDummy.ztext = '# ignore #'
89+
90+
def tearDown(self):
91+
self.text.delete('1.0', 'end')
92+
del self.zz
93+
94+
def checklines(self, text, value):
95+
# Verify that there are lines being checked.
96+
end_line = int(float(text.index('end')))
97+
98+
# Check each line for the starting text.
99+
actual = []
100+
for line in range(1, end_line):
101+
txt = text.get(f'{line}.0', f'{line}.end')
102+
actual.append(txt.startswith(value))
103+
return actual
104+
105+
def test_exists(self):
106+
self.assertIn("ZzDummy", zzdummy.idleConf.GetExtensions())
107+
108+
def test_init(self):
109+
zz = self.zz
110+
self.assertEqual(zz.editwin, self.editor)
111+
self.assertEqual(zz.text, self.editor.text)
112+
113+
def test_reload(self):
114+
self.assertEqual(self.zz.ztext, '# ignore #')
115+
test_usercfg['extensions'].SetOption('ZzDummy', 'z-text', 'spam')
116+
zzdummy.ZzDummy.reload()
117+
self.assertEqual(self.zz.ztext, 'spam')
118+
119+
def test_z_in_event(self):
120+
eq = self.assertEqual
121+
zz = self.zz
122+
text = zz.text
123+
eq(self.zz.ztext, '# ignore #')
124+
125+
# No lines have the leading text.
126+
expected = [False, False, False, False, False, False, False]
127+
actual = self.checklines(text, zz.ztext)
128+
eq(expected, actual)
129+
130+
text.tag_add('sel', '2.0', '4.end')
131+
eq(zz.z_in_event(), 'break')
132+
expected = [False, True, True, True, False, False, False]
133+
actual = self.checklines(text, zz.ztext)
134+
eq(expected, actual)
135+
136+
text.undo_block_start.assert_called_once()
137+
text.undo_block_stop.assert_called_once()
138+
139+
def test_z_out_event(self):
140+
eq = self.assertEqual
141+
zz = self.zz
142+
text = zz.text
143+
eq(self.zz.ztext, '# ignore #')
144+
145+
# Prepend text.
146+
text.tag_add('sel', '2.0', '5.end')
147+
zz.z_in_event()
148+
text.undo_block_start.reset_mock()
149+
text.undo_block_stop.reset_mock()
150+
151+
# Select a few lines to remove text.
152+
text.tag_remove('sel', '1.0', 'end')
153+
text.tag_add('sel', '3.0', '4.end')
154+
eq(zz.z_out_event(), 'break')
155+
expected = [False, True, False, False, True, False, False]
156+
actual = self.checklines(text, zz.ztext)
157+
eq(expected, actual)
158+
159+
text.undo_block_start.assert_called_once()
160+
text.undo_block_stop.assert_called_once()
161+
162+
def test_roundtrip(self):
163+
# Insert and remove to all code should give back original text.
164+
zz = self.zz
165+
text = zz.text
166+
167+
text.tag_add('sel', '1.0', 'end-1c')
168+
zz.z_in_event()
169+
zz.z_out_event()
170+
171+
self.assertEqual(text.get('1.0', 'end-1c'), code_sample)
172+
173+
174+
if __name__ == '__main__':
175+
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)