Skip to content

Commit

Permalink
Merge pull request #44 from go-test/dn/fix-issue-39
Browse files Browse the repository at this point in the history
Fix issue 39: Confusing diff when comparing distinct types with the ame name
  • Loading branch information
daniel-nichter authored Jul 11, 2020
2 parents ce1ba2d + e4c73a6 commit 58784e6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
language: go

go:
- "1.11"
- "1.12"
- "1.13"
- "1.14"

before_install:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cover

script:
- $HOME/gopath/bin/goveralls -service=travis-ci
- $HOME/gopath/bin/goveralls -service=travis-ci -package github.com/go-test/deep
13 changes: 12 additions & 1 deletion deep.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,18 @@ func (c *cmp) equals(a, b reflect.Value, level int) {
aType := a.Type()
bType := b.Type()
if aType != bType {
c.saveDiff(aType, bType)
// Built-in types don't have a name, so don't report [3]int != [2]int as " != "
if aType.Name() == "" || aType.Name() != bType.Name() {
c.saveDiff(aType, bType)
} else {
// Type names can be the same, e.g. pkg/v1.Error and pkg/v2.Error
// are both exported as pkg, so unless we include the full pkg path
// the diff will be "pkg.Error != pkg.Error"
// https://github.com/go-test/deep/issues/39
aFullType := aType.PkgPath() + "." + aType.Name()
bFullType := bType.PkgPath() + "." + bType.Name()
c.saveDiff(aFullType, bFullType)
}
logError(ErrTypeMismatch)
return
}
Expand Down
17 changes: 17 additions & 0 deletions deep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

"github.com/go-test/deep"
v1 "github.com/go-test/deep/test/v1"
v2 "github.com/go-test/deep/test/v2"
)

func TestString(t *testing.T) {
Expand Down Expand Up @@ -135,6 +137,21 @@ func TestTypeMismatch(t *testing.T) {
if diff[0] != "deep_test.T1 != deep_test.T2" {
t.Error("wrong diff:", diff[0])
}

// Same pkg name but differnet full paths
// https://github.com/go-test/deep/issues/39
err1 := v1.Error{}
err2 := v2.Error{}
diff = deep.Equal(err1, err2)
if diff == nil {
t.Fatal("no diff")
}
if len(diff) != 1 {
t.Error("too many diff:", diff)
}
if diff[0] != "github.com/go-test/deep/test/v1.Error != github.com/go-test/deep/test/v2.Error" {
t.Error("wrong diff:", diff[0])
}
}

func TestKindMismatch(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions test/v1/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package deeptest

type Error struct{}

func (e Error) Error() string {
return ""
}
7 changes: 7 additions & 0 deletions test/v2/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package deeptest

type Error struct{}

func (e Error) Error() string {
return ""
}

0 comments on commit 58784e6

Please sign in to comment.