Skip to content

Commit

Permalink
Merge pull request #158 from dnephin/fix-tests-with-latest-go-cmp
Browse files Browse the repository at this point in the history
Fix tests with latest go cmp
  • Loading branch information
dnephin committed Aug 7, 2019
2 parents 400e4d0 + 41b481e commit 04b215e
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 40 deletions.
10 changes: 3 additions & 7 deletions assert/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,9 @@ func TestDeepEqualFailure(t *testing.T) {

fakeT := &fakeTestingT{}
DeepEqual(fakeT, actual, expected, gocmp.AllowUnexported(stub{}))
expectFailNowed(t, fakeT, "assertion failed: "+`
--- actual
+++ expected
{assert.stub}.b:
-: 1
+: 2
`)
if !fakeT.failNowed {
t.Fatal("should have failNowed")
}
}

func TestErrorFailure(t *testing.T) {
Expand Down
37 changes: 19 additions & 18 deletions assert/cmp/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,33 @@ import (
)

func TestDeepEqual(t *testing.T) {
actual := DeepEqual([]string{"a", "b"}, []string{"b", "a"})()
expected := `
--- result
+++ exp
{[]string}[0]:
-: "a"
+: "b"
{[]string}[1]:
-: "b"
+: "a"
`
args := []ast.Expr{&ast.Ident{Name: "result"}, &ast.Ident{Name: "exp"}}
assertFailureTemplate(t, actual, args, expected)
t.Run("failure", func(t *testing.T) {
result := DeepEqual([]string{"a", "b"}, []string{"b", "a"})()
if result.Success() {
t.Errorf("expected failure")
}

args := []ast.Expr{&ast.Ident{Name: "result"}, &ast.Ident{Name: "exp"}}
message := result.(templatedResult).FailureMessage(args)
expected := "\n--- result\n+++ exp\n"
if !strings.HasPrefix(message, expected) {
t.Errorf("expected prefix \n%q\ngot\n%q\n", expected, message)
}
})

actual = DeepEqual([]string{"a"}, []string{"a"})()
assertSuccess(t, actual)
t.Run("success", func(t *testing.T) {
actual := DeepEqual([]string{"a"}, []string{"a"})()
assertSuccess(t, actual)
})
}

type Stub struct {
unx int
}

func TestDeepEqualeWithUnexported(t *testing.T) {
func TestDeepEqualWithUnexported(t *testing.T) {
result := DeepEqual(Stub{}, Stub{unx: 1})()
assertFailure(t, result, `cannot handle unexported field: {cmp.Stub}.unx
consider using AllowUnexported or cmpopts.IgnoreUnexported`)
assertFailureHasPrefix(t, result, `cannot handle unexported field: {cmp.Stub}.unx`)
}

func TestRegexp(t *testing.T) {
Expand Down
36 changes: 34 additions & 2 deletions assert/opt/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

gocmp "github.com/google/go-cmp/cmp"
"gotest.tools/assert"
"gotest.tools/internal/source"
)

func TestDurationWithThreshold(t *testing.T) {
Expand Down Expand Up @@ -186,7 +187,15 @@ func TestPathStringFromStruct(t *testing.T) {

spec := "Ref.Children.Labels.Value"
matches := matchPaths(fixture, PathString(spec))
expected := []string{`{opt.node}.Ref.Children[1].Labels["first"].Value`}
expected := []string{
`{opt.node}.Ref.Children[1].Labels["first"].Value`,
}

if !source.GoVersionLessThan(11) {
// as of google/go-cmp 0.3.0 and go1.11 PathFilter seems to traverse some parts
// of the tree more than once.
expected = append(expected, `{opt.node}.Ref.Children[1].Labels["first"].Value`)
}
assert.DeepEqual(t, matches, expected)
}

Expand All @@ -211,7 +220,19 @@ func TestPathStringFromSlice(t *testing.T) {

spec := "Ref.Children.Labels.Ref.Value"
matches := matchPaths(fixture, PathString(spec))
expected := []string{`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`}
expected := []string{
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
}

if !source.GoVersionLessThan(11) {
// as of google/go-cmp 0.3.0 and go1.11 PathFilter seems to traverse some parts
// of the tree more than once.
expected = append(expected,
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
`{[]opt.node}[0].Ref.Children[1].Labels["second"].Ref.Value`,
)
}
assert.DeepEqual(t, matches, expected)
}

Expand All @@ -234,6 +255,17 @@ func TestPathField(t *testing.T) {
"{opt.node}.Children[2].Value.Value",
"{opt.node}.Children[2].Ref.Value.Value",
}

if !source.GoVersionLessThan(11) {
// as of google/go-cmp 0.3.0 and go1.11 PathFilter seems to traverse some parts
// of the tree more than once.
expected = append(expected,
"{opt.node}.Children[0].Value.Value",
"{opt.node}.Children[1].Value.Value",
"{opt.node}.Children[2].Value.Value",
"{opt.node}.Children[2].Ref.Value.Value",
)
}
assert.DeepEqual(t, matches, expected)
}

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module gotest.tools

require (
github.com/google/go-cmp v0.2.0
github.com/pkg/errors v0.8.0
github.com/google/go-cmp v0.3.0
github.com/pkg/errors v0.8.1
github.com/spf13/pflag v1.0.3
golang.org/x/tools v0.0.0-20180810170437-e96c4e24768d
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4
)
17 changes: 11 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
golang.org/x/tools v0.0.0-20180810170437-e96c4e24768d h1:/VmFqYLs4Kit6ncc9mRWBGPNlBVSusq3xA7p41/7JoY=
golang.org/x/tools v0.0.0-20180810170437-e96c4e24768d/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4 h1:1mMox4TgefDwqluYCv677yNXwlfTkija4owZve/jr78=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
12 changes: 8 additions & 4 deletions internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func nodePosition(fileset *token.FileSet, node ast.Node) token.Position {
return fileset.Position(node.Pos())
}

var goVersionBefore19 = func() bool {
// GoVersionLessThan returns true if runtime.Version() is semantically less than
// version 1.minor.
func GoVersionLessThan(minor int64) bool {
version := runtime.Version()
// not a release version
if !strings.HasPrefix(version, "go") {
Expand All @@ -103,9 +105,11 @@ var goVersionBefore19 = func() bool {
if len(parts) < 2 {
return false
}
minor, err := strconv.ParseInt(parts[1], 10, 32)
return err == nil && parts[0] == "1" && minor < 9
}()
actual, err := strconv.ParseInt(parts[1], 10, 32)
return err == nil && parts[0] == "1" && actual < minor
}

var goVersionBefore19 = GoVersionLessThan(9)

func getCallExprArgs(node ast.Node) ([]ast.Expr, error) {
visitor := &callExprVisitor{}
Expand Down

0 comments on commit 04b215e

Please sign in to comment.