-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathparse_args_test.go
More file actions
70 lines (65 loc) · 1.57 KB
/
parse_args_test.go
File metadata and controls
70 lines (65 loc) · 1.57 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
package main
import (
"bytes"
"errors"
"testing"
)
func TestParseArgs(t *testing.T) {
tests := []struct {
args []string
config
output string
err error
}{
{
args: []string{"-h"},
output: `
A greeter application which prints the name you entered a specified number of times.
Usage of greeter: <options> [name]
Options:
-n int
Number of times to greet
`,
err: errors.New("flag: help requested"),
config: config{numTimes: 0},
},
{
args: []string{"-n", "10"},
err: nil,
config: config{numTimes: 10},
},
{
args: []string{"-n", "abc"},
err: errors.New("invalid value \"abc\" for flag -n: parse error"),
config: config{numTimes: 0},
},
{
args: []string{"-n", "1", "John Doe"},
err: nil,
config: config{numTimes: 1, name: "John Doe"},
},
{
args: []string{"-n", "1", "John", "Doe"},
err: errors.New("More than one positional argument specified"),
config: config{numTimes: 1},
},
}
byteBuf := new(bytes.Buffer)
for _, tc := range tests {
c, err := parseArgs(byteBuf, tc.args)
if tc.err == nil && err != nil {
t.Fatalf("Expected nil error, got: %v\n", err)
}
if tc.err != nil && err.Error() != tc.err.Error() {
t.Fatalf("Expected error to be: %v, got: %v\n", tc.err, err)
}
if c.numTimes != tc.numTimes {
t.Errorf("Expected numTimes to be: %v, got: %v\n", tc.numTimes, c.numTimes)
}
gotMsg := byteBuf.String()
if len(tc.output) != 0 && gotMsg != tc.output {
t.Errorf("Expected stdout message to be: %#v, Got: %#v\n", tc.output, gotMsg)
}
byteBuf.Reset()
}
}