Skip to content

Commit

Permalink
fix: add support to deep equality for DataSlicesMatch type
Browse files Browse the repository at this point in the history
  • Loading branch information
LuigiAndrea committed Mar 13, 2021
1 parent 500eb3e commit 6ea1bd6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion assertions/slices_assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package assertions
import (
"errors"
"fmt"
"reflect"

m "github.com/LuigiAndrea/test-helper/messages"
)
Expand Down Expand Up @@ -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) }
Expand Down
18 changes: 18 additions & 0 deletions assertions/slices_assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 6ea1bd6

Please sign in to comment.