|
| 1 | +package template |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestMustBeOneOf(t *testing.T) { |
| 9 | + testCases := []struct { |
| 10 | + name string |
| 11 | + allowedValues []any |
| 12 | + input string |
| 13 | + wantValue string |
| 14 | + wantErr bool |
| 15 | + errSnippet string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "valid input value", |
| 19 | + allowedValues: []any{"a", "b", "c"}, |
| 20 | + input: "a", |
| 21 | + wantValue: "a", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "invalid input value", |
| 25 | + allowedValues: []any{"a", "b", "c"}, |
| 26 | + input: "d", |
| 27 | + wantErr: true, |
| 28 | + errSnippet: `value must be one of ["a" "b" "c"]`, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "non-string allowed value", |
| 32 | + allowedValues: []any{"a", 1, "c"}, |
| 33 | + input: "a", |
| 34 | + wantErr: true, |
| 35 | + errSnippet: "is not a string", |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tc := range testCases { |
| 40 | + t.Run(tc.name, func(t *testing.T) { |
| 41 | + got, err := mustBeOneOf(tc.allowedValues, tc.input) |
| 42 | + |
| 43 | + if tc.wantErr { |
| 44 | + if err == nil { |
| 45 | + t.Fatalf("mustBeOneOf(%v, %q) expected an error; got nil", tc.allowedValues, tc.input) |
| 46 | + } |
| 47 | + if got != "" { |
| 48 | + t.Fatalf("mustBeOneOf(%v, %q) returned unexpected value on error: %q", tc.allowedValues, tc.input, got) |
| 49 | + } |
| 50 | + if tc.errSnippet != "" && !strings.Contains(err.Error(), tc.errSnippet) { |
| 51 | + t.Fatalf("mustBeOneOf(%v, %q) error %q does not contain %q", tc.allowedValues, tc.input, err.Error(), tc.errSnippet) |
| 52 | + } |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("mustBeOneOf(%v, %q) returned unexpected error: %v", tc.allowedValues, tc.input, err) |
| 58 | + } |
| 59 | + if got != tc.wantValue { |
| 60 | + t.Fatalf("mustBeOneOf(%v, %q) returned %q; want %q", tc.allowedValues, tc.input, got, tc.wantValue) |
| 61 | + } |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestMustBeInt(t *testing.T) { |
| 67 | + testCases := []struct { |
| 68 | + name string |
| 69 | + input string |
| 70 | + wantValue string |
| 71 | + wantErr bool |
| 72 | + errSnippet string |
| 73 | + }{ |
| 74 | + { |
| 75 | + name: "valid zero value", |
| 76 | + input: "0", |
| 77 | + wantValue: "0", |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "valid positive value", |
| 81 | + input: "161", |
| 82 | + wantValue: "161", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "valid negative value", |
| 86 | + input: "-42", |
| 87 | + wantValue: "-42", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "empty value is rejected", |
| 91 | + input: "", |
| 92 | + wantErr: true, |
| 93 | + errSnippet: "empty value", |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "non integer value is rejected", |
| 97 | + input: "abc", |
| 98 | + wantErr: true, |
| 99 | + errSnippet: "must be an integer", |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tc := range testCases { |
| 104 | + t.Run(tc.name, func(t *testing.T) { |
| 105 | + got, err := mustBeInt(tc.input) |
| 106 | + |
| 107 | + if tc.wantErr { |
| 108 | + if err == nil { |
| 109 | + t.Fatalf("mustBeInt(%q) expected an error; got nil", tc.input) |
| 110 | + } |
| 111 | + if got != "" { |
| 112 | + t.Fatalf("mustBeInt(%q) returned unexpected value on error: %q", tc.input, got) |
| 113 | + } |
| 114 | + if tc.errSnippet != "" && !strings.Contains(err.Error(), tc.errSnippet) { |
| 115 | + t.Fatalf("mustBeInt(%q) error %q does not contain %q", tc.input, err.Error(), tc.errSnippet) |
| 116 | + } |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("mustBeInt(%q) returned unexpected error: %v", tc.input, err) |
| 122 | + } |
| 123 | + if got != tc.wantValue { |
| 124 | + t.Fatalf("mustBeInt(%q) returned %q; want %q", tc.input, got, tc.wantValue) |
| 125 | + } |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func TestMustBeIntInRange(t *testing.T) { |
| 131 | + testCases := []struct { |
| 132 | + name string |
| 133 | + min int |
| 134 | + max int |
| 135 | + input string |
| 136 | + wantValue string |
| 137 | + wantErr bool |
| 138 | + errSnippet string |
| 139 | + }{ |
| 140 | + { |
| 141 | + name: "value in range", |
| 142 | + min: -3, |
| 143 | + max: 3, |
| 144 | + input: "2", |
| 145 | + wantValue: "2", |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "value at lower boundary", |
| 149 | + min: -3, |
| 150 | + max: 3, |
| 151 | + input: "-3", |
| 152 | + wantValue: "-3", |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "value at upper boundary", |
| 156 | + min: -3, |
| 157 | + max: 3, |
| 158 | + input: "3", |
| 159 | + wantValue: "3", |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "invalid allowed range", |
| 163 | + min: 5, |
| 164 | + max: -10, |
| 165 | + input: "3", |
| 166 | + wantErr: true, |
| 167 | + errSnippet: "invalid allowed range", |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "empty value is rejected", |
| 171 | + min: -3, |
| 172 | + max: 3, |
| 173 | + input: "", |
| 174 | + wantErr: true, |
| 175 | + errSnippet: "empty value", |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "non integer value is rejected", |
| 179 | + min: -3, |
| 180 | + max: 3, |
| 181 | + input: "abc", |
| 182 | + wantErr: true, |
| 183 | + errSnippet: "must be an integer", |
| 184 | + }, |
| 185 | + { |
| 186 | + name: "value below minimum is rejected", |
| 187 | + min: -3, |
| 188 | + max: 3, |
| 189 | + input: "-4", |
| 190 | + wantErr: true, |
| 191 | + errSnippet: "between -3 and 3", |
| 192 | + }, |
| 193 | + { |
| 194 | + name: "value above maximum is rejected", |
| 195 | + min: -3, |
| 196 | + max: 3, |
| 197 | + input: "4", |
| 198 | + wantErr: true, |
| 199 | + errSnippet: "between -3 and 3", |
| 200 | + }, |
| 201 | + } |
| 202 | + |
| 203 | + for _, tc := range testCases { |
| 204 | + t.Run(tc.name, func(t *testing.T) { |
| 205 | + got, err := mustBeIntInRange(tc.min, tc.max, tc.input) |
| 206 | + |
| 207 | + if tc.wantErr { |
| 208 | + if err == nil { |
| 209 | + t.Fatalf("mustBeIntInRange(%d, %d, %q) expected an error; got nil", tc.min, tc.max, tc.input) |
| 210 | + } |
| 211 | + if got != "" { |
| 212 | + t.Fatalf("mustBeIntInRange(%d, %d, %q) returned unexpected value on error: %q", tc.min, tc.max, tc.input, got) |
| 213 | + } |
| 214 | + if tc.errSnippet != "" && !strings.Contains(err.Error(), tc.errSnippet) { |
| 215 | + t.Fatalf( |
| 216 | + "mustBeIntInRange(%d, %d, %q) error %q does not contain %q", |
| 217 | + tc.min, |
| 218 | + tc.max, |
| 219 | + tc.input, |
| 220 | + err.Error(), |
| 221 | + tc.errSnippet, |
| 222 | + ) |
| 223 | + } |
| 224 | + return |
| 225 | + } |
| 226 | + |
| 227 | + if err != nil { |
| 228 | + t.Fatalf("mustBeIntInRange(%d, %d, %q) returned unexpected error: %v", tc.min, tc.max, tc.input, err) |
| 229 | + } |
| 230 | + if got != tc.wantValue { |
| 231 | + t.Fatalf("mustBeIntInRange(%d, %d, %q) returned %q; want %q", tc.min, tc.max, tc.input, got, tc.wantValue) |
| 232 | + } |
| 233 | + }) |
| 234 | + } |
| 235 | +} |
0 commit comments