Skip to content

Commit

Permalink
Merge pull request #14 from yazgazan/cyclical-error-wrong-check
Browse files Browse the repository at this point in the history
Cyclical error wrong check
  • Loading branch information
yazgazan authored Nov 8, 2018
2 parents e2b9f93 + 55bd912 commit 3846f53
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (v *visited) add(lhs, rhs reflect.Value) error {
}
v.lhs = append(v.lhs, lhs.Pointer())
}
if canAddr(rhs) && !isEmptyMapOrSlice(lhs) {
if canAddr(rhs) && !isEmptyMapOrSlice(rhs) {
if inPointers(v.rhs, rhs) {
return ErrCyclic
}
Expand Down
16 changes: 15 additions & 1 deletion diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ func TestCircular(t *testing.T) {
0: "foo",
},
},
1: []interface{}{
"bar", "baz",
},
}
emptySlice := map[int]interface{}{
0: []interface{}{},
Expand All @@ -634,21 +637,32 @@ func TestCircular(t *testing.T) {
1: map[int]interface{}{},
}

repeatingNotCyclic := map[int]interface{}{
0: []interface{}{"foo", "bar"},
}
repeatingNotCyclic[1] = repeatingNotCyclic[0]

for _, test := range []struct {
lhs interface{}
rhs interface{}
wantError bool
}{
{lhs: first, rhs: first, wantError: true},
{lhs: first, rhs: second, wantError: true},
{lhs: first, rhs: second, wantError: true},
{lhs: first, rhs: notCyclic, wantError: true},
{lhs: notCyclic, rhs: first, wantError: true},
{lhs: notCyclic, rhs: emptySlice},
{lhs: notCyclic, rhs: emptyMap},
{lhs: notCyclic, rhs: notCyclic},
{lhs: emptySlice, rhs: emptySliceNotRepeating},
{lhs: emptySliceNotRepeating, rhs: emptySlice},
{lhs: emptyMap, rhs: emptyMapNotRepeating},
{lhs: emptyMapNotRepeating, rhs: emptyMap},

// Known limitation: our circular reference detection can
// give false-positive when an addressable value is repeated
// in a non-circular pattern.
{lhs: notCyclic, rhs: repeatingNotCyclic, wantError: true},
} {
d, err := Diff(test.lhs, test.rhs)

Expand Down

0 comments on commit 3846f53

Please # to comment.