github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/validation/testing.go (about) 1 package validation 2 3 import ( 4 "regexp" 5 "testing" 6 7 "github.com/hashicorp/terraform-plugin-sdk/helper/schema" 8 ) 9 10 type testCase struct { 11 val interface{} 12 f schema.SchemaValidateFunc 13 expectedErr *regexp.Regexp 14 } 15 16 func runTestCases(t *testing.T, cases []testCase) { 17 matchErr := func(errs []error, r *regexp.Regexp) bool { 18 // err must match one provided 19 for _, err := range errs { 20 if r.MatchString(err.Error()) { 21 return true 22 } 23 } 24 25 return false 26 } 27 28 for i, tc := range cases { 29 _, errs := tc.f(tc.val, "test_property") 30 31 if len(errs) == 0 && tc.expectedErr == nil { 32 continue 33 } 34 35 if len(errs) != 0 && tc.expectedErr == nil { 36 t.Fatalf("expected test case %d to produce no errors, got %v", i, errs) 37 } 38 39 if !matchErr(errs, tc.expectedErr) { 40 t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs) 41 } 42 } 43 }