MCPcopy
hub / github.com/spf13/pflag / TestEverything

Function TestEverything

flag_test.go:42–101  ·  flag_test.go::TestEverything
(t *testing.T)

Source from the content-addressed store, hash-verified

40}
41
42func TestEverything(t *testing.T) {
43 m := make(map[string]*Flag)
44 desired := "0"
45 visitor := func(f *Flag) {
46 if len(f.Name) > 5 && f.Name[0:5] == "test_" {
47 m[f.Name] = f
48 ok := false
49 switch {
50 case f.Value.String() == desired:
51 ok = true
52 case f.Name == "test_bool" && f.Value.String() == boolString(desired):
53 ok = true
54 case f.Name == "test_duration" && f.Value.String() == desired+"s":
55 ok = true
56 }
57 if !ok {
58 t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
59 }
60 }
61 }
62 VisitAll(visitor)
63 if len(m) != 9 {
64 t.Error("VisitAll misses some flags")
65 for k, v := range m {
66 t.Log(k, *v)
67 }
68 }
69 m = make(map[string]*Flag)
70 Visit(visitor)
71 if len(m) != 0 {
72 t.Errorf("Visit sees unset flags")
73 for k, v := range m {
74 t.Log(k, *v)
75 }
76 }
77 // Now set all flags
78 Set("test_bool", "true")
79 Set("test_int", "1")
80 Set("test_int64", "1")
81 Set("test_uint", "1")
82 Set("test_uint64", "1")
83 Set("test_string", "1")
84 Set("test_float64", "1")
85 Set("test_duration", "1s")
86 Set("test_optional_int", "1")
87 desired = "1"
88 Visit(visitor)
89 if len(m) != 9 {
90 t.Error("Visit fails after set")
91 for k, v := range m {
92 t.Log(k, *v)
93 }
94 }
95 // Now test they're visited in sort order.
96 var flagNames []string
97 Visit(func(f *Flag) { flagNames = append(flagNames, f.Name) })
98 if !sort.StringsAreSorted(flagNames) {
99 t.Errorf("flag names not sorted: %v", flagNames)

Callers

nothing calls this directly

Calls 6

boolStringFunction · 0.85
VisitAllFunction · 0.85
VisitFunction · 0.85
SetFunction · 0.85
StringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected