| 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_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"math" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/maxatome/go-testdeep/internal/test" |
| 16 |
"github.com/maxatome/go-testdeep/td" |
| 17 |
) |
| 18 |
|
| 19 |
func TestBetween(t *testing.T) { |
| 20 |
checkOK(t, 12, td.Between(9, 13)) |
| 21 |
checkOK(t, 12, td.Between(13, 9)) |
| 22 |
checkOK(t, 12, td.Between(9, 12, td.BoundsOutIn)) |
| 23 |
checkOK(t, 12, td.Between(12, 13, td.BoundsInOut)) |
| 24 |
|
| 25 |
checkError(t, 10, td.Between(10, 15, td.BoundsOutIn), |
| 26 |
expectedError{ |
| 27 |
Message: mustBe("values differ"), |
| 28 |
Path: mustBe("DATA"), |
| 29 |
Got: mustBe("10"), |
| 30 |
Expected: mustBe("10 < got ≤ 15"), |
| 31 |
}) |
| 32 |
checkError(t, 10, td.Between(10, 15, td.BoundsOutOut), |
| 33 |
expectedError{ |
| 34 |
Message: mustBe("values differ"), |
| 35 |
Path: mustBe("DATA"), |
| 36 |
Got: mustBe("10"), |
| 37 |
Expected: mustBe("10 < got < 15"), |
| 38 |
}) |
| 39 |
checkError(t, 15, td.Between(10, 15, td.BoundsInOut), |
| 40 |
expectedError{ |
| 41 |
Message: mustBe("values differ"), |
| 42 |
Path: mustBe("DATA"), |
| 43 |
Got: mustBe("15"), |
| 44 |
Expected: mustBe("10 ≤ got < 15"), |
| 45 |
}) |
| 46 |
checkError(t, 15, td.Between(10, 15, td.BoundsOutOut), |
| 47 |
expectedError{ |
| 48 |
Message: mustBe("values differ"), |
| 49 |
Path: mustBe("DATA"), |
| 50 |
Got: mustBe("15"), |
| 51 |
Expected: mustBe("10 < got < 15"), |
| 52 |
}) |
| 53 |
|
| 54 |
checkError(t, 15, td.Between(uint(10), uint(15), td.BoundsOutOut), |
| 55 |
expectedError{ |
| 56 |
Message: mustBe("type mismatch"), |
| 57 |
Path: mustBe("DATA"), |
| 58 |
Got: mustBe("int"), |
| 59 |
Expected: mustBe("uint"), |
| 60 |
}) |
| 61 |
|
| 62 |
checkOK(t, uint16(12), td.Between(uint16(9), uint16(13))) |
| 63 |
checkOK(t, uint16(12), td.Between(uint16(13), uint16(9))) |
| 64 |
checkOK(t, uint16(12), |
| 65 |
td.Between(uint16(9), uint16(12), td.BoundsOutIn)) |
| 66 |
checkOK(t, uint16(12), |
| 67 |
td.Between(uint16(12), uint16(13), td.BoundsInOut)) |
| 68 |
|
| 69 |
checkOK(t, 12.1, td.Between(9.5, 13.1)) |
| 70 |
checkOK(t, 12.1, td.Between(13.1, 9.5)) |
| 71 |
checkOK(t, 12.1, td.Between(9.5, 12.1, td.BoundsOutIn)) |
| 72 |
checkOK(t, 12.1, td.Between(12.1, 13.1, td.BoundsInOut)) |
| 73 |
|
| 74 |
checkOK(t, "abc", td.Between("aaa", "bbb")) |
| 75 |
checkOK(t, "abc", td.Between("bbb", "aaa")) |
| 76 |
checkOK(t, "abc", td.Between("aaa", "abc", td.BoundsOutIn)) |
| 77 |
checkOK(t, "abc", td.Between("abc", "bbb", td.BoundsInOut)) |
| 78 |
|
| 79 |
checkOK(t, 12*time.Hour, td.Between(60*time.Second, 24*time.Hour)) |
| 80 |
|
| 81 |
// |
| 82 |
// Bad usage |
| 83 |
checkError(t, "never tested", |
| 84 |
td.Between([]byte("test"), []byte("test")), |
| 85 |
expectedError{ |
| 86 |
Message: mustBe("bad usage of Between operator"), |
| 87 |
Path: mustBe("DATA"), |
| 88 |
Summary: mustBe("usage: Between(NUM|STRING|TIME, NUM|STRING|TIME/DURATION[, BOUNDS_KIND]), but received []uint8 (slice) as 1st parameter"), |
| 89 |
}) |
| 90 |
|
| 91 |
checkError(t, "never tested", |
| 92 |
td.Between(12, "test"), |
| 93 |
expectedError{ |
| 94 |
Message: mustBe("bad usage of Between operator"), |
| 95 |
Path: mustBe("DATA"), |
| 96 |
Summary: mustBe("Between(FROM, TO): FROM and TO must have the same type: int ≠ string"), |
| 97 |
}) |
| 98 |
|
| 99 |
checkError(t, "never tested", |
| 100 |
td.Between("test", 12), |
| 101 |
expectedError{ |
| 102 |
Message: mustBe("bad usage of Between operator"), |
| 103 |
Path: mustBe("DATA"), |
| 104 |
Summary: mustBe("Between(FROM, TO): FROM and TO must have the same type: string ≠ int"), |
| 105 |
}) |
| 106 |
|
| 107 |
checkError(t, "never tested", |
| 108 |
td.Between(1, 2, td.BoundsInIn, td.BoundsInOut), |
| 109 |
expectedError{ |
| 110 |
Message: mustBe("bad usage of Between operator"), |
| 111 |
Path: mustBe("DATA"), |
| 112 |
Summary: mustBe("usage: Between(NUM|STRING|TIME, NUM|STRING|TIME/DURATION[, BOUNDS_KIND]), too many parameters"), |
| 113 |
}) |
| 114 |
|
| 115 |
type notTime struct{} |
| 116 |
checkError(t, "never tested", |
| 117 |
td.Between(notTime{}, notTime{}), |
| 118 |
expectedError{ |
| 119 |
Message: mustBe("bad usage of Between operator"), |
| 120 |
Path: mustBe("DATA"), |
| 121 |
Summary: mustBe("usage: Between(NUM|STRING|TIME, NUM|STRING|TIME/DURATION[, BOUNDS_KIND]), but received td_test.notTime (struct) as 1st parameter"), |
| 122 |
}) |
| 123 |
|
| 124 |
// Erroneous op |
| 125 |
test.EqualStr(t, td.Between("test", 12).String(), "Between(<ERROR>)") |
| 126 |
} |
| 127 |
|
| 128 |
func TestN(t *testing.T) { |
| 129 |
// |
| 130 |
// Unsigned |
| 131 |
checkOK(t, uint(12), td.N(uint(12))) |
| 132 |
checkOK(t, uint(11), td.N(uint(12), uint(1))) |
| 133 |
checkOK(t, uint(13), td.N(uint(12), uint(1))) |
| 134 |
checkError(t, 10, td.N(uint(12), uint(1)), |
| 135 |
expectedError{ |
| 136 |
Message: mustBe("type mismatch"), |
| 137 |
Path: mustBe("DATA"), |
| 138 |
Got: mustBe("int"), |
| 139 |
Expected: mustBe("uint"), |
| 140 |
}) |
| 141 |
|
| 142 |
checkOK(t, uint8(12), td.N(uint8(12))) |
| 143 |
checkOK(t, uint8(11), td.N(uint8(12), uint8(1))) |
| 144 |
checkOK(t, uint8(13), td.N(uint8(12), uint8(1))) |
| 145 |
checkError(t, 10, td.N(uint8(12), uint8(1)), |
| 146 |
expectedError{ |
| 147 |
Message: mustBe("type mismatch"), |
| 148 |
Path: mustBe("DATA"), |
| 149 |
Got: mustBe("int"), |
| 150 |
Expected: mustBe("uint8"), |
| 151 |
}) |
| 152 |
|
| 153 |
checkOK(t, uint16(12), td.N(uint16(12))) |
| 154 |
checkOK(t, uint16(11), td.N(uint16(12), uint16(1))) |
| 155 |
checkOK(t, uint16(13), td.N(uint16(12), uint16(1))) |
| 156 |
checkError(t, 10, td.N(uint16(12), uint16(1)), |
| 157 |
expectedError{ |
| 158 |
Message: mustBe("type mismatch"), |
| 159 |
Path: mustBe("DATA"), |
| 160 |
Got: mustBe("int"), |
| 161 |
Expected: mustBe("uint16"), |
| 162 |
}) |
| 163 |
|
| 164 |
checkOK(t, uint32(12), td.N(uint32(12))) |
| 165 |
checkOK(t, uint32(11), td.N(uint32(12), uint32(1))) |
| 166 |
checkOK(t, uint32(13), td.N(uint32(12), uint32(1))) |
| 167 |
checkError(t, 10, td.N(uint32(12), uint32(1)), |
| 168 |
expectedError{ |
| 169 |
Message: mustBe("type mismatch"), |
| 170 |
Path: mustBe("DATA"), |
| 171 |
Got: mustBe("int"), |
| 172 |
Expected: mustBe("uint32"), |
| 173 |
}) |
| 174 |
|
| 175 |
checkOK(t, uint64(12), td.N(uint64(12))) |
| 176 |
checkOK(t, uint64(11), td.N(uint64(12), uint64(1))) |
| 177 |
checkOK(t, uint64(13), td.N(uint64(12), uint64(1))) |
| 178 |
checkError(t, 10, td.N(uint64(12), uint64(1)), |
| 179 |
expectedError{ |
| 180 |
Message: mustBe("type mismatch"), |
| 181 |
Path: mustBe("DATA"), |
| 182 |
Got: mustBe("int"), |
| 183 |
Expected: mustBe("uint64"), |
| 184 |
}) |
| 185 |
|
| 186 |
checkOK(t, uint64(math.MaxUint64), |
| 187 |
td.N(uint64(math.MaxUint64), uint64(2))) |
| 188 |
checkError(t, uint64(0), td.N(uint64(math.MaxUint64), uint64(2)), |
| 189 |
expectedError{ |
| 190 |
Message: mustBe("values differ"), |
| 191 |
Path: mustBe("DATA"), |
| 192 |
Got: mustBe("(uint64) 0"), |
| 193 |
Expected: mustBe(fmt.Sprintf("(uint64) %v ≤ got ≤ (uint64) %v", |
| 194 |
uint64(math.MaxUint64)-2, uint64(math.MaxUint64))), |
| 195 |
}) |
| 196 |
|
| 197 |
checkOK(t, uint64(0), td.N(uint64(0), uint64(2))) |
| 198 |
checkError(t, uint64(math.MaxUint64), td.N(uint64(0), uint64(2)), |
| 199 |
expectedError{ |
| 200 |
Message: mustBe("values differ"), |
| 201 |
Path: mustBe("DATA"), |
| 202 |
Got: mustBe(fmt.Sprintf("(uint64) %v", uint64(math.MaxUint64))), |
| 203 |
Expected: mustBe("(uint64) 0 ≤ got ≤ (uint64) 2"), |
| 204 |
}) |
| 205 |
|
| 206 |
// |
| 207 |
// Signed |
| 208 |
checkOK(t, 12, td.N(12)) |
| 209 |
checkOK(t, 11, td.N(12, 1)) |
| 210 |
checkOK(t, 13, td.N(12, 1)) |
| 211 |
checkError(t, 10, td.N(12, 1), |
| 212 |
expectedError{ |
| 213 |
Message: mustBe("values differ"), |
| 214 |
Path: mustBe("DATA"), |
| 215 |
Got: mustBe("10"), |
| 216 |
Expected: mustBe("11 ≤ got ≤ 13"), |
| 217 |
}) |
| 218 |
|
| 219 |
checkError(t, 10, td.N(12, 0), |
| 220 |
expectedError{ |
| 221 |
Message: mustBe("values differ"), |
| 222 |
Path: mustBe("DATA"), |
| 223 |
Got: mustBe("10"), |
| 224 |
Expected: mustBe("12 ≤ got ≤ 12"), |
| 225 |
}) |
| 226 |
|
| 227 |
checkOK(t, int8(12), td.N(int8(12))) |
| 228 |
checkOK(t, int8(11), td.N(int8(12), int8(1))) |
| 229 |
checkOK(t, int8(13), td.N(int8(12), int8(1))) |
| 230 |
checkError(t, 10, td.N(int8(12), int8(1)), |
| 231 |
expectedError{ |
| 232 |
Message: mustBe("type mismatch"), |
| 233 |
Path: mustBe("DATA"), |
| 234 |
Got: mustBe("int"), |
| 235 |
Expected: mustBe("int8"), |
| 236 |
}) |
| 237 |
|
| 238 |
checkOK(t, int16(12), td.N(int16(12))) |
| 239 |
checkOK(t, int16(11), td.N(int16(12), int16(1))) |
| 240 |
checkOK(t, int16(13), td.N(int16(12), int16(1))) |
| 241 |
checkError(t, 10, td.N(int16(12), int16(1)), |
| 242 |
expectedError{ |
| 243 |
Message: mustBe("type mismatch"), |
| 244 |
Path: mustBe("DATA"), |
| 245 |
Got: mustBe("int"), |
| 246 |
Expected: mustBe("int16"), |
| 247 |
}) |
| 248 |
|
| 249 |
checkOK(t, int32(12), td.N(int32(12))) |
| 250 |
checkOK(t, int32(11), td.N(int32(12), int32(1))) |
| 251 |
checkOK(t, int32(13), td.N(int32(12), int32(1))) |
| 252 |
checkError(t, 10, td.N(int32(12), int32(1)), |
| 253 |
expectedError{ |
| 254 |
Message: mustBe("type mismatch"), |
| 255 |
Path: mustBe("DATA"), |
| 256 |
Got: mustBe("int"), |
| 257 |
Expected: mustBe("int32"), |
| 258 |
}) |
| 259 |
|
| 260 |
checkOK(t, int64(12), td.N(int64(12))) |
| 261 |
checkOK(t, int64(11), td.N(int64(12), int64(1))) |
| 262 |
checkOK(t, int64(13), td.N(int64(12), int64(1))) |
| 263 |
checkError(t, 10, td.N(int64(12), int64(1)), |
| 264 |
expectedError{ |
| 265 |
Message: mustBe("type mismatch"), |
| 266 |
Path: mustBe("DATA"), |
| 267 |
Got: mustBe("int"), |
| 268 |
Expected: mustBe("int64"), |
| 269 |
}) |
| 270 |
|
| 271 |
checkOK(t, int64(math.MaxInt64), td.N(int64(math.MaxInt64), int64(2))) |
| 272 |
checkError(t, int64(0), td.N(int64(math.MaxInt64), int64(2)), |
| 273 |
expectedError{ |
| 274 |
Message: mustBe("values differ"), |
| 275 |
Path: mustBe("DATA"), |
| 276 |
Got: mustBe("(int64) 0"), |
| 277 |
Expected: mustBe(fmt.Sprintf("(int64) %v ≤ got ≤ (int64) %v", |
| 278 |
int64(math.MaxInt64)-2, int64(math.MaxInt64))), |
| 279 |
}) |
| 280 |
|
| 281 |
checkOK(t, int64(math.MinInt64), td.N(int64(math.MinInt64), int64(2))) |
| 282 |
checkError(t, int64(0), td.N(int64(math.MinInt64), int64(2)), |
| 283 |
expectedError{ |
| 284 |
Message: mustBe("values differ"), |
| 285 |
Path: mustBe("DATA"), |
| 286 |
Got: mustBe("(int64) 0"), |
| 287 |
Expected: mustBe(fmt.Sprintf("(int64) %v ≤ got ≤ (int64) %v", |
| 288 |
int64(math.MinInt64), int64(math.MinInt64)+2)), |
| 289 |
}) |
| 290 |
|
| 291 |
// |
| 292 |
// Float |
| 293 |
checkOK(t, 12.1, td.N(12.1)) |
| 294 |
checkOK(t, 11.9, td.N(12.0, 0.1)) |
| 295 |
checkOK(t, 12.1, td.N(12.0, 0.1)) |
| 296 |
checkError(t, 11.8, td.N(12.0, 0.1), |
| 297 |
expectedError{ |
| 298 |
Message: mustBe("values differ"), |
| 299 |
Path: mustBe("DATA"), |
| 300 |
Got: mustBe("11.8"), |
| 301 |
Expected: mustBe("11.9 ≤ got ≤ 12.1"), |
| 302 |
}) |
| 303 |
|
| 304 |
checkOK(t, float32(12.1), td.N(float32(12.1))) |
| 305 |
checkOK(t, float32(11.9), td.N(float32(12), float32(0.1))) |
| 306 |
checkOK(t, float32(12.1), td.N(float32(12), float32(0.1))) |
| 307 |
checkError(t, 11.8, td.N(float32(12), float32(0.1)), |
| 308 |
expectedError{ |
| 309 |
Message: mustBe("type mismatch"), |
| 310 |
Path: mustBe("DATA"), |
| 311 |
Got: mustBe("float64"), |
| 312 |
Expected: mustBe("float32"), |
| 313 |
}) |
| 314 |
|
| 315 |
floatTol := 10e304 |
| 316 |
checkOK(t, float64(math.MaxFloat64), |
| 317 |
td.N(float64(math.MaxFloat64), floatTol)) |
| 318 |
checkError(t, float64(0), td.N(float64(math.MaxFloat64), floatTol), |
| 319 |
expectedError{ |
| 320 |
Message: mustBe("values differ"), |
| 321 |
Path: mustBe("DATA"), |
| 322 |
Got: mustBe("0.0"), |
| 323 |
Expected: mustBe(fmt.Sprintf("%v ≤ got ≤ +Inf", |
| 324 |
float64(math.MaxFloat64)-floatTol)), |
| 325 |
}) |
| 326 |
|
| 327 |
checkOK(t, -float64(math.MaxFloat64), |
| 328 |
td.N(-float64(math.MaxFloat64), float64(2))) |
| 329 |
checkError(t, float64(0), td.N(-float64(math.MaxFloat64), floatTol), |
| 330 |
expectedError{ |
| 331 |
Message: mustBe("values differ"), |
| 332 |
Path: mustBe("DATA"), |
| 333 |
Got: mustBe("0.0"), |
| 334 |
Expected: mustBe(fmt.Sprintf("-Inf ≤ got ≤ %v", |
| 335 |
-float64(math.MaxFloat64)+floatTol)), |
| 336 |
}) |
| 337 |
|
| 338 |
// |
| 339 |
// Bad usage |
| 340 |
checkError(t, "never tested", |
| 341 |
td.N("test"), |
| 342 |
expectedError{ |
| 343 |
Message: mustBe("bad usage of N operator"), |
| 344 |
Path: mustBe("DATA"), |
| 345 |
Summary: mustBe("usage: N({,U}INT{,8,16,32,64}|FLOAT{32,64}[, TOLERANCE]), but received string as 1st parameter"), |
| 346 |
}) |
| 347 |
|
| 348 |
checkError(t, "never tested", |
| 349 |
td.N(10, 1, 2), |
| 350 |
expectedError{ |
| 351 |
Message: mustBe("bad usage of N operator"), |
| 352 |
Path: mustBe("DATA"), |
| 353 |
Summary: mustBe("usage: N({,U}INT{,8,16,32,64}|FLOAT{32,64}[, TOLERANCE]), too many parameters"), |
| 354 |
}) |
| 355 |
|
| 356 |
checkError(t, "never tested", |
| 357 |
td.N(10, "test"), |
| 358 |
expectedError{ |
| 359 |
Message: mustBe("bad usage of N operator"), |
| 360 |
Path: mustBe("DATA"), |
| 361 |
Summary: mustBe("N(NUM, TOLERANCE): NUM and TOLERANCE must have the same type: int ≠ string"), |
| 362 |
}) |
| 363 |
|
| 364 |
// Erroneous op |
| 365 |
test.EqualStr(t, td.N(10, 1, 2).String(), "N(<ERROR>)") |
| 366 |
} |
| 367 |
|
| 368 |
func TestLGt(t *testing.T) { |
| 369 |
type MyTime time.Time |
| 370 |
|
| 371 |
checkOK(t, 12, td.Gt(11)) |
| 372 |
checkOK(t, 12, td.Gte(12)) |
| 373 |
checkOK(t, 12, td.Lt(13)) |
| 374 |
checkOK(t, 12, td.Lte(12)) |
| 375 |
|
| 376 |
checkOK(t, uint16(12), td.Gt(uint16(11))) |
| 377 |
checkOK(t, uint16(12), td.Gte(uint16(12))) |
| 378 |
checkOK(t, uint16(12), td.Lt(uint16(13))) |
| 379 |
checkOK(t, uint16(12), td.Lte(uint16(12))) |
| 380 |
|
| 381 |
checkOK(t, 12.3, td.Gt(12.2)) |
| 382 |
checkOK(t, 12.3, td.Gte(12.3)) |
| 383 |
checkOK(t, 12.3, td.Lt(12.4)) |
| 384 |
checkOK(t, 12.3, td.Lte(12.3)) |
| 385 |
|
| 386 |
checkOK(t, "abc", td.Gt("abb")) |
| 387 |
checkOK(t, "abc", td.Gte("abc")) |
| 388 |
checkOK(t, "abc", td.Lt("abd")) |
| 389 |
checkOK(t, "abc", td.Lte("abc")) |
| 390 |
|
| 391 |
checkError(t, 12, td.Gt(12), |
| 392 |
expectedError{ |
| 393 |
Message: mustBe("values differ"), |
| 394 |
Path: mustBe("DATA"), |
| 395 |
Got: mustBe("12"), |
| 396 |
Expected: mustBe("> 12"), |
| 397 |
}) |
| 398 |
checkError(t, 12, td.Lt(12), |
| 399 |
expectedError{ |
| 400 |
Message: mustBe("values differ"), |
| 401 |
Path: mustBe("DATA"), |
| 402 |
Got: mustBe("12"), |
| 403 |
Expected: mustBe("< 12"), |
| 404 |
}) |
| 405 |
checkError(t, 12, td.Gte(13), |
| 406 |
expectedError{ |
| 407 |
Message: mustBe("values differ"), |
| 408 |
Path: mustBe("DATA"), |
| 409 |
Got: mustBe("12"), |
| 410 |
Expected: mustBe("≥ 13"), |
| 411 |
}) |
| 412 |
checkError(t, 12, td.Lte(11), |
| 413 |
expectedError{ |
| 414 |
Message: mustBe("values differ"), |
| 415 |
Path: mustBe("DATA"), |
| 416 |
Got: mustBe("12"), |
| 417 |
Expected: mustBe("≤ 11"), |
| 418 |
}) |
| 419 |
|
| 420 |
checkError(t, "abc", td.Gt("abc"), |
| 421 |
expectedError{ |
| 422 |
Message: mustBe("values differ"), |
| 423 |
Path: mustBe("DATA"), |
| 424 |
Got: mustBe(`"abc"`), |
| 425 |
Expected: mustBe(`> "abc"`), |
| 426 |
}) |
| 427 |
checkError(t, "abc", td.Lt("abc"), |
| 428 |
expectedError{ |
| 429 |
Message: mustBe("values differ"), |
| 430 |
Path: mustBe("DATA"), |
| 431 |
Got: mustBe(`"abc"`), |
| 432 |
Expected: mustBe(`< "abc"`), |
| 433 |
}) |
| 434 |
checkError(t, "abc", td.Gte("abd"), |
| 435 |
expectedError{ |
| 436 |
Message: mustBe("values differ"), |
| 437 |
Path: mustBe("DATA"), |
| 438 |
Got: mustBe(`"abc"`), |
| 439 |
Expected: mustBe(`≥ "abd"`), |
| 440 |
}) |
| 441 |
checkError(t, "abc", td.Lte("abb"), |
| 442 |
expectedError{ |
| 443 |
Message: mustBe("values differ"), |
| 444 |
Path: mustBe("DATA"), |
| 445 |
Got: mustBe(`"abc"`), |
| 446 |
Expected: mustBe(`≤ "abb"`), |
| 447 |
}) |
| 448 |
|
| 449 |
gotDate := time.Date(2018, time.March, 4, 1, 2, 3, 0, time.UTC) |
| 450 |
expectedDate := gotDate |
| 451 |
checkOK(t, gotDate, td.Gte(expectedDate)) |
| 452 |
checkOK(t, gotDate, td.Lte(expectedDate)) |
| 453 |
checkOK(t, gotDate, td.Lax(td.Gte(MyTime(expectedDate)))) |
| 454 |
checkOK(t, gotDate, td.Lax(td.Lte(MyTime(expectedDate)))) |
| 455 |
|
| 456 |
checkError(t, gotDate, td.Gt(expectedDate), |
| 457 |
expectedError{ |
| 458 |
Message: mustBe("values differ"), |
| 459 |
Path: mustBe("DATA"), |
| 460 |
Got: mustBe("(time.Time) 2018-03-04 01:02:03 +0000 UTC"), |
| 461 |
Expected: mustBe("> (time.Time) 2018-03-04 01:02:03 +0000 UTC"), |
| 462 |
}) |
| 463 |
checkError(t, gotDate, td.Lt(expectedDate), |
| 464 |
expectedError{ |
| 465 |
Message: mustBe("values differ"), |
| 466 |
Path: mustBe("DATA"), |
| 467 |
Got: mustBe("(time.Time) 2018-03-04 01:02:03 +0000 UTC"), |
| 468 |
Expected: mustBe("< (time.Time) 2018-03-04 01:02:03 +0000 UTC"), |
| 469 |
}) |
| 470 |
|
| 471 |
// |
| 472 |
// Bad usage |
| 473 |
checkError(t, "never tested", |
| 474 |
td.Gt([]byte("test")), |
| 475 |
expectedError{ |
| 476 |
Message: mustBe("bad usage of Gt operator"), |
| 477 |
Path: mustBe("DATA"), |
| 478 |
Summary: mustBe("usage: Gt(NUM|STRING|TIME), but received []uint8 (slice) as 1st parameter"), |
| 479 |
}) |
| 480 |
|
| 481 |
checkError(t, "never tested", |
| 482 |
td.Gte([]byte("test")), |
| 483 |
expectedError{ |
| 484 |
Message: mustBe("bad usage of Gte operator"), |
| 485 |
Path: mustBe("DATA"), |
| 486 |
Summary: mustBe("usage: Gte(NUM|STRING|TIME), but received []uint8 (slice) as 1st parameter"), |
| 487 |
}) |
| 488 |
|
| 489 |
checkError(t, "never tested", |
| 490 |
td.Lt([]byte("test")), |
| 491 |
expectedError{ |
| 492 |
Message: mustBe("bad usage of Lt operator"), |
| 493 |
Path: mustBe("DATA"), |
| 494 |
Summary: mustBe("usage: Lt(NUM|STRING|TIME), but received []uint8 (slice) as 1st parameter"), |
| 495 |
}) |
| 496 |
|
| 497 |
checkError(t, "never tested", |
| 498 |
td.Lte([]byte("test")), |
| 499 |
expectedError{ |
| 500 |
Message: mustBe("bad usage of Lte operator"), |
| 501 |
Path: mustBe("DATA"), |
| 502 |
Summary: mustBe("usage: Lte(NUM|STRING|TIME), but received []uint8 (slice) as 1st parameter"), |
| 503 |
}) |
| 504 |
|
| 505 |
// Erroneous op |
| 506 |
test.EqualStr(t, td.Gt([]byte("test")).String(), "Gt(<ERROR>)") |
| 507 |
test.EqualStr(t, td.Gte([]byte("test")).String(), "Gte(<ERROR>)") |
| 508 |
test.EqualStr(t, td.Lt([]byte("test")).String(), "Lt(<ERROR>)") |
| 509 |
test.EqualStr(t, td.Lte([]byte("test")).String(), "Lte(<ERROR>)") |
| 510 |
} |
| 511 |
|
| 512 |
func TestBetweenTime(t *testing.T) { |
| 513 |
type MyTime time.Time |
| 514 |
|
| 515 |
now := time.Now() |
| 516 |
|
| 517 |
checkOK(t, now, td.Between(now, now)) |
| 518 |
checkOK(t, now, td.Between(now.Add(-time.Second), now.Add(time.Second))) |
| 519 |
checkOK(t, now, td.Between(now.Add(time.Second), now.Add(-time.Second))) |
| 520 |
|
| 521 |
// (TIME, DURATION) |
| 522 |
checkOK(t, now, td.Between(now.Add(-time.Second), 2*time.Second)) |
| 523 |
checkOK(t, now, td.Between(now.Add(time.Second), -2*time.Second)) |
| 524 |
|
| 525 |
checkOK(t, MyTime(now), |
| 526 |
td.Between( |
| 527 |
MyTime(now.Add(-time.Second)), |
| 528 |
MyTime(now.Add(time.Second)))) |
| 529 |
|
| 530 |
// (TIME, DURATION) |
| 531 |
checkOK(t, MyTime(now), |
| 532 |
td.Between( |
| 533 |
MyTime(now.Add(-time.Second)), |
| 534 |
2*time.Second)) |
| 535 |
checkOK(t, MyTime(now), |
| 536 |
td.Between( |
| 537 |
MyTime(now.Add(time.Second)), |
| 538 |
-2*time.Second)) |
| 539 |
|
| 540 |
// Lax mode |
| 541 |
checkOK(t, MyTime(now), |
| 542 |
td.Lax(td.Between( |
| 543 |
now.Add(time.Second), |
| 544 |
now.Add(-time.Second)))) |
| 545 |
checkOK(t, now, |
| 546 |
td.Lax(td.Between( |
| 547 |
MyTime(now.Add(time.Second)), |
| 548 |
MyTime(now.Add(-time.Second))))) |
| 549 |
checkOK(t, MyTime(now), |
| 550 |
td.Lax(td.Between( |
| 551 |
now.Add(-time.Second), |
| 552 |
2*time.Second))) |
| 553 |
checkOK(t, now, |
| 554 |
td.Lax(td.Between( |
| 555 |
MyTime(now.Add(-time.Second)), |
| 556 |
2*time.Second))) |
| 557 |
checkOK(t, now, |
| 558 |
td.Lax(td.Between( |
| 559 |
MyTime(now.Add(-time.Second)), |
| 560 |
2*time.Second, |
| 561 |
td.BoundsOutOut))) |
| 562 |
|
| 563 |
date := time.Date(2018, time.March, 4, 0, 0, 0, 0, time.UTC) |
| 564 |
checkError(t, date, |
| 565 |
td.Between(date.Add(-2*time.Second), date.Add(-time.Second)), |
| 566 |
expectedError{ |
| 567 |
Message: mustBe("values differ"), |
| 568 |
Path: mustBe("DATA"), |
| 569 |
Got: mustBe("(time.Time) 2018-03-04 00:00:00 +0000 UTC"), |
| 570 |
Expected: mustBe("(time.Time) 2018-03-03 23:59:58 +0000 UTC" + |
| 571 |
" ≤ got ≤ " + |
| 572 |
"(time.Time) 2018-03-03 23:59:59 +0000 UTC"), |
| 573 |
}) |
| 574 |
|
| 575 |
checkError(t, MyTime(date), |
| 576 |
td.Between(MyTime(date.Add(-2*time.Second)), MyTime(date.Add(-time.Second))), |
| 577 |
expectedError{ |
| 578 |
Message: mustBe("values differ"), |
| 579 |
Path: mustBe("DATA"), |
| 580 |
Got: mustContain("(td_test.MyTime) "), |
| 581 |
Expected: mustBe( |
| 582 |
"(time.Time) 2018-03-03 23:59:58 +0000 UTC" + |
| 583 |
" ≤ got ≤ " + |
| 584 |
"(time.Time) 2018-03-03 23:59:59 +0000 UTC"), |
| 585 |
}) |
| 586 |
|
| 587 |
checkError(t, date, |
| 588 |
td.Between(date.Add(-2*time.Second), date, td.BoundsInOut), |
| 589 |
expectedError{ |
| 590 |
Message: mustBe("values differ"), |
| 591 |
Path: mustBe("DATA"), |
| 592 |
Got: mustBe("(time.Time) 2018-03-04 00:00:00 +0000 UTC"), |
| 593 |
Expected: mustBe("(time.Time) 2018-03-03 23:59:58 +0000 UTC" + |
| 594 |
" ≤ got < " + |
| 595 |
"(time.Time) 2018-03-04 00:00:00 +0000 UTC"), |
| 596 |
}) |
| 597 |
|
| 598 |
checkError(t, date, |
| 599 |
td.Between(date, date.Add(2*time.Second), td.BoundsOutIn), |
| 600 |
expectedError{ |
| 601 |
Message: mustBe("values differ"), |
| 602 |
Path: mustBe("DATA"), |
| 603 |
Got: mustBe("(time.Time) 2018-03-04 00:00:00 +0000 UTC"), |
| 604 |
Expected: mustBe("(time.Time) 2018-03-04 00:00:00 +0000 UTC" + |
| 605 |
" < got ≤ " + |
| 606 |
"(time.Time) 2018-03-04 00:00:02 +0000 UTC"), |
| 607 |
}) |
| 608 |
|
| 609 |
checkError(t, "string", |
| 610 |
td.Between(date, date.Add(2*time.Second), td.BoundsOutIn), |
| 611 |
expectedError{ |
| 612 |
Message: mustBe("type mismatch"), |
| 613 |
Path: mustBe("DATA"), |
| 614 |
Got: mustBe("string"), |
| 615 |
Expected: mustBe("time.Time"), |
| 616 |
}) |
| 617 |
|
| 618 |
checkError(t, "string", |
| 619 |
td.Between(MyTime(date), MyTime(date.Add(2*time.Second)), td.BoundsOutIn), |
| 620 |
expectedError{ |
| 621 |
Message: mustBe("type mismatch"), |
| 622 |
Path: mustBe("DATA"), |
| 623 |
Got: mustBe("string"), |
| 624 |
Expected: mustBe("td_test.MyTime"), |
| 625 |
}) |
| 626 |
|
| 627 |
checkError(t, "never tested", |
| 628 |
td.Between(date, 12), // (Time, Time) or (Time, Duration) |
| 629 |
expectedError{ |
| 630 |
Message: mustBe("bad usage of Between operator"), |
| 631 |
Path: mustBe("DATA"), |
| 632 |
Summary: mustBe("Between(FROM, TO): when FROM type is time.Time, TO must have the same type or time.Duration: int ≠ time.Time|time.Duration"), |
| 633 |
}) |
| 634 |
|
| 635 |
checkError(t, "never tested", |
| 636 |
td.Between(MyTime(date), 12), // (MyTime, MyTime) or (MyTime, Duration) |
| 637 |
expectedError{ |
| 638 |
Message: mustBe("bad usage of Between operator"), |
| 639 |
Path: mustBe("DATA"), |
| 640 |
Summary: mustBe("Between(FROM, TO): when FROM type is td_test.MyTime, TO must have the same type or time.Duration: int ≠ td_test.MyTime|time.Duration"), |
| 641 |
}) |
| 642 |
|
| 643 |
checkOK(t, now, td.Gt(now.Add(-time.Second))) |
| 644 |
checkOK(t, now, td.Lt(now.Add(time.Second))) |
| 645 |
} |
| 646 |
|
| 647 |
type compareType int |
| 648 |
|
| 649 |
func (i compareType) Compare(j compareType) int { |
| 650 |
if i < j { |
| 651 |
return -1 |
| 652 |
} |
| 653 |
if i > j { |
| 654 |
return 1 |
| 655 |
} |
| 656 |
return 0 |
| 657 |
} |
| 658 |
|
| 659 |
type lessType int |
| 660 |
|
| 661 |
func (i lessType) Less(j lessType) bool { |
| 662 |
return i < j |
| 663 |
} |
| 664 |
|
| 665 |
func TestBetweenCmp(t *testing.T) { |
| 666 |
t.Run("compareType", func(t *testing.T) { |
| 667 |
checkOK(t, compareType(5), td.Between(compareType(4), compareType(6))) |
| 668 |
checkOK(t, compareType(5), td.Between(compareType(6), compareType(4))) |
| 669 |
checkOK(t, compareType(5), td.Between(compareType(5), compareType(6))) |
| 670 |
checkOK(t, compareType(5), td.Between(compareType(4), compareType(5))) |
| 671 |
|
| 672 |
checkOK(t, compareType(5), |
| 673 |
td.Between(compareType(4), compareType(6), td.BoundsOutOut)) |
| 674 |
checkError(t, compareType(5), |
| 675 |
td.Between(compareType(5), compareType(6), td.BoundsOutIn), |
| 676 |
expectedError{ |
| 677 |
Message: mustBe("values differ"), |
| 678 |
Path: mustBe("DATA"), |
| 679 |
Got: mustBe("(td_test.compareType) 5"), |
| 680 |
Expected: mustBe("(td_test.compareType) 5 < got ≤ (td_test.compareType) 6"), |
| 681 |
}) |
| 682 |
checkError(t, compareType(5), |
| 683 |
td.Between(compareType(4), compareType(5), td.BoundsInOut), |
| 684 |
expectedError{ |
| 685 |
Message: mustBe("values differ"), |
| 686 |
Path: mustBe("DATA"), |
| 687 |
Got: mustBe("(td_test.compareType) 5"), |
| 688 |
Expected: mustBe("(td_test.compareType) 4 ≤ got < (td_test.compareType) 5"), |
| 689 |
}) |
| 690 |
|
| 691 |
// Other between forms |
| 692 |
checkOK(t, compareType(5), td.Gt(compareType(4))) |
| 693 |
checkOK(t, compareType(5), td.Gte(compareType(5))) |
| 694 |
checkOK(t, compareType(5), td.Lt(compareType(6))) |
| 695 |
checkOK(t, compareType(5), td.Lte(compareType(5))) |
| 696 |
|
| 697 |
// BeLax or not BeLax |
| 698 |
for i, op := range []td.TestDeep{ |
| 699 |
td.Between(compareType(4), compareType(6)), |
| 700 |
td.Gt(compareType(4)), |
| 701 |
td.Gte(compareType(5)), |
| 702 |
td.Lt(compareType(6)), |
| 703 |
td.Lte(compareType(5)), |
| 704 |
} { |
| 705 |
// Type mismatch if BeLax not enabled |
| 706 |
checkError(t, 5, op, |
| 707 |
expectedError{ |
| 708 |
Message: mustBe("type mismatch"), |
| 709 |
Path: mustBe("DATA"), |
| 710 |
Got: mustBe("int"), |
| 711 |
Expected: mustBe("td_test.compareType"), |
| 712 |
}, |
| 713 |
"Op #%d", i) |
| 714 |
|
| 715 |
// BeLax enabled is OK |
| 716 |
checkOK(t, 5, td.Lax(op), "Op #%d", i) |
| 717 |
} |
| 718 |
|
| 719 |
// In a private field |
| 720 |
type private struct { |
| 721 |
num compareType |
| 722 |
} |
| 723 |
checkOK(t, private{num: 5}, |
| 724 |
td.Struct(private{}, |
| 725 |
td.StructFields{ |
| 726 |
"num": td.Between(compareType(4), compareType(6)), |
| 727 |
})) |
| 728 |
}) |
| 729 |
|
| 730 |
t.Run("lessType", func(t *testing.T) { |
| 731 |
checkOK(t, lessType(5), td.Between(lessType(4), lessType(6))) |
| 732 |
checkOK(t, lessType(5), td.Between(lessType(6), lessType(4))) |
| 733 |
checkOK(t, lessType(5), td.Between(lessType(5), lessType(6))) |
| 734 |
checkOK(t, lessType(5), td.Between(lessType(4), lessType(5))) |
| 735 |
|
| 736 |
checkOK(t, lessType(5), |
| 737 |
td.Between(lessType(4), lessType(6), td.BoundsOutOut)) |
| 738 |
checkError(t, lessType(5), |
| 739 |
td.Between(lessType(5), lessType(6), td.BoundsOutIn), |
| 740 |
expectedError{ |
| 741 |
Message: mustBe("values differ"), |
| 742 |
Path: mustBe("DATA"), |
| 743 |
Got: mustBe("(td_test.lessType) 5"), |
| 744 |
Expected: mustBe("(td_test.lessType) 5 < got ≤ (td_test.lessType) 6"), |
| 745 |
}) |
| 746 |
checkError(t, lessType(5), |
| 747 |
td.Between(lessType(4), lessType(5), td.BoundsInOut), |
| 748 |
expectedError{ |
| 749 |
Message: mustBe("values differ"), |
| 750 |
Path: mustBe("DATA"), |
| 751 |
Got: mustBe("(td_test.lessType) 5"), |
| 752 |
Expected: mustBe("(td_test.lessType) 4 ≤ got < (td_test.lessType) 5"), |
| 753 |
}) |
| 754 |
|
| 755 |
// Other between forms |
| 756 |
checkOK(t, lessType(5), td.Gt(lessType(4))) |
| 757 |
checkOK(t, lessType(5), td.Gte(lessType(5))) |
| 758 |
checkOK(t, lessType(5), td.Lt(lessType(6))) |
| 759 |
checkOK(t, lessType(5), td.Lte(lessType(5))) |
| 760 |
|
| 761 |
// BeLax or not BeLax |
| 762 |
for i, op := range []td.TestDeep{ |
| 763 |
td.Between(lessType(4), lessType(6)), |
| 764 |
td.Gt(lessType(4)), |
| 765 |
td.Gte(lessType(5)), |
| 766 |
td.Lt(lessType(6)), |
| 767 |
td.Lte(lessType(5)), |
| 768 |
} { |
| 769 |
// Type mismatch if BeLax not enabled |
| 770 |
checkError(t, 5, op, |
| 771 |
expectedError{ |
| 772 |
Message: mustBe("type mismatch"), |
| 773 |
Path: mustBe("DATA"), |
| 774 |
Got: mustBe("int"), |
| 775 |
Expected: mustBe("td_test.lessType"), |
| 776 |
}, |
| 777 |
"Op #%d", i) |
| 778 |
|
| 779 |
// BeLax enabled is OK |
| 780 |
checkOK(t, 5, td.Lax(op), "Op #%d", i) |
| 781 |
} |
| 782 |
|
| 783 |
// In a private field |
| 784 |
type private struct { |
| 785 |
num lessType |
| 786 |
} |
| 787 |
checkOK(t, private{num: 5}, |
| 788 |
td.Struct(private{}, |
| 789 |
td.StructFields{ |
| 790 |
"num": td.Between(lessType(4), lessType(6)), |
| 791 |
})) |
| 792 |
}) |
| 793 |
} |
| 794 |
|
| 795 |
func TestBetweenTypeBehind(t *testing.T) { |
| 796 |
type MyTime time.Time |
| 797 |
|
| 798 |
for _, typ := range []any{ |
| 799 |
10, |
| 800 |
int64(23), |
| 801 |
int32(23), |
| 802 |
time.Time{}, |
| 803 |
MyTime{}, |
| 804 |
compareType(0), |
| 805 |
lessType(0), |
| 806 |
} { |
| 807 |
equalTypes(t, td.Between(typ, typ), typ) |
| 808 |
equalTypes(t, td.Gt(typ), typ) |
| 809 |
equalTypes(t, td.Gte(typ), typ) |
| 810 |
equalTypes(t, td.Lt(typ), typ) |
| 811 |
equalTypes(t, td.Lte(typ), typ) |
| 812 |
} |
| 813 |
equalTypes(t, td.N(int64(23), int64(5)), int64(0)) |
| 814 |
|
| 815 |
// Erroneous op |
| 816 |
equalTypes(t, td.Between("test", 12), nil) |
| 817 |
equalTypes(t, td.N(10, 1, 2), nil) |
| 818 |
equalTypes(t, td.Gt([]byte("test")), nil) |
| 819 |
equalTypes(t, td.Gte([]byte("test")), nil) |
| 820 |
equalTypes(t, td.Lt([]byte("test")), nil) |
| 821 |
equalTypes(t, td.Lte([]byte("test")), nil) |
| 822 |
} |