-
Notifications
You must be signed in to change notification settings - Fork 46
/
test_indent_dedent.py
364 lines (317 loc) · 8.38 KB
/
test_indent_dedent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import sys
import pytest
_FUNNY = """\
def foo(
x,
y
"""
_BEFORE_Y = "3.5"
_AFTER_Y = "3.6"
_DEDENTED = """\
def foo(
x,
y
"""
# issue 65
def test_dedent_when_misaligned(filetab):
filetab.settings.set("indent_size", 4)
filetab.settings.set("tabs2spaces", True)
filetab.update()
filetab.textwidget.insert("end", _FUNNY)
assert filetab.textwidget.dedent(_BEFORE_Y)
assert filetab.textwidget.get("1.0", "end - 1 char") == _DEDENTED
# issue 74
def test_doesnt_delete_stuff_far_away_from_cursor(filetab):
filetab.settings.set("indent_size", 4)
filetab.settings.set("tabs2spaces", True)
filetab.update()
filetab.textwidget.insert("end", _FUNNY)
assert not filetab.textwidget.dedent(_AFTER_Y)
assert filetab.textwidget.get("1.0", "end - 1 char") == _FUNNY
def test_dedent_start_of_line(filetab):
filetab.settings.set("indent_size", 4)
for tabs2spaces in [True, False]:
filetab.settings.set("tabs2spaces", tabs2spaces)
filetab.update()
filetab.textwidget.insert("end", (" " * 4 if tabs2spaces else "\t") + "a")
assert filetab.textwidget.dedent("1.0")
assert filetab.textwidget.get("1.0", "end - 1 char") == "a"
assert not filetab.textwidget.dedent("1.0")
assert filetab.textwidget.get("1.0", "end - 1 char") == "a"
filetab.textwidget.delete("1.0", "end")
def test_indent_block_plugin(filetab):
filetab.textwidget.insert(
"1.0",
"""\
foo
bar
biz
baz""",
)
filetab.textwidget.tag_add("sel", "2.1", "3.2")
filetab.textwidget.event_generate("<Tab>")
assert (
filetab.textwidget.get("1.0", "end - 1 char")
== """\
foo
bar
biz
baz"""
)
assert list(map(str, filetab.textwidget.tag_ranges("sel"))) == ["2.0", "4.0"]
# shift-tab is platform specific, see utils.bind_tab_key
[shift_tab] = [
key for key in filetab.textwidget.bind() if key.endswith("Tab>") and key != "<Key-Tab>"
]
filetab.textwidget.event_generate(shift_tab)
assert (
filetab.textwidget.get("1.0", "end - 1 char")
== """\
foo
bar
biz
baz"""
)
assert list(map(str, filetab.textwidget.tag_ranges("sel"))) == ["2.0", "4.0"]
def test_autoindent(filetab):
indent = " " * 4
filetab.textwidget.insert("end", f"{indent}if blah: # comment")
filetab.textwidget.event_generate("<Return>")
filetab.update()
assert (
filetab.textwidget.get("1.0", "end - 1 char")
== f"{indent}if blah: # comment\n{indent}{indent}"
)
# FIXME: figure out how to do this on mac
@pytest.mark.skipif(sys.platform == "darwin", reason="I don't have a mac")
def test_shift_enter_and_alt_enter(filetab):
# See issue #404 (not the HTTP status, lol)
indent = " " * 4
filetab.textwidget.insert("1.0", f"{indent}if blah: # comment")
filetab.textwidget.event_generate("<Shift-Return>") # just like <Return>
filetab.update()
assert filetab.textwidget.get("1.0", "end - 1 char").endswith(f"\n{indent}{indent}")
filetab.textwidget.delete("1.0 lineend", "end")
filetab.textwidget.event_generate("<Alt-Return>")
filetab.update()
assert filetab.textwidget.get("1.0", "end - 1 char").endswith(f"\n{indent}")
def test_dedent_on_closing_paren(filetab):
filetab.textwidget.insert("1.0", "print(\n foo\n ")
filetab.textwidget.event_generate("<Key>", keysym="parenright")
filetab.update()
assert filetab.textwidget.get("1.0", "end - 1 char") == "print(\n foo\n)"
def test_space_inside_braces_bug(filetab):
filetab.textwidget.insert("1.0", "( aa ")
filetab.textwidget.event_generate("<Key>", keysym="parenright")
filetab.update()
assert filetab.textwidget.get("1.0", "end - 1 char") == "( aa )"
def test_double_dedent_bug(filetab):
indent = " " * 4
filetab.textwidget.insert("end", f"{indent}{indent}return foo")
filetab.textwidget.event_generate("<Return>")
filetab.update()
assert filetab.textwidget.get("1.0", "end - 1 char") == f"{indent}{indent}return foo\n{indent}"
filetab.textwidget.event_generate("<Key>", keysym="parenright")
filetab.update()
assert filetab.textwidget.get("1.0", "end - 1 char") == f"{indent}{indent}return foo\n{indent})"
def test_space_in_tabs_file_bug(filetab, tmp_path):
filetab.settings.set("tabs2spaces", False)
filetab.textwidget.insert("end", " a")
filetab.textwidget.mark_set("insert", "1.2")
# Backspacing one char at a time is annoying, but it should be, since
# someone is using tabs when they should be using spaces, and you should
# be mad at them.
filetab.textwidget.event_generate("<<Dedent>>")
assert filetab.textwidget.get("1.0", "end - 1 char") == " a"
filetab.textwidget.event_generate("<<Dedent>>")
assert filetab.textwidget.get("1.0", "end - 1 char") == " a"
filetab.textwidget.event_generate("<<Dedent>>")
assert filetab.textwidget.get("1.0", "end - 1 char") == " a"
filetab.textwidget.event_generate("<<Dedent>>")
assert filetab.textwidget.get("1.0", "end - 1 char") == "a"
def test_dedent_blank_line_in_tabs_file_bug(filetab):
filetab.settings.set("tabs2spaces", False)
filetab.textwidget.insert("1.0", "\tfoo\n\n\tbar")
filetab.textwidget.tag_add("sel", "1.0", "end - 1 char")
filetab.textwidget.event_generate("<Shift-Tab>")
assert filetab.textwidget.get("1.0", "end - 1 char") == "foo\n\nbar"
def test_css_indented_as_if_it_was_python_bug(filetab, tmp_path):
filetab.textwidget.insert("1.0", "body {\n background-color: #ffffff;")
filetab.save_as(tmp_path / "blah.css")
filetab.textwidget.event_generate("<Return>")
filetab.update()
# Cursor should be below the "b" of background-color.
# Before it was below "g", because of unwanted automagic indentation.
assert filetab.textwidget.get("insert - 1 line") == "b"
@pytest.fixture
def check_autoindents(filetab, tmp_path):
def check(filename, input_commands, output):
filetab.save_as(tmp_path / filename)
for command in input_commands.strip().split("\n"):
while command.startswith("<DEDENT>"):
filetab.textwidget.event_generate("<<Dedent>>")
filetab.update()
command = command[8:]
filetab.textwidget.insert("insert", command)
filetab.textwidget.event_generate("<Return>")
filetab.update()
assert filetab.textwidget.get("1.0", "end").strip() == output.strip()
return check
def test_markdown_autoindent(check_autoindents):
check_autoindents(
"hello.md",
"""
1. Lol and
wat.
- Foo and
bar and
baz.
End of list
""",
"""
1. Lol and
wat.
- Foo and
bar and
baz.
End of list
""",
)
def test_shell_autoindent(check_autoindents):
check_autoindents(
"loll.sh",
"""
case foo in
bla*)
echo lol
;;
*)
if foo; then
bar
<DEDENT>else
exit
fi
<DEDENT><DEDENT>esac
while [ 1 == 2 ]; do
blah
<DEDENT>done
for thing in a b c; do
echo $thing
<DEDENT>done
""",
"""
case foo in
bla*)
echo lol
;;
*)
if foo; then
bar
else
exit
fi
esac
while [ 1 == 2 ]; do
blah
done
for thing in a b c; do
echo $thing
done
""",
)
def test_html_autoindent(check_autoindents):
check_autoindents(
"lol.html",
"""
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src="foo/bar.png" />
<br />
<a href="foo/bar.png">
blah
</a>
<p>
blah blah
</p>
<div>
<a>lol</a>
</div>
</body>
</html>
""",
"""
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src="foo/bar.png" />
<br />
<a href="foo/bar.png">
blah
</a>
<p>
blah blah
</p>
<div>
<a>lol</a>
</div>
</body>
</html>
""",
)
def test_json_autoindent(check_autoindents):
check_autoindents(
"spam.json",
"""
{
"pygments_style": "material",
"font_size": 11,
"is_maximized": true,
"directory_tree_projects": [
"spam",
"ham",
{
"python_venvs": {
"project_1": "env",
"project_2": "env"
}
}
],
"python_venvs": {
"project": "env",
"project": [
"foo",
"bar"
]
}
}
""",
"""
{
"pygments_style": "material",
"font_size": 11,
"is_maximized": true,
"directory_tree_projects": [
"spam",
"ham",
{
"python_venvs": {
"project_1": "env",
"project_2": "env"
}
}
],
"python_venvs": {
"project": "env",
"project": [
"foo",
"bar"
]
}
}
""",
)