github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/validation/int_test.go (about) 1 package validation 2 3 import ( 4 "regexp" 5 "testing" 6 ) 7 8 func TestValidationIntBetween(t *testing.T) { 9 runTestCases(t, []testCase{ 10 { 11 val: 1, 12 f: IntBetween(1, 1), 13 }, 14 { 15 val: 1, 16 f: IntBetween(0, 2), 17 }, 18 { 19 val: 1, 20 f: IntBetween(2, 3), 21 expectedErr: regexp.MustCompile("expected [\\w]+ to be in the range \\(2 - 3\\), got 1"), 22 }, 23 { 24 val: "1", 25 f: IntBetween(2, 3), 26 expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), 27 }, 28 }) 29 } 30 31 func TestValidationIntAtLeast(t *testing.T) { 32 runTestCases(t, []testCase{ 33 { 34 val: 1, 35 f: IntAtLeast(1), 36 }, 37 { 38 val: 1, 39 f: IntAtLeast(0), 40 }, 41 { 42 val: 1, 43 f: IntAtLeast(2), 44 expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\), got 1"), 45 }, 46 { 47 val: "1", 48 f: IntAtLeast(2), 49 expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), 50 }, 51 }) 52 } 53 54 func TestValidationIntAtMost(t *testing.T) { 55 runTestCases(t, []testCase{ 56 { 57 val: 1, 58 f: IntAtMost(1), 59 }, 60 { 61 val: 1, 62 f: IntAtMost(2), 63 }, 64 { 65 val: 1, 66 f: IntAtMost(0), 67 expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(0\\), got 1"), 68 }, 69 { 70 val: "1", 71 f: IntAtMost(0), 72 expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), 73 }, 74 }) 75 } 76 77 func TestValidationIntDivisibleBy(t *testing.T) { 78 cases := map[string]struct { 79 Value interface{} 80 Divisor int 81 Error bool 82 }{ 83 "NotInt": { 84 Value: "words", 85 Divisor: 2, 86 Error: true, 87 }, 88 "NotDivisible": { 89 Value: 15, 90 Divisor: 7, 91 Error: true, 92 }, 93 "Divisible": { 94 Value: 14, 95 Divisor: 7, 96 Error: false, 97 }, 98 } 99 100 for tn, tc := range cases { 101 t.Run(tn, func(t *testing.T) { 102 _, errors := IntDivisibleBy(tc.Divisor)(tc.Value, tn) 103 104 if len(errors) > 0 && !tc.Error { 105 t.Errorf("IntDivisibleBy(%v) produced an unexpected error for %v", tc.Divisor, tc.Value) 106 } else if len(errors) == 0 && tc.Error { 107 t.Errorf("IntDivisibleBy(%v) did not error for %v", tc.Divisor, tc.Value) 108 } 109 }) 110 } 111 } 112 113 func TestValidationIntInSlice(t *testing.T) { 114 runTestCases(t, []testCase{ 115 { 116 val: 42, 117 f: IntInSlice([]int{1, 42}), 118 }, 119 { 120 val: 42, 121 f: IntInSlice([]int{10, 20}), 122 expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[10 20\\], got 42"), 123 }, 124 { 125 val: "InvalidValue", 126 f: IntInSlice([]int{10, 20}), 127 expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), 128 }, 129 }) 130 } 131 132 func TestValidationIntNotInSlice(t *testing.T) { 133 cases := map[string]struct { 134 Value interface{} 135 Slice []int 136 Error bool 137 }{ 138 "NotInt": { 139 Value: "words", 140 Slice: []int{7, 77}, 141 Error: true, 142 }, 143 "NotInSlice": { 144 Value: 1, 145 Slice: []int{7, 77}, 146 Error: false, 147 }, 148 "InSlice": { 149 Value: 7, 150 Slice: []int{7, 77}, 151 Error: true, 152 }, 153 "InSliceOfOne": { 154 Value: 7, 155 Slice: []int{7}, 156 Error: true, 157 }, 158 "NotInSliceOfOne": { 159 Value: 1, 160 Slice: []int{7}, 161 Error: false, 162 }, 163 } 164 165 for tn, tc := range cases { 166 t.Run(tn, func(t *testing.T) { 167 _, errors := IntNotInSlice(tc.Slice)(tc.Value, tn) 168 169 if len(errors) > 0 && !tc.Error { 170 t.Errorf("IntNotInSlice(%v) produced an unexpected error for %v", tc.Slice, tc.Value) 171 } else if len(errors) == 0 && tc.Error { 172 t.Errorf("IntNotInSlice(%v) did not error for %v", tc.Slice, tc.Value) 173 } 174 }) 175 } 176 }