-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertions.go
150 lines (132 loc) · 3.17 KB
/
assertions.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cogol
import (
"fmt"
"reflect"
)
const (
failureMsg = "Expected %v, got %v"
)
// assertion represents a typical assertion
// Includes expected and actual values, as well as the context (ctx) of the the test
type assertion struct {
expected interface{}
actual interface{}
ctx *Context
kill func(f *failure)
}
// failure represents a failed assertion, with the test's context (ctx)
// and error message (msg)
type failure struct {
ctx *Context
msg string
}
// fail is a more beautiful way to create a failure instance
func (a *assertion) fail(msg string) *failure {
return &failure{ctx: a.ctx, msg: msg}
}
// UseKiller allows users to use custom assertion killers
func (ctx *Context) UseKiller(killer func(f *failure)) {
ctx.assertionKiller = killer
}
// Expect creates a new assertion with actual value and nil as an expected one
func (ctx *Context) Expect(actual interface{}) *assertion {
return &assertion{nil, actual, ctx, ctx.assertionKiller}
}
// defaultKiller just calls ctx.Kill method instead of doing some fancy stuff
func defaultKiller(f *failure) {
f.ctx.Kill(f.msg)
}
// ToBe receives an expected assertion value and then compares it to the actual one, if they do not match, killer-method id called
func (a *assertion) ToBe(expected interface{}) {
a.expected = expected
if a.actual == nil {
a.kill(a.fail(
fmt.Sprintf(failureMsg,
a.expected, "nil",
)),
)
return
}
if reflect.TypeOf(a.expected) != reflect.TypeOf(a.actual) {
a.kill(a.fail(
fmt.Sprintf(failureMsg,
"type to be "+reflect.TypeOf(a.expected).String(),
reflect.TypeOf(a.actual).String()),
))
return
}
if !reflect.DeepEqual(a.expected, a.actual) {
a.kill(a.fail(
fmt.Sprintf(failureMsg, a.expected, a.actual),
))
return
}
a.ctx.test.success = true
}
func (a *assertion) ToBeNot(unexpected interface{}) {
if reflect.DeepEqual(a.actual, unexpected) {
a.kill(a.fail(
fmt.Sprintf("Expected %v to be not %v", a.actual, unexpected),
))
return
}
a.ctx.test.success = true
}
func (a *assertion) ToBeNil() {
if a.actual != nil {
a.kill(a.fail(
fmt.Sprintf(failureMsg, "nil", a.actual),
))
return
}
a.ctx.test.success = true
}
func (a *assertion) ToBeNotNil() {
if a.actual == nil {
a.kill(a.fail(
fmt.Sprintf(failureMsg, "not nil", "nil"),
))
return
}
a.ctx.test.success = true
}
func (a *assertion) ToBeZero() {
if !reflect.ValueOf(a.actual).IsZero() {
a.kill(a.fail(
fmt.Sprintf(failureMsg,
"zero value for "+reflect.TypeOf(a.actual).String()+" type",
a.actual),
))
return
}
a.ctx.test.success = true
}
func (a *assertion) ToBeNotZero() {
if reflect.ValueOf(a.actual).IsZero() {
a.kill(a.fail(
fmt.Sprintf(failureMsg,
"not zero value for "+reflect.TypeOf(a.actual).String()+" type",
"zero value"),
))
return
}
a.ctx.test.success = true
}
func (a *assertion) ToBeTrue() {
if !reflect.DeepEqual(a.actual, true) {
a.kill(a.fail(
fmt.Sprintf(failureMsg, "true", a.actual),
))
return
}
a.ctx.test.success = true
}
func (a *assertion) ToBeFalse() {
if !reflect.DeepEqual(a.actual, false) {
a.kill(a.fail(
fmt.Sprintf(failureMsg, "false", a.actual),
))
return
}
a.ctx.test.success = true
}