Repository navigation
Expand file tree
/
Copy pathtest_yapf_format.py
More file actions
109 lines (73 loc) · 3.31 KB
/
Copy pathtest_yapf_format.py
File metadata and controls
109 lines (73 loc) · 3.31 KB
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
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.
import pytest
from pylsp import uris
from pylsp.plugins.yapf_format import pylsp_format_document, pylsp_format_range
from pylsp.workspace import Document
from pylsp.text_edit import apply_text_edits
DOC_URI = uris.from_fs_path(__file__)
DOC = """A = [
'h', 'w',
'a'
]
B = ['h',
'w']
"""
GOOD_DOC = """A = ['hello', 'world']\n"""
FOUR_SPACE_DOC = """def hello():
pass
"""
def test_format(workspace):
doc = Document(DOC_URI, workspace, DOC)
res = pylsp_format_document(workspace, doc, None)
assert apply_text_edits(doc, res) == "A = ['h', 'w', 'a']\n\nB = ['h', 'w']\n"
def test_range_format(workspace):
doc = Document(DOC_URI, workspace, DOC)
def_range = {
'start': {'line': 0, 'character': 0},
'end': {'line': 4, 'character': 10}
}
res = pylsp_format_range(workspace, doc, def_range, None)
# Make sure B is still badly formatted
assert apply_text_edits(doc, res) == "A = ['h', 'w', 'a']\n\nB = ['h',\n\n\n'w']\n"
def test_no_change(workspace):
doc = Document(DOC_URI, workspace, GOOD_DOC)
assert not pylsp_format_document(workspace, doc, options=None)
def test_config_file(tmpdir, workspace):
# a config file in the same directory as the source file will be used
conf = tmpdir.join('.style.yapf')
conf.write('[style]\ncolumn_limit = 14')
src = tmpdir.join('test.py')
doc = Document(uris.from_fs_path(src.strpath), workspace, DOC)
res = pylsp_format_document(workspace, doc, options=None)
# A was split on multiple lines because of column_limit from config file
assert apply_text_edits(doc, res) == "A = [\n 'h', 'w',\n 'a'\n]\n\nB = ['h', 'w']\n"
@pytest.mark.parametrize('newline', ['\r\n'])
def test_line_endings(workspace, newline):
doc = Document(DOC_URI, workspace, f'import os;import sys{2 * newline}dict(a=1)')
res = pylsp_format_document(workspace, doc, options=None)
assert apply_text_edits(doc, res) == f'import os{newline}import sys{2 * newline}dict(a=1){newline}'
def test_format_with_tab_size_option(workspace):
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
res = pylsp_format_document(workspace, doc, {"tabSize": "8"})
assert apply_text_edits(doc, res) == FOUR_SPACE_DOC.replace(" ", " ")
def test_format_with_insert_spaces_option(workspace):
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
res = pylsp_format_document(workspace, doc, {"insertSpaces": False})
assert apply_text_edits(doc, res) == FOUR_SPACE_DOC.replace(" ", "\t")
def test_format_with_yapf_specific_option(workspace):
doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
res = pylsp_format_document(workspace, doc, {"USE_TABS": True})
assert apply_text_edits(doc, res) == FOUR_SPACE_DOC.replace(" ", "\t")
def test_format_returns_text_edit_per_line(workspace):
single_space_indent = """def wow():
log("x")
log("hi")"""
doc = Document(DOC_URI, workspace, single_space_indent)
res = pylsp_format_document(workspace, doc, options=None)
# two removes and two adds
assert len(res) == 4
assert res[0]['newText'] == ""
assert res[1]['newText'] == ""
assert res[2]['newText'] == " log(\"x\")\n"
assert res[3]['newText'] == " log(\"hi\")\n"