MCPcopy
hub / github.com/urfave/cli / TestFlagDefaultValidation

Function TestFlagDefaultValidation

flag_validation_test.go:10–37  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

8)
9
10func TestFlagDefaultValidation(t *testing.T) {
11 cmd := &Command{
12 Name: "foo",
13 Flags: []Flag{
14 &Int64Flag{
15 Name: "if",
16 Value: 2, // this value should fail validation
17 Validator: func(i int64) error {
18 if (i >= 3 && i <= 10) || (i >= 20 && i <= 24) {
19 return nil
20 }
21 return fmt.Errorf("Value %d not in range [3,10] or [20,24]", i)
22 },
23 ValidateDefaults: true,
24 },
25 },
26 }
27
28 r := require.New(t)
29
30 // this is a simple call to test PreParse failure before
31 // parsing has been done
32 r.Error(cmd.Set("if", "11"))
33
34 // Default value of flag is 2 which should fail validation
35 err := cmd.Run(buildTestContext(t), []string{"foo", "--if", "5"})
36 r.Error(err)
37}
38
39func TestBoolInverseFlagDefaultValidation(t *testing.T) {
40 cmd := &Command{

Callers

nothing calls this directly

Calls 4

SetMethod · 0.95
RunMethod · 0.95
buildTestContextFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected