Skip to content

Commit

Permalink
Update tests to use cmp.Diff instead of reflect.DeepEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
thempatel committed May 1, 2022
1 parent 56d35f8 commit dcdf49f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
2 changes: 2 additions & 0 deletions language/go/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ go_test(
"generate_test.go",
"resolve_test.go",
"stubs_test.go",
"testutils_test.go",
"update_import_test.go",
],
data = glob(["testdata/**"]),
Expand All @@ -78,6 +79,7 @@ go_test(
"//testtools",
"//walk",
"@com_github_bazelbuild_buildtools//build:go_default_library",
"@com_github_google_go_cmp//cmp",
"@org_golang_x_tools//go/vcs",
],
)
Expand Down
25 changes: 14 additions & 11 deletions language/go/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package golang
import (
"path"
"path/filepath"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -121,14 +120,15 @@ func TestDirectives(t *testing.T) {
if !gc.goGrpcCompilersSet {
t.Error("expected goGrpcCompilersSet to be set")
}
if !reflect.DeepEqual(gc.goGrpcCompilers, []string{"abc", "def"}) {
t.Errorf("got goGrpcCompilers %v; want [abc def]", gc.goGrpcCompilers)
if diff := cmpDiff([]string{"abc", "def"}, gc.goGrpcCompilers); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

if !gc.goProtoCompilersSet {
t.Error("expected goProtoCompilersSet to be set")
}
if !reflect.DeepEqual(gc.goProtoCompilers, []string{"foo", "bar"}) {
t.Errorf("got goProtoCompilers %v; want [foo bar]", gc.goProtoCompilers)
if diff := cmpDiff(gc.goProtoCompilers, []string{"foo", "bar"}); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

subContent := []byte(`
Expand All @@ -146,15 +146,17 @@ func TestDirectives(t *testing.T) {
if gc.goGrpcCompilersSet {
t.Error("expected goGrpcCompilersSet to be unset")
}
if !reflect.DeepEqual(gc.goGrpcCompilers, defaultGoGrpcCompilers) {
t.Errorf("got goGrpcCompilers %v; want %v", gc.goGrpcCompilers, defaultGoGrpcCompilers)
if diff := cmpDiff(defaultGoGrpcCompilers, gc.goGrpcCompilers); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

if gc.goProtoCompilersSet {
t.Error("expected goProtoCompilersSet to be unset")
}
if !reflect.DeepEqual(gc.goProtoCompilers, defaultGoProtoCompilers) {
t.Errorf("got goProtoCompilers %v; want %v", gc.goProtoCompilers, defaultGoProtoCompilers)
if diff := cmpDiff(defaultGoProtoCompilers, gc.goProtoCompilers); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

}

func TestVendorConfig(t *testing.T) {
Expand Down Expand Up @@ -340,8 +342,9 @@ func TestSplitValue(t *testing.T) {
},
} {
parts := splitValue(tc.value)
if !reflect.DeepEqual(tc.parts, parts) {
t.Errorf("want %v; got %v", tc.parts, parts)
if diff := cmpDiff(tc.parts, parts); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

}
}
21 changes: 12 additions & 9 deletions language/go/fileinfo_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -178,9 +177,10 @@ var src string
got.embeds[i] = fileEmbed{path: got.embeds[i].path}
}

if !reflect.DeepEqual(got, tc.want) {
t.Errorf("case %q: got %#v; want %#v", tc.desc, got, tc.want)
if diff := cmpDiff(tc.want, got); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

})
}
}
Expand All @@ -205,9 +205,10 @@ func TestGoFileInfoFailure(t *testing.T) {
goos: "linux",
goarch: "amd64",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %#v ; want %#v", got, want)
if diff := cmpDiff(want, got); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

}

func TestCgo(t *testing.T) {
Expand Down Expand Up @@ -331,9 +332,10 @@ import ("C")
clinkopts: got.clinkopts,
}

if !reflect.DeepEqual(got, tc.want) {
t.Errorf("case %q: got %#v; want %#v", tc.desc, got, tc.want)
if diff := cmpDiff(tc.want, got); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

})
}
}
Expand Down Expand Up @@ -409,9 +411,10 @@ import "C"
}
want.library.sources.addGenericString("sub.go")
want.library.copts.addGenericString("-Isub/..")
if !reflect.DeepEqual(got, want) {
t.Errorf("got %#v ; want %#v", got, want)
if diff := cmpDiff(want, got); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

}

// Copied from go/build build_test.go
Expand Down
9 changes: 5 additions & 4 deletions language/go/fileinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ func TestOtherFileInfo(t *testing.T) {

// Only check that we can extract tags. Everything else is covered
// by other tests.
if !reflect.DeepEqual(got.tags, tc.wantTags) {
t.Errorf("got %#v; want %#v", got.tags, tc.wantTags)
if diff := cmpDiff(tc.wantTags, got.tags); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}

})
}
}
Expand Down Expand Up @@ -331,8 +332,8 @@ package main`,

if got, err := readTags(path); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(got, tc.want) {
t.Errorf("case %q: got %#v; want %#v", tc.desc, got, tc.want)
} else if diff := cmpDiff(tc.want, got); diff != "" {
t.Errorf("(-want, +got): %s", diff)
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions language/go/testutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright 2017 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package golang

import (
"reflect"

"github.com/google/go-cmp/cmp"
)

// cmpDiff provides a diff func that can be used on any type. By default,
// cmp.Diff will only compare exported fields. However, in order to make
// testing un-exported types easier, this function will allow comparison
// for any unexported type.
func cmpDiff(want, got interface{}) string {
allTypes := func(_ reflect.Type) bool { return true }
return cmp.Diff(want, got, cmp.Exporter(allTypes))
}

0 comments on commit dcdf49f

Please sign in to comment.