-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_test.go
More file actions
87 lines (82 loc) · 2.87 KB
/
Copy pathedit_test.go
File metadata and controls
87 lines (82 loc) · 2.87 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
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
package editpro
import (
"bytes"
"errors"
"testing"
)
func TestReplace(t *testing.T) {
tests := []struct {
name string
src, old string
new string
opts ReplaceOptions
want string
matches int
replaced int
wantChange bool
wantErr error
}{
{name: "all", src: "one two one", old: "one", new: "1", want: "1 two 1", matches: 2, replaced: 2, wantChange: true},
{name: "limited", src: "aaa", old: "a", new: "bb", opts: ReplaceOptions{Limit: 2}, want: "bbbba", matches: 3, replaced: 2, wantChange: true},
{name: "non-overlapping", src: "aaa", old: "aa", new: "x", want: "xa", matches: 1, replaced: 1, wantChange: true},
{name: "same bytes", src: "a", old: "a", new: "a", want: "a", matches: 1, replaced: 1},
{name: "missing allowed", src: "a", old: "z", new: "x", want: "a"},
{name: "missing required", src: "a", old: "z", new: "x", opts: ReplaceOptions{RequireMatch: true}, wantErr: ErrNoMatch},
{name: "empty old", src: "a", old: "", new: "x", wantErr: ErrEmptyOld},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Replace([]byte(tt.src), []byte(tt.old), []byte(tt.new), tt.opts)
if !errors.Is(err, tt.wantErr) {
t.Fatalf("error = %v, want %v", err, tt.wantErr)
}
if err != nil {
return
}
if string(got.Data) != tt.want || got.Matches != tt.matches || got.Replaced != tt.replaced || got.Changed != tt.wantChange {
t.Fatalf("result = %#v (%q), want %q matches=%d replaced=%d changed=%v", got, got.Data, tt.want, tt.matches, tt.replaced, tt.wantChange)
}
})
}
}
func TestReplaceExpectedMatchesDoesNotModify(t *testing.T) {
src := []byte("x x")
_, err := Replace(src, []byte("x"), []byte("y"), ReplaceOptions{ExpectedMatches: 1})
if err == nil || !bytes.Equal(src, []byte("x x")) {
t.Fatalf("expected validation error and unchanged input, got %v, %q", err, src)
}
}
func BenchmarkReplaceAll(b *testing.B) {
src := bytes.Repeat([]byte("alpha beta gamma\n"), 64*1024)
b.SetBytes(int64(len(src)))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := Replace(src, []byte("beta"), []byte("delta"), ReplaceOptions{})
if err != nil {
b.Fatal(err)
}
}
}
func FuzzReplaceMatchesStandardLibrary(f *testing.F) {
f.Add([]byte("one two one"), []byte("one"), []byte("1"), uint8(0))
f.Add([]byte("aaaa"), []byte("aa"), []byte("b"), uint8(1))
f.Add([]byte{0, 1, 0, 1}, []byte{0, 1}, []byte{2}, uint8(7))
f.Fuzz(func(t *testing.T, src, old, replacement []byte, rawLimit uint8) {
if len(old) == 0 {
t.Skip()
}
limit := int(rawLimit)
result, err := Replace(src, old, replacement, ReplaceOptions{Limit: limit})
if err != nil {
t.Fatal(err)
}
standardLimit := limit
if standardLimit == 0 {
standardLimit = -1
}
want := bytes.Replace(src, old, replacement, standardLimit)
if !bytes.Equal(result.Data, want) {
t.Fatalf("result=%q, want=%q", result.Data, want)
}
})
}