-
Notifications
You must be signed in to change notification settings - Fork 15
/
assert.go
100 lines (81 loc) · 3.04 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package assert
import (
"reflect"
"testing"
)
// Helper performs assertions for a specific test.
type Helper interface {
// GetTest retrieves the test for which assertion facilities are being provided.
GetTest() *testing.T
// IsTrue asserts that the specified condition is true.
IsTrue(description string, condition bool)
// IsFalse asserts that the specified condition is true.
IsFalse(description string, condition bool)
// IsNil asserts that the specified value is nil.
IsNil(description string, actual interface{})
// IsNil asserts that the specified value is not nil.
NotNil(description string, actual interface{})
// Equals asserts that the specified values are equal.
//
// Uses reflect.DeepEquals.
Equals(description string, expected interface{}, actual interface{})
// EqualsString asserts that the specified string values are equal.
EqualsString(description string, expected string, actual string)
// EqualsInt asserts that the specified int values are equal.
EqualsInt(description string, expected int, actual int)
}
type assertHelper struct {
test *testing.T
}
// ForTest creates a new assertion Helper for the specified test.
func ForTest(test *testing.T) Helper {
return assertHelper{test}
}
// GetTest retrieves the test for which assertion facilities are being provided.
func (assert assertHelper) GetTest() *testing.T {
return assert.test
}
// IsTrue asserts that the specified condition is true.
func (assert assertHelper) IsTrue(description string, condition bool) {
if !condition {
assert.test.Fatalf("Expression was false: %s", description)
}
}
// IsFalse asserts that the specified condition is true.
func (assert assertHelper) IsFalse(description string, condition bool) {
if condition {
assert.test.Fatalf("Expression was true: %s", description)
}
}
// IsNil asserts that the specified value is nil.
func (assert assertHelper) IsNil(description string, actual interface{}) {
if !reflect.ValueOf(actual).IsNil() {
assert.test.Fatalf("%s was not nil.", description)
}
}
// IsNil asserts that the specified value is not nil.
func (assert assertHelper) NotNil(description string, actual interface{}) {
if reflect.ValueOf(actual).IsNil() {
assert.test.Fatalf("%s was nil.", description)
}
}
// Equals asserts that the specified values are equal.
//
// Uses reflect.DeepEqual.
func (assert assertHelper) Equals(description string, expected interface{}, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
assert.test.Fatalf("%s was '%#v' (expected '%#v').", description, actual, expected)
}
}
// EqualsString asserts that the specified string values are equal.
func (assert assertHelper) EqualsString(description string, expected string, actual string) {
if actual != expected {
assert.test.Fatalf("%s was '%s' (expected '%s').", description, actual, expected)
}
}
// EqualsInt asserts that the specified int values are equal.
func (assert assertHelper) EqualsInt(description string, expected int, actual int) {
if actual != expected {
assert.test.Fatalf("%s was %d (expected %d).", description, actual, expected)
}
}