Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: add InDelta for maps with a deltas map #1519

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions assert/assertion_format.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions assert/assertion_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions assert/assertions.go
Expand Up @@ -1424,6 +1424,55 @@ func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, m
return true
}

// InMapDeltasMapValues is the same as InDelta, but it compares all values between two maps with the matching delta on the delta's map. All maps must have exactly the same keys.
func InMapDeltasMapValues(t TestingT, expected, actual interface{}, deltasMap map[interface{}]float64, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if expected == nil || actual == nil ||
reflect.TypeOf(actual).Kind() != reflect.Map ||
reflect.TypeOf(expected).Kind() != reflect.Map {
return Fail(t, "Arguments must be maps", msgAndArgs...)
}

expectedMap := reflect.ValueOf(expected)
actualMap := reflect.ValueOf(actual)

if expectedMap.Len() != actualMap.Len() && expectedMap.Len() != len(deltasMap) {
return Fail(t, "Arguments must have the same number of keys", msgAndArgs...)
}

for _, k := range expectedMap.MapKeys() {
ev := expectedMap.MapIndex(k)
av := actualMap.MapIndex(k)

delta, ok := deltasMap[k.Interface()]
if !ok {
return Fail(t, fmt.Sprintf("missing key %q in delta map", k), msgAndArgs...)
}

if !ev.IsValid() {
return Fail(t, fmt.Sprintf("missing key %q in expected map", k), msgAndArgs...)
}

if !av.IsValid() {
return Fail(t, fmt.Sprintf("missing key %q in actual map", k), msgAndArgs...)
}

if !InDelta(
t,
ev.Interface(),
av.Interface(),
delta,
msgAndArgs...,
) {
return false
}
}

return true
}

func calcRelativeError(expected, actual interface{}) (float64, error) {
af, aok := toFloat(expected)
bf, bok := toFloat(actual)
Expand Down
110 changes: 110 additions & 0 deletions assert/assertions_test.go
Expand Up @@ -1865,6 +1865,116 @@ func TestInDeltaMapValues(t *testing.T) {
}
}

func TestInMapDeltasMapValues(t *testing.T) {
mockT := new(testing.T)

for _, tc := range []struct {
title string
expect interface{}
actual interface{}
f func(TestingT, bool, ...interface{}) bool
deltasMap map[interface{}]float64
}{
{
title: "Within deltas",
expect: map[string]float64{
"foo": 1.0,
"bar": 7,
"baz": math.NaN(),
},
actual: map[string]float64{
"foo": 1.01,
"bar": 7.99,
"baz": math.NaN(),
},
deltasMap: map[interface{}]float64{
"foo": 0.1,
"bar": 1,
"baz": 0.1,
},
f: True,
},
{
title: "Within deltas",
expect: map[int]float64{
1: 1.0,
2: 2.0,
},
actual: map[int]float64{
1: 1.0,
2: 1.99,
},
deltasMap: map[interface{}]float64{
1: 0.1,
2: 0.1,
},
f: True,
},
{
title: "Different number of keys",
expect: map[int]float64{
1: 1.0,
2: 2.0,
},
actual: map[int]float64{
1: 1.0,
},
deltasMap: map[interface{}]float64{
1: 0.1,
2: 0.1,
},
f: False,
},
{
title: "Within delta with zero value",
expect: map[string]float64{
"zero": 0,
},
actual: map[string]float64{
"zero": 0,
},
deltasMap: map[interface{}]float64{
"zero": 0.1,
},
f: True,
},
{
title: "With missing key with zero value",
expect: map[string]float64{
"zero": 0,
"foo": 0,
},
actual: map[string]float64{
"zero": 0,
"bar": 0,
},
deltasMap: map[interface{}]float64{
"zero": 0,
"bar": 0,
},
f: False,
},
{
title: "With missing key with zero value in deltas map",
expect: map[string]float64{
"zero": 0,
"foo": 0,
},
actual: map[string]float64{
"zero": 0,
"foo": 0,
},
deltasMap: map[interface{}]float64{
"zero": 0,
"bar": 0,
},
f: False,
},
} {
tc.f(t, InMapDeltasMapValues(mockT, tc.expect, tc.actual, tc.deltasMap), tc.title+"\n"+diff(tc.expect, tc.actual))
}
}

func TestInEpsilon(t *testing.T) {
mockT := new(testing.T)

Expand Down
22 changes: 22 additions & 0 deletions require/require.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions require/require_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.