Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20250317-182730.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: ENHANCEMENTS
body: 'knownvalue: Updated the `ObjectExact` error message to report extra/missing attributes from the actual object.'
time: 2025-03-17T18:27:30.206493-04:00
custom:
Issue: "451"
42 changes: 32 additions & 10 deletions knownvalue/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package knownvalue

import (
"fmt"
"maps"
"slices"
"sort"
)

Expand All @@ -24,18 +26,14 @@ func (v objectExact) CheckValue(other any) error {
}

if len(otherVal) != len(v.value) {
expectedAttributes := "attributes"
actualAttributes := "attributes"

if len(v.value) == 1 {
expectedAttributes = "attribute"
}

if len(otherVal) == 1 {
actualAttributes = "attribute"
deltaMsg := ""
if len(otherVal) > len(v.value) {
deltaMsg = createDeltaString(otherVal, v.value, "actual value has extra attribute(s): ")
} else {
deltaMsg = createDeltaString(v.value, otherVal, "actual value is missing attribute(s): ")
}

return fmt.Errorf("expected %d %s for ObjectExact check, got %d %s", len(v.value), expectedAttributes, len(otherVal), actualAttributes)
return fmt.Errorf("expected %d attribute(s) for ObjectExact check, got %d attribute(s): %s", len(v.value), len(otherVal), deltaMsg)
}

var keys []string
Expand Down Expand Up @@ -92,3 +90,27 @@ func ObjectExact(value map[string]Check) objectExact {
value: value,
}
}

// createDeltaString prints the map keys that are present in mapA and not present in mapB
func createDeltaString[T any, V any](mapA map[string]T, mapB map[string]V, msgPrefix string) string {
deltaMsg := ""

deltaMap := make(map[string]T, len(mapA))
maps.Copy(deltaMap, mapA)
for key := range mapB {
delete(deltaMap, key)
}

deltaKeys := slices.Sorted(maps.Keys(deltaMap))

for i, k := range deltaKeys {
if i == 0 {
deltaMsg += msgPrefix
} else if i != 0 {
deltaMsg += ", "
}
deltaMsg += fmt.Sprintf("%q", k)
}

return deltaMsg
}
46 changes: 43 additions & 3 deletions knownvalue/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func TestObjectValue_CheckValue(t *testing.T) {
"three": knownvalue.Float64Exact(7.89),
}),
other: map[string]any{},
expectedError: fmt.Errorf("expected 3 attributes for ObjectExact check, got 0 attributes"),
expectedError: fmt.Errorf("expected 3 attribute(s) for ObjectExact check, got 0 attribute(s): actual value is missing attribute(s): \"one\", \"three\", \"two\""),
},
"wrong-length": {
"missing-one-attribute": {
self: knownvalue.ObjectExact(map[string]knownvalue.Check{
"one": knownvalue.Float64Exact(1.23),
"two": knownvalue.Float64Exact(4.56),
Expand All @@ -65,7 +65,47 @@ func TestObjectValue_CheckValue(t *testing.T) {
"one": json.Number("1.23"),
"two": json.Number("4.56"),
},
expectedError: fmt.Errorf("expected 3 attributes for ObjectExact check, got 2 attributes"),
expectedError: fmt.Errorf("expected 3 attribute(s) for ObjectExact check, got 2 attribute(s): actual value is missing attribute(s): \"three\""),
},
"missing-multiple-attributes": {
self: knownvalue.ObjectExact(map[string]knownvalue.Check{
"one": knownvalue.Float64Exact(1.23),
"two": knownvalue.Float64Exact(4.56),
"three": knownvalue.Float64Exact(7.89),
"four": knownvalue.Float64Exact(0.12),
"five": knownvalue.Float64Exact(3.45),
}),
other: map[string]any{
"one": json.Number("1.23"),
"two": json.Number("4.56"),
},
expectedError: fmt.Errorf("expected 5 attribute(s) for ObjectExact check, got 2 attribute(s): actual value is missing attribute(s): \"five\", \"four\", \"three\""),
},
"extra-one-attribute": {
self: knownvalue.ObjectExact(map[string]knownvalue.Check{
"one": knownvalue.Float64Exact(1.23),
"two": knownvalue.Float64Exact(4.56),
}),
other: map[string]any{
"one": json.Number("1.23"),
"two": json.Number("4.56"),
"three": json.Number("7.89"),
},
expectedError: fmt.Errorf("expected 2 attribute(s) for ObjectExact check, got 3 attribute(s): actual value has extra attribute(s): \"three\""),
},
"extra-multiple-attributes": {
self: knownvalue.ObjectExact(map[string]knownvalue.Check{
"one": knownvalue.Float64Exact(1.23),
"two": knownvalue.Float64Exact(4.56),
}),
other: map[string]any{
"one": json.Number("1.23"),
"two": json.Number("4.56"),
"three": json.Number("7.89"),
"four": json.Number("0.12"),
"five": json.Number("3.45"),
},
expectedError: fmt.Errorf("expected 2 attribute(s) for ObjectExact check, got 5 attribute(s): actual value has extra attribute(s): \"five\", \"four\", \"three\""),
},
"not-equal": {
self: knownvalue.ObjectExact(map[string]knownvalue.Check{
Expand Down
Loading