forked from agnivade/wasmbrowsertest
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
217 lines (186 loc) · 5.93 KB
/
parse_test.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"flag"
"fmt"
"os"
"strings"
"testing"
)
// The forms of these tests for the gentleParse function
// are generally that the args are presented to the testParse function
// and the expected results are presented as separate lines to the
// Expect result.
func TestParse(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
// Empty in, empty out.
testParse().Expect(t,
`cpuProfile: ""`,
`passon : []`,
)
})
t.Run("Other", func(t *testing.T) {
// Empty in, empty out, with an extra `other` variable that has a default.
testParseOther().Expect(t,
`cpuProfile: ""`,
`other : "default-other-value"`,
`passon : []`,
)
// Test parsing of a custom flag with a custom value
testParseOther("-test.v", "-test.cpuprofile", "cpu1.out", "-other=another").Expect(t,
`cpuProfile: "cpu1.out"`,
`other : "another"`,
`passon : ["-test.v"]`,
)
})
t.Run("Verbose", func(t *testing.T) {
// One unrecognized in, same out.
testParse("-test.v").Expect(t,
`cpuProfile: ""`,
`passon : ["-test.v"]`,
)
})
t.Run("CPU1", func(t *testing.T) {
// One unrecognized followed by ours.
testParse("-test.v", "-test.cpuprofile", "cpu1.out").Expect(t,
`cpuProfile: "cpu1.out"`,
`passon : ["-test.v"]`,
)
})
t.Run("CPU2", func(t *testing.T) {
// Ours followed by one unrecognized.
testParse("-test.cpuprofile", "cpu2.out", "-test.v").Expect(t,
`cpuProfile: "cpu2.out"`,
`passon : ["-test.v"]`,
)
})
t.Run("CPU3", func(t *testing.T) {
// Ours followed by one unrecognized that uses "=".
testParse("-test.cpuprofile", "cpu3.out", "-test.v=true").Expect(t,
`cpuProfile: "cpu3.out"`,
`passon : ["-test.v=true"]`,
)
})
t.Run("EqualCPU4", func(t *testing.T) {
// Swapping order from Cpu3 test, the unrecognized first, followed by ours.
testParse("-test.v=true", "-test.cpuprofile", "cpu4.out").Expect(t,
`cpuProfile: "cpu4.out"`,
`passon : ["-test.v=true"]`,
)
})
t.Run("ExtraBool1", func(t *testing.T) {
// Ours followed by two unrecognized.
testParse("-test.cpuprofile", "cpu.out", "-test.v", "-bool").Expect(t,
`cpuProfile: "cpu.out"`,
`passon : ["-test.v" "-bool"]`,
)
})
t.Run("ExtraBool2", func(t *testing.T) {
// Ours between two unrecognized.
testParse("-bool", "-test.cpuprofile", "cpu.out", "-test.v").Expect(t,
`cpuProfile: "cpu.out"`,
`passon : ["-bool" "-test.v"]`,
)
})
t.Run("ExtraStringNoDDash1", func(t *testing.T) {
// Ours pulled out from front.
testParse("-test.cpuprofile", "cpu.out", "-test.v", "-bool", "-string", "last").Expect(t,
`cpuProfile: "cpu.out"`,
`passon : ["-test.v" "-bool" "-string" "last"]`,
)
})
t.Run("ExtraStringNoDDash2", func(t *testing.T) {
// Ours pulled out from middle.
testParse("-string", "first", "-test.cpuprofile", "cpu.out", "-test.v", "-bool").Expect(t,
`cpuProfile: "cpu.out"`,
`passon : ["-string" "first" "-test.v" "-bool"]`,
)
})
t.Run("DDash1ExtraString", func(t *testing.T) {
// Ours pulled out from front and the -- appears afterwards.
testParse("-test.cpuprofile", "cpu.out", "-test.v", "--", "-bool", "-string", "abc").Expect(t,
`cpuProfile: "cpu.out"`,
`passon : ["-test.v" "--" "-bool" "-string" "abc"]`,
)
})
t.Run("DDash2ExtraString", func(t *testing.T) {
// Ours pulled out from front and the -- appears afterwards.
testParse("-test.cpuprofile", "cpu.out", "--", "-test.v", "-bool", "-string", "abc").Expect(t,
`cpuProfile: "cpu.out"`,
`passon : ["--" "-test.v" "-bool" "-string" "abc"]`,
)
})
t.Run("DDash3UnprocessedProfile", func(t *testing.T) {
// Ours *not* pulled out because it appears after a --, just as "go test" would handle it.
testParse("--", "-test.cpuprofile", "cpu.other", "-test.v", "-bool", "-string", "abc").Expect(t,
`cpuProfile: ""`,
`passon : ["--" "-test.cpuprofile" "cpu.other" "-test.v" "-bool" "-string" "abc"]`,
)
})
}
type testParseGot struct {
got []string
}
func makeParseGot(lines ...string) testParseGot {
return testParseGot{got: lines}
}
func (g testParseGot) failure(expect []string, format string, args ...interface{}) string {
buf := new(strings.Builder)
fmt.Fprintf(buf, format+"\n", args...)
fmt.Fprintf(buf, " Got:\n")
for i := range g.got {
fmt.Fprintf(buf, " %s\n", g.got[i])
}
fmt.Fprintf(buf, " Expected:\n")
for i := range expect {
fmt.Fprintf(buf, " %s\n", expect[i])
}
return buf.String()
}
func (g testParseGot) Expect(t testing.TB, expect ...string) {
if len(g.got) != len(expect) {
t.Helper()
t.Errorf("%s",
g.failure(expect, "got %d lines, expected %d", len(g.got), len(expect)))
return
}
for i := range g.got {
if g.got[i] != expect[i] {
t.Helper()
t.Errorf("%s",
g.failure(expect, "at least line %d of got and expected don't match", i+1))
return
}
}
}
func testParse(args ...string) testParseGot {
var (
cpuProfile string
)
flagset := flag.NewFlagSet("binname", flag.ExitOnError)
flagset.SetOutput(os.Stdout) // For Examples to catch as output.
flagset.StringVar(&cpuProfile, "test.cpuprofile", "", "")
passon := gentleParse(flagset, args)
return makeParseGot(
fmt.Sprintf("cpuProfile: %q", cpuProfile),
fmt.Sprintf("passon : %q", passon),
)
}
// This one acts more like an example of how to perform a different type of test.
// It was perhaps useful in early stages of building unit tests but then seems
// to have gone unused except for the default, empty, case.
func testParseOther(args ...string) testParseGot {
var (
cpuProfile string
other string
)
flagset := flag.NewFlagSet("binname", flag.ExitOnError)
flagset.SetOutput(os.Stdout) // For Examples to catch as output.
flagset.StringVar(&cpuProfile, "test.cpuprofile", "", "")
flagset.StringVar(&other, "other", "default-other-value", "")
passon := gentleParse(flagset, args)
return makeParseGot(
fmt.Sprintf("cpuProfile: %q", cpuProfile),
fmt.Sprintf("other : %q", other),
fmt.Sprintf("passon : %q", passon),
)
}