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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
package shakers
import (
"reflect"
"time"
"github.com/go-check/check"
)
// As a commodity, we bring all check.Checker variables into the current namespace to avoid having
// to think about check.X versus checker.X.
var (
DeepEquals = check.DeepEquals
ErrorMatches = check.ErrorMatches
FitsTypeOf = check.FitsTypeOf
HasLen = check.HasLen
Implements = check.Implements
IsNil = check.IsNil
Matches = check.Matches
Not = check.Not
NotNil = check.NotNil
PanicMatches = check.PanicMatches
Panics = check.Panics
)
// Equaler is an interface implemented if the type has a Equal method.
// This is used to compare struct using shakers.Equals.
type Equaler interface {
Equal(Equaler) bool
}
// Equals checker verifies the obtained value is equal to the specified one.
// It's is smart in a wait that it supports several *types* (built-in, Equaler,
// time.Time)
//
// c.Assert(myStruct, Equals, aStruct, check.Commentf("bouuuhh"))
// c.Assert(myTime, Equals, aTime, check.Commentf("bouuuhh"))
//
var Equals check.Checker = &equalChecker{
&check.CheckerInfo{
Name: "Equals",
Params: []string{"obtained", "expected"},
},
}
type equalChecker struct {
*check.CheckerInfo
}
func (checker *equalChecker) Check(params []interface{}, names []string) (bool, string) {
return isEqual(params[0], params[1])
}
func isEqual(obtained, expected interface{}) (bool, string) {
switch obtained.(type) {
case time.Time:
return timeEquals(obtained, expected)
case Equaler:
return equalerEquals(obtained, expected)
default:
if reflect.TypeOf(obtained) != reflect.TypeOf(expected) {
return false, "obtained value and expected value have not the same type."
}
return obtained == expected, ""
}
}
func equalerEquals(obtained, expected interface{}) (bool, string) {
expectedEqualer, ok := expected.(Equaler)
if !ok {
return false, "expected value must be an Equaler - implementing Equal(Equaler)."
}
obtainedEqualer, ok := obtained.(Equaler)
if !ok {
return false, "obtained value must be an Equaler - implementing Equal(Equaler)."
}
return obtainedEqualer.Equal(expectedEqualer), ""
}
// GreaterThan checker verifies the obtained value is greater than the specified one.
// It's is smart in a wait that it supports several *types* (built-in, time.Time)
//
// c.Assert(myTime, GreaterThan, aTime, check.Commentf("bouuuhh"))
// c.Assert(myInt, GreaterThan, 2, check.Commentf("bouuuhh"))
//
var GreaterThan check.Checker = &greaterThanChecker{
&check.CheckerInfo{
Name: "GreaterThan",
Params: []string{"obtained", "expected"},
},
}
type greaterThanChecker struct {
*check.CheckerInfo
}
func (checker *greaterThanChecker) Check(params []interface{}, names []string) (bool, string) {
return greaterThan(params[0], params[1])
}
func greaterThan(obtained, expected interface{}) (bool, string) {
if _, ok := obtained.(time.Time); ok {
return isAfter(obtained, expected)
}
if reflect.TypeOf(obtained) != reflect.TypeOf(expected) {
return false, "obtained value and expected value have not the same type."
}
switch v := obtained.(type) {
case float32:
return v > expected.(float32), ""
case float64:
return v > expected.(float64), ""
case int:
return v > expected.(int), ""
case int8:
return v > expected.(int8), ""
case int16:
return v > expected.(int16), ""
case int32:
return v > expected.(int32), ""
case int64:
return v > expected.(int64), ""
case uint:
return v > expected.(uint), ""
case uint8:
return v > expected.(uint8), ""
case uint16:
return v > expected.(uint16), ""
case uint32:
return v > expected.(uint32), ""
case uint64:
return v > expected.(uint64), ""
default:
return false, "obtained value type not supported."
}
}
// GreaterOrEqualThan checker verifies the obtained value is greater or equal than the specified one.
// It's is smart in a wait that it supports several *types* (built-in, time.Time)
//
// c.Assert(myTime, GreaterOrEqualThan, aTime, check.Commentf("bouuuhh"))
// c.Assert(myInt, GreaterOrEqualThan, 2, check.Commentf("bouuuhh"))
//
var GreaterOrEqualThan check.Checker = &greaterOrEqualThanChecker{
&check.CheckerInfo{
Name: "GreaterOrEqualThan",
Params: []string{"obtained", "expected"},
},
}
type greaterOrEqualThanChecker struct {
*check.CheckerInfo
}
func (checker *greaterOrEqualThanChecker) Check(params []interface{}, names []string) (bool, string) {
return greaterOrEqualThan(params[0], params[1])
}
func greaterOrEqualThan(obtained, expected interface{}) (bool, string) {
if _, ok := obtained.(time.Time); ok {
return isAfter(obtained, expected)
}
if reflect.TypeOf(obtained) != reflect.TypeOf(expected) {
return false, "obtained value and expected value have not the same type."
}
switch v := obtained.(type) {
case float32:
return v >= expected.(float32), ""
case float64:
return v >= expected.(float64), ""
case int:
return v >= expected.(int), ""
case int8:
return v >= expected.(int8), ""
case int16:
return v >= expected.(int16), ""
case int32:
return v >= expected.(int32), ""
case int64:
return v >= expected.(int64), ""
case uint:
return v >= expected.(uint), ""
case uint8:
return v >= expected.(uint8), ""
case uint16:
return v >= expected.(uint16), ""
case uint32:
return v >= expected.(uint32), ""
case uint64:
return v >= expected.(uint64), ""
default:
return false, "obtained value type not supported."
}
}
// LessThan checker verifies the obtained value is less than the specified one.
// It's is smart in a wait that it supports several *types* (built-in, time.Time)
//
// c.Assert(myTime, LessThan, aTime, check.Commentf("bouuuhh"))
// c.Assert(myInt, LessThan, 2, check.Commentf("bouuuhh"))
//
var LessThan check.Checker = &lessThanChecker{
&check.CheckerInfo{
Name: "LessThan",
Params: []string{"obtained", "expected"},
},
}
type lessThanChecker struct {
*check.CheckerInfo
}
func (checker *lessThanChecker) Check(params []interface{}, names []string) (bool, string) {
return lessThan(params[0], params[1])
}
func lessThan(obtained, expected interface{}) (bool, string) {
if _, ok := obtained.(time.Time); ok {
return isBefore(obtained, expected)
}
if reflect.TypeOf(obtained) != reflect.TypeOf(expected) {
return false, "obtained value and expected value have not the same type."
}
switch v := obtained.(type) {
case float32:
return v < expected.(float32), ""
case float64:
return v < expected.(float64), ""
case int:
return v < expected.(int), ""
case int8:
return v < expected.(int8), ""
case int16:
return v < expected.(int16), ""
case int32:
return v < expected.(int32), ""
case int64:
return v < expected.(int64), ""
case uint:
return v < expected.(uint), ""
case uint8:
return v < expected.(uint8), ""
case uint16:
return v < expected.(uint16), ""
case uint32:
return v < expected.(uint32), ""
case uint64:
return v < expected.(uint64), ""
default:
return false, "obtained value type not supported."
}
}
// LessOrEqualThan checker verifies the obtained value is less or equal than the specified one.
// It's is smart in a wait that it supports several *types* (built-in, time.Time)
//
// c.Assert(myTime, LessThan, aTime, check.Commentf("bouuuhh"))
// c.Assert(myInt, LessThan, 2, check.Commentf("bouuuhh"))
//
var LessOrEqualThan check.Checker = &lessOrEqualThanChecker{
&check.CheckerInfo{
Name: "LessOrEqualThan",
Params: []string{"obtained", "expected"},
},
}
type lessOrEqualThanChecker struct {
*check.CheckerInfo
}
func (checker *lessOrEqualThanChecker) Check(params []interface{}, names []string) (bool, string) {
return lessOrEqualThan(params[0], params[1])
}
func lessOrEqualThan(obtained, expected interface{}) (bool, string) {
if _, ok := obtained.(time.Time); ok {
return isBefore(obtained, expected)
}
if reflect.TypeOf(obtained) != reflect.TypeOf(expected) {
return false, "obtained value and expected value have not the same type."
}
switch v := obtained.(type) {
case float32:
return v <= expected.(float32), ""
case float64:
return v <= expected.(float64), ""
case int:
return v <= expected.(int), ""
case int8:
return v <= expected.(int8), ""
case int16:
return v <= expected.(int16), ""
case int32:
return v <= expected.(int32), ""
case int64:
return v <= expected.(int64), ""
case uint:
return v <= expected.(uint), ""
case uint8:
return v <= expected.(uint8), ""
case uint16:
return v <= expected.(uint16), ""
case uint32:
return v <= expected.(uint32), ""
case uint64:
return v <= expected.(uint64), ""
default:
return false, "obtained value type not supported."
}
}
|