/
assert.go
56 lines (45 loc) · 1.06 KB
/
assert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package assert
import (
"fmt"
"reflect"
"strings"
"testing"
"github.com/golang/protobuf/proto"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
// Diff returns a human readable diff between v1 and v2
func Diff(v1, v2 interface{}) string {
return cmp.Diff(v1, v2, cmpopts.EquateErrors(), cmp.Exporter(func(r reflect.Type) bool {
return true
}), cmp.Comparer(proto.Equal))
}
// Equal asserts exp == act.
func Equal(t testing.TB, name string, exp, act interface{}) {
t.Helper()
if diff := Diff(exp, act); diff != "" {
t.Fatalf("unexpected %v: %v", name, diff)
}
}
// Success asserts err == nil.
func Success(t testing.TB, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
// Error asserts err != nil.
func Error(t testing.TB, err error) {
t.Helper()
if err == nil {
t.Fatal("expected error")
}
}
// Contains asserts the fmt.Sprint(v) contains sub.
func Contains(t testing.TB, v interface{}, sub string) {
t.Helper()
s := fmt.Sprint(v)
if !strings.Contains(s, sub) {
t.Fatalf("expected %q to contain %q", s, sub)
}
}