Skip to content

Commit b30eb99

Browse files
committed
all: use go-cmp.Diff more consistently
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í <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/540482 Unity-Result: CUEcueckoo <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 621f437 commit b30eb99

File tree

13 files changed

+32
-31
lines changed

13 files changed

+32
-31
lines changed

cue/ast/astutil/util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func TestImportInfo(t *testing.T) {
6666
if err != nil {
6767
t.Fatal(err)
6868
}
69-
if !cmp.Equal(got, tc.want) {
70-
t.Error(cmp.Diff(got, tc.want))
69+
if diff := cmp.Diff(got, tc.want); diff != "" {
70+
t.Error(diff)
7171
}
7272
})
7373
}

cue/decode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ func TestDecode(t *testing.T) {
238238
checkFatal(t, err, tc.err, "init")
239239

240240
got := reflect.ValueOf(tc.dst).Elem().Interface()
241-
if !cmp.Equal(got, tc.want) {
242-
t.Error(cmp.Diff(got, tc.want))
241+
if diff := cmp.Diff(got, tc.want); diff != "" {
242+
t.Error(diff)
243243
t.Errorf("\n%#v\n%#v", got, tc.want)
244244
}
245245
})

cue/literal/num_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ func TestNumbers(t *testing.T) {
100100
n.src = ""
101101
n.p = 0
102102
n.ch = 0
103-
if !cmp.Equal(n, tc.n, diffOpts...) {
104-
t.Error(cmp.Diff(n, tc.n, diffOpts...))
103+
if diff := cmp.Diff(n, tc.n, diffOpts...); diff != "" {
104+
t.Error(diff)
105105
t.Errorf("%#v, %#v\n", n, tc.n)
106106
}
107107
if n.String() != tc.norm {

cue/marshal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ func TestMarshalMultiPackage(t *testing.T) {
194194
}
195195
got := strValue(insts)
196196

197-
if !cmp.Equal(got, want) {
198-
t.Error(cmp.Diff(got, want))
197+
if diff := cmp.Diff(got, want); diff != "" {
198+
t.Error(diff)
199199
}
200200
})
201201
}

cue/scanner/scanner_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ func TestRelative(t *testing.T) {
506506
got = append(got, fmt.Sprintf("%-7s %-8s %s", pos.RelPos(), tok, lit))
507507
pos, tok, lit = S.Scan()
508508
}
509-
if !cmp.Equal(got, want) {
510-
t.Error(cmp.Diff(got, want))
509+
if diff := cmp.Diff(got, want); diff != "" {
510+
t.Error(diff)
511511
}
512512
}
513513

cue/types_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -978,8 +978,8 @@ func TestFill(t *testing.T) {
978978

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

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

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

1242-
if !cmp.Equal(goValue(v), goValue(w)) {
1242+
if diff := cmp.Diff(goValue(v), goValue(w)); diff != "" {
1243+
t.Error(diff)
12431244
t.Error(cmp.Diff(goValue(v), goValue(w)))
12441245
t.Errorf("\ngot: %s\nwant: %s", v, w)
12451246
}

encoding/gocode/gocodec/codec_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"testing"
2121

2222
"github.com/google/go-cmp/cmp"
23-
"github.com/kr/pretty"
2423

2524
"cuelang.org/go/cue"
2625
)
@@ -219,8 +218,8 @@ func TestComplete(t *testing.T) {
219218

220219
err = codec.Complete(v, tc.value)
221220
checkErr(t, err, tc.err)
222-
if !reflect.DeepEqual(tc.value, tc.result) {
223-
t.Error(pretty.Diff(tc.value, tc.result))
221+
if diff := cmp.Diff(tc.value, tc.result); diff != "" {
222+
t.Error(diff)
224223
}
225224
})
226225
}
@@ -252,8 +251,8 @@ func TestEncode(t *testing.T) {
252251
}
253252

254253
got := reflect.ValueOf(tc.dst).Elem().Interface()
255-
if !cmp.Equal(got, tc.want) {
256-
t.Error(cmp.Diff(got, tc.want))
254+
if diff := cmp.Diff(got, tc.want); diff != "" {
255+
t.Error(diff)
257256
}
258257
})
259258
}

encoding/openapi/decode_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func TestDecode(t *testing.T) {
8787
if err != nil {
8888
got = []byte(err.Error())
8989
}
90-
if !cmp.Equal(errout, got) {
91-
t.Error(cmp.Diff(string(got), string(errout)))
90+
if diff := cmp.Diff(errout, got); diff != "" {
91+
t.Error(diff)
9292
}
9393

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

108-
if !cmp.Equal(b, out) {
108+
if diff := cmp.Diff(b, out); diff != "" {
109+
t.Error(diff)
109110
if cuetest.UpdateGoldenFiles {
110111
a.Files[outIndex].Data = b
111112
b = txtar.Format(a)

encoding/protobuf/protobuf_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"strings"
2525
"testing"
2626

27-
"github.com/kr/pretty"
27+
"github.com/google/go-cmp/cmp"
2828

2929
"cuelang.org/go/cue/ast"
3030
"cuelang.org/go/cue/errors"
@@ -67,8 +67,8 @@ func TestExtractDefinitions(t *testing.T) {
6767
t.Fatal(err)
6868
}
6969

70-
if desc := pretty.Diff(out.String(), string(b)); len(desc) > 0 {
71-
t.Errorf("files differ:\n%v", desc)
70+
if diff := cmp.Diff(out.String(), string(b)); diff != "" {
71+
t.Error(diff)
7272
}
7373
})
7474
}

internal/core/export/toposort_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestSortArcs(t *testing.T) {
8080

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

83-
if !cmp.Equal(keys, want) {
83+
if diff := cmp.Diff(keys, want); diff != "" {
8484
got := ""
8585
for _, f := range keys {
8686
got += " " + f.SelectorString(r)

0 commit comments

Comments
 (0)