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
| package main
import (
"flag"
"testing")
func TestAll(t *testing.T) {
tests := map[string]struct{
args []string
wantsDebug bool
wantsHost string
}{
"args set 1" : {
args: []string{"prog", "--debug", "--host", "google.com"},
wantsDebug: true,
wantsHost: "google.com",
},
"args set 2" : {
args: []string{"prog", "--host", "github.com"},
wantsDebug: false,
wantsHost: "github.com",
},
}
for name, data := range tests {
t.Run(name, func(t *testing.T) {
var flagDebug bool
var flagHost string
flags := flag.NewFlagSet(data.args[0], flag.ContinueOnError)
flags.BoolVar(&flagDebug, "debug", false, "enable debug logging")
flags.StringVar(&flagHost, "host", "example.com", "set the host to connect to")
err := flags.Parse(data.args[1:])
if err != nil {
t.Fatalf("failed to parse args: %s", err.Error())
}
if flagDebug != data.wantsDebug {
t.Fatalf("didn't get expected debug value")
}
if flagHost != data.wantsHost{
t.Fatalf("didn't get expected host value")
}
})
}
}
|