TestBoolFlagTerminator tests the bool flag
(t *testing.T)
| 5175 | |
| 5176 | // TestBoolFlagTerminator tests the bool flag |
| 5177 | func TestBoolFlagTerminator(t *testing.T) { |
| 5178 | tests := []struct { |
| 5179 | name string |
| 5180 | input []string |
| 5181 | expectFlag bool |
| 5182 | expectArgs []string |
| 5183 | expectErr bool |
| 5184 | errorContain string |
| 5185 | }{ |
| 5186 | /*{ |
| 5187 | name: "bool flag with invalid non-bool value", |
| 5188 | input: []string{"test", "--flag", "x", "--", "test", "a1", "a2", "a3"}, |
| 5189 | expectErr: true, |
| 5190 | errorContain: "invalid syntax", |
| 5191 | },*/ |
| 5192 | { |
| 5193 | name: "bool flag omitted value defaults to true", |
| 5194 | input: []string{"test", "--flag", "--", "x"}, |
| 5195 | expectFlag: true, |
| 5196 | expectArgs: []string{"x"}, |
| 5197 | }, |
| 5198 | { |
| 5199 | name: "bool flag explicitly set to false", |
| 5200 | input: []string{"test", "--flag=false", "--", "x"}, |
| 5201 | expectFlag: false, |
| 5202 | expectArgs: []string{"x"}, |
| 5203 | }, |
| 5204 | { |
| 5205 | name: "bool flag defined after --", |
| 5206 | input: []string{"test", "--", "x", "--flag=true"}, |
| 5207 | expectFlag: false, |
| 5208 | expectArgs: []string{"x", "--flag=true"}, |
| 5209 | }, |
| 5210 | { |
| 5211 | name: "bool flag and without --", |
| 5212 | input: []string{"test", "--flag=true", "x"}, |
| 5213 | expectFlag: true, |
| 5214 | expectArgs: []string{"x"}, |
| 5215 | }, |
| 5216 | } |
| 5217 | |
| 5218 | for _, tc := range tests { |
| 5219 | t.Run(tc.name, func(t *testing.T) { |
| 5220 | var flagVal bool |
| 5221 | var argsVal []string |
| 5222 | |
| 5223 | // build minimal command with a BoolFlag "flag" |
| 5224 | cmd := &Command{ |
| 5225 | Name: "test", |
| 5226 | Flags: []Flag{ |
| 5227 | &BoolFlag{ |
| 5228 | Name: "flag", |
| 5229 | Usage: "a bool flag", |
| 5230 | Destination: &flagVal, |
| 5231 | }, |
| 5232 | }, |
| 5233 | Action: func(ctx context.Context, c *Command) error { |
| 5234 | argsVal = c.Args().Slice() |