| 1194 | } |
| 1195 | |
| 1196 | func TestEnvVariableValidation(t *testing.T) { |
| 1197 | tcs := []struct { |
| 1198 | name string |
| 1199 | opts []EnvOption |
| 1200 | wantErr string |
| 1201 | }{ |
| 1202 | { |
| 1203 | name: "compatible_duplicate", |
| 1204 | opts: []EnvOption{ |
| 1205 | Variable("foo", IntType), |
| 1206 | Variable("foo", IntType), |
| 1207 | }, |
| 1208 | }, |
| 1209 | { |
| 1210 | name: "variable_and_constant", |
| 1211 | opts: []EnvOption{ |
| 1212 | Variable("foo", IntType), |
| 1213 | Constant("foo", IntType, types.Int(50)), |
| 1214 | }, |
| 1215 | }, |
| 1216 | { |
| 1217 | name: "compatible_constant", |
| 1218 | opts: []EnvOption{ |
| 1219 | Constant("foo", StringType, types.String("foo")), |
| 1220 | Constant("foo", StringType, types.String("foo")), |
| 1221 | }, |
| 1222 | }, |
| 1223 | { |
| 1224 | name: "incompatible_variable", |
| 1225 | opts: []EnvOption{ |
| 1226 | Variable("foo", IntType), |
| 1227 | Variable("foo", DoubleType), |
| 1228 | }, |
| 1229 | wantErr: "overlapping identifier for name", |
| 1230 | }, |
| 1231 | { |
| 1232 | name: "incompatible_constant", |
| 1233 | opts: []EnvOption{ |
| 1234 | Constant("foo", IntType, types.Int(42)), |
| 1235 | Constant("foo", IntType, types.Int(43)), |
| 1236 | }, |
| 1237 | wantErr: "conflicting constant definitions", |
| 1238 | }, |
| 1239 | } |
| 1240 | |
| 1241 | for _, tc := range tcs { |
| 1242 | t.Run(tc.name, func(*testing.T) { |
| 1243 | env, err := NewEnv(tc.opts...) |
| 1244 | if err != nil { |
| 1245 | t.Fatalf("NewEnv() returned error: %v", err) |
| 1246 | } |
| 1247 | // Variables are lazily validated. |
| 1248 | _, iss := env.Compile("foo") |
| 1249 | err = iss.Err() |
| 1250 | if tc.wantErr != "" { |
| 1251 | if err == nil || !strings.Contains(err.Error(), tc.wantErr) { |
| 1252 | t.Errorf("Compile() returned error %v, wanted %s", err, tc.wantErr) |
| 1253 | } |