github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/newrelic/validation_test.go (about) 1 package newrelic 2 3 import ( 4 "regexp" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 ) 9 10 type testCase struct { 11 val interface{} 12 f schema.SchemaValidateFunc 13 expectedErr *regexp.Regexp 14 } 15 16 func TestValidationIntInInSlice(t *testing.T) { 17 runTestCases(t, []testCase{ 18 { 19 val: 2, 20 f: intInSlice([]int{1, 2, 3}), 21 }, 22 { 23 val: 4, 24 f: intInSlice([]int{1, 2, 3}), 25 expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[1 2 3\\], got 4"), 26 }, 27 { 28 val: "foo", 29 f: intInSlice([]int{1, 2, 3}), 30 expectedErr: regexp.MustCompile("expected type of [\\w]+ to be int"), 31 }, 32 }) 33 } 34 35 func TestValidationFloat64Gte(t *testing.T) { 36 runTestCases(t, []testCase{ 37 { 38 val: 1.1, 39 f: float64Gte(1.1), 40 }, 41 { 42 val: 1.2, 43 f: float64Gte(1.1), 44 }, 45 { 46 val: "foo", 47 f: float64Gte(1.1), 48 expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float64"), 49 }, 50 { 51 val: 0.1, 52 f: float64Gte(1.1), 53 expectedErr: regexp.MustCompile("expected [\\w]+ to be greater than or equal to 1.1, got 0.1"), 54 }, 55 }) 56 } 57 58 func runTestCases(t *testing.T, cases []testCase) { 59 matchErr := func(errs []error, r *regexp.Regexp) bool { 60 // err must match one provided 61 for _, err := range errs { 62 if r.MatchString(err.Error()) { 63 return true 64 } 65 } 66 67 return false 68 } 69 70 for i, tc := range cases { 71 _, errs := tc.f(tc.val, "test_property") 72 73 if len(errs) == 0 && tc.expectedErr == nil { 74 continue 75 } 76 77 if !matchErr(errs, tc.expectedErr) { 78 t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs) 79 } 80 } 81 }