| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/require" |
| 8 | ) |
| 9 | |
| 10 | func 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 | |
| 39 | func TestBoolInverseFlagDefaultValidation(t *testing.T) { |
| 40 | cmd := &Command{ |
| 41 | Name: "foo", |
| 42 | Flags: []Flag{ |
| 43 | &BoolWithInverseFlag{ |
| 44 | Name: "bif", |
| 45 | Value: true, // this value should fail validation |
| 46 | Validator: func(i bool) error { |
| 47 | if i { |
| 48 | return fmt.Errorf("invalid value") |
| 49 | } |
| 50 | return nil |
| 51 | }, |
| 52 | ValidateDefaults: true, |
| 53 | }, |
| 54 | }, |
| 55 | } |
| 56 | |
| 57 | r := require.New(t) |
| 58 | |
| 59 | // Default value of flag is 2 which should fail validation |
| 60 | err := cmd.Run(buildTestContext(t), []string{"foo", "--bif"}) |
| 61 | r.Error(err) |
| 62 | } |
| 63 | |
| 64 | func TestFlagValidation(t *testing.T) { |
| 65 | r := require.New(t) |
| 66 | |
| 67 | testCases := []struct { |
| 68 | name string |
| 69 | arg string |
| 70 | errExpected bool |
| 71 | }{ |
| 72 | { |
| 73 | name: "first range less than min", |
| 74 | arg: "2", |
| 75 | errExpected: true, |
| 76 | }, |
| 77 | { |
| 78 | name: "first range min", |
| 79 | arg: "3", |
| 80 | }, |
| 81 | { |
| 82 | name: "first range mid", |
| 83 | arg: "7", |
| 84 | }, |
| 85 | { |
| 86 | name: "first range max", |
| 87 | arg: "10", |
| 88 | }, |
| 89 | { |
| 90 | name: "first range greater than max", |
| 91 | arg: "15", |
| 92 | errExpected: true, |
| 93 | }, |
| 94 | { |
| 95 | name: "second range less than min", |
| 96 | arg: "19", |
| 97 | errExpected: true, |
| 98 | }, |
| 99 | { |
| 100 | name: "second range min", |
| 101 | arg: "20", |
| 102 | }, |
| 103 | { |
| 104 | name: "second range mid", |
| 105 | arg: "21", |
| 106 | }, |
| 107 | { |
| 108 | name: "second range max", |
| 109 | arg: "24", |
| 110 | }, |
| 111 | { |
| 112 | name: "second range greater than max", |
| 113 | arg: "27", |
| 114 | errExpected: true, |
| 115 | }, |
| 116 | } |
| 117 | |
| 118 | for _, testCase := range testCases { |
| 119 | cmd := &Command{ |
| 120 | Name: "foo", |
| 121 | Flags: []Flag{ |
| 122 | &Int64Flag{ |
| 123 | Name: "it", |
| 124 | Value: 5, // note that this value should pass validation |
| 125 | Validator: func(i int64) error { |
| 126 | if (i >= 3 && i <= 10) || (i >= 20 && i <= 24) { |
| 127 | return nil |
| 128 | } |
| 129 | return fmt.Errorf("Value %d not in range [3,10]U[20,24]", i) |
| 130 | }, |
| 131 | }, |
| 132 | }, |
| 133 | } |
| 134 | |
| 135 | err := cmd.Run(buildTestContext(t), []string{"foo", "--it", testCase.arg}) |
| 136 | if !testCase.errExpected { |
| 137 | r.NoError(err) |
| 138 | } else { |
| 139 | r.Error(err) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestBoolInverseFlagValidation(t *testing.T) { |
| 145 | r := require.New(t) |
| 146 | |
| 147 | cmd := &Command{ |
| 148 | Name: "foo", |
| 149 | Flags: []Flag{ |
| 150 | &BoolWithInverseFlag{ |
| 151 | Name: "it", |
| 152 | Validator: func(b bool) error { |
| 153 | if b { |
| 154 | return nil |
| 155 | } |
| 156 | return fmt.Errorf("not valid") |
| 157 | }, |
| 158 | }, |
| 159 | }, |
| 160 | } |
| 161 | |
| 162 | err := cmd.Run(buildTestContext(t), []string{"foo", "--it=false"}) |
| 163 | r.Error(err) |
| 164 | } |