@@ -856,6 +856,83 @@ func TestMapValueType(t *testing.T) {
856856 }
857857}
858858
859+ func TestMapValueLength (t * testing.T ) {
860+ t .Parallel ()
861+
862+ testCases := map [string ]struct {
863+ input MapValue
864+ opts CollectionLengthOptions
865+ expected int
866+ }{
867+ "known-empty" : {
868+ input : NewMapValueMust (StringType {}, map [string ]attr.Value {}),
869+ opts : CollectionLengthOptions {},
870+ expected : 0 ,
871+ },
872+ "known-single" : {
873+ input : NewMapValueMust (StringType {}, map [string ]attr.Value {"key" : NewStringValue ("test" )}),
874+ opts : CollectionLengthOptions {},
875+ expected : 1 ,
876+ },
877+ "known-multiple" : {
878+ input : NewMapValueMust (StringType {}, map [string ]attr.Value {
879+ "key1" : NewStringValue ("hello" ),
880+ "key2" : NewStringValue ("world" ),
881+ }),
882+ opts : CollectionLengthOptions {},
883+ expected : 2 ,
884+ },
885+ "null-unhandled-as-zero" : {
886+ input : NewMapNull (StringType {}),
887+ opts : CollectionLengthOptions {UnhandledNullAsZero : true },
888+ expected : 0 ,
889+ },
890+ "unknown-unhandled-as-zero" : {
891+ input : NewMapUnknown (StringType {}),
892+ opts : CollectionLengthOptions {UnhandledUnknownAsZero : true },
893+ expected : 0 ,
894+ },
895+ }
896+
897+ for name , testCase := range testCases {
898+ t .Run (name , func (t * testing.T ) {
899+ t .Parallel ()
900+
901+ got := testCase .input .Length (testCase .opts )
902+
903+ if got != testCase .expected {
904+ t .Errorf ("Expected %d, got %d" , testCase .expected , got )
905+ }
906+ })
907+ }
908+ }
909+
910+ func TestMapValueLength_PanicOnNull (t * testing.T ) {
911+ t .Parallel ()
912+
913+ defer func () {
914+ if r := recover (); r == nil {
915+ t .Errorf ("Expected panic when calling Length on null Map with UnhandledNullAsZero=false" )
916+ }
917+ }()
918+
919+ m := NewMapNull (StringType {})
920+ m .Length (CollectionLengthOptions {UnhandledNullAsZero : false })
921+ }
922+
923+ func TestMapValueLength_PanicOnUnknown (t * testing.T ) {
924+ t .Parallel ()
925+
926+ defer func () {
927+ if r := recover (); r == nil {
928+ t .Errorf ("Expected panic when calling Length on unknown Map with UnhandledUnknownAsZero=false" )
929+ }
930+ }()
931+
932+ m := NewMapUnknown (StringType {})
933+ m .Length (CollectionLengthOptions {UnhandledUnknownAsZero : false })
934+ }
935+
859936func TestMapTypeValidate (t * testing.T ) {
860937 t .Parallel ()
861938
0 commit comments