| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package fake |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
|
| 10 |
"golang.org/x/tools/gopls/internal/protocol" |
| 11 |
) |
| 12 |
|
| 13 |
func TestApplyEdits(t *testing.T) { |
| 14 |
tests := []struct { |
| 15 |
label string |
| 16 |
content string |
| 17 |
edits []protocol.TextEdit |
| 18 |
want string |
| 19 |
wantErr bool |
| 20 |
}{ |
| 21 |
{ |
| 22 |
label: "empty content", |
| 23 |
}, |
| 24 |
{ |
| 25 |
label: "empty edit", |
| 26 |
content: "hello", |
| 27 |
edits: []protocol.TextEdit{}, |
| 28 |
want: "hello", |
| 29 |
}, |
| 30 |
{ |
| 31 |
label: "unicode edit", |
| 32 |
content: "hello, 日本語", |
| 33 |
edits: []protocol.TextEdit{ |
| 34 |
NewEdit(0, 7, 0, 10, "world"), |
| 35 |
}, |
| 36 |
want: "hello, world", |
| 37 |
}, |
| 38 |
{ |
| 39 |
label: "range edit", |
| 40 |
content: "ABC\nDEF\nGHI\nJKL", |
| 41 |
edits: []protocol.TextEdit{ |
| 42 |
NewEdit(1, 1, 2, 3, "12\n345"), |
| 43 |
}, |
| 44 |
want: "ABC\nD12\n345\nJKL", |
| 45 |
}, |
| 46 |
{ |
| 47 |
label: "regression test for issue #57627", |
| 48 |
content: "go 1.18\nuse moda/a", |
| 49 |
edits: []protocol.TextEdit{ |
| 50 |
NewEdit(1, 0, 1, 0, "\n"), |
| 51 |
NewEdit(2, 0, 2, 0, "\n"), |
| 52 |
}, |
| 53 |
want: "go 1.18\n\nuse moda/a\n", |
| 54 |
}, |
| 55 |
{ |
| 56 |
label: "end before start", |
| 57 |
content: "ABC\nDEF\nGHI\nJKL", |
| 58 |
edits: []protocol.TextEdit{ |
| 59 |
NewEdit(2, 3, 1, 1, "12\n345"), |
| 60 |
}, |
| 61 |
wantErr: true, |
| 62 |
}, |
| 63 |
{ |
| 64 |
label: "out of bounds line", |
| 65 |
content: "ABC\nDEF\nGHI\nJKL", |
| 66 |
edits: []protocol.TextEdit{ |
| 67 |
NewEdit(1, 1, 4, 3, "12\n345"), |
| 68 |
}, |
| 69 |
wantErr: true, |
| 70 |
}, |
| 71 |
{ |
| 72 |
label: "out of bounds column", |
| 73 |
content: "ABC\nDEF\nGHI\nJKL", |
| 74 |
edits: []protocol.TextEdit{ |
| 75 |
NewEdit(1, 4, 2, 3, "12\n345"), |
| 76 |
}, |
| 77 |
wantErr: true, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
for _, test := range tests { |
| 82 |
t.Run(test.label, func(t *testing.T) { |
| 83 |
got, err := applyEdits(protocol.NewMapper("", []byte(test.content)), test.edits, false) |
| 84 |
if (err != nil) != test.wantErr { |
| 85 |
t.Errorf("got err %v, want error: %t", err, test.wantErr) |
| 86 |
} |
| 87 |
if err != nil { |
| 88 |
return |
| 89 |
} |
| 90 |
if got := string(got); got != test.want { |
| 91 |
t.Errorf("got %q, want %q", got, test.want) |
| 92 |
} |
| 93 |
}) |
| 94 |
} |
| 95 |
} |