Skip to content

Commit

Permalink
pass msg to underlying func + add position to msgAndArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrywiranto committed Jan 22, 2024
1 parent caa42d7 commit e073c9f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
31 changes: 26 additions & 5 deletions assert/assertions.go
Expand Up @@ -1357,9 +1357,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 @@ -1396,12 +1396,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 @@ -1475,14 +1476,34 @@ 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{} {
if len(msgAndArgs) == 0 || msgAndArgs == nil {
return []interface{}{message}
}
if len(msgAndArgs) == 1 {
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
msgAndArgs = []interface{}{fmt.Sprintf("%s %s", msgAsStr, message)}
}
}
if len(msgAndArgs) > 1 {
msgAndArgs[0] = fmt.Sprintf("%s %s", msgAndArgs[0].(string), message)
}

return msgAndArgs
}

/*
Errors
*/
Expand Down
68 changes: 67 additions & 1 deletion assert/assertions_test.go
Expand Up @@ -1783,6 +1783,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 @@ -1795,6 +1807,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 @@ -1824,6 +1837,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 @@ -1859,8 +1885,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 @@ -1924,6 +1978,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

0 comments on commit e073c9f

Please sign in to comment.