2020-02-02 10:53:30 -07:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package fake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestApplyEdit(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
label string
|
|
|
|
content string
|
2020-02-27 15:20:53 -07:00
|
|
|
edits []Edit
|
2020-02-02 10:53:30 -07:00
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
label: "empty content",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "empty edit",
|
|
|
|
content: "hello",
|
2020-02-27 15:20:53 -07:00
|
|
|
edits: []Edit{},
|
2020-02-02 10:53:30 -07:00
|
|
|
want: "hello",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "unicode edit",
|
|
|
|
content: "hello, 日本語",
|
2020-02-27 15:20:53 -07:00
|
|
|
edits: []Edit{{
|
2020-02-02 10:53:30 -07:00
|
|
|
Start: Pos{Line: 0, Column: 7},
|
|
|
|
End: Pos{Line: 0, Column: 10},
|
|
|
|
Text: "world",
|
2020-02-27 15:20:53 -07:00
|
|
|
}},
|
2020-02-02 10:53:30 -07:00
|
|
|
want: "hello, world",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "range edit",
|
|
|
|
content: "ABC\nDEF\nGHI\nJKL",
|
2020-02-27 15:20:53 -07:00
|
|
|
edits: []Edit{{
|
2020-02-02 10:53:30 -07:00
|
|
|
Start: Pos{Line: 1, Column: 1},
|
|
|
|
End: Pos{Line: 2, Column: 3},
|
|
|
|
Text: "12\n345",
|
2020-02-27 15:20:53 -07:00
|
|
|
}},
|
2020-02-02 10:53:30 -07:00
|
|
|
want: "ABC\nD12\n345\nJKL",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "end before start",
|
|
|
|
content: "ABC\nDEF\nGHI\nJKL",
|
2020-02-27 15:20:53 -07:00
|
|
|
edits: []Edit{{
|
2020-02-02 10:53:30 -07:00
|
|
|
End: Pos{Line: 1, Column: 1},
|
|
|
|
Start: Pos{Line: 2, Column: 3},
|
|
|
|
Text: "12\n345",
|
2020-02-27 15:20:53 -07:00
|
|
|
}},
|
2020-02-02 10:53:30 -07:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "out of bounds line",
|
|
|
|
content: "ABC\nDEF\nGHI\nJKL",
|
2020-02-27 15:20:53 -07:00
|
|
|
edits: []Edit{{
|
2020-02-02 10:53:30 -07:00
|
|
|
Start: Pos{Line: 1, Column: 1},
|
|
|
|
End: Pos{Line: 4, Column: 3},
|
|
|
|
Text: "12\n345",
|
2020-02-27 15:20:53 -07:00
|
|
|
}},
|
2020-02-02 10:53:30 -07:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "out of bounds column",
|
|
|
|
content: "ABC\nDEF\nGHI\nJKL",
|
2020-02-27 15:20:53 -07:00
|
|
|
edits: []Edit{{
|
2020-02-02 10:53:30 -07:00
|
|
|
Start: Pos{Line: 1, Column: 4},
|
|
|
|
End: Pos{Line: 2, Column: 3},
|
|
|
|
Text: "12\n345",
|
2020-02-27 15:20:53 -07:00
|
|
|
}},
|
2020-02-02 10:53:30 -07:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.label, func(t *testing.T) {
|
|
|
|
lines := strings.Split(test.content, "\n")
|
2020-02-27 15:20:53 -07:00
|
|
|
newLines, err := editContent(lines, test.edits)
|
2020-02-02 10:53:30 -07:00
|
|
|
if (err != nil) != test.wantErr {
|
|
|
|
t.Errorf("got err %v, want error: %t", err, test.wantErr)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got := strings.Join(newLines, "\n"); got != test.want {
|
|
|
|
t.Errorf("got %q, want %q", got, test.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|