forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregexp_writer_test.go
More file actions
160 lines (156 loc) · 3.39 KB
/
regexp_writer_test.go
File metadata and controls
160 lines (156 loc) · 3.39 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package create
import (
"bytes"
"regexp"
"testing"
"github.com/MakeNowJust/heredoc"
"github.com/stretchr/testify/assert"
)
func Test_Write(t *testing.T) {
type input struct {
in []string
re *regexp.Regexp
repl string
}
type output struct {
wantsErr bool
out string
length int
}
tests := []struct {
name string
input input
output output
}{
{
name: "single line input",
input: input{
in: []string{"some input line that has wrong information"},
re: regexp.MustCompile("wrong"),
repl: "right",
},
output: output{
wantsErr: false,
out: "some input line that has right information",
length: 42,
},
},
{
name: "multiple line input",
input: input{
in: []string{"multiple lines\nin this\ninput lines"},
re: regexp.MustCompile("lines"),
repl: "tests",
},
output: output{
wantsErr: false,
out: "multiple tests\nin this\ninput tests",
length: 34,
},
},
{
name: "no matches",
input: input{
in: []string{"this line has no matches"},
re: regexp.MustCompile("wrong"),
repl: "right",
},
output: output{
wantsErr: false,
out: "this line has no matches",
length: 24,
},
},
{
name: "no output",
input: input{
in: []string{"remove this whole line"},
re: regexp.MustCompile("^remove.*$"),
repl: "",
},
output: output{
wantsErr: false,
out: "",
length: 22,
},
},
{
name: "no input",
input: input{
in: []string{""},
re: regexp.MustCompile("remove"),
repl: "",
},
output: output{
wantsErr: false,
out: "",
length: 0,
},
},
{
name: "multiple lines removed",
input: input{
in: []string{"beginning line\nremove this whole line\nremove this one also\nnot this one"},
re: regexp.MustCompile("(?s)^remove.*$"),
repl: "",
},
output: output{
wantsErr: false,
out: "beginning line\nnot this one",
length: 71,
},
},
{
name: "removes remote from git push output",
input: input{
in: []string{heredoc.Doc(`
output: some information
remote:
remote: Create a pull request for 'regex' on GitHub by visiting:
remote: https://github.com/owner/repo/pull/new/regex
remote:
output: more information
`)},
re: regexp.MustCompile("^remote: (Create a pull request.*by visiting|[[:space:]]*https://.*/pull/new/).*\n?$"),
repl: "",
},
output: output{
wantsErr: false,
out: "output: some information\nremote:\nremote:\noutput: more information\n",
length: 189,
},
},
{
name: "multiple writes",
input: input{
in: []string{"first write\n", "second write ", "third write"},
re: regexp.MustCompile("write"),
repl: "read",
},
output: output{
wantsErr: false,
out: "first read\nsecond read third read",
length: 36,
},
},
}
for _, tt := range tests {
out := &bytes.Buffer{}
writer := NewRegexpWriter(out, tt.input.re, tt.input.repl)
t.Run(tt.name, func(t *testing.T) {
length := 0
for _, in := range tt.input.in {
l, err := writer.Write([]byte(in))
length = length + l
if tt.output.wantsErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
}
writer.Flush()
assert.Equal(t, tt.output.out, out.String())
assert.Equal(t, tt.output.length, length)
})
}
}