@@ -5,9 +5,11 @@ package boolvalidator_test
55
66import (
77 "context"
8+ "fmt"
89 "testing"
910
1011 "github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
12+ "github.com/hashicorp/terraform-plugin-framework/function"
1113 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
1214 "github.com/hashicorp/terraform-plugin-framework/types"
1315)
@@ -16,53 +18,65 @@ func TestEqualsValidator(t *testing.T) {
1618 t .Parallel ()
1719
1820 type testCase struct {
19- in types.Bool
20- validator validator. Bool
21- expErrors int
21+ in types.Bool
22+ equalsValue bool
23+ expectError bool
2224 }
2325
2426 testCases := map [string ]testCase {
2527 "simple-match" : {
26- in : types .BoolValue (true ),
27- validator : boolvalidator .Equals (true ),
28- expErrors : 0 ,
28+ in : types .BoolValue (true ),
29+ equalsValue : true ,
2930 },
3031 "simple-mismatch" : {
31- in : types .BoolValue (false ),
32- validator : boolvalidator . Equals ( true ) ,
33- expErrors : 1 ,
32+ in : types .BoolValue (false ),
33+ equalsValue : true ,
34+ expectError : true ,
3435 },
3536 "skip-validation-on-null" : {
36- in : types .BoolNull (),
37- validator : boolvalidator .Equals (true ),
38- expErrors : 0 ,
37+ in : types .BoolNull (),
38+ equalsValue : true ,
3939 },
4040 "skip-validation-on-unknown" : {
41- in : types .BoolUnknown (),
42- validator : boolvalidator .Equals (true ),
43- expErrors : 0 ,
41+ in : types .BoolUnknown (),
42+ equalsValue : true ,
4443 },
4544 }
4645
4746 for name , test := range testCases {
48- t .Run (name , func (t * testing.T ) {
47+ name , test := name , test
48+
49+ t .Run (fmt .Sprintf ("ValidateBool - %s" , name ), func (t * testing.T ) {
4950 t .Parallel ()
5051 req := validator.BoolRequest {
5152 ConfigValue : test .in ,
5253 }
5354 res := validator.BoolResponse {}
54- test .validator .ValidateBool (context .TODO (), req , & res )
55+ boolvalidator .Equals (test .equalsValue ).ValidateBool (context .TODO (), req , & res )
56+
57+ if ! res .Diagnostics .HasError () && test .expectError {
58+ t .Fatal ("expected error, got no error" )
59+ }
5560
56- if test .expErrors > 0 && ! res .Diagnostics .HasError () {
57- t .Fatalf ("expected %d error(s), got none" , test .expErrors )
61+ if res .Diagnostics .HasError () && ! test .expectError {
62+ t .Fatalf ("got unexpected error: %s" , res .Diagnostics )
63+ }
64+ })
65+
66+ t .Run (fmt .Sprintf ("ValidateParameterBool - %s" , name ), func (t * testing.T ) {
67+ t .Parallel ()
68+ req := function.BoolParameterValidatorRequest {
69+ Value : test .in ,
5870 }
71+ res := function.BoolParameterValidatorResponse {}
72+ boolvalidator .Equals (test .equalsValue ).ValidateParameterBool (context .TODO (), req , & res )
5973
60- if test . expErrors > 0 && test .expErrors != res . Diagnostics . ErrorsCount () {
61- t .Fatalf ("expected %d error(s) , got %d: %v" , test . expErrors , res . Diagnostics . ErrorsCount (), res . Diagnostics )
74+ if res . Error == nil && test .expectError {
75+ t .Fatal ("expected error, got no error" )
6276 }
6377
64- if test . expErrors == 0 && res . Diagnostics . HasError () {
65- t .Fatalf ("expected no error(s), got %d : %v " , res .Diagnostics . ErrorsCount (), res . Diagnostics )
78+ if res . Error != nil && ! test . expectError {
79+ t .Fatalf ("got unexpected error: %s " , res .Error )
6680 }
6781 })
6882 }
0 commit comments