| 1 |
// Copyright (c) 2018-2022, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"math" |
| 12 |
"reflect" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 16 |
"github.com/maxatome/go-testdeep/internal/types" |
| 17 |
"github.com/maxatome/go-testdeep/internal/util" |
| 18 |
) |
| 19 |
|
| 20 |
type boundCmp uint8 |
| 21 |
|
| 22 |
const ( |
| 23 |
boundNone boundCmp = iota |
| 24 |
boundIn |
| 25 |
boundOut |
| 26 |
) |
| 27 |
|
| 28 |
type tdBetween struct { |
| 29 |
base |
| 30 |
expectedMin reflect.Value |
| 31 |
expectedMax reflect.Value |
| 32 |
|
| 33 |
minBound boundCmp |
| 34 |
maxBound boundCmp |
| 35 |
} |
| 36 |
|
| 37 |
var _ TestDeep = &tdBetween{} |
| 38 |
|
| 39 |
// BoundsKind type qualifies the [Between] bounds. |
| 40 |
type BoundsKind uint8 |
| 41 |
|
| 42 |
const ( |
| 43 |
_ BoundsKind = (iota - 1) & 3 |
| 44 |
BoundsInIn // allows to match between "from" and "to" both included. |
| 45 |
BoundsInOut // allows to match between "from" included and "to" excluded. |
| 46 |
BoundsOutIn // allows to match between "from" excluded and "to" included. |
| 47 |
BoundsOutOut // allows to match between "from" and "to" both excluded. |
| 48 |
) |
| 49 |
|
| 50 |
type tdBetweenTime struct { |
| 51 |
tdBetween |
| 52 |
expectedType reflect.Type |
| 53 |
mustConvert bool |
| 54 |
} |
| 55 |
|
| 56 |
var _ TestDeep = &tdBetweenTime{} |
| 57 |
|
| 58 |
type tdBetweenCmp struct { |
| 59 |
tdBetween |
| 60 |
expectedType reflect.Type |
| 61 |
cmp func(a, b reflect.Value) int |
| 62 |
} |
| 63 |
|
| 64 |
// summary(Between): checks that a number, string or time.Time is |
| 65 |
// between two bounds |
| 66 |
// input(Between): str,int,float,cplx(todo),struct(time.Time) |
| 67 |
|
| 68 |
// Between operator checks that data is between from and |
| 69 |
// to. from and to can be any numeric, string, [time.Time] (or |
| 70 |
// assignable) value or implement at least one of the two following |
| 71 |
// methods: |
| 72 |
// |
| 73 |
// func (a T) Less(b T) bool // returns true if a < b |
| 74 |
// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b |
| 75 |
// |
| 76 |
// from and to must be the same type as the compared value, except |
| 77 |
// if BeLax config flag is true. [time.Duration] type is accepted as |
| 78 |
// to when from is [time.Time] or convertible. bounds allows to |
| 79 |
// specify whether bounds are included or not: |
| 80 |
// - [BoundsInIn] (default): between from and to both included |
| 81 |
// - [BoundsInOut]: between from included and to excluded |
| 82 |
// - [BoundsOutIn]: between from excluded and to included |
| 83 |
// - [BoundsOutOut]: between from and to both excluded |
| 84 |
// |
| 85 |
// If bounds is missing, it defaults to [BoundsInIn]. |
| 86 |
// |
| 87 |
// tc.Cmp(t, 17, td.Between(17, 20)) // succeeds, BoundsInIn by default |
| 88 |
// tc.Cmp(t, 17, td.Between(10, 17, BoundsInOut)) // fails |
| 89 |
// tc.Cmp(t, 17, td.Between(10, 17, BoundsOutIn)) // succeeds |
| 90 |
// tc.Cmp(t, 17, td.Between(17, 20, BoundsOutOut)) // fails |
| 91 |
// tc.Cmp(t, // succeeds |
| 92 |
// netip.MustParse("127.0.0.1"), |
| 93 |
// td.Between(netip.MustParse("127.0.0.0"), netip.MustParse("127.255.255.255"))) |
| 94 |
// |
| 95 |
// TypeBehind method returns the [reflect.Type] of from. |
| 96 |
func Between(from, to any, bounds ...BoundsKind) TestDeep { |
| 97 |
b := tdBetween{ |
| 98 |
base: newBase(3), |
| 99 |
expectedMin: reflect.ValueOf(from), |
| 100 |
expectedMax: reflect.ValueOf(to), |
| 101 |
} |
| 102 |
|
| 103 |
const usage = "(NUM|STRING|TIME, NUM|STRING|TIME/DURATION[, BOUNDS_KIND])" |
| 104 |
|
| 105 |
if len(bounds) > 0 { |
| 106 |
if len(bounds) > 1 { |
| 107 |
b.err = ctxerr.OpTooManyParams("Between", usage) |
| 108 |
return &b |
| 109 |
} |
| 110 |
|
| 111 |
if bounds[0] == BoundsInIn || bounds[0] == BoundsInOut { |
| 112 |
b.minBound = boundIn |
| 113 |
} else { |
| 114 |
b.minBound = boundOut |
| 115 |
} |
| 116 |
|
| 117 |
if bounds[0] == BoundsInIn || bounds[0] == BoundsOutIn { |
| 118 |
b.maxBound = boundIn |
| 119 |
} else { |
| 120 |
b.maxBound = boundOut |
| 121 |
} |
| 122 |
} else { |
| 123 |
b.minBound = boundIn |
| 124 |
b.maxBound = boundIn |
| 125 |
} |
| 126 |
|
| 127 |
if b.expectedMax.Type() == b.expectedMin.Type() { |
| 128 |
return b.initBetween(usage) |
| 129 |
} |
| 130 |
|
| 131 |
// Special case for (TIME, DURATION) |
| 132 |
ok, convertible := types.IsTypeOrConvertible(b.expectedMin, types.Time) |
| 133 |
if ok { |
| 134 |
if d, ok := to.(time.Duration); ok { |
| 135 |
if convertible { |
| 136 |
b.expectedMax = reflect.ValueOf( |
| 137 |
b.expectedMin. |
| 138 |
Convert(types.Time). |
| 139 |
Interface().(time.Time). |
| 140 |
Add(d)). |
| 141 |
Convert(b.expectedMin.Type()) |
| 142 |
} else { |
| 143 |
b.expectedMax = reflect.ValueOf(from.(time.Time).Add(d)) |
| 144 |
} |
| 145 |
return b.initBetween(usage) |
| 146 |
} |
| 147 |
b.err = ctxerr.OpBad("Between", |
| 148 |
"Between(FROM, TO): when FROM type is %[1]s, TO must have the same type or time.Duration: %[2]s ≠ %[1]s|time.Duration", |
| 149 |
b.expectedMin.Type(), |
| 150 |
b.expectedMax.Type(), |
| 151 |
) |
| 152 |
return &b |
| 153 |
} |
| 154 |
|
| 155 |
b.err = ctxerr.OpBad("Between", |
| 156 |
"Between(FROM, TO): FROM and TO must have the same type: %s ≠ %s", |
| 157 |
b.expectedMin.Type(), |
| 158 |
b.expectedMax.Type(), |
| 159 |
) |
| 160 |
return &b |
| 161 |
} |
| 162 |
|
| 163 |
func (b *tdBetween) initBetween(usage string) TestDeep { |
| 164 |
if !b.expectedMax.IsValid() { |
| 165 |
b.expectedMax = b.expectedMin |
| 166 |
} |
| 167 |
|
| 168 |
// Is any of: |
| 169 |
// (T) Compare(T) int |
| 170 |
// or |
| 171 |
// (T) Less(T) bool |
| 172 |
// available? |
| 173 |
if cmp := types.NewOrder(b.expectedMin.Type()); cmp != nil { |
| 174 |
if order := cmp(b.expectedMin, b.expectedMax); order > 0 { |
| 175 |
b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin |
| 176 |
} |
| 177 |
return &tdBetweenCmp{ |
| 178 |
tdBetween: *b, |
| 179 |
expectedType: b.expectedMin.Type(), |
| 180 |
cmp: cmp, |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
switch b.expectedMin.Kind() { |
| 185 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 186 |
if b.expectedMin.Int() > b.expectedMax.Int() { |
| 187 |
b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin |
| 188 |
} |
| 189 |
return b |
| 190 |
|
| 191 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 192 |
if b.expectedMin.Uint() > b.expectedMax.Uint() { |
| 193 |
b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin |
| 194 |
} |
| 195 |
return b |
| 196 |
|
| 197 |
case reflect.Float32, reflect.Float64: |
| 198 |
if b.expectedMin.Float() > b.expectedMax.Float() { |
| 199 |
b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin |
| 200 |
} |
| 201 |
return b |
| 202 |
|
| 203 |
case reflect.String: |
| 204 |
if b.expectedMin.String() > b.expectedMax.String() { |
| 205 |
b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin |
| 206 |
} |
| 207 |
return b |
| 208 |
|
| 209 |
case reflect.Struct: |
| 210 |
ok, convertible := types.IsTypeOrConvertible(b.expectedMin, types.Time) |
| 211 |
if !ok { |
| 212 |
break |
| 213 |
} |
| 214 |
|
| 215 |
bt := tdBetweenTime{ |
| 216 |
tdBetween: *b, |
| 217 |
expectedType: b.expectedMin.Type(), |
| 218 |
mustConvert: convertible, |
| 219 |
} |
| 220 |
if convertible { |
| 221 |
bt.expectedMin = b.expectedMin.Convert(types.Time) |
| 222 |
bt.expectedMax = b.expectedMax.Convert(types.Time) |
| 223 |
} |
| 224 |
|
| 225 |
if bt.expectedMin.Interface().(time.Time). |
| 226 |
After(bt.expectedMax.Interface().(time.Time)) { |
| 227 |
bt.expectedMin, bt.expectedMax = bt.expectedMax, bt.expectedMin |
| 228 |
} |
| 229 |
|
| 230 |
return &bt |
| 231 |
} |
| 232 |
|
| 233 |
b.err = ctxerr.OpBadUsage(b.GetLocation().Func, |
| 234 |
usage, b.expectedMin.Interface(), 1, true) |
| 235 |
return b |
| 236 |
} |
| 237 |
|
| 238 |
func (b *tdBetween) nInt(tolerance reflect.Value) { |
| 239 |
if diff := tolerance.Int(); diff != 0 { |
| 240 |
expectedBase := b.expectedMin.Int() |
| 241 |
|
| 242 |
max := expectedBase + diff |
| 243 |
if max < expectedBase { |
| 244 |
max = math.MaxInt64 |
| 245 |
} |
| 246 |
|
| 247 |
min := expectedBase - diff |
| 248 |
if min > expectedBase { |
| 249 |
min = math.MinInt64 |
| 250 |
} |
| 251 |
|
| 252 |
b.expectedMin = reflect.New(tolerance.Type()).Elem() |
| 253 |
b.expectedMin.SetInt(min) |
| 254 |
|
| 255 |
b.expectedMax = reflect.New(tolerance.Type()).Elem() |
| 256 |
b.expectedMax.SetInt(max) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
func (b *tdBetween) nUint(tolerance reflect.Value) { |
| 261 |
if diff := tolerance.Uint(); diff != 0 { |
| 262 |
base := b.expectedMin.Uint() |
| 263 |
|
| 264 |
max := base + diff |
| 265 |
if max < base { |
| 266 |
max = math.MaxUint64 |
| 267 |
} |
| 268 |
|
| 269 |
min := base - diff |
| 270 |
if min > base { |
| 271 |
min = 0 |
| 272 |
} |
| 273 |
|
| 274 |
b.expectedMin = reflect.New(tolerance.Type()).Elem() |
| 275 |
b.expectedMin.SetUint(min) |
| 276 |
|
| 277 |
b.expectedMax = reflect.New(tolerance.Type()).Elem() |
| 278 |
b.expectedMax.SetUint(max) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func (b *tdBetween) nFloat(tolerance reflect.Value) { |
| 283 |
if diff := tolerance.Float(); diff != 0 { |
| 284 |
base := b.expectedMin.Float() |
| 285 |
|
| 286 |
b.expectedMin = reflect.New(tolerance.Type()).Elem() |
| 287 |
b.expectedMin.SetFloat(base - diff) |
| 288 |
|
| 289 |
b.expectedMax = reflect.New(tolerance.Type()).Elem() |
| 290 |
b.expectedMax.SetFloat(base + diff) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// summary(N): compares a number with a tolerance value |
| 295 |
// input(N): int,float,cplx(todo) |
| 296 |
|
| 297 |
// N operator compares a numeric data against num ± tolerance. If |
| 298 |
// tolerance is missing, it defaults to 0. num and tolerance |
| 299 |
// must be the same type as the compared value, except if BeLax config |
| 300 |
// flag is true. |
| 301 |
// |
| 302 |
// td.Cmp(t, 12.2, td.N(12., 0.3)) // succeeds |
| 303 |
// td.Cmp(t, 12.2, td.N(12., 0.1)) // fails |
| 304 |
// |
| 305 |
// TypeBehind method returns the [reflect.Type] of num. |
| 306 |
func N(num any, tolerance ...any) TestDeep { |
| 307 |
n := tdBetween{ |
| 308 |
base: newBase(3), |
| 309 |
expectedMin: reflect.ValueOf(num), |
| 310 |
minBound: boundIn, |
| 311 |
maxBound: boundIn, |
| 312 |
} |
| 313 |
|
| 314 |
const usage = "({,U}INT{,8,16,32,64}|FLOAT{32,64}[, TOLERANCE])" |
| 315 |
|
| 316 |
switch n.expectedMin.Kind() { |
| 317 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 318 |
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, |
| 319 |
reflect.Float32, reflect.Float64: |
| 320 |
default: |
| 321 |
n.err = ctxerr.OpBadUsage("N", usage, num, 1, true) |
| 322 |
return &n |
| 323 |
} |
| 324 |
|
| 325 |
n.expectedMax = n.expectedMin |
| 326 |
|
| 327 |
if len(tolerance) > 0 { |
| 328 |
if len(tolerance) > 1 { |
| 329 |
n.err = ctxerr.OpTooManyParams("N", usage) |
| 330 |
return &n |
| 331 |
} |
| 332 |
|
| 333 |
tol := reflect.ValueOf(tolerance[0]) |
| 334 |
if tol.Type() != n.expectedMin.Type() { |
| 335 |
n.err = ctxerr.OpBad("N", |
| 336 |
"N(NUM, TOLERANCE): NUM and TOLERANCE must have the same type: %s ≠ %s", |
| 337 |
n.expectedMin.Type(), tol.Type()) |
| 338 |
return &n |
| 339 |
} |
| 340 |
|
| 341 |
switch tol.Kind() { |
| 342 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 343 |
n.nInt(tol) |
| 344 |
|
| 345 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, |
| 346 |
reflect.Uint64: |
| 347 |
n.nUint(tol) |
| 348 |
|
| 349 |
default: // case reflect.Float32, reflect.Float64: |
| 350 |
n.nFloat(tol) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
return &n |
| 355 |
} |
| 356 |
|
| 357 |
// summary(Gt): checks that a number, string or time.Time is |
| 358 |
// greater than a value |
| 359 |
// input(Gt): str,int,float,cplx(todo),struct(time.Time) |
| 360 |
|
| 361 |
// Gt operator checks that data is greater than |
| 362 |
// minExpectedValue. minExpectedValue can be any numeric, string, |
| 363 |
// [time.Time] (or assignable) value or implements at least one of the |
| 364 |
// two following methods: |
| 365 |
// |
| 366 |
// func (a T) Less(b T) bool // returns true if a < b |
| 367 |
// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b |
| 368 |
// |
| 369 |
// minExpectedValue must be the same type as the compared value, |
| 370 |
// except if BeLax config flag is true. |
| 371 |
// |
| 372 |
// td.Cmp(t, 17, td.Gt(15)) |
| 373 |
// before := time.Now() |
| 374 |
// td.Cmp(t, time.Now(), td.Gt(before)) |
| 375 |
// |
| 376 |
// TypeBehind method returns the [reflect.Type] of minExpectedValue. |
| 377 |
func Gt(minExpectedValue any) TestDeep { |
| 378 |
b := &tdBetween{ |
| 379 |
base: newBase(3), |
| 380 |
expectedMin: reflect.ValueOf(minExpectedValue), |
| 381 |
minBound: boundOut, |
| 382 |
} |
| 383 |
return b.initBetween("(NUM|STRING|TIME)") |
| 384 |
} |
| 385 |
|
| 386 |
// summary(Gte): checks that a number, string or time.Time is |
| 387 |
// greater or equal than a value |
| 388 |
// input(Gte): str,int,float,cplx(todo),struct(time.Time) |
| 389 |
|
| 390 |
// Gte operator checks that data is greater or equal than |
| 391 |
// minExpectedValue. minExpectedValue can be any numeric, string, |
| 392 |
// [time.Time] (or assignable) value or implements at least one of the |
| 393 |
// two following methods: |
| 394 |
// |
| 395 |
// func (a T) Less(b T) bool // returns true if a < b |
| 396 |
// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b |
| 397 |
// |
| 398 |
// minExpectedValue must be the same type as the compared value, |
| 399 |
// except if BeLax config flag is true. |
| 400 |
// |
| 401 |
// td.Cmp(t, 17, td.Gte(17)) |
| 402 |
// before := time.Now() |
| 403 |
// td.Cmp(t, time.Now(), td.Gte(before)) |
| 404 |
// |
| 405 |
// TypeBehind method returns the [reflect.Type] of minExpectedValue. |
| 406 |
func Gte(minExpectedValue any) TestDeep { |
| 407 |
b := &tdBetween{ |
| 408 |
base: newBase(3), |
| 409 |
expectedMin: reflect.ValueOf(minExpectedValue), |
| 410 |
minBound: boundIn, |
| 411 |
} |
| 412 |
return b.initBetween("(NUM|STRING|TIME)") |
| 413 |
} |
| 414 |
|
| 415 |
// summary(Lt): checks that a number, string or time.Time is |
| 416 |
// lesser than a value |
| 417 |
// input(Lt): str,int,float,cplx(todo),struct(time.Time) |
| 418 |
|
| 419 |
// Lt operator checks that data is lesser than |
| 420 |
// maxExpectedValue. maxExpectedValue can be any numeric, string, |
| 421 |
// [time.Time] (or assignable) value or implements at least one of the |
| 422 |
// two following methods: |
| 423 |
// |
| 424 |
// func (a T) Less(b T) bool // returns true if a < b |
| 425 |
// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b |
| 426 |
// |
| 427 |
// maxExpectedValue must be the same type as the compared value, |
| 428 |
// except if BeLax config flag is true. |
| 429 |
// |
| 430 |
// td.Cmp(t, 17, td.Lt(19)) |
| 431 |
// before := time.Now() |
| 432 |
// td.Cmp(t, before, td.Lt(time.Now())) |
| 433 |
// |
| 434 |
// TypeBehind method returns the [reflect.Type] of maxExpectedValue. |
| 435 |
func Lt(maxExpectedValue any) TestDeep { |
| 436 |
b := &tdBetween{ |
| 437 |
base: newBase(3), |
| 438 |
expectedMin: reflect.ValueOf(maxExpectedValue), |
| 439 |
maxBound: boundOut, |
| 440 |
} |
| 441 |
return b.initBetween("(NUM|STRING|TIME)") |
| 442 |
} |
| 443 |
|
| 444 |
// summary(Lte): checks that a number, string or time.Time is |
| 445 |
// lesser or equal than a value |
| 446 |
// input(Lte): str,int,float,cplx(todo),struct(time.Time) |
| 447 |
|
| 448 |
// Lte operator checks that data is lesser or equal than |
| 449 |
// maxExpectedValue. maxExpectedValue can be any numeric, string, |
| 450 |
// [time.Time] (or assignable) value or implements at least one of the |
| 451 |
// two following methods: |
| 452 |
// |
| 453 |
// func (a T) Less(b T) bool // returns true if a < b |
| 454 |
// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b |
| 455 |
// |
| 456 |
// maxExpectedValue must be the same type as the compared value, |
| 457 |
// except if BeLax config flag is true. |
| 458 |
// |
| 459 |
// td.Cmp(t, 17, td.Lte(17)) |
| 460 |
// before := time.Now() |
| 461 |
// td.Cmp(t, before, td.Lt(time.Now())) |
| 462 |
// |
| 463 |
// TypeBehind method returns the [reflect.Type] of maxExpectedValue. |
| 464 |
func Lte(maxExpectedValue any) TestDeep { |
| 465 |
b := &tdBetween{ |
| 466 |
base: newBase(3), |
| 467 |
expectedMin: reflect.ValueOf(maxExpectedValue), |
| 468 |
maxBound: boundIn, |
| 469 |
} |
| 470 |
return b.initBetween("(NUM|STRING|TIME)") |
| 471 |
} |
| 472 |
|
| 473 |
func (b *tdBetween) matchInt(got reflect.Value) (ok bool) { |
| 474 |
switch b.minBound { |
| 475 |
case boundIn: |
| 476 |
ok = got.Int() >= b.expectedMin.Int() |
| 477 |
case boundOut: |
| 478 |
ok = got.Int() > b.expectedMin.Int() |
| 479 |
default: |
| 480 |
ok = true |
| 481 |
} |
| 482 |
if ok { |
| 483 |
switch b.maxBound { |
| 484 |
case boundIn: |
| 485 |
ok = got.Int() <= b.expectedMax.Int() |
| 486 |
case boundOut: |
| 487 |
ok = got.Int() < b.expectedMax.Int() |
| 488 |
default: |
| 489 |
ok = true |
| 490 |
} |
| 491 |
} |
| 492 |
return |
| 493 |
} |
| 494 |
|
| 495 |
func (b *tdBetween) matchUint(got reflect.Value) (ok bool) { |
| 496 |
switch b.minBound { |
| 497 |
case boundIn: |
| 498 |
ok = got.Uint() >= b.expectedMin.Uint() |
| 499 |
case boundOut: |
| 500 |
ok = got.Uint() > b.expectedMin.Uint() |
| 501 |
default: |
| 502 |
ok = true |
| 503 |
} |
| 504 |
if ok { |
| 505 |
switch b.maxBound { |
| 506 |
case boundIn: |
| 507 |
ok = got.Uint() <= b.expectedMax.Uint() |
| 508 |
case boundOut: |
| 509 |
ok = got.Uint() < b.expectedMax.Uint() |
| 510 |
default: |
| 511 |
ok = true |
| 512 |
} |
| 513 |
} |
| 514 |
return |
| 515 |
} |
| 516 |
|
| 517 |
func (b *tdBetween) matchFloat(got reflect.Value) (ok bool) { |
| 518 |
switch b.minBound { |
| 519 |
case boundIn: |
| 520 |
ok = got.Float() >= b.expectedMin.Float() |
| 521 |
case boundOut: |
| 522 |
ok = got.Float() > b.expectedMin.Float() |
| 523 |
default: |
| 524 |
ok = true |
| 525 |
} |
| 526 |
if ok { |
| 527 |
switch b.maxBound { |
| 528 |
case boundIn: |
| 529 |
ok = got.Float() <= b.expectedMax.Float() |
| 530 |
case boundOut: |
| 531 |
ok = got.Float() < b.expectedMax.Float() |
| 532 |
default: |
| 533 |
ok = true |
| 534 |
} |
| 535 |
} |
| 536 |
return |
| 537 |
} |
| 538 |
|
| 539 |
func (b *tdBetween) matchString(got reflect.Value) (ok bool) { |
| 540 |
switch b.minBound { |
| 541 |
case boundIn: |
| 542 |
ok = got.String() >= b.expectedMin.String() |
| 543 |
case boundOut: |
| 544 |
ok = got.String() > b.expectedMin.String() |
| 545 |
default: |
| 546 |
ok = true |
| 547 |
} |
| 548 |
if ok { |
| 549 |
switch b.maxBound { |
| 550 |
case boundIn: |
| 551 |
ok = got.String() <= b.expectedMax.String() |
| 552 |
case boundOut: |
| 553 |
ok = got.String() < b.expectedMax.String() |
| 554 |
default: |
| 555 |
ok = true |
| 556 |
} |
| 557 |
} |
| 558 |
return |
| 559 |
} |
| 560 |
|
| 561 |
func (b *tdBetween) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 562 |
if b.err != nil { |
| 563 |
return ctx.CollectError(b.err) |
| 564 |
} |
| 565 |
|
| 566 |
if got.Type() != b.expectedMin.Type() { |
| 567 |
if !ctx.BeLax || !types.IsConvertible(b.expectedMin, got.Type()) { |
| 568 |
if ctx.BooleanError { |
| 569 |
return ctxerr.BooleanError |
| 570 |
} |
| 571 |
return ctx.CollectError(ctxerr.TypeMismatch(got.Type(), b.expectedMin.Type())) |
| 572 |
} |
| 573 |
nb := *b |
| 574 |
nb.expectedMin = b.expectedMin.Convert(got.Type()) |
| 575 |
nb.expectedMax = b.expectedMax.Convert(got.Type()) |
| 576 |
b = &nb |
| 577 |
} |
| 578 |
|
| 579 |
var ok bool |
| 580 |
|
| 581 |
switch got.Kind() { |
| 582 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 583 |
ok = b.matchInt(got) |
| 584 |
|
| 585 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 586 |
ok = b.matchUint(got) |
| 587 |
|
| 588 |
case reflect.Float32, reflect.Float64: |
| 589 |
ok = b.matchFloat(got) |
| 590 |
|
| 591 |
case reflect.String: |
| 592 |
ok = b.matchString(got) |
| 593 |
} |
| 594 |
|
| 595 |
if ok { |
| 596 |
return nil |
| 597 |
} |
| 598 |
|
| 599 |
if ctx.BooleanError { |
| 600 |
return ctxerr.BooleanError |
| 601 |
} |
| 602 |
|
| 603 |
return ctx.CollectError(&ctxerr.Error{ |
| 604 |
Message: "values differ", |
| 605 |
Got: got, |
| 606 |
Expected: types.RawString(b.String()), |
| 607 |
}) |
| 608 |
} |
| 609 |
|
| 610 |
func (b *tdBetween) String() string { |
| 611 |
if b.err != nil { |
| 612 |
return b.stringError() |
| 613 |
} |
| 614 |
|
| 615 |
var ( |
| 616 |
min, max any |
| 617 |
minStr, maxStr string |
| 618 |
) |
| 619 |
|
| 620 |
if b.minBound != boundNone { |
| 621 |
min = b.expectedMin.Interface() |
| 622 |
minStr = util.ToString(min) |
| 623 |
} |
| 624 |
if b.maxBound != boundNone { |
| 625 |
max = b.expectedMax.Interface() |
| 626 |
maxStr = util.ToString(max) |
| 627 |
} |
| 628 |
|
| 629 |
if min != nil { |
| 630 |
if max != nil { |
| 631 |
return fmt.Sprintf("%s %c got %c %s", |
| 632 |
minStr, |
| 633 |
util.TernRune(b.minBound == boundIn, '≤', '<'), |
| 634 |
util.TernRune(b.maxBound == boundIn, '≤', '<'), |
| 635 |
maxStr) |
| 636 |
} |
| 637 |
|
| 638 |
return fmt.Sprintf("%c %s", |
| 639 |
util.TernRune(b.minBound == boundIn, '≥', '>'), minStr) |
| 640 |
} |
| 641 |
|
| 642 |
return fmt.Sprintf("%c %s", |
| 643 |
util.TernRune(b.maxBound == boundIn, '≤', '<'), maxStr) |
| 644 |
} |
| 645 |
|
| 646 |
func (b *tdBetween) TypeBehind() reflect.Type { |
| 647 |
if b.err != nil { |
| 648 |
return nil |
| 649 |
} |
| 650 |
return b.expectedMin.Type() |
| 651 |
} |
| 652 |
|
| 653 |
var _ TestDeep = &tdBetweenTime{} |
| 654 |
|
| 655 |
func (b *tdBetweenTime) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 656 |
// b.err != nil is not possible here, as when a *tdBetweenTime is |
| 657 |
// built, there is never an error |
| 658 |
|
| 659 |
if got.Type() != b.expectedType { |
| 660 |
if !ctx.BeLax || !types.IsConvertible(got, b.expectedType) { |
| 661 |
if ctx.BooleanError { |
| 662 |
return ctxerr.BooleanError |
| 663 |
} |
| 664 |
return ctx.CollectError(ctxerr.TypeMismatch(got.Type(), b.expectedType)) |
| 665 |
} |
| 666 |
got = got.Convert(b.expectedType) |
| 667 |
} |
| 668 |
|
| 669 |
cmpGot, err := getTime(ctx, got, b.mustConvert) |
| 670 |
if err != nil { |
| 671 |
return ctx.CollectError(err) |
| 672 |
} |
| 673 |
|
| 674 |
var ok bool |
| 675 |
if b.minBound != boundNone { |
| 676 |
min := b.expectedMin.Interface().(time.Time) |
| 677 |
|
| 678 |
if b.minBound == boundIn { |
| 679 |
ok = !min.After(cmpGot) |
| 680 |
} else { |
| 681 |
ok = cmpGot.After(min) |
| 682 |
} |
| 683 |
} else { |
| 684 |
ok = true |
| 685 |
} |
| 686 |
|
| 687 |
if ok && b.maxBound != boundNone { |
| 688 |
max := b.expectedMax.Interface().(time.Time) |
| 689 |
|
| 690 |
if b.maxBound == boundIn { |
| 691 |
ok = !max.Before(cmpGot) |
| 692 |
} else { |
| 693 |
ok = cmpGot.Before(max) |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
if ok { |
| 698 |
return nil |
| 699 |
} |
| 700 |
|
| 701 |
if ctx.BooleanError { |
| 702 |
return ctxerr.BooleanError |
| 703 |
} |
| 704 |
return ctx.CollectError(&ctxerr.Error{ |
| 705 |
Message: "values differ", |
| 706 |
Got: got, |
| 707 |
Expected: types.RawString(b.String()), |
| 708 |
}) |
| 709 |
} |
| 710 |
|
| 711 |
func (b *tdBetweenTime) TypeBehind() reflect.Type { |
| 712 |
// b.err != nil is not possible here, as when a *tdBetweenTime is |
| 713 |
// built, there is never an error |
| 714 |
return b.expectedType |
| 715 |
} |
| 716 |
|
| 717 |
var _ TestDeep = &tdBetweenCmp{} |
| 718 |
|
| 719 |
func (b *tdBetweenCmp) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 720 |
// b.err != nil is not possible here, as when a *tdBetweenCmp is |
| 721 |
// built, there is never an error |
| 722 |
|
| 723 |
if got.Type() != b.expectedType { |
| 724 |
if ctx.BeLax && types.IsConvertible(got, b.expectedType) { |
| 725 |
got = got.Convert(b.expectedType) |
| 726 |
} else { |
| 727 |
if ctx.BooleanError { |
| 728 |
return ctxerr.BooleanError |
| 729 |
} |
| 730 |
return ctx.CollectError(ctxerr.TypeMismatch(got.Type(), b.expectedType)) |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
var ok bool |
| 735 |
if b.minBound != boundNone { |
| 736 |
order := b.cmp(got, b.expectedMin) |
| 737 |
if b.minBound == boundIn { |
| 738 |
ok = order >= 0 |
| 739 |
} else { |
| 740 |
ok = order > 0 |
| 741 |
} |
| 742 |
} else { |
| 743 |
ok = true |
| 744 |
} |
| 745 |
|
| 746 |
if ok && b.maxBound != boundNone { |
| 747 |
order := b.cmp(got, b.expectedMax) |
| 748 |
if b.maxBound == boundIn { |
| 749 |
ok = order <= 0 |
| 750 |
} else { |
| 751 |
ok = order < 0 |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
if ok { |
| 756 |
return nil |
| 757 |
} |
| 758 |
|
| 759 |
if ctx.BooleanError { |
| 760 |
return ctxerr.BooleanError |
| 761 |
} |
| 762 |
return ctx.CollectError(&ctxerr.Error{ |
| 763 |
Message: "values differ", |
| 764 |
Got: got, |
| 765 |
Expected: types.RawString(b.String()), |
| 766 |
}) |
| 767 |
} |
| 768 |
|
| 769 |
func (b *tdBetweenCmp) TypeBehind() reflect.Type { |
| 770 |
// b.err != nil is not possible here, as when a *tdBetweenCmp is |
| 771 |
// built, there is never an error |
| 772 |
return b.expectedType |
| 773 |
} |