This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
/
matcher.go
169 lines (157 loc) · 4.83 KB
/
matcher.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package match
import (
"reflect"
"regexp"
"strings"
"testing"
)
// Matcher is a helper object for chaining matcher conditions.
type Matcher struct {
t *testing.T
value interface{}
}
// Fatal causes the unit test to immediately fail is one of the previous
// conditions has fail.
// Note: This will only affect previous conditions, not subsequent ones. You
// can build complex conditions this way.
// Ex: match.IsNotNil(t, value).Fatal().Equals(other)
func (m *Matcher) Fatal() *Matcher {
if m.t.Failed() {
m.t.FailNow()
}
return m
}
// IsNil asserts that the matched value is nil.
func (m *Matcher) IsNil() *Matcher {
if m.value != nil {
m.t.Errorf("expected == nil, got %v", m.value)
}
return m
}
// IsNotNil asserts that the matched value is not nil.
func (m *Matcher) IsNotNil() *Matcher {
if m.value == nil {
m.t.Error("expected != nil, got nil")
}
return m
}
// Equals asserts that the matched value is equal to value.
func (m *Matcher) Equals(value interface{}) *Matcher {
if !reflect.DeepEqual(m.value, value) {
m.t.Errorf("expected == %v, got %v", value, m.value)
}
return m
}
// NotEquals asserts that the match value is not equal to value.
func (m *Matcher) NotEquals(value interface{}) *Matcher {
if reflect.DeepEqual(m.value, value) {
m.t.Errorf("expected != %v, got %v", value, m.value)
}
return m
}
// LessThan asserts that the matched value is less than value.
// Note: This function will only work on numeric values.
// If a non-numeric value is passed, a fatal error will be thrown.
func (m *Matcher) LessThan(value interface{}) *Matcher {
ok := true
rv := reflect.ValueOf(m.value)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ok = rv.Int() < reflect.ValueOf(value).Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
ok = rv.Uint() < uint64(reflect.ValueOf(value).Int())
case reflect.Float32, reflect.Float64:
ok = rv.Float() < reflect.ValueOf(value).Float()
default:
m.t.Fatalf("expected numeric value, got %v", m.value)
}
if !ok {
m.t.Errorf("expected < %v, got %v", value, m.value)
}
return m
}
// GreaterThan asserts that the matched value is greater then value.
// Note: This function will only work on numeric values.
// If a non-numeric value is passed, a fatal error will be thrown.
func (m *Matcher) GreaterThan(value interface{}) *Matcher {
ok := true
rv := reflect.ValueOf(m.value)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ok = rv.Int() > reflect.ValueOf(value).Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
ok = rv.Uint() > uint64(reflect.ValueOf(value).Int())
case reflect.Float32, reflect.Float64:
ok = rv.Float() > reflect.ValueOf(value).Float()
default:
m.t.Fatalf("expected numeric value, got %v", m.value)
}
if !ok {
m.t.Errorf("expected < %v, got %v", value, m.value)
}
return m
}
// Contains asserts that the matched value contains value.
// If used on an array, asserts that the array contains value.
// If used on a map, asserts that the map contains a key with value.
// If used on a string, asserts that the string contains value.
// If used on an incompatible value, a fatal error will be thrown.
func (m *Matcher) Contains(value interface{}) *Matcher {
rv := reflect.ValueOf(m.value)
switch rv.Kind() {
case reflect.Array, reflect.Slice:
ok := false
for i := 0; i < rv.Len(); i++ {
if rv.Index(i).Interface() == value {
ok = true
break
}
}
if !ok {
m.t.Errorf("expected array to contain %v", value)
}
case reflect.Map:
ok := false
for _, key := range rv.MapKeys() {
if key.Interface() == value {
ok = true
break
}
}
if !ok {
m.t.Errorf("expected map to have key %v", value)
}
case reflect.String:
if !strings.Contains(rv.String(), value.(string)) {
m.t.Errorf("expected %#q to contain %#q", rv.String(), value.(string))
}
default:
m.t.Fatalf("expected array, map or string, got %s", rv.Kind().String())
}
return m
}
// Matches asserts that the matched value matches the regex in pattern.
// Note: If the regex cannot be compiled, a fatal error will be thrown.
// If used on a non-string value, a fatal error will be thrown.
func (m *Matcher) Matches(pattern string) *Matcher {
rv := reflect.ValueOf(m.value)
if rv.Kind() != reflect.String {
m.t.Fatalf("expected string, got %s", rv.Kind().String())
}
re, err := regexp.Compile(pattern)
if err != nil {
m.t.Fatal(err)
}
if !re.MatchString(rv.String()) {
m.t.Errorf("expected %#q to match pattern %#q", rv.String(), pattern)
}
return m
}
// KindOf asserts that the matched value of the kind.
func (m *Matcher) KindOf(kind reflect.Kind) *Matcher {
rv := reflect.ValueOf(m.value)
if rv.Kind() != kind {
m.t.Errorf("expected kind %s, got %s", kind.String(), rv.Kind().String())
}
return m
}