From ab3b9743a738c228e875cb71b3c285698a908885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 10 Oct 2023 03:32:57 +0200 Subject: [PATCH 1/7] assert.InEpsilonSlice: refactor --- assert/assertions.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 1e55fbf54..44a16259f 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1466,14 +1466,18 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m if h, ok := t.(tHelper); ok { h.Helper() } - if expected == nil || actual == nil || - reflect.TypeOf(actual).Kind() != reflect.Slice || - reflect.TypeOf(expected).Kind() != reflect.Slice { + + if expected == nil || actual == nil { return Fail(t, "Parameters must be slice", msgAndArgs...) } - actualSlice := reflect.ValueOf(actual) expectedSlice := reflect.ValueOf(expected) + actualSlice := reflect.ValueOf(actual) + + if expectedSlice.Type().Kind() != reflect.Slice || + actualSlice.Type().Kind() != reflect.Slice { + return Fail(t, "Parameters must be slice", msgAndArgs...) + } for i := 0; i < actualSlice.Len(); i++ { result := InEpsilon(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), epsilon) From b5dec80529a3053e277fad8f82f3814b9c495f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 10 Oct 2023 03:33:27 +0200 Subject: [PATCH 2/7] assert.InEpsilonSlice: add more slice checks --- assert/assertions.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/assert/assertions.go b/assert/assertions.go index 44a16259f..5f6f7425d 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1479,6 +1479,10 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m return Fail(t, "Parameters must be slice", msgAndArgs...) } + if !IsType(t, expected, actual) || !Len(t, actual, expectedSlice.Len()) { + return false + } + for i := 0; i < actualSlice.Len(); i++ { result := InEpsilon(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), epsilon) if !result { From f728d3c50f6131b37d2cfeec52bc33937f1b3aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 10 Oct 2023 03:37:39 +0200 Subject: [PATCH 3/7] assert.InEpsilonSlice: refactor Remove multiple calls to reflect.Value.Len() --- assert/assertions.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 5f6f7425d..be6cc26df 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1479,11 +1479,12 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m return Fail(t, "Parameters must be slice", msgAndArgs...) } - if !IsType(t, expected, actual) || !Len(t, actual, expectedSlice.Len()) { + expectedLen := expectedSlice.Len() + if !IsType(t, expected, actual) || !Len(t, actual, expectedLen) { return false } - for i := 0; i < actualSlice.Len(); i++ { + for i := 0; i < expectedLen; i++ { result := InEpsilon(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), epsilon) if !result { return result From 8fd5aae061631fe8427f9f06e163574032ec1011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 10 Oct 2023 03:40:29 +0200 Subject: [PATCH 4/7] assert.InEpsilonSlice: remove redundant check Remove check of actual's kind redundant with Type()'s check. --- assert/assertions.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index be6cc26df..be3f4f52c 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1474,9 +1474,8 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m expectedSlice := reflect.ValueOf(expected) actualSlice := reflect.ValueOf(actual) - if expectedSlice.Type().Kind() != reflect.Slice || - actualSlice.Type().Kind() != reflect.Slice { - return Fail(t, "Parameters must be slice", msgAndArgs...) + if expectedSlice.Type().Kind() != reflect.Slice { + return Fail(t, "Expected value must be slice", msgAndArgs...) } expectedLen := expectedSlice.Len() From f7fbc7da15bc38f2f8ff9b955969e22ec2041dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 10 Oct 2023 03:48:32 +0200 Subject: [PATCH 5/7] assert.InEpsilonSlice: fix order of expected vs actual (#1231) --- assert/assertions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index be3f4f52c..28f775183 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1484,7 +1484,7 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m } for i := 0; i < expectedLen; i++ { - result := InEpsilon(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), epsilon) + result := InEpsilon(t, expectedSlice.Index(i).Interface(), actualSlice.Index(i).Interface(), epsilon) if !result { return result } From 7d383ba73269a8751aedc9e8ca0b3280935db217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 10 Oct 2023 04:18:28 +0200 Subject: [PATCH 6/7] assert.InEpsilonSlice: refactor --- assert/assertions.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 28f775183..e78ac3103 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1484,9 +1484,8 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m } for i := 0; i < expectedLen; i++ { - result := InEpsilon(t, expectedSlice.Index(i).Interface(), actualSlice.Index(i).Interface(), epsilon) - if !result { - return result + if !InEpsilon(t, expectedSlice.Index(i).Interface(), actualSlice.Index(i).Interface(), epsilon) { + return false } } From f8dcfd66187e703f37d04bf2b17a55c20e4029aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 10 Oct 2023 04:22:18 +0200 Subject: [PATCH 7/7] assert.InEpsilonSlice: mention index of error in fail message --- assert/assertions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index e78ac3103..e92f5883a 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -1484,7 +1484,7 @@ 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) { + if !InEpsilon(t, expectedSlice.Index(i).Interface(), actualSlice.Index(i).Interface(), epsilon, "at index %d", i) { return false } }