|
1 | | -"Test zzdummy, coverage 100%." |
| 1 | +"Test zzdummy with user config, coverage 100%." |
2 | 2 |
|
3 | 3 | from idlelib import zzdummy |
4 | 4 | import unittest |
5 | 5 | from test.support import requires |
6 | 6 | from tkinter import Tk, Text |
7 | | -from unittest import mock |
8 | 7 | from idlelib import config |
9 | | -from idlelib import editor |
10 | | -from idlelib import format |
| 8 | + |
| 9 | +from idlelib.idle_test.test_zzdummy import ( |
| 10 | + ZZDummyMixin, DummyEditwin, code_sample, |
| 11 | +) |
11 | 12 |
|
12 | 13 |
|
13 | 14 | real_usercfg = zzdummy.idleConf.userCfg |
|
18 | 19 | 'extensions': config.IdleUserConfParser(''), |
19 | 20 | } |
20 | 21 | test_usercfg["extensions"].read_dict({ |
21 | | - "ZzDummy": {'enable': 'True', 'enable_shell': 'False', 'enable_editor': 'True', 'z-text': 'Z'}, |
22 | | - "ZzDummy_cfgBindings": {'z-in': '<Control-Shift-KeyRelease-Insert>'}, |
23 | | - "ZzDummy_bindings": {'z-out': '<Control-Shift-KeyRelease-Delete>'}, |
| 22 | + "ZzDummy": {'enable': 'True', 'enable_shell': 'False', |
| 23 | + 'enable_editor': 'True', 'z-text': 'Z'}, |
| 24 | + "ZzDummy_cfgBindings": { |
| 25 | + 'z-in': '<Control-Shift-KeyRelease-Insert>'}, |
| 26 | + "ZzDummy_bindings": { |
| 27 | + 'z-out': '<Control-Shift-KeyRelease-Delete>'}, |
24 | 28 | }) |
25 | 29 | real_defaultcfg = zzdummy.idleConf.defaultCfg |
26 | 30 | test_defaultcfg = { |
|
33 | 37 | "AutoComplete": {'popupwait': '2000'}, |
34 | 38 | "CodeContext": {'maxlines': '15'}, |
35 | 39 | "FormatParagraph": {'max-width': '72'}, |
36 | | - "ParenMatch": {'style': 'expression', 'flash-delay': '500', 'bell': 'True'}, |
| 40 | + "ParenMatch": {'style': 'expression', |
| 41 | + 'flash-delay': '500', 'bell': 'True'}, |
37 | 42 | }) |
38 | 43 | test_defaultcfg["main"].read_dict({ |
39 | 44 | "Theme": {"default": 1, "name": "IDLE Classic", "name2": ""}, |
|
43 | 48 | real_default = real_defaultcfg[key] |
44 | 49 | value = {name: dict(real_default[name]) for name in real_default} |
45 | 50 | test_defaultcfg[key].read_dict(value) |
46 | | -code_sample = """\ |
47 | | -
|
48 | | -class C1: |
49 | | - # Class comment. |
50 | | - def __init__(self, a, b): |
51 | | - self.a = a |
52 | | - self.b = b |
53 | | -""" |
54 | | - |
55 | 51 |
|
56 | | -class DummyEditwin: |
57 | | - get_selection_indices = editor.EditorWindow.get_selection_indices |
58 | | - def __init__(self, root, text): |
59 | | - self.root = root |
60 | | - self.top = root |
61 | | - self.text = text |
62 | | - self.fregion = format.FormatRegion(self) |
63 | | - self.text.undo_block_start = mock.Mock() |
64 | | - self.text.undo_block_stop = mock.Mock() |
65 | 52 |
|
66 | | - |
67 | | -class ZZDummyTest(unittest.TestCase): |
| 53 | +class ZZDummyTest(ZZDummyMixin, unittest.TestCase): |
68 | 54 |
|
69 | 55 | @classmethod |
70 | 56 | def setUpClass(cls): |
@@ -99,88 +85,23 @@ def tearDown(self): |
99 | 85 | self.text.delete('1.0', 'end') |
100 | 86 | del self.zz |
101 | 87 |
|
102 | | - def checklines(self, text, value): |
103 | | - # Verify that there are lines being checked. |
104 | | - end_line = int(float(text.index('end'))) |
105 | | - |
106 | | - # Check each line for the starting text. |
107 | | - actual = [] |
108 | | - for line in range(1, end_line): |
109 | | - txt = text.get(f'{line}.0', f'{line}.end') |
110 | | - actual.append(txt.startswith(value)) |
111 | | - return actual |
112 | | - |
113 | 88 | def test_exists(self): |
114 | | - self.assertEqual(zzdummy.idleConf.GetSectionList('user', 'extensions'), ['ZzDummy', 'ZzDummy_cfgBindings', 'ZzDummy_bindings']) |
115 | | - self.assertEqual(zzdummy.idleConf.GetSectionList('default', 'extensions'), ['AutoComplete', 'CodeContext', 'FormatParagraph', 'ParenMatch']) |
116 | | - self.assertIn("ZzDummy", zzdummy.idleConf.GetExtensions()) |
117 | | - self.assertEqual(zzdummy.idleConf.GetExtensionKeys("ZzDummy"), {'<<z-in>>': ['<Control-Shift-KeyRelease-Insert>']}) |
118 | | - self.assertEqual(zzdummy.idleConf.GetExtensionBindings("ZzDummy"), {'<<z-in>>': ['<Control-Shift-KeyRelease-Insert>'], '<<z-out>>': ['<Control-Shift-KeyRelease-Delete>']}) |
119 | | - |
120 | | - def test_init(self): |
121 | | - zz = self.zz |
122 | | - self.assertEqual(zz.editwin, self.editor) |
123 | | - self.assertEqual(zz.text, self.editor.text) |
124 | | - |
125 | | - def test_reload(self): |
126 | | - self.assertEqual(self.zz.ztext, '# ignore #') |
127 | | - test_usercfg['extensions'].SetOption('ZzDummy', 'z-text', 'spam') |
128 | | - zzdummy.ZzDummy.reload() |
129 | | - self.assertEqual(self.zz.ztext, 'spam') |
130 | | - |
131 | | - def test_z_in_event(self): |
132 | | - eq = self.assertEqual |
133 | | - zz = self.zz |
134 | | - text = zz.text |
135 | | - eq(self.zz.ztext, '# ignore #') |
136 | | - |
137 | | - # No lines have the leading text. |
138 | | - expected = [False, False, False, False, False, False, False] |
139 | | - actual = self.checklines(text, zz.ztext) |
140 | | - eq(expected, actual) |
141 | | - |
142 | | - text.tag_add('sel', '2.0', '4.end') |
143 | | - eq(zz.z_in_event(), 'break') |
144 | | - expected = [False, True, True, True, False, False, False] |
145 | | - actual = self.checklines(text, zz.ztext) |
146 | | - eq(expected, actual) |
147 | | - |
148 | | - text.undo_block_start.assert_called_once() |
149 | | - text.undo_block_stop.assert_called_once() |
150 | | - |
151 | | - def test_z_out_event(self): |
152 | | - eq = self.assertEqual |
153 | | - zz = self.zz |
154 | | - text = zz.text |
155 | | - eq(self.zz.ztext, '# ignore #') |
156 | | - |
157 | | - # Prepend text. |
158 | | - text.tag_add('sel', '2.0', '5.end') |
159 | | - zz.z_in_event() |
160 | | - text.undo_block_start.reset_mock() |
161 | | - text.undo_block_stop.reset_mock() |
162 | | - |
163 | | - # Select a few lines to remove text. |
164 | | - text.tag_remove('sel', '1.0', 'end') |
165 | | - text.tag_add('sel', '3.0', '4.end') |
166 | | - eq(zz.z_out_event(), 'break') |
167 | | - expected = [False, True, False, False, True, False, False] |
168 | | - actual = self.checklines(text, zz.ztext) |
169 | | - eq(expected, actual) |
170 | | - |
171 | | - text.undo_block_start.assert_called_once() |
172 | | - text.undo_block_stop.assert_called_once() |
173 | | - |
174 | | - def test_roundtrip(self): |
175 | | - # Insert and remove to all code should give back original text. |
176 | | - zz = self.zz |
177 | | - text = zz.text |
178 | | - |
179 | | - text.tag_add('sel', '1.0', 'end-1c') |
180 | | - zz.z_in_event() |
181 | | - zz.z_out_event() |
182 | | - |
183 | | - self.assertEqual(text.get('1.0', 'end-1c'), code_sample) |
| 89 | + self.assertEqual( |
| 90 | + zzdummy.idleConf.GetSectionList('user', 'extensions'), |
| 91 | + ['ZzDummy', 'ZzDummy_cfgBindings', 'ZzDummy_bindings']) |
| 92 | + self.assertEqual( |
| 93 | + zzdummy.idleConf.GetSectionList('default', 'extensions'), |
| 94 | + ['AutoComplete', 'CodeContext', 'FormatParagraph', |
| 95 | + 'ParenMatch']) |
| 96 | + self.assertIn("ZzDummy", |
| 97 | + zzdummy.idleConf.GetExtensions()) |
| 98 | + self.assertEqual( |
| 99 | + zzdummy.idleConf.GetExtensionKeys("ZzDummy"), |
| 100 | + {'<<z-in>>': ['<Control-Shift-KeyRelease-Insert>']}) |
| 101 | + self.assertEqual( |
| 102 | + zzdummy.idleConf.GetExtensionBindings("ZzDummy"), |
| 103 | + {'<<z-in>>': ['<Control-Shift-KeyRelease-Insert>'], |
| 104 | + '<<z-out>>': ['<Control-Shift-KeyRelease-Delete>']}) |
184 | 105 |
|
185 | 106 |
|
186 | 107 | if __name__ == '__main__': |
|
0 commit comments