...
1
2 package assert
3
4 import (
5 "reflect"
6 "testing"
7
8 "github.com/google/go-cmp/cmp"
9 "github.com/google/go-cmp/cmp/cmpopts"
10 )
11
12
13 func Diff(exp, act interface{}, opts ...cmp.Option) string {
14 opts = append(opts, cmpopts.EquateErrors(), cmp.Exporter(func(r reflect.Type) bool {
15 return true
16 }))
17 return cmp.Diff(exp, act, opts...)
18 }
19
20
21 func Equal(t testing.TB, name string, exp, act interface{}) {
22 t.Helper()
23 if diff := Diff(exp, act); diff != "" {
24 t.Fatalf(`unexpected %v: diff:
25 %v`, name, diff)
26 }
27 }
28
29
30 func Success(t testing.TB, name string, err error) {
31 t.Helper()
32 if err != nil {
33 t.Fatalf("unexpected error for %v: %+v", name, err)
34 }
35 }
36
37
38 func Error(t testing.TB, name string, err error) {
39 t.Helper()
40 if err == nil {
41 t.Fatalf("expected error from %v", name)
42 }
43 }
44
45
46 func True(t testing.TB, name string, act bool) {
47 t.Helper()
48 Equal(t, name, true, act)
49 }
50
51
52 func False(t testing.TB, name string, act bool) {
53 t.Helper()
54 Equal(t, name, false, act)
55 }
56
57
58 func Len(t testing.TB, name string, n int, a interface{}) {
59 t.Helper()
60 act := reflect.ValueOf(a).Len()
61 if n != act {
62 t.Fatalf("expected len(%v) == %v but got %v", name, n, act)
63 }
64 }
65
View as plain text