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

Function TestToGoflags

golangflag_test.go:64–135  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

62}
63
64func TestToGoflags(t *testing.T) {
65 pfs := FlagSet{}
66 gfs := goflag.FlagSet{}
67 pfs.String("StringFlag", "String value", "String flag usage")
68 pfs.Int("IntFlag", 1, "Int flag usage")
69 pfs.Uint("UintFlag", 2, "Uint flag usage")
70 pfs.Int64("Int64Flag", 3, "Int64 flag usage")
71 pfs.Uint64("Uint64Flag", 4, "Uint64 flag usage")
72 pfs.Int8("Int8Flag", 5, "Int8 flag usage")
73 pfs.Float64("Float64Flag", 6.0, "Float64 flag usage")
74 pfs.Duration("DurationFlag", time.Second, "Duration flag usage")
75 pfs.Bool("BoolFlag", true, "Bool flag usage")
76 pfs.String("deprecated", "Deprecated value", "Deprecated flag usage")
77 pfs.MarkDeprecated("deprecated", "obsolete")
78
79 pfs.CopyToGoFlagSet(&gfs)
80
81 // Modify via pfs. Should be visible via gfs because both share the
82 // same values.
83 for name, value := range map[string]string{
84 "StringFlag": "Modified String value",
85 "IntFlag": "11",
86 "UintFlag": "12",
87 "Int64Flag": "13",
88 "Uint64Flag": "14",
89 "Int8Flag": "15",
90 "Float64Flag": "16.0",
91 "BoolFlag": "false",
92 } {
93 pf := pfs.Lookup(name)
94 if pf == nil {
95 t.Errorf("%s: not found in pflag flag set", name)
96 continue
97 }
98 if err := pf.Value.Set(value); err != nil {
99 t.Errorf("error setting %s = %s: %v", name, value, err)
100 }
101 }
102
103 // Check that all flags were added and share the same value.
104 pfs.VisitAll(func(pf *Flag) {
105 gf := gfs.Lookup(pf.Name)
106 if gf == nil {
107 t.Errorf("%s: not found in Go flag set", pf.Name)
108 return
109 }
110 if gf.Value.String() != pf.Value.String() {
111 t.Errorf("%s: expected value %v from Go flag set, got %v",
112 pf.Name, pf.Value, gf.Value)
113 return
114 }
115 })
116
117 // Check for unexpected additional flags.
118 gfs.VisitAll(func(gf *goflag.Flag) {
119 pf := gfs.Lookup(gf.Name)
120 if pf == nil {
121 t.Errorf("%s: not found in pflag flag set", gf.Name)

Callers

nothing calls this directly

Calls 15

StringMethod · 0.95
IntMethod · 0.95
UintMethod · 0.95
Int64Method · 0.95
Uint64Method · 0.95
Int8Method · 0.95
Float64Method · 0.95
DurationMethod · 0.95
BoolMethod · 0.95
MarkDeprecatedMethod · 0.95
CopyToGoFlagSetMethod · 0.95
LookupMethod · 0.95

Tested by

no test coverage detected