Skip to content
Open
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
3 changes: 3 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,9 @@ func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, m
}

for _, k := range expectedMap.MapKeys() {
if !k.Type().AssignableTo(actualMap.Type().Key()) {
return Fail(t, fmt.Sprintf("Cannot compare map keys of type %v with %v", k.Type(), actualMap.Type().Key()), msgAndArgs...)
}
ev := expectedMap.MapIndex(k)
av := actualMap.MapIndex(k)

Expand Down
25 changes: 25 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,31 @@ func TestInDeltaMapValues(t *testing.T) {
}
}

func TestInDeltaMapValuesKeyTypes(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
expected interface{}
actual interface{}
}{
{"string and int", map[string]float64{"1": 1}, map[int]float64{1: 1}},
{"int and int64", map[int]float64{1: 1}, map[int64]float64{1: 1}},
{"interface and string", map[interface{}]float64{"key": 1}, map[string]float64{"key": 1}},
} {
t.Run(tc.name, func(t *testing.T) {
out := &outputT{buf: bytes.NewBuffer(nil)}
NotPanics(t, func() {
False(t, InDeltaMapValues(out, tc.expected, tc.actual, 0.1, "map comparison"))
})
Contains(t, out.buf.String(), "map comparison")
})
}

// A concrete key can already be assigned to an interface-keyed map.
True(t, InDeltaMapValues(t, map[string]float64{"key": 1}, map[interface{}]float64{"key": 1.01}, 0.1))
}

func TestInEpsilon(t *testing.T) {
t.Parallel()

Expand Down
Loading