-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert.go
71 lines (59 loc) · 1.87 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package assert
import (
"fmt"
"reflect"
"runtime"
"strings"
"testing"
)
// Usage:
// assert := assert.Assert(t)
// ...
// assert.Equal(actual, expected)
// assert.Nil(err, "X threw error")
func Assert(t *testing.T) *Assertion {
return &Assertion{t}
}
type Assertion struct {
t *testing.T
}
func caller() string {
fname, line, caller := "assert.go", 0, 1
for ; strings.HasSuffix(fname, "assert.go") && caller < 4; caller++ {
_, fname, line, _ = runtime.Caller(caller)
}
fname = fname[strings.LastIndex(fname, "/")+1:]
return fmt.Sprintf("\n%v:%v ", fname, line)
}
func (a *Assertion) True(b bool, message string, messageParams ...interface{}) {
if !b {
a.t.Fatalf(caller()+message, messageParams...)
}
}
func (a *Assertion) False(b bool, message string, messageParams ...interface{}) {
a.True(!b, message, messageParams...)
}
func (a *Assertion) Nil(val interface{}) {
isNil := val == nil || reflect.ValueOf(val).IsNil()
a.True(isNil, fmt.Sprintf("Expected nil but was %v", val))
}
func (a *Assertion) NotNil(val interface{}) {
isNil := val == nil || reflect.ValueOf(val).IsNil()
a.False(isNil, "Expected not nil but was nil")
}
func (a *Assertion) Same(actual, expected interface{}) {
eq := reflect.ValueOf(actual).Pointer() == reflect.ValueOf(expected).Pointer()
a.True(eq, "\nExpected: %v\nReceived: %v", expected, actual)
}
func (a *Assertion) NotSame(actual, expected interface{}) {
eq := reflect.ValueOf(actual).Pointer() == reflect.ValueOf(expected).Pointer()
a.False(eq, "\nExpected: %v\nReceived: %v", expected, actual)
}
func (a *Assertion) Equal(actual, expected interface{}) {
eq := reflect.DeepEqual(actual, expected)
a.True(eq, "\nExpected: %v\nReceived: %v", expected, actual)
}
func (a *Assertion) NotEqual(actual, expected interface{}) {
eq := reflect.DeepEqual(actual, expected)
a.False(eq, "Expected %v to not equal %v, but it did", expected, actual)
}