File: test_lsp_diff.py

package info (click to toggle)
python-lsp-rope 0.1.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 340 kB
  • sloc: python: 2,319; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 2,493 bytes parent folder | download | duplicates (2)
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
from pylsp_rope.lsp_diff import _difflib_ops_to_text_edit_ops, lsp_diff
from test.conftest import create_document


def test_lsp_diff(workspace):
    expected = [
        {
            "range": {
                "start": {"line": 2, "character": 0},
                "end": {"line": 3, "character": 0},
            },
            "newText": "",
        },
        {
            "range": {
                "start": {"line": 4, "character": 0},
                "end": {"line": 5, "character": 0},
            },
            "newText": 'print("world")\n',
        },
        {
            "range": {
                "start": {"line": 15, "character": 0},
                "end": {"line": 16, "character": 0},
            },
            "newText": '    os.path.join("world", roses)\n',
        },
    ]

    old_document = create_document(workspace, "many_changes.py")
    new_document = create_document(workspace, "many_changes_inlined.py")

    changes = list(lsp_diff(old_document.lines, new_document.lines))
    assert changes == expected


def test_difflib_ops_to_text_edit_ops_insert(workspace):
    expected = {
        "range": {
            "start": {"line": 5, "character": 0},
            "end": {"line": 5, "character": 0},
        },
        "newText": 'are = "here"\nred = "here"\n',
    }

    new_document = create_document(workspace, "many_changes_inlined.py")

    difflib_ops = ("insert", 5, 5, 6, 8)
    text_edit_ops = _difflib_ops_to_text_edit_ops(difflib_ops, new_document.lines)

    assert text_edit_ops == expected


def test_difflib_ops_to_text_edit_ops_delete(workspace):
    expected = {
        "range": {
            "start": {"line": 2, "character": 0},
            "end": {"line": 3, "character": 0},
        },
        "newText": "",
    }

    new_document = create_document(workspace, "many_changes_inlined.py")

    difflib_ops = ("delete", 2, 3, 2, 2)
    text_edit_ops = _difflib_ops_to_text_edit_ops(difflib_ops, new_document.lines)

    assert text_edit_ops == expected


def test_difflib_ops_to_text_edit_ops_replace(workspace):
    expected = {
        "range": {
            "start": {"line": 4, "character": 0},
            "end": {"line": 5, "character": 0},
        },
        "newText": 'print("world")\n',
    }

    new_document = create_document(workspace, "many_changes_inlined.py")

    difflib_ops = ("replace", 4, 5, 3, 4)
    text_edit_ops = _difflib_ops_to_text_edit_ops(difflib_ops, new_document.lines)

    assert text_edit_ops == expected