File: test_yapf_format.py

package info (click to toggle)
python-lsp-server 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 796 kB
  • sloc: python: 7,791; sh: 12; makefile: 4
file content (115 lines) | stat: -rw-r--r-- 3,492 bytes parent folder | download
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
# 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.text_edit import apply_text_edits
from pylsp.workspace import Document

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) -> None:
    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) -> None:
    doc = Document(DOC_URI, workspace, DOC)

    def_range = {
        "start": {"line": 0, "character": 0},
        "end": {"line": 4, "character": 10},
    }
    res = pylsp_format_range(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) -> None:
    doc = Document(DOC_URI, workspace, GOOD_DOC)
    assert not pylsp_format_document(workspace, doc, options=None)


def test_config_file(tmpdir, workspace) -> None:
    # 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) -> None:
    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) -> None:
    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) -> None:
    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) -> None:
    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) -> None:
    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'