|
1 | 1 | package pflag |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | | - "flag" |
6 | | - "io" |
7 | 4 | "strings" |
8 | 5 | "testing" |
9 | 6 | ) |
@@ -48,104 +45,6 @@ func TestBoolFuncP(t *testing.T) { |
48 | 45 | } |
49 | 46 | } |
50 | 47 |
|
51 | | -func TestBoolFuncCompat(t *testing.T) { |
52 | | - // compare behavior with the stdlib 'flag' package |
53 | | - type BoolFuncFlagSet interface { |
54 | | - BoolFunc(name string, usage string, fn func(string) error) |
55 | | - Parse([]string) error |
56 | | - } |
57 | | - |
58 | | - unitTestErr := errors.New("unit test error") |
59 | | - runCase := func(f BoolFuncFlagSet, name string, args []string) (values []string, err error) { |
60 | | - fn := func(s string) error { |
61 | | - values = append(values, s) |
62 | | - if s == "err" { |
63 | | - return unitTestErr |
64 | | - } |
65 | | - return nil |
66 | | - } |
67 | | - f.BoolFunc(name, "Callback function", fn) |
68 | | - |
69 | | - err = f.Parse(args) |
70 | | - return values, err |
71 | | - } |
72 | | - |
73 | | - t.Run("regular parsing", func(t *testing.T) { |
74 | | - flagName := "bflag" |
75 | | - args := []string{"--bflag", "--bflag=false", "--bflag=1", "--bflag=bar", "--bflag="} |
76 | | - |
77 | | - // It turns out that, even though the function is called "BoolFunc", |
78 | | - // the standard flag package does not try to parse the value assigned to |
79 | | - // that cli flag as a boolean. The string provided on the command line is |
80 | | - // passed as is to the callback. |
81 | | - // e.g: with "--bflag=not_a_bool" on the command line, the FlagSet does not |
82 | | - // generate an error stating "invalid boolean value", and `fn` will be called |
83 | | - // with "not_a_bool" as an argument. |
84 | | - |
85 | | - stdFSet := flag.NewFlagSet("std test", flag.ContinueOnError) |
86 | | - stdValues, err := runCase(stdFSet, flagName, args) |
87 | | - if err != nil { |
88 | | - t.Fatalf("std flag: expected no error, got %v", err) |
89 | | - } |
90 | | - expected := []string{"true", "false", "1", "bar", ""} |
91 | | - if !cmpLists(expected, stdValues) { |
92 | | - t.Fatalf("std flag: expected %v, got %v", expected, stdValues) |
93 | | - } |
94 | | - |
95 | | - fset := NewFlagSet("pflag test", ContinueOnError) |
96 | | - pflagValues, err := runCase(fset, flagName, args) |
97 | | - if err != nil { |
98 | | - t.Fatalf("pflag: expected no error, got %v", err) |
99 | | - } |
100 | | - if !cmpLists(stdValues, pflagValues) { |
101 | | - t.Fatalf("pflag: expected %v, got %v", stdValues, pflagValues) |
102 | | - } |
103 | | - }) |
104 | | - |
105 | | - t.Run("error triggered by callback", func(t *testing.T) { |
106 | | - flagName := "bflag" |
107 | | - args := []string{"--bflag", "--bflag=err", "--bflag=after"} |
108 | | - |
109 | | - // test behavior of standard flag.Fset with an error triggered by the callback: |
110 | | - // (note: as can be seen in 'runCase()', if the callback sees "err" as a value |
111 | | - // for the bool flag, it will return an error) |
112 | | - stdFSet := flag.NewFlagSet("std test", flag.ContinueOnError) |
113 | | - stdFSet.SetOutput(io.Discard) // suppress output |
114 | | - |
115 | | - // run test case with standard flag.Fset |
116 | | - stdValues, err := runCase(stdFSet, flagName, args) |
117 | | - |
118 | | - // double check the standard behavior: |
119 | | - // - .Parse() should return an error, which contains the error message |
120 | | - if err == nil { |
121 | | - t.Fatalf("std flag: expected an error triggered by callback, got no error instead") |
122 | | - } |
123 | | - if !strings.HasSuffix(err.Error(), unitTestErr.Error()) { |
124 | | - t.Fatalf("std flag: expected unittest error, got unexpected error value: %T %v", err, err) |
125 | | - } |
126 | | - // - the function should have been called twice, with the first two values, |
127 | | - // the final "=after" should not be recorded |
128 | | - expected := []string{"true", "err"} |
129 | | - if !cmpLists(expected, stdValues) { |
130 | | - t.Fatalf("std flag: expected %v, got %v", expected, stdValues) |
131 | | - } |
132 | | - |
133 | | - // now run the test case on a pflag FlagSet: |
134 | | - fset := NewFlagSet("pflag test", ContinueOnError) |
135 | | - pflagValues, err := runCase(fset, flagName, args) |
136 | | - |
137 | | - // check that there is a similar error (note: pflag will _wrap_ the error, while the stdlib |
138 | | - // currently keeps the original message but creates a flat errors.Error) |
139 | | - if !errors.Is(err, unitTestErr) { |
140 | | - t.Fatalf("pflag: got unexpected error value: %T %v", err, err) |
141 | | - } |
142 | | - // the callback should be called the same number of times, with the same values: |
143 | | - if !cmpLists(stdValues, pflagValues) { |
144 | | - t.Fatalf("pflag: expected %v, got %v", stdValues, pflagValues) |
145 | | - } |
146 | | - }) |
147 | | -} |
148 | | - |
149 | 48 | func TestBoolFuncUsage(t *testing.T) { |
150 | 49 | t.Run("regular func flag", func(t *testing.T) { |
151 | 50 | // regular boolfunc flag: |
|
0 commit comments