@@ -3,50 +3,65 @@ package validation
33import (
44 "regexp"
55 "testing"
6+
7+ "github.com/hashicorp/go-cty/cty"
8+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
69)
710
811func TestValidationMapKeyLenBetween (t * testing.T ) {
912 cases := map [string ]struct {
10- Value interface {}
11- Error bool
13+ Value interface {}
14+ ExpectedDiags diag. Diagnostics
1215 }{
13- "NotMap" : {
14- Value : "the map is a lie" ,
15- Error : true ,
16- },
1716 "TooLong" : {
1817 Value : map [string ]interface {}{
1918 "ABC" : "123" ,
2019 "UVWXYZ" : "123456" ,
2120 },
22- Error : true ,
21+ ExpectedDiags : diag.Diagnostics {
22+ {
23+ Severity : diag .Error ,
24+ AttributePath : append (cty.Path {}, cty.IndexStep {Key : cty .StringVal ("UVWXYZ" )}),
25+ },
26+ },
2327 },
2428 "TooShort" : {
2529 Value : map [string ]interface {}{
2630 "ABC" : "123" ,
2731 "U" : "1" ,
2832 },
29- Error : true ,
33+ ExpectedDiags : diag.Diagnostics {
34+ {
35+ Severity : diag .Error ,
36+ AttributePath : append (cty.Path {}, cty.IndexStep {Key : cty .StringVal ("U" )}),
37+ },
38+ },
3039 },
3140 "AllGood" : {
3241 Value : map [string ]interface {}{
3342 "AB" : "12" ,
3443 "UVWXY" : "12345" ,
3544 },
36- Error : false ,
45+ ExpectedDiags : nil ,
3746 },
3847 }
3948
4049 fn := MapKeyLenBetween (2 , 5 )
4150
4251 for tn , tc := range cases {
4352 t .Run (tn , func (t * testing.T ) {
44- _ , errors := fn (tc .Value , tn )
53+ diags := fn (tc .Value , cty. Path {} )
4554
46- if len (errors ) > 0 && ! tc .Error {
47- t .Errorf ("MapKeyLenBetween(%s) produced an unexpected error" , tc .Value )
48- } else if len (errors ) == 0 && tc .Error {
49- t .Errorf ("MapKeyLenBetween(%s) did not error" , tc .Value )
55+ if len (diags ) != len (tc .ExpectedDiags ) {
56+ t .Fatalf ("%s: wrong number of diags, expected %d, got %d" , tn , len (tc .ExpectedDiags ), len (diags ))
57+ }
58+ for j := range diags {
59+ if diags [j ].Severity != tc .ExpectedDiags [j ].Severity {
60+ t .Fatalf ("%s: expected severity %v, got %v" , tn , tc .ExpectedDiags [j ].Severity , diags [j ].Severity )
61+ }
62+ if ! diags [j ].AttributePath .Equals (tc .ExpectedDiags [j ].AttributePath ) {
63+ t .Fatalf ("%s: attribute paths do not match expected: %v, got %v" , tn , tc .ExpectedDiags [j ].AttributePath , diags [j ].AttributePath )
64+ }
5065 }
5166 })
5267 }
0 commit comments