-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps_test.go
53 lines (48 loc) · 1.34 KB
/
maps_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
package flagx_test
import (
"flag"
"strconv"
"testing"
"github.com/dolmen-go/flagx"
)
func checkIntMap(tester *varTester) {
tester.CheckParse([]string{}, map[string]int{})
tester.CheckParse([]string{"-kv", "a=0"}, map[string]int{"a": 0})
tester.CheckParse([]string{"-kv", "a=0", "-kv", "b=1"}, map[string]int{"a": 0, "b": 1})
}
func checkStringMap(tester *varTester) {
tester.CheckParse([]string{}, map[string]string{})
tester.CheckParse([]string{"-kv", "a=b"}, map[string]string{"a": "b"})
tester.CheckParse([]string{"-kv", "a=b", "-kv", "b=a"}, map[string]string{"a": "b", "b": "a"})
}
func TestMap(t *testing.T) {
checkIntMap(&varTester{
t: t,
flagName: "kv",
buildVar: func() (flag.Getter, interface{}) {
m := make(map[string]int)
return flagx.Map(m, func(s string) (interface{}, error) {
n, err := strconv.ParseInt(s, 0, 0)
if err != nil {
return nil, nil
}
return int(n), nil
}), m
}})
checkStringMap(&varTester{
t: t,
flagName: "kv",
buildVar: func() (flag.Getter, interface{}) {
m := make(map[string]string)
return flagx.Map(m, func(s string) (interface{}, error) {
return s, nil
}), m
}})
checkStringMap(&varTester{
t: t,
flagName: "kv",
buildVar: func() (flag.Getter, interface{}) {
m := make(map[string]string)
return flagx.Map(m, nil), m
}})
}