-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_test.go
More file actions
41 lines (39 loc) · 2.04 KB
/
Copy pathinsert_test.go
File metadata and controls
41 lines (39 loc) · 2.04 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
package editpro
import (
"errors"
"testing"
)
func TestInsert(t *testing.T) {
tests := []struct {
name string
src string
content string
opts InsertOptions
want string
offset int
wantErr error
}{
{name: "start", src: "a\nb", content: "x", opts: InsertOptions{Position: InsertStart}, want: "x\na\nb"},
{name: "end adds separator", src: "a\nb", content: "x", opts: InsertOptions{Position: InsertEnd}, want: "a\nb\nx", offset: 3},
{name: "end after newline", src: "a\n", content: "x", opts: InsertOptions{Position: InsertEnd}, want: "a\nx", offset: 2},
{name: "before", src: "a\nb\n", content: "x", opts: InsertOptions{Position: InsertBeforeLine, Line: 2}, want: "a\nx\nb\n", offset: 2},
{name: "after", src: "a\nb\n", content: "x", opts: InsertOptions{Position: InsertAfterLine, Line: 1}, want: "a\nx\nb\n", offset: 2},
{name: "after last unterminated", src: "a\nb", content: "x", opts: InsertOptions{Position: InsertAfterLine, Line: 2}, want: "a\nb\nx", offset: 3},
{name: "trailing empty line", src: "a\n", content: "x", opts: InsertOptions{Position: InsertBeforeLine, Line: 2}, want: "a\nx", offset: 2},
{name: "crlf", src: "a\r\nb", content: "x", opts: InsertOptions{Position: InsertAfterLine, Line: 1}, want: "a\r\nx\r\nb", offset: 3},
{name: "raw", src: "ab", content: "x", opts: InsertOptions{Position: InsertEnd, Raw: true}, want: "abx", offset: 2},
{name: "invalid eol", src: "a", content: "x", opts: InsertOptions{Position: InsertEnd, EOL: []byte("x")}, wantErr: ErrInvalidEOL},
{name: "bad line", src: "a", content: "x", opts: InsertOptions{Position: InsertBeforeLine, Line: 2}, wantErr: ErrLineNotFound},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Insert([]byte(tt.src), []byte(tt.content), tt.opts)
if !errors.Is(err, tt.wantErr) {
t.Fatalf("error = %v, want %v", err, tt.wantErr)
}
if err == nil && (string(got.Data) != tt.want || got.Offset != tt.offset) {
t.Fatalf("result = %q offset=%d, want %q offset=%d", got.Data, got.Offset, tt.want, tt.offset)
}
})
}
}