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 position and pass msg to underlying func in InDeltaMapValues, InDeltaMapValues, and InEpsilonSlice #1514

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
30 changes: 25 additions & 5 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1405,9 +1405,9 @@ func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAn
expectedSlice := reflect.ValueOf(expected)

for i := 0; i < actualSlice.Len(); i++ {
result := InDelta(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), delta, msgAndArgs...)
if !result {
return result
elemMsgAndArgs := appendMsgAndArgs(msgAndArgs, fmt.Sprint("at index: ", i))
if !InDelta(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), delta, elemMsgAndArgs...) {
return false
}
}

Expand Down Expand Up @@ -1444,12 +1444,13 @@ func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, m
return Fail(t, fmt.Sprintf("missing key %q in actual map", k), msgAndArgs...)
}

elemMsgAndArgs := appendMsgAndArgs(msgAndArgs, fmt.Sprintf("at key: %v", k))
if !InDelta(
t,
ev.Interface(),
av.Interface(),
delta,
msgAndArgs...,
elemMsgAndArgs...,
) {
return false
}
Expand Down Expand Up @@ -1523,14 +1524,33 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m
}

for i := 0; i < expectedLen; i++ {
if !InEpsilon(t, expectedSlice.Index(i).Interface(), actualSlice.Index(i).Interface(), epsilon, "at index %d", i) {
elemMsgAndArgs := appendMsgAndArgs(msgAndArgs, fmt.Sprint("at index: ", i))
if !InEpsilon(t, expectedSlice.Index(i).Interface(), actualSlice.Index(i).Interface(), epsilon, elemMsgAndArgs...) {
return false
}
}

return true
}

// appendMsgAndArgs appends a message string to the first element of the msgAndArgs slice
// and returns the updated slice.
func appendMsgAndArgs(msgAndArgs []interface{}, message string) []interface{} {
switch len(msgAndArgs) {
case 0:
return []interface{}{message}
case 1:
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
msgAndArgs = []interface{}{fmt.Sprintf("%s %s", msgAsStr, message)}
}
default:
msgAndArgs[0] = fmt.Sprintf("%s %s", msgAndArgs[0].(string), message)
}

return msgAndArgs
}
Copy link
Collaborator

@dolmen dolmen May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor the function body to use a switch on len(msgAndArgs) instead of multiple if.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the review, I have adjusted the function to use switch


/*
Errors
*/
Expand Down
68 changes: 67 additions & 1 deletion assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,18 @@ func TestInDeltaSlice(t *testing.T) {
[]float64{0, math.NaN(), 3},
0.1), "{1, NaN, 2} is not element-wise close to {0, NaN, 3} in delta=0.1")

False(t, InDeltaSlice(mockT,
[]float64{1, math.NaN(), 2},
[]float64{0, math.NaN(), 3},
0.1,
"additional message"), "{1, NaN, 2} is not element-wise close to {0, NaN, 3} in delta=0.1")

False(t, InDeltaSlice(mockT,
[]float64{1, math.NaN(), 2},
[]float64{0, math.NaN(), 3},
0.1,
"additional message <%s> #%d", "test", 1), "{1, NaN, 2} is not element-wise close to {0, NaN, 3} in delta=0.1")

False(t, InDeltaSlice(mockT, "", nil, 1), "Expected non numeral slices to fail")
}

Expand All @@ -1832,6 +1844,7 @@ func TestInDeltaMapValues(t *testing.T) {
actual interface{}
f func(TestingT, bool, ...interface{}) bool
delta float64
msg []interface{}
}{
{
title: "Within delta",
Expand Down Expand Up @@ -1861,6 +1874,19 @@ func TestInDeltaMapValues(t *testing.T) {
delta: 0.1,
f: True,
},
{
title: "Outside delta",
expect: map[int]float64{
1: 1.0,
2: 2.0,
},
actual: map[int]float64{
1: 2.0,
2: 1.99,
},
delta: 0.1,
f: False,
},
{
title: "Different number of keys",
expect: map[int]float64{
Expand Down Expand Up @@ -1896,8 +1922,36 @@ func TestInDeltaMapValues(t *testing.T) {
},
f: False,
},
{
title: "False with custom message",
expect: map[string]float64{
"foo": 1.0,
"bar": 2.0,
},
actual: map[string]float64{
"foo": 2.0,
"bar": 2.0,
},
delta: 0.1,
f: False,
msg: []interface{}{"custom message"},
},
{
title: "False with custom message format",
expect: map[string]float64{
"foo": 1.0,
"bar": 2.0,
},
actual: map[string]float64{
"foo": 2.0,
"bar": 2.0,
},
delta: 0.1,
f: False,
msg: []interface{}{"custom message format <%s> #%d", "test", 1},
},
} {
tc.f(t, InDeltaMapValues(mockT, tc.expect, tc.actual, tc.delta), tc.title+"\n"+diff(tc.expect, tc.actual))
tc.f(t, InDeltaMapValues(mockT, tc.expect, tc.actual, tc.delta, tc.msg...), tc.title+"\n"+diff(tc.expect, tc.actual))
}
}

Expand Down Expand Up @@ -1961,6 +2015,18 @@ func TestInEpsilonSlice(t *testing.T) {
[]float64{2.1, 2.1},
0.04), "{2.2, 2.0} is not element-wise close to {2.1, 2.1} in epsilon=0.04")

False(t, InEpsilonSlice(mockT,
[]float64{2.2, 2.0},
[]float64{2.1, 2.1},
0.04,
"additional message"), "{2.2, 2.0} is not element-wise close to {2.1, 2.1} in epsilon=0.04")

False(t, InEpsilonSlice(mockT,
[]float64{2.2, 2.0},
[]float64{2.1, 2.1},
0.04,
"additional message <%s> #%d", "test", 1), "{2.2, 2.0} is not element-wise close to {2.1, 2.1} in epsilon=0.04")

False(t, InEpsilonSlice(mockT, "", nil, 1), "Expected non numeral slices to fail")
}

Expand Down