From 6ea1bd61e388a636f01527652c6a22a9f1708f1f Mon Sep 17 00:00:00 2001 From: Luigi Date: Fri, 12 Mar 2021 23:47:27 -0500 Subject: [PATCH] fix: add support to deep equality for DataSlicesMatch type --- assertions/slices_assert.go | 3 ++- assertions/slices_assert_test.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/assertions/slices_assert.go b/assertions/slices_assert.go index 52ab6af..c318ffe 100644 --- a/assertions/slices_assert.go +++ b/assertions/slices_assert.go @@ -3,6 +3,7 @@ package assertions import ( "errors" "fmt" + "reflect" m "github.com/LuigiAndrea/test-helper/messages" ) @@ -179,7 +180,7 @@ type DataSlicesMatch struct { func (d DataSlicesMatch) SameLength() bool { return len(d.Expected) == len(d.Actual) } // AreEqual checks if the two slices have the same value at position i -func (d DataSlicesMatch) AreEqual(i int) bool { return d.Expected[i] == d.Actual[i] } +func (d DataSlicesMatch) AreEqual(i int) bool { return reflect.DeepEqual(d.Expected[i], d.Actual[i]) } // Size is the length of DataSlicesMatch struct func (d DataSlicesMatch) Size() int { return len(d.Expected) } diff --git a/assertions/slices_assert_test.go b/assertions/slices_assert_test.go index 0267d53..d2041c4 100644 --- a/assertions/slices_assert_test.go +++ b/assertions/slices_assert_test.go @@ -210,6 +210,24 @@ func TestHelperDataSlices(t *testing.T) { } } +func TestHelperDataStructSlices(t *testing.T) { + type objectToTest struct { + value1 int + value2 string + } + + tests := []testData{ + {input1: []interface{}{&objectToTest{}}, input2: []interface{}{&objectToTest{}}}, + {input1: []interface{}{&objectToTest{value1: 51, value2: "casa"}}, input2: []interface{}{&objectToTest{value1: 51, value2: "casa"}}}, + {input1: []interface{}{&objectToTest{value1: -10, value2: "hotel"}}, input2: []interface{}{&objectToTest{value1: -10, value2: "hotel"}}}} + + for i, test := range tests { + if err := AssertSlicesEqual(DataSlicesMatch{Expected: test.input1, Actual: test.input2}); err != nil { + t.Error(m.ErrorMessageTestCount(i+1, err.Error())) + } + } +} + func TestHelperDifferentElementsDataSlices(t *testing.T) { var ve *ValueError tests := []testData{