Skip to content

Commit

Permalink
all: use go-cmp.Diff more consistently
Browse files Browse the repository at this point in the history
There's no need to first call cmp.Equal and then cmp.Diff;
use cmp.Diff alone, and test whether the diff is empty.

Also prefer cmp.Diff over pretty.Diff, for the sake of consistency and
fewer dependencies in the future. Note that we still need kr/pretty for
pretty.Print, which has no equivalent in go-cmp.

Change-Id: I9701b588822ac9c490e514e6767aebf26b044931
Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/540482
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed Jun 24, 2022
1 parent 621f437 commit b30eb99
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 31 deletions.
4 changes: 2 additions & 2 deletions cue/ast/astutil/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func TestImportInfo(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(got, tc.want) {
t.Error(cmp.Diff(got, tc.want))
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Error(diff)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions cue/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ func TestDecode(t *testing.T) {
checkFatal(t, err, tc.err, "init")

got := reflect.ValueOf(tc.dst).Elem().Interface()
if !cmp.Equal(got, tc.want) {
t.Error(cmp.Diff(got, tc.want))
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Error(diff)
t.Errorf("\n%#v\n%#v", got, tc.want)
}
})
Expand Down
4 changes: 2 additions & 2 deletions cue/literal/num_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func TestNumbers(t *testing.T) {
n.src = ""
n.p = 0
n.ch = 0
if !cmp.Equal(n, tc.n, diffOpts...) {
t.Error(cmp.Diff(n, tc.n, diffOpts...))
if diff := cmp.Diff(n, tc.n, diffOpts...); diff != "" {
t.Error(diff)
t.Errorf("%#v, %#v\n", n, tc.n)
}
if n.String() != tc.norm {
Expand Down
4 changes: 2 additions & 2 deletions cue/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ func TestMarshalMultiPackage(t *testing.T) {
}
got := strValue(insts)

if !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
if diff := cmp.Diff(got, want); diff != "" {
t.Error(diff)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions cue/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ func TestRelative(t *testing.T) {
got = append(got, fmt.Sprintf("%-7s %-8s %s", pos.RelPos(), tok, lit))
pos, tok, lit = S.Scan()
}
if !cmp.Equal(got, want) {
t.Error(cmp.Diff(got, want))
if diff := cmp.Diff(got, want); diff != "" {
t.Error(diff)
}
}

Expand Down
7 changes: 4 additions & 3 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,8 +978,8 @@ func TestFill(t *testing.T) {

w := compileT(t, r, tc.out).Value()

if !cmp.Equal(goValue(v), goValue(w)) {
t.Error(cmp.Diff(goValue(v), goValue(w)))
if diff := cmp.Diff(goValue(v), goValue(w)); diff != "" {
t.Error(diff)
t.Errorf("\ngot: %s\nwant: %s", v, w)
}
}
Expand Down Expand Up @@ -1239,7 +1239,8 @@ func TestFillPath(t *testing.T) {

w := compileT(t, r, tc.out).Value()

if !cmp.Equal(goValue(v), goValue(w)) {
if diff := cmp.Diff(goValue(v), goValue(w)); diff != "" {
t.Error(diff)
t.Error(cmp.Diff(goValue(v), goValue(w)))
t.Errorf("\ngot: %s\nwant: %s", v, w)
}
Expand Down
9 changes: 4 additions & 5 deletions encoding/gocode/gocodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/kr/pretty"

"cuelang.org/go/cue"
)
Expand Down Expand Up @@ -219,8 +218,8 @@ func TestComplete(t *testing.T) {

err = codec.Complete(v, tc.value)
checkErr(t, err, tc.err)
if !reflect.DeepEqual(tc.value, tc.result) {
t.Error(pretty.Diff(tc.value, tc.result))
if diff := cmp.Diff(tc.value, tc.result); diff != "" {
t.Error(diff)
}
})
}
Expand Down Expand Up @@ -252,8 +251,8 @@ func TestEncode(t *testing.T) {
}

got := reflect.ValueOf(tc.dst).Elem().Interface()
if !cmp.Equal(got, tc.want) {
t.Error(cmp.Diff(got, tc.want))
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Error(diff)
}
})
}
Expand Down
7 changes: 4 additions & 3 deletions encoding/openapi/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func TestDecode(t *testing.T) {
if err != nil {
got = []byte(err.Error())
}
if !cmp.Equal(errout, got) {
t.Error(cmp.Diff(string(got), string(errout)))
if diff := cmp.Diff(errout, got); diff != "" {
t.Error(diff)
}

if expr != nil {
Expand All @@ -105,7 +105,8 @@ func TestDecode(t *testing.T) {
b = bytes.TrimSpace(b)
out = bytes.TrimSpace(out)

if !cmp.Equal(b, out) {
if diff := cmp.Diff(b, out); diff != "" {
t.Error(diff)
if cuetest.UpdateGoldenFiles {
a.Files[outIndex].Data = b
b = txtar.Format(a)
Expand Down
6 changes: 3 additions & 3 deletions encoding/protobuf/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"strings"
"testing"

"github.com/kr/pretty"
"github.com/google/go-cmp/cmp"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
Expand Down Expand Up @@ -67,8 +67,8 @@ func TestExtractDefinitions(t *testing.T) {
t.Fatal(err)
}

if desc := pretty.Diff(out.String(), string(b)); len(desc) > 0 {
t.Errorf("files differ:\n%v", desc)
if diff := cmp.Diff(out.String(), string(b)); diff != "" {
t.Error(diff)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/export/toposort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestSortArcs(t *testing.T) {

want := parseFeatures(r, tc.out)[0]

if !cmp.Equal(keys, want) {
if diff := cmp.Diff(keys, want); diff != "" {
got := ""
for _, f := range keys {
got += " " + f.SelectorString(r)
Expand Down
4 changes: 2 additions & 2 deletions internal/filetypes/filetypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func check(t *testing.T, want, x interface{}, err error) {
if err != nil {
x = errors.String(err.(errors.Error))
}
if !cmp.Equal(x, want, cmpopts.EquateEmpty()) {
t.Error(cmp.Diff(want, x))
if diff := cmp.Diff(x, want, cmpopts.EquateEmpty()); diff != "" {
t.Error(diff)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/tool/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func TestEnv(t *testing.T) {
t.Fatal(err)
}

if !cmp.Equal(cmd.Env, tc.env) {
t.Error(cmp.Diff(cmd.Env, tc.env))
if diff := cmp.Diff(cmd.Env, tc.env); diff != "" {
t.Error(diff)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/tool/os/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func TestGetenv(t *testing.T) {
}),
}

if !cmp.Equal(got, want, opts...) {
t.Error(cmp.Diff(got, want, opts...))
if diff := cmp.Diff(got, want, opts...); diff != "" {
t.Error(diff)
}

// Errors:
Expand Down

0 comments on commit b30eb99

Please sign in to comment.