-
Notifications
You must be signed in to change notification settings - Fork 1
/
checker.go
executable file
·202 lines (164 loc) · 5.25 KB
/
checker.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package checkers
import (
"fmt"
"reflect"
"strings"
"time"
gc "launchpad.net/gocheck"
)
func TimeBetween(start, end time.Time) gc.Checker {
if end.Before(start) {
return &timeBetweenChecker{end, start}
}
return &timeBetweenChecker{start, end}
}
type timeBetweenChecker struct {
start, end time.Time
}
func (checker *timeBetweenChecker) Info() *gc.CheckerInfo {
info := gc.CheckerInfo{
Name: "TimeBetween",
Params: []string{"obtained"},
}
return &info
}
func (checker *timeBetweenChecker) Check(params []interface{}, names []string) (result bool, error string) {
when, ok := params[0].(time.Time)
if !ok {
return false, "obtained value type must be time.Time"
}
if when.Before(checker.start) {
return false, fmt.Sprintf("obtained value %#v type must before start value of %#v", when, checker.start)
}
if when.After(checker.end) {
return false, fmt.Sprintf("obtained value %#v type must after end value of %#v", when, checker.end)
}
return true, ""
}
// DurationLessThan checker
type durationLessThanChecker struct {
*gc.CheckerInfo
}
var DurationLessThan gc.Checker = &durationLessThanChecker{
&gc.CheckerInfo{Name: "DurationLessThan", Params: []string{"obtained", "expected"}},
}
func (checker *durationLessThanChecker) Check(params []interface{}, names []string) (result bool, error string) {
obtained, ok := params[0].(time.Duration)
if !ok {
return false, "obtained value type must be time.Duration"
}
expected, ok := params[1].(time.Duration)
if !ok {
return false, "expected value type must be time.Duration"
}
return obtained.Nanoseconds() < expected.Nanoseconds(), ""
}
// HasPrefix checker for checking strings
func stringOrStringer(value interface{}) (string, bool) {
result, isString := value.(string)
if !isString {
if stringer, isStringer := value.(fmt.Stringer); isStringer {
result, isString = stringer.String(), true
}
}
return result, isString
}
type hasPrefixChecker struct {
*gc.CheckerInfo
}
var HasPrefix gc.Checker = &hasPrefixChecker{
&gc.CheckerInfo{Name: "HasPrefix", Params: []string{"obtained", "expected"}},
}
func (checker *hasPrefixChecker) Check(params []interface{}, names []string) (result bool, error string) {
expected, ok := params[1].(string)
if !ok {
return false, "expected must be a string"
}
obtained, isString := stringOrStringer(params[0])
if isString {
return strings.HasPrefix(obtained, expected), ""
}
return false, "Obtained value is not a string and has no .String()"
}
type hasSuffixChecker struct {
*gc.CheckerInfo
}
var HasSuffix gc.Checker = &hasSuffixChecker{
&gc.CheckerInfo{Name: "HasSuffix", Params: []string{"obtained", "expected"}},
}
func (checker *hasSuffixChecker) Check(params []interface{}, names []string) (result bool, error string) {
expected, ok := params[1].(string)
if !ok {
return false, "expected must be a string"
}
obtained, isString := stringOrStringer(params[0])
if isString {
return strings.HasSuffix(obtained, expected), ""
}
return false, "Obtained value is not a string and has no .String()"
}
type containsChecker struct {
*gc.CheckerInfo
}
var Contains gc.Checker = &containsChecker{
&gc.CheckerInfo{Name: "Contains", Params: []string{"obtained", "expected"}},
}
func (checker *containsChecker) Check(params []interface{}, names []string) (result bool, error string) {
expected, ok := params[1].(string)
if !ok {
return false, "expected must be a string"
}
obtained, isString := stringOrStringer(params[0])
if isString {
return strings.Contains(obtained, expected), ""
}
return false, "Obtained value is not a string and has no .String()"
}
type sameContents struct {
*gc.CheckerInfo
}
// SameContents checks that the obtained slice contains all the values (and
// same number of values) of the expected slice and vice versa, without respect
// to order or duplicates. Uses DeepEquals on mapped contents to compare.
var SameContents gc.Checker = &sameContents{
&gc.CheckerInfo{Name: "SameContents", Params: []string{"obtained", "expected"}},
}
func (checker *sameContents) Check(params []interface{}, names []string) (result bool, error string) {
if len(params) != 2 {
return false, "SameContents expects two slice arguments"
}
obtained := params[0]
expected := params[1]
tob := reflect.TypeOf(obtained)
if tob.Kind() != reflect.Slice {
return false, fmt.Sprintf("SameContents expects the obtained value to be a slice, got %q",
tob.Kind())
}
texp := reflect.TypeOf(expected)
if texp.Kind() != reflect.Slice {
return false, fmt.Sprintf("SameContents expects the expected value to be a slice, got %q",
texp.Kind())
}
if texp != tob {
return false, fmt.Sprintf(
"SameContents expects two slices of the same type, expected: %q, got: %q",
texp, tob)
}
vexp := reflect.ValueOf(expected)
vob := reflect.ValueOf(obtained)
length := vexp.Len()
if vob.Len() != length {
// Slice has incorrect number of elements
return false, ""
}
// spin up maps with the entries as keys and the counts as values
mob := make(map[interface{}]int, length)
mexp := make(map[interface{}]int, length)
for i := 0; i < length; i++ {
mexp[vexp.Index(i).Interface()]++
mob[vob.Index(i).Interface()]++
}
return reflect.DeepEqual(mob, mexp), ""
}