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