| 1 |
// Copyright (c) 2018-2026, 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 |
// DO NOT EDIT!!! AUTOMATICALLY GENERATED!!! |
| 8 |
|
| 9 |
package td_test |
| 10 |
|
| 11 |
import ( |
| 12 |
"bytes" |
| 13 |
"encoding/json" |
| 14 |
"errors" |
| 15 |
"fmt" |
| 16 |
"math" |
| 17 |
"os" |
| 18 |
"regexp" |
| 19 |
"strconv" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/maxatome/go-testdeep/td" |
| 25 |
) |
| 26 |
|
| 27 |
func ExampleCmpAll() { |
| 28 |
t := &testing.T{} |
| 29 |
|
| 30 |
got := "foo/bar" |
| 31 |
|
| 32 |
// Checks got string against: |
| 33 |
// "o/b" regexp *AND* "bar" suffix *AND* exact "foo/bar" string |
| 34 |
ok := td.CmpAll(t, got, []any{td.Re("o/b"), td.HasSuffix("bar"), "foo/bar"}, |
| 35 |
"checks value %s", got) |
| 36 |
fmt.Println(ok) |
| 37 |
|
| 38 |
// Checks got string against: |
| 39 |
// "o/b" regexp *AND* "bar" suffix *AND* exact "fooX/Ybar" string |
| 40 |
ok = td.CmpAll(t, got, []any{td.Re("o/b"), td.HasSuffix("bar"), "fooX/Ybar"}, |
| 41 |
"checks value %s", got) |
| 42 |
fmt.Println(ok) |
| 43 |
|
| 44 |
// When some operators or values have to be reused and mixed between |
| 45 |
// several calls, Flatten can be used to avoid boring and |
| 46 |
// inefficient []any copies: |
| 47 |
regOps := td.Flatten([]td.TestDeep{td.Re("o/b"), td.Re(`^fo`), td.Re(`ar$`)}) |
| 48 |
ok = td.CmpAll(t, got, []any{td.HasPrefix("foo"), regOps, td.HasSuffix("bar")}, |
| 49 |
"checks all operators against value %s", got) |
| 50 |
fmt.Println(ok) |
| 51 |
|
| 52 |
// Output: |
| 53 |
// true |
| 54 |
// false |
| 55 |
// true |
| 56 |
} |
| 57 |
|
| 58 |
func ExampleCmpAny() { |
| 59 |
t := &testing.T{} |
| 60 |
|
| 61 |
got := "foo/bar" |
| 62 |
|
| 63 |
// Checks got string against: |
| 64 |
// "zip" regexp *OR* "bar" suffix |
| 65 |
ok := td.CmpAny(t, got, []any{td.Re("zip"), td.HasSuffix("bar")}, |
| 66 |
"checks value %s", got) |
| 67 |
fmt.Println(ok) |
| 68 |
|
| 69 |
// Checks got string against: |
| 70 |
// "zip" regexp *OR* "foo" suffix |
| 71 |
ok = td.CmpAny(t, got, []any{td.Re("zip"), td.HasSuffix("foo")}, |
| 72 |
"checks value %s", got) |
| 73 |
fmt.Println(ok) |
| 74 |
|
| 75 |
// When some operators or values have to be reused and mixed between |
| 76 |
// several calls, Flatten can be used to avoid boring and |
| 77 |
// inefficient []any copies: |
| 78 |
regOps := td.Flatten([]td.TestDeep{td.Re("a/c"), td.Re(`^xx`), td.Re(`ar$`)}) |
| 79 |
ok = td.CmpAny(t, got, []any{td.HasPrefix("xxx"), regOps, td.HasSuffix("zip")}, |
| 80 |
"check at least one operator matches value %s", got) |
| 81 |
fmt.Println(ok) |
| 82 |
|
| 83 |
// Output: |
| 84 |
// true |
| 85 |
// false |
| 86 |
// true |
| 87 |
} |
| 88 |
|
| 89 |
func ExampleCmpArray_array() { |
| 90 |
t := &testing.T{} |
| 91 |
|
| 92 |
got := [3]int{42, 58, 26} |
| 93 |
|
| 94 |
ok := td.CmpArray(t, got, [3]int{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}, |
| 95 |
"checks array %v", got) |
| 96 |
fmt.Println("Simple array:", ok) |
| 97 |
|
| 98 |
ok = td.CmpArray(t, &got, &[3]int{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}, |
| 99 |
"checks array %v", got) |
| 100 |
fmt.Println("Array pointer:", ok) |
| 101 |
|
| 102 |
ok = td.CmpArray(t, &got, (*[3]int)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}, |
| 103 |
"checks array %v", got) |
| 104 |
fmt.Println("Array pointer, nil model:", ok) |
| 105 |
|
| 106 |
// Output: |
| 107 |
// Simple array: true |
| 108 |
// Array pointer: true |
| 109 |
// Array pointer, nil model: true |
| 110 |
} |
| 111 |
|
| 112 |
func ExampleCmpArray_typedArray() { |
| 113 |
t := &testing.T{} |
| 114 |
|
| 115 |
type MyArray [3]int |
| 116 |
|
| 117 |
got := MyArray{42, 58, 26} |
| 118 |
|
| 119 |
ok := td.CmpArray(t, got, MyArray{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}, |
| 120 |
"checks typed array %v", got) |
| 121 |
fmt.Println("Typed array:", ok) |
| 122 |
|
| 123 |
ok = td.CmpArray(t, &got, &MyArray{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}, |
| 124 |
"checks pointer on typed array %v", got) |
| 125 |
fmt.Println("Pointer on a typed array:", ok) |
| 126 |
|
| 127 |
ok = td.CmpArray(t, &got, &MyArray{}, td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}, |
| 128 |
"checks pointer on typed array %v", got) |
| 129 |
fmt.Println("Pointer on a typed array, empty model:", ok) |
| 130 |
|
| 131 |
ok = td.CmpArray(t, &got, (*MyArray)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}, |
| 132 |
"checks pointer on typed array %v", got) |
| 133 |
fmt.Println("Pointer on a typed array, nil model:", ok) |
| 134 |
|
| 135 |
// Output: |
| 136 |
// Typed array: true |
| 137 |
// Pointer on a typed array: true |
| 138 |
// Pointer on a typed array, empty model: true |
| 139 |
// Pointer on a typed array, nil model: true |
| 140 |
} |
| 141 |
|
| 142 |
func ExampleCmpArrayEach_array() { |
| 143 |
t := &testing.T{} |
| 144 |
|
| 145 |
got := [3]int{42, 58, 26} |
| 146 |
|
| 147 |
ok := td.CmpArrayEach(t, got, td.Between(25, 60), |
| 148 |
"checks each item of array %v is in [25 .. 60]", got) |
| 149 |
fmt.Println(ok) |
| 150 |
|
| 151 |
// Output: |
| 152 |
// true |
| 153 |
} |
| 154 |
|
| 155 |
func ExampleCmpArrayEach_typedArray() { |
| 156 |
t := &testing.T{} |
| 157 |
|
| 158 |
type MyArray [3]int |
| 159 |
|
| 160 |
got := MyArray{42, 58, 26} |
| 161 |
|
| 162 |
ok := td.CmpArrayEach(t, got, td.Between(25, 60), |
| 163 |
"checks each item of typed array %v is in [25 .. 60]", got) |
| 164 |
fmt.Println(ok) |
| 165 |
|
| 166 |
ok = td.CmpArrayEach(t, &got, td.Between(25, 60), |
| 167 |
"checks each item of typed array pointer %v is in [25 .. 60]", got) |
| 168 |
fmt.Println(ok) |
| 169 |
|
| 170 |
// Output: |
| 171 |
// true |
| 172 |
// true |
| 173 |
} |
| 174 |
|
| 175 |
func ExampleCmpArrayEach_slice() { |
| 176 |
t := &testing.T{} |
| 177 |
|
| 178 |
got := []int{42, 58, 26} |
| 179 |
|
| 180 |
ok := td.CmpArrayEach(t, got, td.Between(25, 60), |
| 181 |
"checks each item of slice %v is in [25 .. 60]", got) |
| 182 |
fmt.Println(ok) |
| 183 |
|
| 184 |
// Output: |
| 185 |
// true |
| 186 |
} |
| 187 |
|
| 188 |
func ExampleCmpArrayEach_typedSlice() { |
| 189 |
t := &testing.T{} |
| 190 |
|
| 191 |
type MySlice []int |
| 192 |
|
| 193 |
got := MySlice{42, 58, 26} |
| 194 |
|
| 195 |
ok := td.CmpArrayEach(t, got, td.Between(25, 60), |
| 196 |
"checks each item of typed slice %v is in [25 .. 60]", got) |
| 197 |
fmt.Println(ok) |
| 198 |
|
| 199 |
ok = td.CmpArrayEach(t, &got, td.Between(25, 60), |
| 200 |
"checks each item of typed slice pointer %v is in [25 .. 60]", got) |
| 201 |
fmt.Println(ok) |
| 202 |
|
| 203 |
// Output: |
| 204 |
// true |
| 205 |
// true |
| 206 |
} |
| 207 |
|
| 208 |
func ExampleCmpBag() { |
| 209 |
t := &testing.T{} |
| 210 |
|
| 211 |
got := []int{1, 3, 5, 8, 8, 1, 2} |
| 212 |
|
| 213 |
// Matches as all items are present |
| 214 |
ok := td.CmpBag(t, got, []any{1, 1, 2, 3, 5, 8, 8}, |
| 215 |
"checks all items are present, in any order") |
| 216 |
fmt.Println(ok) |
| 217 |
|
| 218 |
// Does not match as got contains 2 times 1 and 8, and these |
| 219 |
// duplicates are not expected |
| 220 |
ok = td.CmpBag(t, got, []any{1, 2, 3, 5, 8}, |
| 221 |
"checks all items are present, in any order") |
| 222 |
fmt.Println(ok) |
| 223 |
|
| 224 |
got = []int{1, 3, 5, 8, 2} |
| 225 |
|
| 226 |
// Duplicates of 1 and 8 are expected but not present in got |
| 227 |
ok = td.CmpBag(t, got, []any{1, 1, 2, 3, 5, 8, 8}, |
| 228 |
"checks all items are present, in any order") |
| 229 |
fmt.Println(ok) |
| 230 |
|
| 231 |
// Matches as all items are present |
| 232 |
ok = td.CmpBag(t, got, []any{1, 2, 3, 5, td.Gt(7)}, |
| 233 |
"checks all items are present, in any order") |
| 234 |
fmt.Println(ok) |
| 235 |
|
| 236 |
// When expected is already a non-[]any slice, it cannot be |
| 237 |
// flattened directly using expected... without copying it to a new |
| 238 |
// []any slice, then use td.Flatten! |
| 239 |
expected := []int{1, 2, 3, 5} |
| 240 |
ok = td.CmpBag(t, got, []any{td.Flatten(expected), td.Gt(7)}, |
| 241 |
"checks all expected items are present, in any order") |
| 242 |
fmt.Println(ok) |
| 243 |
|
| 244 |
// Output: |
| 245 |
// true |
| 246 |
// false |
| 247 |
// false |
| 248 |
// true |
| 249 |
// true |
| 250 |
} |
| 251 |
|
| 252 |
func ExampleCmpBetween_int() { |
| 253 |
t := &testing.T{} |
| 254 |
|
| 255 |
got := 156 |
| 256 |
|
| 257 |
ok := td.CmpBetween(t, got, 154, 156, td.BoundsInIn, |
| 258 |
"checks %v is in [154 .. 156]", got) |
| 259 |
fmt.Println(ok) |
| 260 |
|
| 261 |
// BoundsInIn is implicit |
| 262 |
ok = td.CmpBetween(t, got, 154, 156, td.BoundsInIn, |
| 263 |
"checks %v is in [154 .. 156]", got) |
| 264 |
fmt.Println(ok) |
| 265 |
|
| 266 |
ok = td.CmpBetween(t, got, 154, 156, td.BoundsInOut, |
| 267 |
"checks %v is in [154 .. 156[", got) |
| 268 |
fmt.Println(ok) |
| 269 |
|
| 270 |
ok = td.CmpBetween(t, got, 154, 156, td.BoundsOutIn, |
| 271 |
"checks %v is in ]154 .. 156]", got) |
| 272 |
fmt.Println(ok) |
| 273 |
|
| 274 |
ok = td.CmpBetween(t, got, 154, 156, td.BoundsOutOut, |
| 275 |
"checks %v is in ]154 .. 156[", got) |
| 276 |
fmt.Println(ok) |
| 277 |
|
| 278 |
// Output: |
| 279 |
// true |
| 280 |
// true |
| 281 |
// false |
| 282 |
// true |
| 283 |
// false |
| 284 |
} |
| 285 |
|
| 286 |
func ExampleCmpBetween_string() { |
| 287 |
t := &testing.T{} |
| 288 |
|
| 289 |
got := "abc" |
| 290 |
|
| 291 |
ok := td.CmpBetween(t, got, "aaa", "abc", td.BoundsInIn, |
| 292 |
`checks "%v" is in ["aaa" .. "abc"]`, got) |
| 293 |
fmt.Println(ok) |
| 294 |
|
| 295 |
// BoundsInIn is implicit |
| 296 |
ok = td.CmpBetween(t, got, "aaa", "abc", td.BoundsInIn, |
| 297 |
`checks "%v" is in ["aaa" .. "abc"]`, got) |
| 298 |
fmt.Println(ok) |
| 299 |
|
| 300 |
ok = td.CmpBetween(t, got, "aaa", "abc", td.BoundsInOut, |
| 301 |
`checks "%v" is in ["aaa" .. "abc"[`, got) |
| 302 |
fmt.Println(ok) |
| 303 |
|
| 304 |
ok = td.CmpBetween(t, got, "aaa", "abc", td.BoundsOutIn, |
| 305 |
`checks "%v" is in ]"aaa" .. "abc"]`, got) |
| 306 |
fmt.Println(ok) |
| 307 |
|
| 308 |
ok = td.CmpBetween(t, got, "aaa", "abc", td.BoundsOutOut, |
| 309 |
`checks "%v" is in ]"aaa" .. "abc"[`, got) |
| 310 |
fmt.Println(ok) |
| 311 |
|
| 312 |
// Output: |
| 313 |
// true |
| 314 |
// true |
| 315 |
// false |
| 316 |
// true |
| 317 |
// false |
| 318 |
} |
| 319 |
|
| 320 |
func ExampleCmpBetween_time() { |
| 321 |
t := &testing.T{} |
| 322 |
|
| 323 |
before := time.Now() |
| 324 |
occurredAt := time.Now() |
| 325 |
after := time.Now() |
| 326 |
|
| 327 |
ok := td.CmpBetween(t, occurredAt, before, after, td.BoundsInIn) |
| 328 |
fmt.Println("It occurred between before and after:", ok) |
| 329 |
|
| 330 |
type MyTime time.Time |
| 331 |
ok = td.CmpBetween(t, MyTime(occurredAt), MyTime(before), MyTime(after), td.BoundsInIn) |
| 332 |
fmt.Println("Same for convertible MyTime type:", ok) |
| 333 |
|
| 334 |
ok = td.CmpBetween(t, MyTime(occurredAt), before, after, td.BoundsInIn) |
| 335 |
fmt.Println("MyTime vs time.Time:", ok) |
| 336 |
|
| 337 |
ok = td.CmpBetween(t, occurredAt, before, 10*time.Second, td.BoundsInIn) |
| 338 |
fmt.Println("Using a time.Duration as TO:", ok) |
| 339 |
|
| 340 |
ok = td.CmpBetween(t, MyTime(occurredAt), MyTime(before), 10*time.Second, td.BoundsInIn) |
| 341 |
fmt.Println("Using MyTime as FROM and time.Duration as TO:", ok) |
| 342 |
|
| 343 |
// Output: |
| 344 |
// It occurred between before and after: true |
| 345 |
// Same for convertible MyTime type: true |
| 346 |
// MyTime vs time.Time: false |
| 347 |
// Using a time.Duration as TO: true |
| 348 |
// Using MyTime as FROM and time.Duration as TO: true |
| 349 |
} |
| 350 |
|
| 351 |
func ExampleCmpCap() { |
| 352 |
t := &testing.T{} |
| 353 |
|
| 354 |
got := make([]int, 0, 12) |
| 355 |
|
| 356 |
ok := td.CmpCap(t, got, 12, "checks %v capacity is 12", got) |
| 357 |
fmt.Println(ok) |
| 358 |
|
| 359 |
ok = td.CmpCap(t, got, 0, "checks %v capacity is 0", got) |
| 360 |
fmt.Println(ok) |
| 361 |
|
| 362 |
got = nil |
| 363 |
|
| 364 |
ok = td.CmpCap(t, got, 0, "checks %v capacity is 0", got) |
| 365 |
fmt.Println(ok) |
| 366 |
|
| 367 |
// Output: |
| 368 |
// true |
| 369 |
// false |
| 370 |
// true |
| 371 |
} |
| 372 |
|
| 373 |
func ExampleCmpCap_operator() { |
| 374 |
t := &testing.T{} |
| 375 |
|
| 376 |
got := make([]int, 0, 12) |
| 377 |
|
| 378 |
ok := td.CmpCap(t, got, td.Between(10, 12), |
| 379 |
"checks %v capacity is in [10 .. 12]", got) |
| 380 |
fmt.Println(ok) |
| 381 |
|
| 382 |
ok = td.CmpCap(t, got, td.Gt(10), |
| 383 |
"checks %v capacity is in [10 .. 12]", got) |
| 384 |
fmt.Println(ok) |
| 385 |
|
| 386 |
// Output: |
| 387 |
// true |
| 388 |
// true |
| 389 |
} |
| 390 |
|
| 391 |
func ExampleCmpCode() { |
| 392 |
t := &testing.T{} |
| 393 |
|
| 394 |
got := "12" |
| 395 |
|
| 396 |
ok := td.CmpCode(t, got, func(num string) bool { |
| 397 |
n, err := strconv.Atoi(num) |
| 398 |
return err == nil && n > 10 && n < 100 |
| 399 |
}, |
| 400 |
"checks string `%s` contains a number and this number is in ]10 .. 100[", |
| 401 |
got) |
| 402 |
fmt.Println(ok) |
| 403 |
|
| 404 |
// Same with failure reason |
| 405 |
ok = td.CmpCode(t, got, func(num string) (bool, string) { |
| 406 |
n, err := strconv.Atoi(num) |
| 407 |
if err != nil { |
| 408 |
return false, "not a number" |
| 409 |
} |
| 410 |
if n > 10 && n < 100 { |
| 411 |
return true, "" |
| 412 |
} |
| 413 |
return false, "not in ]10 .. 100[" |
| 414 |
}, |
| 415 |
"checks string `%s` contains a number and this number is in ]10 .. 100[", |
| 416 |
got) |
| 417 |
fmt.Println(ok) |
| 418 |
|
| 419 |
// Same with failure reason thanks to error |
| 420 |
ok = td.CmpCode(t, got, func(num string) error { |
| 421 |
n, err := strconv.Atoi(num) |
| 422 |
if err != nil { |
| 423 |
return err |
| 424 |
} |
| 425 |
if n > 10 && n < 100 { |
| 426 |
return nil |
| 427 |
} |
| 428 |
return fmt.Errorf("%d not in ]10 .. 100[", n) |
| 429 |
}, |
| 430 |
"checks string `%s` contains a number and this number is in ]10 .. 100[", |
| 431 |
got) |
| 432 |
fmt.Println(ok) |
| 433 |
|
| 434 |
// Output: |
| 435 |
// true |
| 436 |
// true |
| 437 |
// true |
| 438 |
} |
| 439 |
|
| 440 |
func ExampleCmpCode_custom() { |
| 441 |
t := &testing.T{} |
| 442 |
|
| 443 |
got := 123 |
| 444 |
|
| 445 |
ok := td.CmpCode(t, got, func(t *td.T, num int) { |
| 446 |
t.Cmp(num, 123) |
| 447 |
}) |
| 448 |
fmt.Println("with one *td.T:", ok) |
| 449 |
|
| 450 |
ok = td.CmpCode(t, got, func(assert, require *td.T, num int) { |
| 451 |
assert.Cmp(num, 123) |
| 452 |
require.Cmp(num, 123) |
| 453 |
}) |
| 454 |
fmt.Println("with assert & require *td.T:", ok) |
| 455 |
|
| 456 |
// Output: |
| 457 |
// with one *td.T: true |
| 458 |
// with assert & require *td.T: true |
| 459 |
} |
| 460 |
|
| 461 |
func ExampleCmpContains_arraySlice() { |
| 462 |
t := &testing.T{} |
| 463 |
|
| 464 |
ok := td.CmpContains(t, [...]int{11, 22, 33, 44}, 22) |
| 465 |
fmt.Println("array contains 22:", ok) |
| 466 |
|
| 467 |
ok = td.CmpContains(t, [...]int{11, 22, 33, 44}, td.Between(20, 25)) |
| 468 |
fmt.Println("array contains at least one item in [20 .. 25]:", ok) |
| 469 |
|
| 470 |
ok = td.CmpContains(t, []int{11, 22, 33, 44}, 22) |
| 471 |
fmt.Println("slice contains 22:", ok) |
| 472 |
|
| 473 |
ok = td.CmpContains(t, []int{11, 22, 33, 44}, td.Between(20, 25)) |
| 474 |
fmt.Println("slice contains at least one item in [20 .. 25]:", ok) |
| 475 |
|
| 476 |
ok = td.CmpContains(t, []int{11, 22, 33, 44}, []int{22, 33}) |
| 477 |
fmt.Println("slice contains the sub-slice [22, 33]:", ok) |
| 478 |
|
| 479 |
// Output: |
| 480 |
// array contains 22: true |
| 481 |
// array contains at least one item in [20 .. 25]: true |
| 482 |
// slice contains 22: true |
| 483 |
// slice contains at least one item in [20 .. 25]: true |
| 484 |
// slice contains the sub-slice [22, 33]: true |
| 485 |
} |
| 486 |
|
| 487 |
func ExampleCmpContains_nil() { |
| 488 |
t := &testing.T{} |
| 489 |
|
| 490 |
num := 123 |
| 491 |
got := [...]*int{&num, nil} |
| 492 |
|
| 493 |
ok := td.CmpContains(t, got, nil) |
| 494 |
fmt.Println("array contains untyped nil:", ok) |
| 495 |
|
| 496 |
ok = td.CmpContains(t, got, (*int)(nil)) |
| 497 |
fmt.Println("array contains *int nil:", ok) |
| 498 |
|
| 499 |
ok = td.CmpContains(t, got, td.Nil()) |
| 500 |
fmt.Println("array contains Nil():", ok) |
| 501 |
|
| 502 |
ok = td.CmpContains(t, got, (*byte)(nil)) |
| 503 |
fmt.Println("array contains *byte nil:", ok) // types differ: *byte ≠ *int |
| 504 |
|
| 505 |
// Output: |
| 506 |
// array contains untyped nil: true |
| 507 |
// array contains *int nil: true |
| 508 |
// array contains Nil(): true |
| 509 |
// array contains *byte nil: false |
| 510 |
} |
| 511 |
|
| 512 |
func ExampleCmpContains_map() { |
| 513 |
t := &testing.T{} |
| 514 |
|
| 515 |
ok := td.CmpContains(t, map[string]int{"foo": 11, "bar": 22, "zip": 33}, 22) |
| 516 |
fmt.Println("map contains value 22:", ok) |
| 517 |
|
| 518 |
ok = td.CmpContains(t, map[string]int{"foo": 11, "bar": 22, "zip": 33}, td.Between(20, 25)) |
| 519 |
fmt.Println("map contains at least one value in [20 .. 25]:", ok) |
| 520 |
|
| 521 |
// Output: |
| 522 |
// map contains value 22: true |
| 523 |
// map contains at least one value in [20 .. 25]: true |
| 524 |
} |
| 525 |
|
| 526 |
func ExampleCmpContains_string() { |
| 527 |
t := &testing.T{} |
| 528 |
|
| 529 |
got := "foobar" |
| 530 |
|
| 531 |
ok := td.CmpContains(t, got, "oob", "checks %s", got) |
| 532 |
fmt.Println("contains `oob` string:", ok) |
| 533 |
|
| 534 |
ok = td.CmpContains(t, got, []byte("oob"), "checks %s", got) |
| 535 |
fmt.Println("contains `oob` []byte:", ok) |
| 536 |
|
| 537 |
ok = td.CmpContains(t, got, 'b', "checks %s", got) |
| 538 |
fmt.Println("contains 'b' rune:", ok) |
| 539 |
|
| 540 |
ok = td.CmpContains(t, got, byte('a'), "checks %s", got) |
| 541 |
fmt.Println("contains 'a' byte:", ok) |
| 542 |
|
| 543 |
ok = td.CmpContains(t, got, td.Between('n', 'p'), "checks %s", got) |
| 544 |
fmt.Println("contains at least one character ['n' .. 'p']:", ok) |
| 545 |
|
| 546 |
// Output: |
| 547 |
// contains `oob` string: true |
| 548 |
// contains `oob` []byte: true |
| 549 |
// contains 'b' rune: true |
| 550 |
// contains 'a' byte: true |
| 551 |
// contains at least one character ['n' .. 'p']: true |
| 552 |
} |
| 553 |
|
| 554 |
func ExampleCmpContains_stringer() { |
| 555 |
t := &testing.T{} |
| 556 |
|
| 557 |
// bytes.Buffer implements fmt.Stringer |
| 558 |
got := bytes.NewBufferString("foobar") |
| 559 |
|
| 560 |
ok := td.CmpContains(t, got, "oob", "checks %s", got) |
| 561 |
fmt.Println("contains `oob` string:", ok) |
| 562 |
|
| 563 |
ok = td.CmpContains(t, got, 'b', "checks %s", got) |
| 564 |
fmt.Println("contains 'b' rune:", ok) |
| 565 |
|
| 566 |
ok = td.CmpContains(t, got, byte('a'), "checks %s", got) |
| 567 |
fmt.Println("contains 'a' byte:", ok) |
| 568 |
|
| 569 |
ok = td.CmpContains(t, got, td.Between('n', 'p'), "checks %s", got) |
| 570 |
fmt.Println("contains at least one character ['n' .. 'p']:", ok) |
| 571 |
|
| 572 |
// Output: |
| 573 |
// contains `oob` string: true |
| 574 |
// contains 'b' rune: true |
| 575 |
// contains 'a' byte: true |
| 576 |
// contains at least one character ['n' .. 'p']: true |
| 577 |
} |
| 578 |
|
| 579 |
func ExampleCmpContains_error() { |
| 580 |
t := &testing.T{} |
| 581 |
|
| 582 |
got := errors.New("foobar") |
| 583 |
|
| 584 |
ok := td.CmpContains(t, got, "oob", "checks %s", got) |
| 585 |
fmt.Println("contains `oob` string:", ok) |
| 586 |
|
| 587 |
ok = td.CmpContains(t, got, 'b', "checks %s", got) |
| 588 |
fmt.Println("contains 'b' rune:", ok) |
| 589 |
|
| 590 |
ok = td.CmpContains(t, got, byte('a'), "checks %s", got) |
| 591 |
fmt.Println("contains 'a' byte:", ok) |
| 592 |
|
| 593 |
ok = td.CmpContains(t, got, td.Between('n', 'p'), "checks %s", got) |
| 594 |
fmt.Println("contains at least one character ['n' .. 'p']:", ok) |
| 595 |
|
| 596 |
// Output: |
| 597 |
// contains `oob` string: true |
| 598 |
// contains 'b' rune: true |
| 599 |
// contains 'a' byte: true |
| 600 |
// contains at least one character ['n' .. 'p']: true |
| 601 |
} |
| 602 |
|
| 603 |
func ExampleCmpContainsKey() { |
| 604 |
t := &testing.T{} |
| 605 |
|
| 606 |
ok := td.CmpContainsKey(t, map[string]int{"foo": 11, "bar": 22, "zip": 33}, "foo") |
| 607 |
fmt.Println(`map contains key "foo":`, ok) |
| 608 |
|
| 609 |
ok = td.CmpContainsKey(t, map[int]bool{12: true, 24: false, 42: true, 51: false}, td.Between(40, 50)) |
| 610 |
fmt.Println("map contains at least a key in [40 .. 50]:", ok) |
| 611 |
|
| 612 |
ok = td.CmpContainsKey(t, map[string]int{"FOO": 11, "bar": 22, "zip": 33}, td.Smuggle(strings.ToLower, "foo")) |
| 613 |
fmt.Println(`map contains key "foo" without taking case into account:`, ok) |
| 614 |
|
| 615 |
// Output: |
| 616 |
// map contains key "foo": true |
| 617 |
// map contains at least a key in [40 .. 50]: true |
| 618 |
// map contains key "foo" without taking case into account: true |
| 619 |
} |
| 620 |
|
| 621 |
func ExampleCmpContainsKey_nil() { |
| 622 |
t := &testing.T{} |
| 623 |
|
| 624 |
num := 1234 |
| 625 |
got := map[*int]bool{&num: false, nil: true} |
| 626 |
|
| 627 |
ok := td.CmpContainsKey(t, got, nil) |
| 628 |
fmt.Println("map contains untyped nil key:", ok) |
| 629 |
|
| 630 |
ok = td.CmpContainsKey(t, got, (*int)(nil)) |
| 631 |
fmt.Println("map contains *int nil key:", ok) |
| 632 |
|
| 633 |
ok = td.CmpContainsKey(t, got, td.Nil()) |
| 634 |
fmt.Println("map contains Nil() key:", ok) |
| 635 |
|
| 636 |
ok = td.CmpContainsKey(t, got, (*byte)(nil)) |
| 637 |
fmt.Println("map contains *byte nil key:", ok) // types differ: *byte ≠ *int |
| 638 |
|
| 639 |
// Output: |
| 640 |
// map contains untyped nil key: true |
| 641 |
// map contains *int nil key: true |
| 642 |
// map contains Nil() key: true |
| 643 |
// map contains *byte nil key: false |
| 644 |
} |
| 645 |
|
| 646 |
func ExampleCmpEmpty() { |
| 647 |
t := &testing.T{} |
| 648 |
|
| 649 |
ok := td.CmpEmpty(t, nil) // special case: nil is considered empty |
| 650 |
fmt.Println(ok) |
| 651 |
|
| 652 |
// fails, typed nil is not empty (expect for channel, map, slice or |
| 653 |
// pointers on array, channel, map slice and strings) |
| 654 |
ok = td.CmpEmpty(t, (*int)(nil)) |
| 655 |
fmt.Println(ok) |
| 656 |
|
| 657 |
ok = td.CmpEmpty(t, "") |
| 658 |
fmt.Println(ok) |
| 659 |
|
| 660 |
// Fails as 0 is a number, so not empty. Use Zero() instead |
| 661 |
ok = td.CmpEmpty(t, 0) |
| 662 |
fmt.Println(ok) |
| 663 |
|
| 664 |
ok = td.CmpEmpty(t, (map[string]int)(nil)) |
| 665 |
fmt.Println(ok) |
| 666 |
|
| 667 |
ok = td.CmpEmpty(t, map[string]int{}) |
| 668 |
fmt.Println(ok) |
| 669 |
|
| 670 |
ok = td.CmpEmpty(t, ([]int)(nil)) |
| 671 |
fmt.Println(ok) |
| 672 |
|
| 673 |
ok = td.CmpEmpty(t, []int{}) |
| 674 |
fmt.Println(ok) |
| 675 |
|
| 676 |
ok = td.CmpEmpty(t, []int{3}) // fails, as not empty |
| 677 |
fmt.Println(ok) |
| 678 |
|
| 679 |
ok = td.CmpEmpty(t, [3]int{}) // fails, Empty() is not Zero()! |
| 680 |
fmt.Println(ok) |
| 681 |
|
| 682 |
// Output: |
| 683 |
// true |
| 684 |
// false |
| 685 |
// true |
| 686 |
// false |
| 687 |
// true |
| 688 |
// true |
| 689 |
// true |
| 690 |
// true |
| 691 |
// false |
| 692 |
// false |
| 693 |
} |
| 694 |
|
| 695 |
func ExampleCmpEmpty_pointers() { |
| 696 |
t := &testing.T{} |
| 697 |
|
| 698 |
type MySlice []int |
| 699 |
|
| 700 |
ok := td.CmpEmpty(t, MySlice{}) // Ptr() not needed |
| 701 |
fmt.Println(ok) |
| 702 |
|
| 703 |
ok = td.CmpEmpty(t, &MySlice{}) |
| 704 |
fmt.Println(ok) |
| 705 |
|
| 706 |
l1 := &MySlice{} |
| 707 |
l2 := &l1 |
| 708 |
l3 := &l2 |
| 709 |
ok = td.CmpEmpty(t, &l3) |
| 710 |
fmt.Println(ok) |
| 711 |
|
| 712 |
// Works the same for array, map, channel and string |
| 713 |
|
| 714 |
// But not for others types as: |
| 715 |
type MyStruct struct { |
| 716 |
Value int |
| 717 |
} |
| 718 |
|
| 719 |
ok = td.CmpEmpty(t, &MyStruct{}) // fails, use Zero() instead |
| 720 |
fmt.Println(ok) |
| 721 |
|
| 722 |
// Output: |
| 723 |
// true |
| 724 |
// true |
| 725 |
// true |
| 726 |
// false |
| 727 |
} |
| 728 |
|
| 729 |
func ExampleCmpErrorIs() { |
| 730 |
t := &testing.T{} |
| 731 |
|
| 732 |
err1 := fmt.Errorf("failure1") |
| 733 |
err2 := fmt.Errorf("failure2: %w", err1) |
| 734 |
err3 := fmt.Errorf("failure3: %w", err2) |
| 735 |
err := fmt.Errorf("failure4: %w", err3) |
| 736 |
|
| 737 |
ok := td.CmpErrorIs(t, err, err) |
| 738 |
fmt.Println("error is itself:", ok) |
| 739 |
|
| 740 |
ok = td.CmpErrorIs(t, err, err1) |
| 741 |
fmt.Println("error is also err1:", ok) |
| 742 |
|
| 743 |
ok = td.CmpErrorIs(t, err1, err) |
| 744 |
fmt.Println("err1 is err:", ok) |
| 745 |
|
| 746 |
// Output: |
| 747 |
// error is itself: true |
| 748 |
// error is also err1: true |
| 749 |
// err1 is err: false |
| 750 |
} |
| 751 |
|
| 752 |
func ExampleCmpFirst_classic() { |
| 753 |
t := &testing.T{} |
| 754 |
|
| 755 |
got := []int{-3, -2, -1, 0, 1, 2, 3} |
| 756 |
|
| 757 |
ok := td.CmpFirst(t, got, td.Gt(0), 1) |
| 758 |
fmt.Println("first positive number is 1:", ok) |
| 759 |
|
| 760 |
isEven := func(x int) bool { return x%2 == 0 } |
| 761 |
|
| 762 |
ok = td.CmpFirst(t, got, isEven, -2) |
| 763 |
fmt.Println("first even number is -2:", ok) |
| 764 |
|
| 765 |
ok = td.CmpFirst(t, got, isEven, td.Lt(0)) |
| 766 |
fmt.Println("first even number is < 0:", ok) |
| 767 |
|
| 768 |
ok = td.CmpFirst(t, got, isEven, td.Code(isEven)) |
| 769 |
fmt.Println("first even number is well even:", ok) |
| 770 |
|
| 771 |
// Output: |
| 772 |
// first positive number is 1: true |
| 773 |
// first even number is -2: true |
| 774 |
// first even number is < 0: true |
| 775 |
// first even number is well even: true |
| 776 |
} |
| 777 |
|
| 778 |
func ExampleCmpFirst_empty() { |
| 779 |
t := &testing.T{} |
| 780 |
|
| 781 |
ok := td.CmpFirst(t, ([]int)(nil), td.Gt(0), td.Gt(0)) |
| 782 |
fmt.Println("first in nil slice:", ok) |
| 783 |
|
| 784 |
ok = td.CmpFirst(t, []int{}, td.Gt(0), td.Gt(0)) |
| 785 |
fmt.Println("first in empty slice:", ok) |
| 786 |
|
| 787 |
ok = td.CmpFirst(t, &[]int{}, td.Gt(0), td.Gt(0)) |
| 788 |
fmt.Println("first in empty pointed slice:", ok) |
| 789 |
|
| 790 |
ok = td.CmpFirst(t, [0]int{}, td.Gt(0), td.Gt(0)) |
| 791 |
fmt.Println("first in empty array:", ok) |
| 792 |
|
| 793 |
// Output: |
| 794 |
// first in nil slice: false |
| 795 |
// first in empty slice: false |
| 796 |
// first in empty pointed slice: false |
| 797 |
// first in empty array: false |
| 798 |
} |
| 799 |
|
| 800 |
func ExampleCmpFirst_struct() { |
| 801 |
t := &testing.T{} |
| 802 |
|
| 803 |
type Person struct { |
| 804 |
Fullname string `json:"fullname"` |
| 805 |
Age int `json:"age"` |
| 806 |
} |
| 807 |
|
| 808 |
got := []*Person{ |
| 809 |
{ |
| 810 |
Fullname: "Bob Foobar", |
| 811 |
Age: 42, |
| 812 |
}, |
| 813 |
{ |
| 814 |
Fullname: "Alice Bingo", |
| 815 |
Age: 37, |
| 816 |
}, |
| 817 |
} |
| 818 |
|
| 819 |
ok := td.CmpFirst(t, got, td.Smuggle("Age", td.Gt(30)), td.Smuggle("Fullname", "Bob Foobar")) |
| 820 |
fmt.Println("first person.Age > 30 → Bob:", ok) |
| 821 |
|
| 822 |
ok = td.CmpFirst(t, got, td.JSONPointer("/age", td.Gt(30)), td.SuperJSONOf(`{"fullname":"Bob Foobar"}`)) |
| 823 |
fmt.Println("first person.Age > 30 → Bob, using JSON:", ok) |
| 824 |
|
| 825 |
ok = td.CmpFirst(t, got, td.JSONPointer("/age", td.Gt(30)), td.JSONPointer("/fullname", td.HasPrefix("Bob"))) |
| 826 |
fmt.Println("first person.Age > 30 → Bob, using JSONPointer:", ok) |
| 827 |
|
| 828 |
// Output: |
| 829 |
// first person.Age > 30 → Bob: true |
| 830 |
// first person.Age > 30 → Bob, using JSON: true |
| 831 |
// first person.Age > 30 → Bob, using JSONPointer: true |
| 832 |
} |
| 833 |
|
| 834 |
func ExampleCmpGrep_classic() { |
| 835 |
t := &testing.T{} |
| 836 |
|
| 837 |
got := []int{-3, -2, -1, 0, 1, 2, 3} |
| 838 |
|
| 839 |
ok := td.CmpGrep(t, got, td.Gt(0), []int{1, 2, 3}) |
| 840 |
fmt.Println("check positive numbers:", ok) |
| 841 |
|
| 842 |
isEven := func(x int) bool { return x%2 == 0 } |
| 843 |
|
| 844 |
ok = td.CmpGrep(t, got, isEven, []int{-2, 0, 2}) |
| 845 |
fmt.Println("even numbers are -2, 0 and 2:", ok) |
| 846 |
|
| 847 |
ok = td.CmpGrep(t, got, isEven, td.Set(0, 2, -2)) |
| 848 |
fmt.Println("even numbers are also 0, 2 and -2:", ok) |
| 849 |
|
| 850 |
ok = td.CmpGrep(t, got, isEven, td.ArrayEach(td.Code(isEven))) |
| 851 |
fmt.Println("even numbers are each even:", ok) |
| 852 |
|
| 853 |
// Output: |
| 854 |
// check positive numbers: true |
| 855 |
// even numbers are -2, 0 and 2: true |
| 856 |
// even numbers are also 0, 2 and -2: true |
| 857 |
// even numbers are each even: true |
| 858 |
} |
| 859 |
|
| 860 |
func ExampleCmpGrep_nil() { |
| 861 |
t := &testing.T{} |
| 862 |
|
| 863 |
var got []int |
| 864 |
ok := td.CmpGrep(t, got, td.Gt(0), ([]int)(nil)) |
| 865 |
fmt.Println("typed []int nil:", ok) |
| 866 |
|
| 867 |
ok = td.CmpGrep(t, got, td.Gt(0), ([]string)(nil)) |
| 868 |
fmt.Println("typed []string nil:", ok) |
| 869 |
|
| 870 |
ok = td.CmpGrep(t, got, td.Gt(0), td.Nil()) |
| 871 |
fmt.Println("td.Nil:", ok) |
| 872 |
|
| 873 |
ok = td.CmpGrep(t, got, td.Gt(0), []int{}) |
| 874 |
fmt.Println("empty non-nil slice:", ok) |
| 875 |
|
| 876 |
// Output: |
| 877 |
// typed []int nil: true |
| 878 |
// typed []string nil: false |
| 879 |
// td.Nil: true |
| 880 |
// empty non-nil slice: false |
| 881 |
} |
| 882 |
|
| 883 |
func ExampleCmpGrep_struct() { |
| 884 |
t := &testing.T{} |
| 885 |
|
| 886 |
type Person struct { |
| 887 |
Fullname string `json:"fullname"` |
| 888 |
Age int `json:"age"` |
| 889 |
} |
| 890 |
|
| 891 |
got := []*Person{ |
| 892 |
{ |
| 893 |
Fullname: "Bob Foobar", |
| 894 |
Age: 42, |
| 895 |
}, |
| 896 |
{ |
| 897 |
Fullname: "Alice Bingo", |
| 898 |
Age: 27, |
| 899 |
}, |
| 900 |
} |
| 901 |
|
| 902 |
ok := td.CmpGrep(t, got, td.Smuggle("Age", td.Gt(30)), td.All( |
| 903 |
td.Len(1), |
| 904 |
td.ArrayEach(td.Smuggle("Fullname", "Bob Foobar")), |
| 905 |
)) |
| 906 |
fmt.Println("person.Age > 30 → only Bob:", ok) |
| 907 |
|
| 908 |
ok = td.CmpGrep(t, got, td.JSONPointer("/age", td.Gt(30)), td.JSON(`[ SuperMapOf({"fullname":"Bob Foobar"}) ]`)) |
| 909 |
fmt.Println("person.Age > 30 → only Bob, using JSON:", ok) |
| 910 |
|
| 911 |
// Output: |
| 912 |
// person.Age > 30 → only Bob: true |
| 913 |
// person.Age > 30 → only Bob, using JSON: true |
| 914 |
} |
| 915 |
|
| 916 |
func ExampleCmpGt_int() { |
| 917 |
t := &testing.T{} |
| 918 |
|
| 919 |
got := 156 |
| 920 |
|
| 921 |
ok := td.CmpGt(t, got, 155, "checks %v is > 155", got) |
| 922 |
fmt.Println(ok) |
| 923 |
|
| 924 |
ok = td.CmpGt(t, got, 156, "checks %v is > 156", got) |
| 925 |
fmt.Println(ok) |
| 926 |
|
| 927 |
// Output: |
| 928 |
// true |
| 929 |
// false |
| 930 |
} |
| 931 |
|
| 932 |
func ExampleCmpGt_string() { |
| 933 |
t := &testing.T{} |
| 934 |
|
| 935 |
got := "abc" |
| 936 |
|
| 937 |
ok := td.CmpGt(t, got, "abb", `checks "%v" is > "abb"`, got) |
| 938 |
fmt.Println(ok) |
| 939 |
|
| 940 |
ok = td.CmpGt(t, got, "abc", `checks "%v" is > "abc"`, got) |
| 941 |
fmt.Println(ok) |
| 942 |
|
| 943 |
// Output: |
| 944 |
// true |
| 945 |
// false |
| 946 |
} |
| 947 |
|
| 948 |
func ExampleCmpGte_int() { |
| 949 |
t := &testing.T{} |
| 950 |
|
| 951 |
got := 156 |
| 952 |
|
| 953 |
ok := td.CmpGte(t, got, 156, "checks %v is ≥ 156", got) |
| 954 |
fmt.Println(ok) |
| 955 |
|
| 956 |
ok = td.CmpGte(t, got, 155, "checks %v is ≥ 155", got) |
| 957 |
fmt.Println(ok) |
| 958 |
|
| 959 |
ok = td.CmpGte(t, got, 157, "checks %v is ≥ 157", got) |
| 960 |
fmt.Println(ok) |
| 961 |
|
| 962 |
// Output: |
| 963 |
// true |
| 964 |
// true |
| 965 |
// false |
| 966 |
} |
| 967 |
|
| 968 |
func ExampleCmpGte_string() { |
| 969 |
t := &testing.T{} |
| 970 |
|
| 971 |
got := "abc" |
| 972 |
|
| 973 |
ok := td.CmpGte(t, got, "abc", `checks "%v" is ≥ "abc"`, got) |
| 974 |
fmt.Println(ok) |
| 975 |
|
| 976 |
ok = td.CmpGte(t, got, "abb", `checks "%v" is ≥ "abb"`, got) |
| 977 |
fmt.Println(ok) |
| 978 |
|
| 979 |
ok = td.CmpGte(t, got, "abd", `checks "%v" is ≥ "abd"`, got) |
| 980 |
fmt.Println(ok) |
| 981 |
|
| 982 |
// Output: |
| 983 |
// true |
| 984 |
// true |
| 985 |
// false |
| 986 |
} |
| 987 |
|
| 988 |
func ExampleCmpHasPrefix() { |
| 989 |
t := &testing.T{} |
| 990 |
|
| 991 |
got := "foobar" |
| 992 |
|
| 993 |
ok := td.CmpHasPrefix(t, got, "foo", "checks %s", got) |
| 994 |
fmt.Println("using string:", ok) |
| 995 |
|
| 996 |
ok = td.Cmp(t, []byte(got), td.HasPrefix("foo"), "checks %s", got) |
| 997 |
fmt.Println("using []byte:", ok) |
| 998 |
|
| 999 |
// Output: |
| 1000 |
// using string: true |
| 1001 |
// using []byte: true |
| 1002 |
} |
| 1003 |
|
| 1004 |
func ExampleCmpHasPrefix_stringer() { |
| 1005 |
t := &testing.T{} |
| 1006 |
|
| 1007 |
// bytes.Buffer implements fmt.Stringer |
| 1008 |
got := bytes.NewBufferString("foobar") |
| 1009 |
|
| 1010 |
ok := td.CmpHasPrefix(t, got, "foo", "checks %s", got) |
| 1011 |
fmt.Println(ok) |
| 1012 |
|
| 1013 |
// Output: |
| 1014 |
// true |
| 1015 |
} |
| 1016 |
|
| 1017 |
func ExampleCmpHasPrefix_error() { |
| 1018 |
t := &testing.T{} |
| 1019 |
|
| 1020 |
got := errors.New("foobar") |
| 1021 |
|
| 1022 |
ok := td.CmpHasPrefix(t, got, "foo", "checks %s", got) |
| 1023 |
fmt.Println(ok) |
| 1024 |
|
| 1025 |
// Output: |
| 1026 |
// true |
| 1027 |
} |
| 1028 |
|
| 1029 |
func ExampleCmpHasSuffix() { |
| 1030 |
t := &testing.T{} |
| 1031 |
|
| 1032 |
got := "foobar" |
| 1033 |
|
| 1034 |
ok := td.CmpHasSuffix(t, got, "bar", "checks %s", got) |
| 1035 |
fmt.Println("using string:", ok) |
| 1036 |
|
| 1037 |
ok = td.Cmp(t, []byte(got), td.HasSuffix("bar"), "checks %s", got) |
| 1038 |
fmt.Println("using []byte:", ok) |
| 1039 |
|
| 1040 |
// Output: |
| 1041 |
// using string: true |
| 1042 |
// using []byte: true |
| 1043 |
} |
| 1044 |
|
| 1045 |
func ExampleCmpHasSuffix_stringer() { |
| 1046 |
t := &testing.T{} |
| 1047 |
|
| 1048 |
// bytes.Buffer implements fmt.Stringer |
| 1049 |
got := bytes.NewBufferString("foobar") |
| 1050 |
|
| 1051 |
ok := td.CmpHasSuffix(t, got, "bar", "checks %s", got) |
| 1052 |
fmt.Println(ok) |
| 1053 |
|
| 1054 |
// Output: |
| 1055 |
// true |
| 1056 |
} |
| 1057 |
|
| 1058 |
func ExampleCmpHasSuffix_error() { |
| 1059 |
t := &testing.T{} |
| 1060 |
|
| 1061 |
got := errors.New("foobar") |
| 1062 |
|
| 1063 |
ok := td.CmpHasSuffix(t, got, "bar", "checks %s", got) |
| 1064 |
fmt.Println(ok) |
| 1065 |
|
| 1066 |
// Output: |
| 1067 |
// true |
| 1068 |
} |
| 1069 |
|
| 1070 |
func ExampleCmpIsa() { |
| 1071 |
t := &testing.T{} |
| 1072 |
|
| 1073 |
type TstStruct struct { |
| 1074 |
Field int |
| 1075 |
} |
| 1076 |
|
| 1077 |
got := TstStruct{Field: 1} |
| 1078 |
|
| 1079 |
ok := td.CmpIsa(t, got, TstStruct{}, "checks got is a TstStruct") |
| 1080 |
fmt.Println(ok) |
| 1081 |
|
| 1082 |
ok = td.CmpIsa(t, got, &TstStruct{}, |
| 1083 |
"checks got is a pointer on a TstStruct") |
| 1084 |
fmt.Println(ok) |
| 1085 |
|
| 1086 |
ok = td.CmpIsa(t, &got, &TstStruct{}, |
| 1087 |
"checks &got is a pointer on a TstStruct") |
| 1088 |
fmt.Println(ok) |
| 1089 |
|
| 1090 |
// Output: |
| 1091 |
// true |
| 1092 |
// false |
| 1093 |
// true |
| 1094 |
} |
| 1095 |
|
| 1096 |
func ExampleCmpIsa_interface() { |
| 1097 |
t := &testing.T{} |
| 1098 |
|
| 1099 |
got := bytes.NewBufferString("foobar") |
| 1100 |
|
| 1101 |
ok := td.CmpIsa(t, got, (*fmt.Stringer)(nil), |
| 1102 |
"checks got implements fmt.Stringer interface") |
| 1103 |
fmt.Println(ok) |
| 1104 |
|
| 1105 |
errGot := fmt.Errorf("An error #%d occurred", 123) |
| 1106 |
|
| 1107 |
ok = td.CmpIsa(t, errGot, (*error)(nil), |
| 1108 |
"checks errGot is a *error or implements error interface") |
| 1109 |
fmt.Println(ok) |
| 1110 |
|
| 1111 |
// As nil, is passed below, it is not an interface but nil… So it |
| 1112 |
// does not match |
| 1113 |
errGot = nil |
| 1114 |
|
| 1115 |
ok = td.CmpIsa(t, errGot, (*error)(nil), |
| 1116 |
"checks errGot is a *error or implements error interface") |
| 1117 |
fmt.Println(ok) |
| 1118 |
|
| 1119 |
// BUT if its address is passed, now it is OK as the types match |
| 1120 |
ok = td.CmpIsa(t, &errGot, (*error)(nil), |
| 1121 |
"checks &errGot is a *error or implements error interface") |
| 1122 |
fmt.Println(ok) |
| 1123 |
|
| 1124 |
// Output: |
| 1125 |
// true |
| 1126 |
// true |
| 1127 |
// false |
| 1128 |
// true |
| 1129 |
} |
| 1130 |
|
| 1131 |
func ExampleCmpJSON_basic() { |
| 1132 |
t := &testing.T{} |
| 1133 |
|
| 1134 |
got := &struct { |
| 1135 |
Fullname string `json:"fullname"` |
| 1136 |
Age int `json:"age"` |
| 1137 |
}{ |
| 1138 |
Fullname: "Bob", |
| 1139 |
Age: 42, |
| 1140 |
} |
| 1141 |
|
| 1142 |
ok := td.CmpJSON(t, got, `{"age":42,"fullname":"Bob"}`, nil) |
| 1143 |
fmt.Println("check got with age then fullname:", ok) |
| 1144 |
|
| 1145 |
ok = td.CmpJSON(t, got, `{"fullname":"Bob","age":42}`, nil) |
| 1146 |
fmt.Println("check got with fullname then age:", ok) |
| 1147 |
|
| 1148 |
ok = td.CmpJSON(t, got, ` |
| 1149 |
// This should be the JSON representation of a struct |
| 1150 |
{ |
| 1151 |
// A person: |
| 1152 |
"fullname": "Bob", // The name of this person |
| 1153 |
"age": 42 /* The age of this person: |
| 1154 |
- 42 of course |
| 1155 |
- to demonstrate a multi-lines comment */ |
| 1156 |
}`, nil) |
| 1157 |
fmt.Println("check got with nicely formatted and commented JSON:", ok) |
| 1158 |
|
| 1159 |
ok = td.CmpJSON(t, got, `{"fullname":"Bob","age":42,"gender":"male"}`, nil) |
| 1160 |
fmt.Println("check got with gender field:", ok) |
| 1161 |
|
| 1162 |
ok = td.CmpJSON(t, got, `{"fullname":"Bob"}`, nil) |
| 1163 |
fmt.Println("check got with fullname only:", ok) |
| 1164 |
|
| 1165 |
ok = td.CmpJSON(t, true, `true`, nil) |
| 1166 |
fmt.Println("check boolean got is true:", ok) |
| 1167 |
|
| 1168 |
ok = td.CmpJSON(t, 42, `42`, nil) |
| 1169 |
fmt.Println("check numeric got is 42:", ok) |
| 1170 |
|
| 1171 |
got = nil |
| 1172 |
ok = td.CmpJSON(t, got, `null`, nil) |
| 1173 |
fmt.Println("check nil got is null:", ok) |
| 1174 |
|
| 1175 |
// Output: |
| 1176 |
// check got with age then fullname: true |
| 1177 |
// check got with fullname then age: true |
| 1178 |
// check got with nicely formatted and commented JSON: true |
| 1179 |
// check got with gender field: false |
| 1180 |
// check got with fullname only: false |
| 1181 |
// check boolean got is true: true |
| 1182 |
// check numeric got is 42: true |
| 1183 |
// check nil got is null: true |
| 1184 |
} |
| 1185 |
|
| 1186 |
func ExampleCmpJSON_placeholders() { |
| 1187 |
t := &testing.T{} |
| 1188 |
|
| 1189 |
type Person struct { |
| 1190 |
Fullname string `json:"fullname"` |
| 1191 |
Age int `json:"age"` |
| 1192 |
Children []*Person `json:"children,omitempty"` |
| 1193 |
} |
| 1194 |
|
| 1195 |
got := &Person{ |
| 1196 |
Fullname: "Bob Foobar", |
| 1197 |
Age: 42, |
| 1198 |
} |
| 1199 |
|
| 1200 |
ok := td.CmpJSON(t, got, `{"age": $1, "fullname": $2}`, []any{42, "Bob Foobar"}) |
| 1201 |
fmt.Println("check got with numeric placeholders without operators:", ok) |
| 1202 |
|
| 1203 |
ok = td.CmpJSON(t, got, `{"age": $1, "fullname": $2}`, []any{td.Between(40, 45), td.HasSuffix("Foobar")}) |
| 1204 |
fmt.Println("check got with numeric placeholders:", ok) |
| 1205 |
|
| 1206 |
ok = td.CmpJSON(t, got, `{"age": "$1", "fullname": "$2"}`, []any{td.Between(40, 45), td.HasSuffix("Foobar")}) |
| 1207 |
fmt.Println("check got with double-quoted numeric placeholders:", ok) |
| 1208 |
|
| 1209 |
ok = td.CmpJSON(t, got, `{"age": $age, "fullname": $name}`, []any{td.Tag("age", td.Between(40, 45)), td.Tag("name", td.HasSuffix("Foobar"))}) |
| 1210 |
fmt.Println("check got with named placeholders:", ok) |
| 1211 |
|
| 1212 |
got.Children = []*Person{ |
| 1213 |
{Fullname: "Alice", Age: 28}, |
| 1214 |
{Fullname: "Brian", Age: 22}, |
| 1215 |
} |
| 1216 |
ok = td.CmpJSON(t, got, `{"age": $age, "fullname": $name, "children": $children}`, []any{td.Tag("age", td.Between(40, 45)), td.Tag("name", td.HasSuffix("Foobar")), td.Tag("children", td.Bag( |
| 1217 |
&Person{Fullname: "Brian", Age: 22}, |
| 1218 |
&Person{Fullname: "Alice", Age: 28}, |
| 1219 |
))}) |
| 1220 |
fmt.Println("check got w/named placeholders, and children w/go structs:", ok) |
| 1221 |
|
| 1222 |
ok = td.CmpJSON(t, got, `{"age": Between($1, $2), "fullname": HasSuffix($suffix), "children": Len(2)}`, []any{40, 45, td.Tag("suffix", "Foobar")}) |
| 1223 |
fmt.Println("check got w/num & named placeholders:", ok) |
| 1224 |
|
| 1225 |
// Output: |
| 1226 |
// check got with numeric placeholders without operators: true |
| 1227 |
// check got with numeric placeholders: true |
| 1228 |
// check got with double-quoted numeric placeholders: true |
| 1229 |
// check got with named placeholders: true |
| 1230 |
// check got w/named placeholders, and children w/go structs: true |
| 1231 |
// check got w/num & named placeholders: true |
| 1232 |
} |
| 1233 |
|
| 1234 |
func ExampleCmpJSON_embedding() { |
| 1235 |
t := &testing.T{} |
| 1236 |
|
| 1237 |
got := &struct { |
| 1238 |
Fullname string `json:"fullname"` |
| 1239 |
Age int `json:"age"` |
| 1240 |
}{ |
| 1241 |
Fullname: "Bob Foobar", |
| 1242 |
Age: 42, |
| 1243 |
} |
| 1244 |
|
| 1245 |
ok := td.CmpJSON(t, got, `{"age": NotZero(), "fullname": NotEmpty()}`, nil) |
| 1246 |
fmt.Println("check got with simple operators:", ok) |
| 1247 |
|
| 1248 |
ok = td.CmpJSON(t, got, `{"age": $^NotZero, "fullname": $^NotEmpty}`, nil) |
| 1249 |
fmt.Println("check got with operator shortcuts:", ok) |
| 1250 |
|
| 1251 |
ok = td.CmpJSON(t, got, ` |
| 1252 |
{ |
| 1253 |
"age": Between(40, 42, "]]"), // in ]40; 42] |
| 1254 |
"fullname": All( |
| 1255 |
HasPrefix("Bob"), |
| 1256 |
HasSuffix("bar") // ← comma is optional here |
| 1257 |
) |
| 1258 |
}`, nil) |
| 1259 |
fmt.Println("check got with complex operators:", ok) |
| 1260 |
|
| 1261 |
ok = td.CmpJSON(t, got, ` |
| 1262 |
{ |
| 1263 |
"age": Between(40, 42, "]["), // in ]40; 42[ → 42 excluded |
| 1264 |
"fullname": All( |
| 1265 |
HasPrefix("Bob"), |
| 1266 |
HasSuffix("bar"), |
| 1267 |
) |
| 1268 |
}`, nil) |
| 1269 |
fmt.Println("check got with complex operators:", ok) |
| 1270 |
|
| 1271 |
ok = td.CmpJSON(t, got, ` |
| 1272 |
{ |
| 1273 |
"age": Between($1, $2, $3), // in ]40; 42] |
| 1274 |
"fullname": All( |
| 1275 |
HasPrefix($4), |
| 1276 |
HasSuffix("bar") // ← comma is optional here |
| 1277 |
) |
| 1278 |
}`, []any{40, 42, td.BoundsOutIn, "Bob"}) |
| 1279 |
fmt.Println("check got with complex operators, w/placeholder args:", ok) |
| 1280 |
|
| 1281 |
// Output: |
| 1282 |
// check got with simple operators: true |
| 1283 |
// check got with operator shortcuts: true |
| 1284 |
// check got with complex operators: true |
| 1285 |
// check got with complex operators: false |
| 1286 |
// check got with complex operators, w/placeholder args: true |
| 1287 |
} |
| 1288 |
|
| 1289 |
func ExampleCmpJSON_rawStrings() { |
| 1290 |
t := &testing.T{} |
| 1291 |
|
| 1292 |
type details struct { |
| 1293 |
Address string `json:"address"` |
| 1294 |
Car string `json:"car"` |
| 1295 |
} |
| 1296 |
|
| 1297 |
got := &struct { |
| 1298 |
Fullname string `json:"fullname"` |
| 1299 |
Age int `json:"age"` |
| 1300 |
Details details `json:"details"` |
| 1301 |
}{ |
| 1302 |
Fullname: "Foo Bar", |
| 1303 |
Age: 42, |
| 1304 |
Details: details{ |
| 1305 |
Address: "something", |
| 1306 |
Car: "Peugeot", |
| 1307 |
}, |
| 1308 |
} |
| 1309 |
|
| 1310 |
ok := td.CmpJSON(t, got, ` |
| 1311 |
{ |
| 1312 |
"fullname": HasPrefix("Foo"), |
| 1313 |
"age": Between(41, 43), |
| 1314 |
"details": SuperMapOf({ |
| 1315 |
"address": NotEmpty, // () are optional when no parameters |
| 1316 |
"car": Any("Peugeot", "Tesla", "Jeep") // any of these |
| 1317 |
}) |
| 1318 |
}`, nil) |
| 1319 |
fmt.Println("Original:", ok) |
| 1320 |
|
| 1321 |
ok = td.CmpJSON(t, got, ` |
| 1322 |
{ |
| 1323 |
"fullname": "$^HasPrefix(\"Foo\")", |
| 1324 |
"age": "$^Between(41, 43)", |
| 1325 |
"details": "$^SuperMapOf({\n\"address\": NotEmpty,\n\"car\": Any(\"Peugeot\", \"Tesla\", \"Jeep\")\n})" |
| 1326 |
}`, nil) |
| 1327 |
fmt.Println("JSON compliant:", ok) |
| 1328 |
|
| 1329 |
ok = td.CmpJSON(t, got, ` |
| 1330 |
{ |
| 1331 |
"fullname": "$^HasPrefix(\"Foo\")", |
| 1332 |
"age": "$^Between(41, 43)", |
| 1333 |
"details": "$^SuperMapOf({ |
| 1334 |
\"address\": NotEmpty, // () are optional when no parameters |
| 1335 |
\"car\": Any(\"Peugeot\", \"Tesla\", \"Jeep\") // any of these |
| 1336 |
})" |
| 1337 |
}`, nil) |
| 1338 |
fmt.Println("JSON multilines strings:", ok) |
| 1339 |
|
| 1340 |
ok = td.CmpJSON(t, got, ` |
| 1341 |
{ |
| 1342 |
"fullname": "$^HasPrefix(r<Foo>)", |
| 1343 |
"age": "$^Between(41, 43)", |
| 1344 |
"details": "$^SuperMapOf({ |
| 1345 |
r<address>: NotEmpty, // () are optional when no parameters |
| 1346 |
r<car>: Any(r<Peugeot>, r<Tesla>, r<Jeep>) // any of these |
| 1347 |
})" |
| 1348 |
}`, nil) |
| 1349 |
fmt.Println("Raw strings:", ok) |
| 1350 |
|
| 1351 |
// Output: |
| 1352 |
// Original: true |
| 1353 |
// JSON compliant: true |
| 1354 |
// JSON multilines strings: true |
| 1355 |
// Raw strings: true |
| 1356 |
} |
| 1357 |
|
| 1358 |
func ExampleCmpJSON_file() { |
| 1359 |
t := &testing.T{} |
| 1360 |
|
| 1361 |
got := &struct { |
| 1362 |
Fullname string `json:"fullname"` |
| 1363 |
Age int `json:"age"` |
| 1364 |
Gender string `json:"gender"` |
| 1365 |
}{ |
| 1366 |
Fullname: "Bob Foobar", |
| 1367 |
Age: 42, |
| 1368 |
Gender: "male", |
| 1369 |
} |
| 1370 |
|
| 1371 |
tmpDir, err := os.MkdirTemp("", "") |
| 1372 |
if err != nil { |
| 1373 |
t.Fatal(err) |
| 1374 |
} |
| 1375 |
defer os.RemoveAll(tmpDir) //nolint: errcheck // clean up |
| 1376 |
|
| 1377 |
filename := tmpDir + "/test.json" |
| 1378 |
if err = os.WriteFile(filename, []byte(` |
| 1379 |
{ |
| 1380 |
"fullname": "$name", |
| 1381 |
"age": "$age", |
| 1382 |
"gender": "$gender" |
| 1383 |
}`), 0644); err != nil { |
| 1384 |
t.Fatal(err) |
| 1385 |
} |
| 1386 |
|
| 1387 |
// OK let's test with this file |
| 1388 |
ok := td.CmpJSON(t, got, filename, []any{td.Tag("name", td.HasPrefix("Bob")), td.Tag("age", td.Between(40, 45)), td.Tag("gender", td.Re(`^(male|female)\z`))}) |
| 1389 |
fmt.Println("Full match from file name:", ok) |
| 1390 |
|
| 1391 |
// When the file is already open |
| 1392 |
file, err := os.Open(filename) |
| 1393 |
if err != nil { |
| 1394 |
t.Fatal(err) |
| 1395 |
} |
| 1396 |
ok = td.CmpJSON(t, got, file, []any{td.Tag("name", td.HasPrefix("Bob")), td.Tag("age", td.Between(40, 45)), td.Tag("gender", td.Re(`^(male|female)\z`))}) |
| 1397 |
fmt.Println("Full match from io.Reader:", ok) |
| 1398 |
|
| 1399 |
// Output: |
| 1400 |
// Full match from file name: true |
| 1401 |
// Full match from io.Reader: true |
| 1402 |
} |
| 1403 |
|
| 1404 |
func ExampleCmpJSONPointer_rfc6901() { |
| 1405 |
t := &testing.T{} |
| 1406 |
|
| 1407 |
got := json.RawMessage(` |
| 1408 |
{ |
| 1409 |
"foo": ["bar", "baz"], |
| 1410 |
"": 0, |
| 1411 |
"a/b": 1, |
| 1412 |
"c%d": 2, |
| 1413 |
"e^f": 3, |
| 1414 |
"g|h": 4, |
| 1415 |
"i\\j": 5, |
| 1416 |
"k\"l": 6, |
| 1417 |
" ": 7, |
| 1418 |
"m~n": 8 |
| 1419 |
}`) |
| 1420 |
|
| 1421 |
expected := map[string]any{ |
| 1422 |
"foo": []any{"bar", "baz"}, |
| 1423 |
"": 0, |
| 1424 |
"a/b": 1, |
| 1425 |
"c%d": 2, |
| 1426 |
"e^f": 3, |
| 1427 |
"g|h": 4, |
| 1428 |
`i\j`: 5, |
| 1429 |
`k"l`: 6, |
| 1430 |
" ": 7, |
| 1431 |
"m~n": 8, |
| 1432 |
} |
| 1433 |
ok := td.CmpJSONPointer(t, got, "", expected) |
| 1434 |
fmt.Println("Empty JSON pointer means all:", ok) |
| 1435 |
|
| 1436 |
ok = td.CmpJSONPointer(t, got, `/foo`, []any{"bar", "baz"}) |
| 1437 |
fmt.Println("Extract `foo` key:", ok) |
| 1438 |
|
| 1439 |
ok = td.CmpJSONPointer(t, got, `/foo/0`, "bar") |
| 1440 |
fmt.Println("First item of `foo` key slice:", ok) |
| 1441 |
|
| 1442 |
ok = td.CmpJSONPointer(t, got, `/`, 0) |
| 1443 |
fmt.Println("Empty key:", ok) |
| 1444 |
|
| 1445 |
ok = td.CmpJSONPointer(t, got, `/a~1b`, 1) |
| 1446 |
fmt.Println("Slash has to be escaped using `~1`:", ok) |
| 1447 |
|
| 1448 |
ok = td.CmpJSONPointer(t, got, `/c%d`, 2) |
| 1449 |
fmt.Println("% in key:", ok) |
| 1450 |
|
| 1451 |
ok = td.CmpJSONPointer(t, got, `/e^f`, 3) |
| 1452 |
fmt.Println("^ in key:", ok) |
| 1453 |
|
| 1454 |
ok = td.CmpJSONPointer(t, got, `/g|h`, 4) |
| 1455 |
fmt.Println("| in key:", ok) |
| 1456 |
|
| 1457 |
ok = td.CmpJSONPointer(t, got, `/i\j`, 5) |
| 1458 |
fmt.Println("Backslash in key:", ok) |
| 1459 |
|
| 1460 |
ok = td.CmpJSONPointer(t, got, `/k"l`, 6) |
| 1461 |
fmt.Println("Double-quote in key:", ok) |
| 1462 |
|
| 1463 |
ok = td.CmpJSONPointer(t, got, `/ `, 7) |
| 1464 |
fmt.Println("Space key:", ok) |
| 1465 |
|
| 1466 |
ok = td.CmpJSONPointer(t, got, `/m~0n`, 8) |
| 1467 |
fmt.Println("Tilde has to be escaped using `~0`:", ok) |
| 1468 |
|
| 1469 |
// Output: |
| 1470 |
// Empty JSON pointer means all: true |
| 1471 |
// Extract `foo` key: true |
| 1472 |
// First item of `foo` key slice: true |
| 1473 |
// Empty key: true |
| 1474 |
// Slash has to be escaped using `~1`: true |
| 1475 |
// % in key: true |
| 1476 |
// ^ in key: true |
| 1477 |
// | in key: true |
| 1478 |
// Backslash in key: true |
| 1479 |
// Double-quote in key: true |
| 1480 |
// Space key: true |
| 1481 |
// Tilde has to be escaped using `~0`: true |
| 1482 |
} |
| 1483 |
|
| 1484 |
func ExampleCmpJSONPointer_struct() { |
| 1485 |
t := &testing.T{} |
| 1486 |
|
| 1487 |
// Without json tags, encoding/json uses public fields name |
| 1488 |
type Item struct { |
| 1489 |
Name string |
| 1490 |
Value int64 |
| 1491 |
Next *Item |
| 1492 |
} |
| 1493 |
|
| 1494 |
got := Item{ |
| 1495 |
Name: "first", |
| 1496 |
Value: 1, |
| 1497 |
Next: &Item{ |
| 1498 |
Name: "second", |
| 1499 |
Value: 2, |
| 1500 |
Next: &Item{ |
| 1501 |
Name: "third", |
| 1502 |
Value: 3, |
| 1503 |
}, |
| 1504 |
}, |
| 1505 |
} |
| 1506 |
|
| 1507 |
ok := td.CmpJSONPointer(t, got, "/Next/Next/Name", "third") |
| 1508 |
fmt.Println("3rd item name is `third`:", ok) |
| 1509 |
|
| 1510 |
ok = td.CmpJSONPointer(t, got, "/Next/Next/Value", td.Gte(int64(3))) |
| 1511 |
fmt.Println("3rd item value is greater or equal than 3:", ok) |
| 1512 |
|
| 1513 |
ok = td.CmpJSONPointer(t, got, "/Next", td.JSONPointer("/Next", |
| 1514 |
td.JSONPointer("/Value", td.Gte(int64(3))))) |
| 1515 |
fmt.Println("3rd item value is still greater or equal than 3:", ok) |
| 1516 |
|
| 1517 |
ok = td.CmpJSONPointer(t, got, "/Next/Next/Next/Name", td.Ignore()) |
| 1518 |
fmt.Println("4th item exists and has a name:", ok) |
| 1519 |
|
| 1520 |
// Struct comparison work with or without pointer: &Item{…} works too |
| 1521 |
ok = td.CmpJSONPointer(t, got, "/Next/Next", Item{ |
| 1522 |
Name: "third", |
| 1523 |
Value: 3, |
| 1524 |
}) |
| 1525 |
fmt.Println("3rd item full comparison:", ok) |
| 1526 |
|
| 1527 |
// Output: |
| 1528 |
// 3rd item name is `third`: true |
| 1529 |
// 3rd item value is greater or equal than 3: true |
| 1530 |
// 3rd item value is still greater or equal than 3: true |
| 1531 |
// 4th item exists and has a name: false |
| 1532 |
// 3rd item full comparison: true |
| 1533 |
} |
| 1534 |
|
| 1535 |
func ExampleCmpJSONPointer_has_hasnt() { |
| 1536 |
t := &testing.T{} |
| 1537 |
|
| 1538 |
got := json.RawMessage(` |
| 1539 |
{ |
| 1540 |
"name": "Bob", |
| 1541 |
"age": 42, |
| 1542 |
"children": [ |
| 1543 |
{ |
| 1544 |
"name": "Alice", |
| 1545 |
"age": 16 |
| 1546 |
}, |
| 1547 |
{ |
| 1548 |
"name": "Britt", |
| 1549 |
"age": 21, |
| 1550 |
"children": [ |
| 1551 |
{ |
| 1552 |
"name": "John", |
| 1553 |
"age": 1 |
| 1554 |
} |
| 1555 |
] |
| 1556 |
} |
| 1557 |
] |
| 1558 |
}`) |
| 1559 |
|
| 1560 |
// Has Bob some children? |
| 1561 |
ok := td.CmpJSONPointer(t, got, "/children", td.Len(td.Gt(0))) |
| 1562 |
fmt.Println("Bob has at least one child:", ok) |
| 1563 |
|
| 1564 |
// But checking "children" exists is enough here |
| 1565 |
ok = td.CmpJSONPointer(t, got, "/children/0/children", td.Ignore()) |
| 1566 |
fmt.Println("Alice has children:", ok) |
| 1567 |
|
| 1568 |
ok = td.CmpJSONPointer(t, got, "/children/1/children", td.Ignore()) |
| 1569 |
fmt.Println("Britt has children:", ok) |
| 1570 |
|
| 1571 |
// The reverse can be checked too |
| 1572 |
ok = td.Cmp(t, got, td.Not(td.JSONPointer("/children/0/children", td.Ignore()))) |
| 1573 |
fmt.Println("Alice hasn't children:", ok) |
| 1574 |
|
| 1575 |
ok = td.Cmp(t, got, td.Not(td.JSONPointer("/children/1/children", td.Ignore()))) |
| 1576 |
fmt.Println("Britt hasn't children:", ok) |
| 1577 |
|
| 1578 |
// Output: |
| 1579 |
// Bob has at least one child: true |
| 1580 |
// Alice has children: false |
| 1581 |
// Britt has children: true |
| 1582 |
// Alice hasn't children: true |
| 1583 |
// Britt hasn't children: false |
| 1584 |
} |
| 1585 |
|
| 1586 |
func ExampleCmpKeys() { |
| 1587 |
t := &testing.T{} |
| 1588 |
|
| 1589 |
got := map[string]int{"foo": 1, "bar": 2, "zip": 3} |
| 1590 |
|
| 1591 |
// Keys tests keys in an ordered manner |
| 1592 |
ok := td.CmpKeys(t, got, []string{"bar", "foo", "zip"}) |
| 1593 |
fmt.Println("All sorted keys are found:", ok) |
| 1594 |
|
| 1595 |
// If the expected keys are not ordered, it fails |
| 1596 |
ok = td.CmpKeys(t, got, []string{"zip", "bar", "foo"}) |
| 1597 |
fmt.Println("All unsorted keys are found:", ok) |
| 1598 |
|
| 1599 |
// To circumvent that, one can use Bag operator |
| 1600 |
ok = td.CmpKeys(t, got, td.Bag("zip", "bar", "foo")) |
| 1601 |
fmt.Println("All unsorted keys are found, with the help of Bag operator:", ok) |
| 1602 |
|
| 1603 |
// Check that each key is 3 bytes long |
| 1604 |
ok = td.CmpKeys(t, got, td.ArrayEach(td.Len(3))) |
| 1605 |
fmt.Println("Each key is 3 bytes long:", ok) |
| 1606 |
|
| 1607 |
// Output: |
| 1608 |
// All sorted keys are found: true |
| 1609 |
// All unsorted keys are found: false |
| 1610 |
// All unsorted keys are found, with the help of Bag operator: true |
| 1611 |
// Each key is 3 bytes long: true |
| 1612 |
} |
| 1613 |
|
| 1614 |
func ExampleCmpLast_classic() { |
| 1615 |
t := &testing.T{} |
| 1616 |
|
| 1617 |
got := []int{-3, -2, -1, 0, 1, 2, 3} |
| 1618 |
|
| 1619 |
ok := td.CmpLast(t, got, td.Lt(0), -1) |
| 1620 |
fmt.Println("last negative number is -1:", ok) |
| 1621 |
|
| 1622 |
isEven := func(x int) bool { return x%2 == 0 } |
| 1623 |
|
| 1624 |
ok = td.CmpLast(t, got, isEven, 2) |
| 1625 |
fmt.Println("last even number is 2:", ok) |
| 1626 |
|
| 1627 |
ok = td.CmpLast(t, got, isEven, td.Gt(0)) |
| 1628 |
fmt.Println("last even number is > 0:", ok) |
| 1629 |
|
| 1630 |
ok = td.CmpLast(t, got, isEven, td.Code(isEven)) |
| 1631 |
fmt.Println("last even number is well even:", ok) |
| 1632 |
|
| 1633 |
// Output: |
| 1634 |
// last negative number is -1: true |
| 1635 |
// last even number is 2: true |
| 1636 |
// last even number is > 0: true |
| 1637 |
// last even number is well even: true |
| 1638 |
} |
| 1639 |
|
| 1640 |
func ExampleCmpLast_empty() { |
| 1641 |
t := &testing.T{} |
| 1642 |
|
| 1643 |
ok := td.CmpLast(t, ([]int)(nil), td.Gt(0), td.Gt(0)) |
| 1644 |
fmt.Println("last in nil slice:", ok) |
| 1645 |
|
| 1646 |
ok = td.CmpLast(t, []int{}, td.Gt(0), td.Gt(0)) |
| 1647 |
fmt.Println("last in empty slice:", ok) |
| 1648 |
|
| 1649 |
ok = td.CmpLast(t, &[]int{}, td.Gt(0), td.Gt(0)) |
| 1650 |
fmt.Println("last in empty pointed slice:", ok) |
| 1651 |
|
| 1652 |
ok = td.CmpLast(t, [0]int{}, td.Gt(0), td.Gt(0)) |
| 1653 |
fmt.Println("last in empty array:", ok) |
| 1654 |
|
| 1655 |
// Output: |
| 1656 |
// last in nil slice: false |
| 1657 |
// last in empty slice: false |
| 1658 |
// last in empty pointed slice: false |
| 1659 |
// last in empty array: false |
| 1660 |
} |
| 1661 |
|
| 1662 |
func ExampleCmpLast_struct() { |
| 1663 |
t := &testing.T{} |
| 1664 |
|
| 1665 |
type Person struct { |
| 1666 |
Fullname string `json:"fullname"` |
| 1667 |
Age int `json:"age"` |
| 1668 |
} |
| 1669 |
|
| 1670 |
got := []*Person{ |
| 1671 |
{ |
| 1672 |
Fullname: "Bob Foobar", |
| 1673 |
Age: 42, |
| 1674 |
}, |
| 1675 |
{ |
| 1676 |
Fullname: "Alice Bingo", |
| 1677 |
Age: 37, |
| 1678 |
}, |
| 1679 |
} |
| 1680 |
|
| 1681 |
ok := td.CmpLast(t, got, td.Smuggle("Age", td.Gt(30)), td.Smuggle("Fullname", "Alice Bingo")) |
| 1682 |
fmt.Println("last person.Age > 30 → Alice:", ok) |
| 1683 |
|
| 1684 |
ok = td.CmpLast(t, got, td.JSONPointer("/age", td.Gt(30)), td.SuperJSONOf(`{"fullname":"Alice Bingo"}`)) |
| 1685 |
fmt.Println("last person.Age > 30 → Alice, using JSON:", ok) |
| 1686 |
|
| 1687 |
ok = td.CmpLast(t, got, td.JSONPointer("/age", td.Gt(30)), td.JSONPointer("/fullname", td.HasPrefix("Alice"))) |
| 1688 |
fmt.Println("first person.Age > 30 → Alice, using JSONPointer:", ok) |
| 1689 |
|
| 1690 |
// Output: |
| 1691 |
// last person.Age > 30 → Alice: true |
| 1692 |
// last person.Age > 30 → Alice, using JSON: true |
| 1693 |
// first person.Age > 30 → Alice, using JSONPointer: true |
| 1694 |
} |
| 1695 |
|
| 1696 |
func ExampleCmpLax() { |
| 1697 |
t := &testing.T{} |
| 1698 |
|
| 1699 |
gotInt64 := int64(1234) |
| 1700 |
gotInt32 := int32(1235) |
| 1701 |
|
| 1702 |
type myInt uint16 |
| 1703 |
gotMyInt := myInt(1236) |
| 1704 |
|
| 1705 |
expected := td.Between(1230, 1240) // int type here |
| 1706 |
|
| 1707 |
ok := td.CmpLax(t, gotInt64, expected) |
| 1708 |
fmt.Println("int64 got between ints [1230 .. 1240]:", ok) |
| 1709 |
|
| 1710 |
ok = td.CmpLax(t, gotInt32, expected) |
| 1711 |
fmt.Println("int32 got between ints [1230 .. 1240]:", ok) |
| 1712 |
|
| 1713 |
ok = td.CmpLax(t, gotMyInt, expected) |
| 1714 |
fmt.Println("myInt got between ints [1230 .. 1240]:", ok) |
| 1715 |
|
| 1716 |
// Output: |
| 1717 |
// int64 got between ints [1230 .. 1240]: true |
| 1718 |
// int32 got between ints [1230 .. 1240]: true |
| 1719 |
// myInt got between ints [1230 .. 1240]: true |
| 1720 |
} |
| 1721 |
|
| 1722 |
func ExampleCmpLen_slice() { |
| 1723 |
t := &testing.T{} |
| 1724 |
|
| 1725 |
got := []int{11, 22, 33} |
| 1726 |
|
| 1727 |
ok := td.CmpLen(t, got, 3, "checks %v len is 3", got) |
| 1728 |
fmt.Println(ok) |
| 1729 |
|
| 1730 |
ok = td.CmpLen(t, got, 0, "checks %v len is 0", got) |
| 1731 |
fmt.Println(ok) |
| 1732 |
|
| 1733 |
got = nil |
| 1734 |
|
| 1735 |
ok = td.CmpLen(t, got, 0, "checks %v len is 0", got) |
| 1736 |
fmt.Println(ok) |
| 1737 |
|
| 1738 |
// Output: |
| 1739 |
// true |
| 1740 |
// false |
| 1741 |
// true |
| 1742 |
} |
| 1743 |
|
| 1744 |
func ExampleCmpLen_map() { |
| 1745 |
t := &testing.T{} |
| 1746 |
|
| 1747 |
got := map[int]bool{11: true, 22: false, 33: false} |
| 1748 |
|
| 1749 |
ok := td.CmpLen(t, got, 3, "checks %v len is 3", got) |
| 1750 |
fmt.Println(ok) |
| 1751 |
|
| 1752 |
ok = td.CmpLen(t, got, 0, "checks %v len is 0", got) |
| 1753 |
fmt.Println(ok) |
| 1754 |
|
| 1755 |
got = nil |
| 1756 |
|
| 1757 |
ok = td.CmpLen(t, got, 0, "checks %v len is 0", got) |
| 1758 |
fmt.Println(ok) |
| 1759 |
|
| 1760 |
// Output: |
| 1761 |
// true |
| 1762 |
// false |
| 1763 |
// true |
| 1764 |
} |
| 1765 |
|
| 1766 |
func ExampleCmpLen_operatorSlice() { |
| 1767 |
t := &testing.T{} |
| 1768 |
|
| 1769 |
got := []int{11, 22, 33} |
| 1770 |
|
| 1771 |
ok := td.CmpLen(t, got, td.Between(3, 8), |
| 1772 |
"checks %v len is in [3 .. 8]", got) |
| 1773 |
fmt.Println(ok) |
| 1774 |
|
| 1775 |
ok = td.CmpLen(t, got, td.Lt(5), "checks %v len is < 5", got) |
| 1776 |
fmt.Println(ok) |
| 1777 |
|
| 1778 |
// Output: |
| 1779 |
// true |
| 1780 |
// true |
| 1781 |
} |
| 1782 |
|
| 1783 |
func ExampleCmpLen_operatorMap() { |
| 1784 |
t := &testing.T{} |
| 1785 |
|
| 1786 |
got := map[int]bool{11: true, 22: false, 33: false} |
| 1787 |
|
| 1788 |
ok := td.CmpLen(t, got, td.Between(3, 8), |
| 1789 |
"checks %v len is in [3 .. 8]", got) |
| 1790 |
fmt.Println(ok) |
| 1791 |
|
| 1792 |
ok = td.CmpLen(t, got, td.Gte(3), "checks %v len is ≥ 3", got) |
| 1793 |
fmt.Println(ok) |
| 1794 |
|
| 1795 |
// Output: |
| 1796 |
// true |
| 1797 |
// true |
| 1798 |
} |
| 1799 |
|
| 1800 |
func ExampleCmpList() { |
| 1801 |
t := &testing.T{} |
| 1802 |
|
| 1803 |
got := []int{1, 33, 8, 2} |
| 1804 |
|
| 1805 |
// Matches as all items are present |
| 1806 |
ok := td.CmpList(t, got, []any{1, td.Between(32, 34), td.Gt(7), 2}) |
| 1807 |
fmt.Println("checks all items match, in this order:", ok) |
| 1808 |
|
| 1809 |
// Does not match as got does not use the same order as expected |
| 1810 |
ok = td.CmpList(t, got, []any{1, td.Gt(7), 2, td.Between(32, 34)}) |
| 1811 |
fmt.Println("checks all items match, in wrong order:", ok) |
| 1812 |
|
| 1813 |
// When expected is already a non-[]any slice, it cannot be |
| 1814 |
// flattened directly using expected... without copying it to a new |
| 1815 |
// []any slice, then use td.Flatten! |
| 1816 |
expected := []int{1, 33, 8} |
| 1817 |
ok = td.CmpList(t, got, []any{td.Flatten(expected), td.Lte(2)}) |
| 1818 |
fmt.Println("checks all expected items are present + last one ≤ 2:", ok) |
| 1819 |
|
| 1820 |
// Output: |
| 1821 |
// checks all items match, in this order: true |
| 1822 |
// checks all items match, in wrong order: false |
| 1823 |
// checks all expected items are present + last one ≤ 2: true |
| 1824 |
} |
| 1825 |
|
| 1826 |
func ExampleCmpLt_int() { |
| 1827 |
t := &testing.T{} |
| 1828 |
|
| 1829 |
got := 156 |
| 1830 |
|
| 1831 |
ok := td.CmpLt(t, got, 157, "checks %v is < 157", got) |
| 1832 |
fmt.Println(ok) |
| 1833 |
|
| 1834 |
ok = td.CmpLt(t, got, 156, "checks %v is < 156", got) |
| 1835 |
fmt.Println(ok) |
| 1836 |
|
| 1837 |
// Output: |
| 1838 |
// true |
| 1839 |
// false |
| 1840 |
} |
| 1841 |
|
| 1842 |
func ExampleCmpLt_string() { |
| 1843 |
t := &testing.T{} |
| 1844 |
|
| 1845 |
got := "abc" |
| 1846 |
|
| 1847 |
ok := td.CmpLt(t, got, "abd", `checks "%v" is < "abd"`, got) |
| 1848 |
fmt.Println(ok) |
| 1849 |
|
| 1850 |
ok = td.CmpLt(t, got, "abc", `checks "%v" is < "abc"`, got) |
| 1851 |
fmt.Println(ok) |
| 1852 |
|
| 1853 |
// Output: |
| 1854 |
// true |
| 1855 |
// false |
| 1856 |
} |
| 1857 |
|
| 1858 |
func ExampleCmpLte_int() { |
| 1859 |
t := &testing.T{} |
| 1860 |
|
| 1861 |
got := 156 |
| 1862 |
|
| 1863 |
ok := td.CmpLte(t, got, 156, "checks %v is ≤ 156", got) |
| 1864 |
fmt.Println(ok) |
| 1865 |
|
| 1866 |
ok = td.CmpLte(t, got, 157, "checks %v is ≤ 157", got) |
| 1867 |
fmt.Println(ok) |
| 1868 |
|
| 1869 |
ok = td.CmpLte(t, got, 155, "checks %v is ≤ 155", got) |
| 1870 |
fmt.Println(ok) |
| 1871 |
|
| 1872 |
// Output: |
| 1873 |
// true |
| 1874 |
// true |
| 1875 |
// false |
| 1876 |
} |
| 1877 |
|
| 1878 |
func ExampleCmpLte_string() { |
| 1879 |
t := &testing.T{} |
| 1880 |
|
| 1881 |
got := "abc" |
| 1882 |
|
| 1883 |
ok := td.CmpLte(t, got, "abc", `checks "%v" is ≤ "abc"`, got) |
| 1884 |
fmt.Println(ok) |
| 1885 |
|
| 1886 |
ok = td.CmpLte(t, got, "abd", `checks "%v" is ≤ "abd"`, got) |
| 1887 |
fmt.Println(ok) |
| 1888 |
|
| 1889 |
ok = td.CmpLte(t, got, "abb", `checks "%v" is ≤ "abb"`, got) |
| 1890 |
fmt.Println(ok) |
| 1891 |
|
| 1892 |
// Output: |
| 1893 |
// true |
| 1894 |
// true |
| 1895 |
// false |
| 1896 |
} |
| 1897 |
|
| 1898 |
func ExampleCmpMap_map() { |
| 1899 |
t := &testing.T{} |
| 1900 |
|
| 1901 |
got := map[string]int{"foo": 12, "bar": 42, "zip": 89} |
| 1902 |
|
| 1903 |
ok := td.CmpMap(t, got, map[string]int{"bar": 42}, td.MapEntries{"foo": td.Lt(15), "zip": td.Ignore()}, |
| 1904 |
"checks map %v", got) |
| 1905 |
fmt.Println(ok) |
| 1906 |
|
| 1907 |
ok = td.CmpMap(t, got, map[string]int{}, td.MapEntries{"bar": 42, "foo": td.Lt(15), "zip": td.Ignore()}, |
| 1908 |
"checks map %v", got) |
| 1909 |
fmt.Println(ok) |
| 1910 |
|
| 1911 |
ok = td.CmpMap(t, got, (map[string]int)(nil), td.MapEntries{"bar": 42, "foo": td.Lt(15), "zip": td.Ignore()}, |
| 1912 |
"checks map %v", got) |
| 1913 |
fmt.Println(ok) |
| 1914 |
|
| 1915 |
// Output: |
| 1916 |
// true |
| 1917 |
// true |
| 1918 |
// true |
| 1919 |
} |
| 1920 |
|
| 1921 |
func ExampleCmpMap_typedMap() { |
| 1922 |
t := &testing.T{} |
| 1923 |
|
| 1924 |
type MyMap map[string]int |
| 1925 |
|
| 1926 |
got := MyMap{"foo": 12, "bar": 42, "zip": 89} |
| 1927 |
|
| 1928 |
ok := td.CmpMap(t, got, MyMap{"bar": 42}, td.MapEntries{"foo": td.Lt(15), "zip": td.Ignore()}, |
| 1929 |
"checks typed map %v", got) |
| 1930 |
fmt.Println(ok) |
| 1931 |
|
| 1932 |
ok = td.CmpMap(t, &got, &MyMap{"bar": 42}, td.MapEntries{"foo": td.Lt(15), "zip": td.Ignore()}, |
| 1933 |
"checks pointer on typed map %v", got) |
| 1934 |
fmt.Println(ok) |
| 1935 |
|
| 1936 |
ok = td.CmpMap(t, &got, &MyMap{}, td.MapEntries{"bar": 42, "foo": td.Lt(15), "zip": td.Ignore()}, |
| 1937 |
"checks pointer on typed map %v", got) |
| 1938 |
fmt.Println(ok) |
| 1939 |
|
| 1940 |
ok = td.CmpMap(t, &got, (*MyMap)(nil), td.MapEntries{"bar": 42, "foo": td.Lt(15), "zip": td.Ignore()}, |
| 1941 |
"checks pointer on typed map %v", got) |
| 1942 |
fmt.Println(ok) |
| 1943 |
|
| 1944 |
// Output: |
| 1945 |
// true |
| 1946 |
// true |
| 1947 |
// true |
| 1948 |
// true |
| 1949 |
} |
| 1950 |
|
| 1951 |
func ExampleCmpMapEach_map() { |
| 1952 |
t := &testing.T{} |
| 1953 |
|
| 1954 |
got := map[string]int{"foo": 12, "bar": 42, "zip": 89} |
| 1955 |
|
| 1956 |
ok := td.CmpMapEach(t, got, td.Between(10, 90), |
| 1957 |
"checks each value of map %v is in [10 .. 90]", got) |
| 1958 |
fmt.Println(ok) |
| 1959 |
|
| 1960 |
// Output: |
| 1961 |
// true |
| 1962 |
} |
| 1963 |
|
| 1964 |
func ExampleCmpMapEach_typedMap() { |
| 1965 |
t := &testing.T{} |
| 1966 |
|
| 1967 |
type MyMap map[string]int |
| 1968 |
|
| 1969 |
got := MyMap{"foo": 12, "bar": 42, "zip": 89} |
| 1970 |
|
| 1971 |
ok := td.CmpMapEach(t, got, td.Between(10, 90), |
| 1972 |
"checks each value of typed map %v is in [10 .. 90]", got) |
| 1973 |
fmt.Println(ok) |
| 1974 |
|
| 1975 |
ok = td.CmpMapEach(t, &got, td.Between(10, 90), |
| 1976 |
"checks each value of typed map pointer %v is in [10 .. 90]", got) |
| 1977 |
fmt.Println(ok) |
| 1978 |
|
| 1979 |
// Output: |
| 1980 |
// true |
| 1981 |
// true |
| 1982 |
} |
| 1983 |
|
| 1984 |
func ExampleCmpN() { |
| 1985 |
t := &testing.T{} |
| 1986 |
|
| 1987 |
got := 1.12345 |
| 1988 |
|
| 1989 |
ok := td.CmpN(t, got, 1.1234, 0.00006, |
| 1990 |
"checks %v = 1.1234 ± 0.00006", got) |
| 1991 |
fmt.Println(ok) |
| 1992 |
|
| 1993 |
// Output: |
| 1994 |
// true |
| 1995 |
} |
| 1996 |
|
| 1997 |
func ExampleCmpNaN_float32() { |
| 1998 |
t := &testing.T{} |
| 1999 |
|
| 2000 |
got := float32(math.NaN()) |
| 2001 |
|
| 2002 |
ok := td.CmpNaN(t, got, |
| 2003 |
"checks %v is not-a-number", got) |
| 2004 |
|
| 2005 |
fmt.Println("float32(math.NaN()) is float32 not-a-number:", ok) |
| 2006 |
|
| 2007 |
got = 12 |
| 2008 |
|
| 2009 |
ok = td.CmpNaN(t, got, |
| 2010 |
"checks %v is not-a-number", got) |
| 2011 |
|
| 2012 |
fmt.Println("float32(12) is float32 not-a-number:", ok) |
| 2013 |
|
| 2014 |
// Output: |
| 2015 |
// float32(math.NaN()) is float32 not-a-number: true |
| 2016 |
// float32(12) is float32 not-a-number: false |
| 2017 |
} |
| 2018 |
|
| 2019 |
func ExampleCmpNaN_float64() { |
| 2020 |
t := &testing.T{} |
| 2021 |
|
| 2022 |
got := math.NaN() |
| 2023 |
|
| 2024 |
ok := td.CmpNaN(t, got, |
| 2025 |
"checks %v is not-a-number", got) |
| 2026 |
|
| 2027 |
fmt.Println("math.NaN() is not-a-number:", ok) |
| 2028 |
|
| 2029 |
got = 12 |
| 2030 |
|
| 2031 |
ok = td.CmpNaN(t, got, |
| 2032 |
"checks %v is not-a-number", got) |
| 2033 |
|
| 2034 |
fmt.Println("float64(12) is not-a-number:", ok) |
| 2035 |
|
| 2036 |
// Output: |
| 2037 |
// math.NaN() is not-a-number: true |
| 2038 |
// float64(12) is not-a-number: false |
| 2039 |
} |
| 2040 |
|
| 2041 |
func ExampleCmpNil() { |
| 2042 |
t := &testing.T{} |
| 2043 |
|
| 2044 |
var got fmt.Stringer // interface |
| 2045 |
|
| 2046 |
// nil value can be compared directly with nil, no need of Nil() here |
| 2047 |
ok := td.Cmp(t, got, nil) |
| 2048 |
fmt.Println(ok) |
| 2049 |
|
| 2050 |
// But it works with Nil() anyway |
| 2051 |
ok = td.CmpNil(t, got) |
| 2052 |
fmt.Println(ok) |
| 2053 |
|
| 2054 |
got = (*bytes.Buffer)(nil) |
| 2055 |
|
| 2056 |
// In the case of an interface containing a nil pointer, comparing |
| 2057 |
// with nil fails, as the interface is not nil |
| 2058 |
ok = td.Cmp(t, got, nil) |
| 2059 |
fmt.Println(ok) |
| 2060 |
|
| 2061 |
// In this case Nil() succeed |
| 2062 |
ok = td.CmpNil(t, got) |
| 2063 |
fmt.Println(ok) |
| 2064 |
|
| 2065 |
// Output: |
| 2066 |
// true |
| 2067 |
// true |
| 2068 |
// false |
| 2069 |
// true |
| 2070 |
} |
| 2071 |
|
| 2072 |
func ExampleCmpNone() { |
| 2073 |
t := &testing.T{} |
| 2074 |
|
| 2075 |
got := 18 |
| 2076 |
|
| 2077 |
ok := td.CmpNone(t, got, []any{0, 10, 20, 30, td.Between(100, 199)}, |
| 2078 |
"checks %v is non-null, and ≠ 10, 20 & 30, and not in [100-199]", got) |
| 2079 |
fmt.Println(ok) |
| 2080 |
|
| 2081 |
got = 20 |
| 2082 |
|
| 2083 |
ok = td.CmpNone(t, got, []any{0, 10, 20, 30, td.Between(100, 199)}, |
| 2084 |
"checks %v is non-null, and ≠ 10, 20 & 30, and not in [100-199]", got) |
| 2085 |
fmt.Println(ok) |
| 2086 |
|
| 2087 |
got = 142 |
| 2088 |
|
| 2089 |
ok = td.CmpNone(t, got, []any{0, 10, 20, 30, td.Between(100, 199)}, |
| 2090 |
"checks %v is non-null, and ≠ 10, 20 & 30, and not in [100-199]", got) |
| 2091 |
fmt.Println(ok) |
| 2092 |
|
| 2093 |
prime := td.Flatten([]int{1, 2, 3, 5, 7, 11, 13}) |
| 2094 |
even := td.Flatten([]int{2, 4, 6, 8, 10, 12, 14}) |
| 2095 |
for _, got := range [...]int{9, 3, 8, 15} { |
| 2096 |
ok = td.CmpNone(t, got, []any{prime, even, td.Gt(14)}, |
| 2097 |
"checks %v is not prime number, nor an even number and not > 14") |
| 2098 |
fmt.Printf("%d → %t\n", got, ok) |
| 2099 |
} |
| 2100 |
|
| 2101 |
// Output: |
| 2102 |
// true |
| 2103 |
// false |
| 2104 |
// false |
| 2105 |
// 9 → true |
| 2106 |
// 3 → false |
| 2107 |
// 8 → false |
| 2108 |
// 15 → false |
| 2109 |
} |
| 2110 |
|
| 2111 |
func ExampleCmpNot() { |
| 2112 |
t := &testing.T{} |
| 2113 |
|
| 2114 |
got := 42 |
| 2115 |
|
| 2116 |
ok := td.CmpNot(t, got, 0, "checks %v is non-null", got) |
| 2117 |
fmt.Println(ok) |
| 2118 |
|
| 2119 |
ok = td.CmpNot(t, got, td.Between(10, 30), |
| 2120 |
"checks %v is not in [10 .. 30]", got) |
| 2121 |
fmt.Println(ok) |
| 2122 |
|
| 2123 |
got = 0 |
| 2124 |
|
| 2125 |
ok = td.CmpNot(t, got, 0, "checks %v is non-null", got) |
| 2126 |
fmt.Println(ok) |
| 2127 |
|
| 2128 |
// Output: |
| 2129 |
// true |
| 2130 |
// true |
| 2131 |
// false |
| 2132 |
} |
| 2133 |
|
| 2134 |
func ExampleCmpNotAny() { |
| 2135 |
t := &testing.T{} |
| 2136 |
|
| 2137 |
got := []int{4, 5, 9, 42} |
| 2138 |
|
| 2139 |
ok := td.CmpNotAny(t, got, []any{3, 6, 8, 41, 43}, |
| 2140 |
"checks %v contains no item listed in NotAny()", got) |
| 2141 |
fmt.Println(ok) |
| 2142 |
|
| 2143 |
ok = td.CmpNotAny(t, got, []any{3, 6, 8, 42, 43}, |
| 2144 |
"checks %v contains no item listed in NotAny()", got) |
| 2145 |
fmt.Println(ok) |
| 2146 |
|
| 2147 |
// When expected is already a non-[]any slice, it cannot be |
| 2148 |
// flattened directly using notExpected... without copying it to a new |
| 2149 |
// []any slice, then use td.Flatten! |
| 2150 |
notExpected := []int{3, 6, 8, 41, 43} |
| 2151 |
ok = td.CmpNotAny(t, got, []any{td.Flatten(notExpected)}, |
| 2152 |
"checks %v contains no item listed in notExpected", got) |
| 2153 |
fmt.Println(ok) |
| 2154 |
|
| 2155 |
// Output: |
| 2156 |
// true |
| 2157 |
// false |
| 2158 |
// true |
| 2159 |
} |
| 2160 |
|
| 2161 |
func ExampleCmpNotEmpty() { |
| 2162 |
t := &testing.T{} |
| 2163 |
|
| 2164 |
ok := td.CmpNotEmpty(t, nil) // fails, as nil is considered empty |
| 2165 |
fmt.Println(ok) |
| 2166 |
|
| 2167 |
ok = td.CmpNotEmpty(t, "foobar") |
| 2168 |
fmt.Println(ok) |
| 2169 |
|
| 2170 |
// Fails as 0 is a number, so not empty. Use NotZero() instead |
| 2171 |
ok = td.CmpNotEmpty(t, 0) |
| 2172 |
fmt.Println(ok) |
| 2173 |
|
| 2174 |
ok = td.CmpNotEmpty(t, map[string]int{"foobar": 42}) |
| 2175 |
fmt.Println(ok) |
| 2176 |
|
| 2177 |
ok = td.CmpNotEmpty(t, []int{1}) |
| 2178 |
fmt.Println(ok) |
| 2179 |
|
| 2180 |
ok = td.CmpNotEmpty(t, [3]int{}) // succeeds, NotEmpty() is not NotZero()! |
| 2181 |
fmt.Println(ok) |
| 2182 |
|
| 2183 |
// Output: |
| 2184 |
// false |
| 2185 |
// true |
| 2186 |
// false |
| 2187 |
// true |
| 2188 |
// true |
| 2189 |
// true |
| 2190 |
} |
| 2191 |
|
| 2192 |
func ExampleCmpNotEmpty_pointers() { |
| 2193 |
t := &testing.T{} |
| 2194 |
|
| 2195 |
type MySlice []int |
| 2196 |
|
| 2197 |
ok := td.CmpNotEmpty(t, MySlice{12}) |
| 2198 |
fmt.Println(ok) |
| 2199 |
|
| 2200 |
ok = td.CmpNotEmpty(t, &MySlice{12}) // Ptr() not needed |
| 2201 |
fmt.Println(ok) |
| 2202 |
|
| 2203 |
l1 := &MySlice{12} |
| 2204 |
l2 := &l1 |
| 2205 |
l3 := &l2 |
| 2206 |
ok = td.CmpNotEmpty(t, &l3) |
| 2207 |
fmt.Println(ok) |
| 2208 |
|
| 2209 |
// Works the same for array, map, channel and string |
| 2210 |
|
| 2211 |
// But not for others types as: |
| 2212 |
type MyStruct struct { |
| 2213 |
Value int |
| 2214 |
} |
| 2215 |
|
| 2216 |
ok = td.CmpNotEmpty(t, &MyStruct{}) // fails, use NotZero() instead |
| 2217 |
fmt.Println(ok) |
| 2218 |
|
| 2219 |
// Output: |
| 2220 |
// true |
| 2221 |
// true |
| 2222 |
// true |
| 2223 |
// false |
| 2224 |
} |
| 2225 |
|
| 2226 |
func ExampleCmpNotNaN_float32() { |
| 2227 |
t := &testing.T{} |
| 2228 |
|
| 2229 |
got := float32(math.NaN()) |
| 2230 |
|
| 2231 |
ok := td.CmpNotNaN(t, got, |
| 2232 |
"checks %v is not-a-number", got) |
| 2233 |
|
| 2234 |
fmt.Println("float32(math.NaN()) is NOT float32 not-a-number:", ok) |
| 2235 |
|
| 2236 |
got = 12 |
| 2237 |
|
| 2238 |
ok = td.CmpNotNaN(t, got, |
| 2239 |
"checks %v is not-a-number", got) |
| 2240 |
|
| 2241 |
fmt.Println("float32(12) is NOT float32 not-a-number:", ok) |
| 2242 |
|
| 2243 |
// Output: |
| 2244 |
// float32(math.NaN()) is NOT float32 not-a-number: false |
| 2245 |
// float32(12) is NOT float32 not-a-number: true |
| 2246 |
} |
| 2247 |
|
| 2248 |
func ExampleCmpNotNaN_float64() { |
| 2249 |
t := &testing.T{} |
| 2250 |
|
| 2251 |
got := math.NaN() |
| 2252 |
|
| 2253 |
ok := td.CmpNotNaN(t, got, |
| 2254 |
"checks %v is NOT not-a-number", got) |
| 2255 |
|
| 2256 |
fmt.Println("math.NaN() is NOT not-a-number:", ok) |
| 2257 |
|
| 2258 |
got = 12 |
| 2259 |
|
| 2260 |
ok = td.CmpNotNaN(t, got, |
| 2261 |
"checks %v is NOT not-a-number", got) |
| 2262 |
|
| 2263 |
fmt.Println("float64(12) is NOT not-a-number:", ok) |
| 2264 |
|
| 2265 |
// Output: |
| 2266 |
// math.NaN() is NOT not-a-number: false |
| 2267 |
// float64(12) is NOT not-a-number: true |
| 2268 |
} |
| 2269 |
|
| 2270 |
func ExampleCmpNotNil() { |
| 2271 |
t := &testing.T{} |
| 2272 |
|
| 2273 |
var got fmt.Stringer = &bytes.Buffer{} |
| 2274 |
|
| 2275 |
// nil value can be compared directly with Not(nil), no need of NotNil() here |
| 2276 |
ok := td.Cmp(t, got, td.Not(nil)) |
| 2277 |
fmt.Println(ok) |
| 2278 |
|
| 2279 |
// But it works with NotNil() anyway |
| 2280 |
ok = td.CmpNotNil(t, got) |
| 2281 |
fmt.Println(ok) |
| 2282 |
|
| 2283 |
got = (*bytes.Buffer)(nil) |
| 2284 |
|
| 2285 |
// In the case of an interface containing a nil pointer, comparing |
| 2286 |
// with Not(nil) succeeds, as the interface is not nil |
| 2287 |
ok = td.Cmp(t, got, td.Not(nil)) |
| 2288 |
fmt.Println(ok) |
| 2289 |
|
| 2290 |
// In this case NotNil() fails |
| 2291 |
ok = td.CmpNotNil(t, got) |
| 2292 |
fmt.Println(ok) |
| 2293 |
|
| 2294 |
// Output: |
| 2295 |
// true |
| 2296 |
// true |
| 2297 |
// true |
| 2298 |
// false |
| 2299 |
} |
| 2300 |
|
| 2301 |
func ExampleCmpNotZero() { |
| 2302 |
t := &testing.T{} |
| 2303 |
|
| 2304 |
ok := td.CmpNotZero(t, 0) // fails |
| 2305 |
fmt.Println(ok) |
| 2306 |
|
| 2307 |
ok = td.CmpNotZero(t, float64(0)) // fails |
| 2308 |
fmt.Println(ok) |
| 2309 |
|
| 2310 |
ok = td.CmpNotZero(t, 12) |
| 2311 |
fmt.Println(ok) |
| 2312 |
|
| 2313 |
ok = td.CmpNotZero(t, (map[string]int)(nil)) // fails, as nil |
| 2314 |
fmt.Println(ok) |
| 2315 |
|
| 2316 |
ok = td.CmpNotZero(t, map[string]int{}) // succeeds, as not nil |
| 2317 |
fmt.Println(ok) |
| 2318 |
|
| 2319 |
ok = td.CmpNotZero(t, ([]int)(nil)) // fails, as nil |
| 2320 |
fmt.Println(ok) |
| 2321 |
|
| 2322 |
ok = td.CmpNotZero(t, []int{}) // succeeds, as not nil |
| 2323 |
fmt.Println(ok) |
| 2324 |
|
| 2325 |
ok = td.CmpNotZero(t, [3]int{}) // fails |
| 2326 |
fmt.Println(ok) |
| 2327 |
|
| 2328 |
ok = td.CmpNotZero(t, [3]int{0, 1}) // succeeds, DATA[1] is not 0 |
| 2329 |
fmt.Println(ok) |
| 2330 |
|
| 2331 |
ok = td.CmpNotZero(t, bytes.Buffer{}) // fails |
| 2332 |
fmt.Println(ok) |
| 2333 |
|
| 2334 |
ok = td.CmpNotZero(t, &bytes.Buffer{}) // succeeds, as pointer not nil |
| 2335 |
fmt.Println(ok) |
| 2336 |
|
| 2337 |
ok = td.Cmp(t, &bytes.Buffer{}, td.Ptr(td.NotZero())) // fails as deref by Ptr() |
| 2338 |
fmt.Println(ok) |
| 2339 |
|
| 2340 |
// Output: |
| 2341 |
// false |
| 2342 |
// false |
| 2343 |
// true |
| 2344 |
// false |
| 2345 |
// true |
| 2346 |
// false |
| 2347 |
// true |
| 2348 |
// false |
| 2349 |
// true |
| 2350 |
// false |
| 2351 |
// true |
| 2352 |
// false |
| 2353 |
} |
| 2354 |
|
| 2355 |
func ExampleCmpPPtr() { |
| 2356 |
t := &testing.T{} |
| 2357 |
|
| 2358 |
num := 12 |
| 2359 |
got := &num |
| 2360 |
|
| 2361 |
ok := td.CmpPPtr(t, &got, 12) |
| 2362 |
fmt.Println(ok) |
| 2363 |
|
| 2364 |
ok = td.CmpPPtr(t, &got, td.Between(4, 15)) |
| 2365 |
fmt.Println(ok) |
| 2366 |
|
| 2367 |
// Output: |
| 2368 |
// true |
| 2369 |
// true |
| 2370 |
} |
| 2371 |
|
| 2372 |
func ExampleCmpPtr() { |
| 2373 |
t := &testing.T{} |
| 2374 |
|
| 2375 |
got := 12 |
| 2376 |
|
| 2377 |
ok := td.CmpPtr(t, &got, 12) |
| 2378 |
fmt.Println(ok) |
| 2379 |
|
| 2380 |
ok = td.CmpPtr(t, &got, td.Between(4, 15)) |
| 2381 |
fmt.Println(ok) |
| 2382 |
|
| 2383 |
// Output: |
| 2384 |
// true |
| 2385 |
// true |
| 2386 |
} |
| 2387 |
|
| 2388 |
func ExampleCmpRe() { |
| 2389 |
t := &testing.T{} |
| 2390 |
|
| 2391 |
got := "foo bar" |
| 2392 |
ok := td.CmpRe(t, got, "(zip|bar)$", nil, "checks value %s", got) |
| 2393 |
fmt.Println(ok) |
| 2394 |
|
| 2395 |
got = "bar foo" |
| 2396 |
ok = td.CmpRe(t, got, "(zip|bar)$", nil, "checks value %s", got) |
| 2397 |
fmt.Println(ok) |
| 2398 |
|
| 2399 |
// Output: |
| 2400 |
// true |
| 2401 |
// false |
| 2402 |
} |
| 2403 |
|
| 2404 |
func ExampleCmpRe_stringer() { |
| 2405 |
t := &testing.T{} |
| 2406 |
|
| 2407 |
// bytes.Buffer implements fmt.Stringer |
| 2408 |
got := bytes.NewBufferString("foo bar") |
| 2409 |
ok := td.CmpRe(t, got, "(zip|bar)$", nil, "checks value %s", got) |
| 2410 |
fmt.Println(ok) |
| 2411 |
|
| 2412 |
// Output: |
| 2413 |
// true |
| 2414 |
} |
| 2415 |
|
| 2416 |
func ExampleCmpRe_error() { |
| 2417 |
t := &testing.T{} |
| 2418 |
|
| 2419 |
got := errors.New("foo bar") |
| 2420 |
ok := td.CmpRe(t, got, "(zip|bar)$", nil, "checks value %s", got) |
| 2421 |
fmt.Println(ok) |
| 2422 |
|
| 2423 |
// Output: |
| 2424 |
// true |
| 2425 |
} |
| 2426 |
|
| 2427 |
func ExampleCmpRe_capture() { |
| 2428 |
t := &testing.T{} |
| 2429 |
|
| 2430 |
got := "foo bar biz" |
| 2431 |
ok := td.CmpRe(t, got, `^(\w+) (\w+) (\w+)$`, td.Set("biz", "foo", "bar"), |
| 2432 |
"checks value %s", got) |
| 2433 |
fmt.Println(ok) |
| 2434 |
|
| 2435 |
got = "foo bar! biz" |
| 2436 |
ok = td.CmpRe(t, got, `^(\w+) (\w+) (\w+)$`, td.Set("biz", "foo", "bar"), |
| 2437 |
"checks value %s", got) |
| 2438 |
fmt.Println(ok) |
| 2439 |
|
| 2440 |
// Output: |
| 2441 |
// true |
| 2442 |
// false |
| 2443 |
} |
| 2444 |
|
| 2445 |
func ExampleCmpRe_multilines() { |
| 2446 |
t := &testing.T{} |
| 2447 |
|
| 2448 |
got := `multi |
| 2449 |
lines |
| 2450 |
probably |
| 2451 |
more |
| 2452 |
than 4 |
| 2453 |
` |
| 2454 |
expectedRe := `^multi |
| 2455 |
lines? |
| 2456 |
(probably|possibly) |
| 2457 |
more |
| 2458 |
than \d+ |
| 2459 |
\z` |
| 2460 |
|
| 2461 |
ok := td.CmpRe(t, got, expectedRe, nil) |
| 2462 |
fmt.Println("Raw multi-lines string matches:", ok) |
| 2463 |
|
| 2464 |
// But for strings with many, many, many lines, when the regexp |
| 2465 |
// doesn't match, it is sometimes difficult to see where the regexp |
| 2466 |
// failed in the string. Here td.List & td.Flatten can help to apply |
| 2467 |
// regexp line per line (note expectedRe is not anchored anymore): |
| 2468 |
expectedRe = `multi |
| 2469 |
lines? |
| 2470 |
(probably|possibly) |
| 2471 |
more |
| 2472 |
than \d+ |
| 2473 |
` |
| 2474 |
ok = td.Cmp(t, |
| 2475 |
strings.Split(got, "\n"), |
| 2476 |
td.List(td.Flatten(strings.Split(expectedRe, "\n"), |
| 2477 |
func(line string) any { |
| 2478 |
return td.Re(`^` + line + `\z`) |
| 2479 |
}))) |
| 2480 |
fmt.Println("All string lines match:", ok) |
| 2481 |
|
| 2482 |
// Output: |
| 2483 |
// Raw multi-lines string matches: true |
| 2484 |
// All string lines match: true |
| 2485 |
} |
| 2486 |
|
| 2487 |
func ExampleCmpRe_compiled() { |
| 2488 |
t := &testing.T{} |
| 2489 |
|
| 2490 |
expected := regexp.MustCompile("(zip|bar)$") |
| 2491 |
|
| 2492 |
got := "foo bar" |
| 2493 |
ok := td.CmpRe(t, got, expected, nil, "checks value %s", got) |
| 2494 |
fmt.Println(ok) |
| 2495 |
|
| 2496 |
got = "bar foo" |
| 2497 |
ok = td.CmpRe(t, got, expected, nil, "checks value %s", got) |
| 2498 |
fmt.Println(ok) |
| 2499 |
|
| 2500 |
// Output: |
| 2501 |
// true |
| 2502 |
// false |
| 2503 |
} |
| 2504 |
|
| 2505 |
func ExampleCmpRe_compiledStringer() { |
| 2506 |
t := &testing.T{} |
| 2507 |
|
| 2508 |
expected := regexp.MustCompile("(zip|bar)$") |
| 2509 |
|
| 2510 |
// bytes.Buffer implements fmt.Stringer |
| 2511 |
got := bytes.NewBufferString("foo bar") |
| 2512 |
ok := td.CmpRe(t, got, expected, nil, "checks value %s", got) |
| 2513 |
fmt.Println(ok) |
| 2514 |
|
| 2515 |
// Output: |
| 2516 |
// true |
| 2517 |
} |
| 2518 |
|
| 2519 |
func ExampleCmpRe_compiledError() { |
| 2520 |
t := &testing.T{} |
| 2521 |
|
| 2522 |
expected := regexp.MustCompile("(zip|bar)$") |
| 2523 |
|
| 2524 |
got := errors.New("foo bar") |
| 2525 |
ok := td.CmpRe(t, got, expected, nil, "checks value %s", got) |
| 2526 |
fmt.Println(ok) |
| 2527 |
|
| 2528 |
// Output: |
| 2529 |
// true |
| 2530 |
} |
| 2531 |
|
| 2532 |
func ExampleCmpRe_compiledCapture() { |
| 2533 |
t := &testing.T{} |
| 2534 |
|
| 2535 |
expected := regexp.MustCompile(`^(\w+) (\w+) (\w+)$`) |
| 2536 |
|
| 2537 |
got := "foo bar biz" |
| 2538 |
ok := td.CmpRe(t, got, expected, td.Set("biz", "foo", "bar"), |
| 2539 |
"checks value %s", got) |
| 2540 |
fmt.Println(ok) |
| 2541 |
|
| 2542 |
got = "foo bar! biz" |
| 2543 |
ok = td.CmpRe(t, got, expected, td.Set("biz", "foo", "bar"), |
| 2544 |
"checks value %s", got) |
| 2545 |
fmt.Println(ok) |
| 2546 |
|
| 2547 |
// Output: |
| 2548 |
// true |
| 2549 |
// false |
| 2550 |
} |
| 2551 |
|
| 2552 |
func ExampleCmpReAll_capture() { |
| 2553 |
t := &testing.T{} |
| 2554 |
|
| 2555 |
got := "foo bar biz" |
| 2556 |
ok := td.CmpReAll(t, got, `(\w+)`, td.Set("biz", "foo", "bar"), |
| 2557 |
"checks value %s", got) |
| 2558 |
fmt.Println(ok) |
| 2559 |
|
| 2560 |
// Matches, but all catured groups do not match Set |
| 2561 |
got = "foo BAR biz" |
| 2562 |
ok = td.CmpReAll(t, got, `(\w+)`, td.Set("biz", "foo", "bar"), |
| 2563 |
"checks value %s", got) |
| 2564 |
fmt.Println(ok) |
| 2565 |
|
| 2566 |
// Output: |
| 2567 |
// true |
| 2568 |
// false |
| 2569 |
} |
| 2570 |
|
| 2571 |
func ExampleCmpReAll_captureComplex() { |
| 2572 |
t := &testing.T{} |
| 2573 |
|
| 2574 |
got := "11 45 23 56 85 96" |
| 2575 |
ok := td.CmpReAll(t, got, `(\d+)`, td.ArrayEach(td.Code(func(num string) bool { |
| 2576 |
n, err := strconv.Atoi(num) |
| 2577 |
return err == nil && n > 10 && n < 100 |
| 2578 |
})), |
| 2579 |
"checks value %s", got) |
| 2580 |
fmt.Println(ok) |
| 2581 |
|
| 2582 |
// Matches, but 11 is not greater than 20 |
| 2583 |
ok = td.CmpReAll(t, got, `(\d+)`, td.ArrayEach(td.Code(func(num string) bool { |
| 2584 |
n, err := strconv.Atoi(num) |
| 2585 |
return err == nil && n > 20 && n < 100 |
| 2586 |
})), |
| 2587 |
"checks value %s", got) |
| 2588 |
fmt.Println(ok) |
| 2589 |
|
| 2590 |
// Output: |
| 2591 |
// true |
| 2592 |
// false |
| 2593 |
} |
| 2594 |
|
| 2595 |
func ExampleCmpReAll_compiledCapture() { |
| 2596 |
t := &testing.T{} |
| 2597 |
|
| 2598 |
expected := regexp.MustCompile(`(\w+)`) |
| 2599 |
|
| 2600 |
got := "foo bar biz" |
| 2601 |
ok := td.CmpReAll(t, got, expected, td.Set("biz", "foo", "bar"), |
| 2602 |
"checks value %s", got) |
| 2603 |
fmt.Println(ok) |
| 2604 |
|
| 2605 |
// Matches, but all catured groups do not match Set |
| 2606 |
got = "foo BAR biz" |
| 2607 |
ok = td.CmpReAll(t, got, expected, td.Set("biz", "foo", "bar"), |
| 2608 |
"checks value %s", got) |
| 2609 |
fmt.Println(ok) |
| 2610 |
|
| 2611 |
// Output: |
| 2612 |
// true |
| 2613 |
// false |
| 2614 |
} |
| 2615 |
|
| 2616 |
func ExampleCmpReAll_compiledCaptureComplex() { |
| 2617 |
t := &testing.T{} |
| 2618 |
|
| 2619 |
expected := regexp.MustCompile(`(\d+)`) |
| 2620 |
|
| 2621 |
got := "11 45 23 56 85 96" |
| 2622 |
ok := td.CmpReAll(t, got, expected, td.ArrayEach(td.Code(func(num string) bool { |
| 2623 |
n, err := strconv.Atoi(num) |
| 2624 |
return err == nil && n > 10 && n < 100 |
| 2625 |
})), |
| 2626 |
"checks value %s", got) |
| 2627 |
fmt.Println(ok) |
| 2628 |
|
| 2629 |
// Matches, but 11 is not greater than 20 |
| 2630 |
ok = td.CmpReAll(t, got, expected, td.ArrayEach(td.Code(func(num string) bool { |
| 2631 |
n, err := strconv.Atoi(num) |
| 2632 |
return err == nil && n > 20 && n < 100 |
| 2633 |
})), |
| 2634 |
"checks value %s", got) |
| 2635 |
fmt.Println(ok) |
| 2636 |
|
| 2637 |
// Output: |
| 2638 |
// true |
| 2639 |
// false |
| 2640 |
} |
| 2641 |
|
| 2642 |
func ExampleCmpRecv_basic() { |
| 2643 |
t := &testing.T{} |
| 2644 |
|
| 2645 |
got := make(chan int, 3) |
| 2646 |
|
| 2647 |
ok := td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2648 |
fmt.Println("nothing to receive:", ok) |
| 2649 |
|
| 2650 |
got <- 1 |
| 2651 |
got <- 2 |
| 2652 |
got <- 3 |
| 2653 |
close(got) |
| 2654 |
|
| 2655 |
ok = td.CmpRecv(t, got, 1, 0) |
| 2656 |
fmt.Println("1st receive is 1:", ok) |
| 2657 |
|
| 2658 |
ok = td.Cmp(t, got, td.All( |
| 2659 |
td.Recv(2), |
| 2660 |
td.Recv(td.Between(3, 4)), |
| 2661 |
td.Recv(td.RecvClosed), |
| 2662 |
)) |
| 2663 |
fmt.Println("next receives are 2, 3 then closed:", ok) |
| 2664 |
|
| 2665 |
ok = td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2666 |
fmt.Println("nothing to receive:", ok) |
| 2667 |
|
| 2668 |
// Output: |
| 2669 |
// nothing to receive: true |
| 2670 |
// 1st receive is 1: true |
| 2671 |
// next receives are 2, 3 then closed: true |
| 2672 |
// nothing to receive: false |
| 2673 |
} |
| 2674 |
|
| 2675 |
func ExampleCmpRecv_channelPointer() { |
| 2676 |
t := &testing.T{} |
| 2677 |
|
| 2678 |
got := make(chan int, 3) |
| 2679 |
|
| 2680 |
ok := td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2681 |
fmt.Println("nothing to receive:", ok) |
| 2682 |
|
| 2683 |
got <- 1 |
| 2684 |
got <- 2 |
| 2685 |
got <- 3 |
| 2686 |
close(got) |
| 2687 |
|
| 2688 |
ok = td.CmpRecv(t, &got, 1, 0) |
| 2689 |
fmt.Println("1st receive is 1:", ok) |
| 2690 |
|
| 2691 |
ok = td.Cmp(t, &got, td.All( |
| 2692 |
td.Recv(2), |
| 2693 |
td.Recv(td.Between(3, 4)), |
| 2694 |
td.Recv(td.RecvClosed), |
| 2695 |
)) |
| 2696 |
fmt.Println("next receives are 2, 3 then closed:", ok) |
| 2697 |
|
| 2698 |
ok = td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2699 |
fmt.Println("nothing to receive:", ok) |
| 2700 |
|
| 2701 |
// Output: |
| 2702 |
// nothing to receive: true |
| 2703 |
// 1st receive is 1: true |
| 2704 |
// next receives are 2, 3 then closed: true |
| 2705 |
// nothing to receive: false |
| 2706 |
} |
| 2707 |
|
| 2708 |
func ExampleCmpRecv_withTimeout() { |
| 2709 |
t := &testing.T{} |
| 2710 |
|
| 2711 |
got := make(chan int, 1) |
| 2712 |
tick := make(chan struct{}) |
| 2713 |
|
| 2714 |
go func() { |
| 2715 |
// ① |
| 2716 |
<-tick |
| 2717 |
time.Sleep(100 * time.Millisecond) |
| 2718 |
got <- 0 |
| 2719 |
|
| 2720 |
// ② |
| 2721 |
<-tick |
| 2722 |
time.Sleep(100 * time.Millisecond) |
| 2723 |
got <- 1 |
| 2724 |
|
| 2725 |
// ③ |
| 2726 |
<-tick |
| 2727 |
time.Sleep(100 * time.Millisecond) |
| 2728 |
close(got) |
| 2729 |
}() |
| 2730 |
|
| 2731 |
td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2732 |
|
| 2733 |
// ① |
| 2734 |
tick <- struct{}{} |
| 2735 |
ok := td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2736 |
fmt.Println("① RecvNothing:", ok) |
| 2737 |
ok = td.CmpRecv(t, got, 0, 150*time.Millisecond) |
| 2738 |
fmt.Println("① receive 0 w/150ms timeout:", ok) |
| 2739 |
ok = td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2740 |
fmt.Println("① RecvNothing:", ok) |
| 2741 |
|
| 2742 |
// ② |
| 2743 |
tick <- struct{}{} |
| 2744 |
ok = td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2745 |
fmt.Println("② RecvNothing:", ok) |
| 2746 |
ok = td.CmpRecv(t, got, 1, 150*time.Millisecond) |
| 2747 |
fmt.Println("② receive 1 w/150ms timeout:", ok) |
| 2748 |
ok = td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2749 |
fmt.Println("② RecvNothing:", ok) |
| 2750 |
|
| 2751 |
// ③ |
| 2752 |
tick <- struct{}{} |
| 2753 |
ok = td.CmpRecv(t, got, td.RecvNothing, 0) |
| 2754 |
fmt.Println("③ RecvNothing:", ok) |
| 2755 |
ok = td.CmpRecv(t, got, td.RecvClosed, 150*time.Millisecond) |
| 2756 |
fmt.Println("③ check closed w/150ms timeout:", ok) |
| 2757 |
|
| 2758 |
// Output: |
| 2759 |
// ① RecvNothing: true |
| 2760 |
// ① receive 0 w/150ms timeout: true |
| 2761 |
// ① RecvNothing: true |
| 2762 |
// ② RecvNothing: true |
| 2763 |
// ② receive 1 w/150ms timeout: true |
| 2764 |
// ② RecvNothing: true |
| 2765 |
// ③ RecvNothing: true |
| 2766 |
// ③ check closed w/150ms timeout: true |
| 2767 |
} |
| 2768 |
|
| 2769 |
func ExampleCmpRecv_nilChannel() { |
| 2770 |
t := &testing.T{} |
| 2771 |
|
| 2772 |
var ch chan int |
| 2773 |
|
| 2774 |
ok := td.CmpRecv(t, ch, td.RecvNothing, 0) |
| 2775 |
fmt.Println("nothing to receive from nil channel:", ok) |
| 2776 |
|
| 2777 |
ok = td.CmpRecv(t, ch, 42, 0) |
| 2778 |
fmt.Println("something to receive from nil channel:", ok) |
| 2779 |
|
| 2780 |
ok = td.CmpRecv(t, ch, td.RecvClosed, 0) |
| 2781 |
fmt.Println("is a nil channel closed:", ok) |
| 2782 |
|
| 2783 |
// Output: |
| 2784 |
// nothing to receive from nil channel: true |
| 2785 |
// something to receive from nil channel: false |
| 2786 |
// is a nil channel closed: false |
| 2787 |
} |
| 2788 |
|
| 2789 |
func ExampleCmpSet() { |
| 2790 |
t := &testing.T{} |
| 2791 |
|
| 2792 |
got := []int{1, 3, 5, 8, 8, 1, 2} |
| 2793 |
|
| 2794 |
// Matches as all items are present, ignoring duplicates |
| 2795 |
ok := td.CmpSet(t, got, []any{1, 2, 3, 5, 8}, |
| 2796 |
"checks all items are present, in any order") |
| 2797 |
fmt.Println(ok) |
| 2798 |
|
| 2799 |
// Duplicates are ignored in a Set |
| 2800 |
ok = td.CmpSet(t, got, []any{1, 2, 2, 2, 2, 2, 3, 5, 8}, |
| 2801 |
"checks all items are present, in any order") |
| 2802 |
fmt.Println(ok) |
| 2803 |
|
| 2804 |
// Tries its best not to raise an error when a value can be matched |
| 2805 |
// by several Set entries |
| 2806 |
ok = td.CmpSet(t, got, []any{td.Between(1, 4), 3, td.Between(2, 10)}, |
| 2807 |
"checks all items are present, in any order") |
| 2808 |
fmt.Println(ok) |
| 2809 |
|
| 2810 |
// When expected is already a non-[]any slice, it cannot be |
| 2811 |
// flattened directly using expected... without copying it to a new |
| 2812 |
// []any slice, then use td.Flatten! |
| 2813 |
expected := []int{1, 2, 3, 5, 8} |
| 2814 |
ok = td.CmpSet(t, got, []any{td.Flatten(expected)}, |
| 2815 |
"checks all expected items are present, in any order") |
| 2816 |
fmt.Println(ok) |
| 2817 |
|
| 2818 |
// Output: |
| 2819 |
// true |
| 2820 |
// true |
| 2821 |
// true |
| 2822 |
// true |
| 2823 |
} |
| 2824 |
|
| 2825 |
func ExampleCmpShallow() { |
| 2826 |
t := &testing.T{} |
| 2827 |
|
| 2828 |
type MyStruct struct { |
| 2829 |
Value int |
| 2830 |
} |
| 2831 |
data := MyStruct{Value: 12} |
| 2832 |
got := &data |
| 2833 |
|
| 2834 |
ok := td.CmpShallow(t, got, &data, |
| 2835 |
"checks pointers only, not contents") |
| 2836 |
fmt.Println(ok) |
| 2837 |
|
| 2838 |
// Same contents, but not same pointer |
| 2839 |
ok = td.CmpShallow(t, got, &MyStruct{Value: 12}, |
| 2840 |
"checks pointers only, not contents") |
| 2841 |
fmt.Println(ok) |
| 2842 |
|
| 2843 |
// Output: |
| 2844 |
// true |
| 2845 |
// false |
| 2846 |
} |
| 2847 |
|
| 2848 |
func ExampleCmpShallow_slice() { |
| 2849 |
t := &testing.T{} |
| 2850 |
|
| 2851 |
back := []int{1, 2, 3, 1, 2, 3} |
| 2852 |
a := back[:3] |
| 2853 |
b := back[3:] |
| 2854 |
|
| 2855 |
ok := td.CmpShallow(t, a, back) |
| 2856 |
fmt.Println("are ≠ but share the same area:", ok) |
| 2857 |
|
| 2858 |
ok = td.CmpShallow(t, b, back) |
| 2859 |
fmt.Println("are = but do not point to same area:", ok) |
| 2860 |
|
| 2861 |
// Output: |
| 2862 |
// are ≠ but share the same area: true |
| 2863 |
// are = but do not point to same area: false |
| 2864 |
} |
| 2865 |
|
| 2866 |
func ExampleCmpShallow_string() { |
| 2867 |
t := &testing.T{} |
| 2868 |
|
| 2869 |
back := "foobarfoobar" |
| 2870 |
a := back[:6] |
| 2871 |
b := back[6:] |
| 2872 |
|
| 2873 |
ok := td.CmpShallow(t, a, back) |
| 2874 |
fmt.Println("are ≠ but share the same area:", ok) |
| 2875 |
|
| 2876 |
ok = td.CmpShallow(t, b, a) |
| 2877 |
fmt.Println("are = but do not point to same area:", ok) |
| 2878 |
|
| 2879 |
// Output: |
| 2880 |
// are ≠ but share the same area: true |
| 2881 |
// are = but do not point to same area: false |
| 2882 |
} |
| 2883 |
|
| 2884 |
func ExampleCmpSlice_slice() { |
| 2885 |
t := &testing.T{} |
| 2886 |
|
| 2887 |
got := []int{42, 58, 26} |
| 2888 |
|
| 2889 |
ok := td.CmpSlice(t, got, []int{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}, |
| 2890 |
"checks slice %v", got) |
| 2891 |
fmt.Println(ok) |
| 2892 |
|
| 2893 |
ok = td.CmpSlice(t, got, []int{}, td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}, |
| 2894 |
"checks slice %v", got) |
| 2895 |
fmt.Println(ok) |
| 2896 |
|
| 2897 |
ok = td.CmpSlice(t, got, ([]int)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}, |
| 2898 |
"checks slice %v", got) |
| 2899 |
fmt.Println(ok) |
| 2900 |
|
| 2901 |
// Output: |
| 2902 |
// true |
| 2903 |
// true |
| 2904 |
// true |
| 2905 |
} |
| 2906 |
|
| 2907 |
func ExampleCmpSlice_typedSlice() { |
| 2908 |
t := &testing.T{} |
| 2909 |
|
| 2910 |
type MySlice []int |
| 2911 |
|
| 2912 |
got := MySlice{42, 58, 26} |
| 2913 |
|
| 2914 |
ok := td.CmpSlice(t, got, MySlice{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}, |
| 2915 |
"checks typed slice %v", got) |
| 2916 |
fmt.Println(ok) |
| 2917 |
|
| 2918 |
ok = td.CmpSlice(t, &got, &MySlice{42}, td.ArrayEntries{1: 58, 2: td.Ignore()}, |
| 2919 |
"checks pointer on typed slice %v", got) |
| 2920 |
fmt.Println(ok) |
| 2921 |
|
| 2922 |
ok = td.CmpSlice(t, &got, &MySlice{}, td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}, |
| 2923 |
"checks pointer on typed slice %v", got) |
| 2924 |
fmt.Println(ok) |
| 2925 |
|
| 2926 |
ok = td.CmpSlice(t, &got, (*MySlice)(nil), td.ArrayEntries{0: 42, 1: 58, 2: td.Ignore()}, |
| 2927 |
"checks pointer on typed slice %v", got) |
| 2928 |
fmt.Println(ok) |
| 2929 |
|
| 2930 |
// Output: |
| 2931 |
// true |
| 2932 |
// true |
| 2933 |
// true |
| 2934 |
// true |
| 2935 |
} |
| 2936 |
|
| 2937 |
func ExampleCmpSmuggle_convert() { |
| 2938 |
t := &testing.T{} |
| 2939 |
|
| 2940 |
got := int64(123) |
| 2941 |
|
| 2942 |
ok := td.CmpSmuggle(t, got, func(n int64) int { return int(n) }, 123, |
| 2943 |
"checks int64 got against an int value") |
| 2944 |
fmt.Println(ok) |
| 2945 |
|
| 2946 |
ok = td.CmpSmuggle(t, "123", func(numStr string) (int, bool) { |
| 2947 |
n, err := strconv.Atoi(numStr) |
| 2948 |
return n, err == nil |
| 2949 |
}, td.Between(120, 130), |
| 2950 |
"checks that number in %#v is in [120 .. 130]") |
| 2951 |
fmt.Println(ok) |
| 2952 |
|
| 2953 |
ok = td.CmpSmuggle(t, "123", func(numStr string) (int, bool, string) { |
| 2954 |
n, err := strconv.Atoi(numStr) |
| 2955 |
if err != nil { |
| 2956 |
return 0, false, "string must contain a number" |
| 2957 |
} |
| 2958 |
return n, true, "" |
| 2959 |
}, td.Between(120, 130), |
| 2960 |
"checks that number in %#v is in [120 .. 130]") |
| 2961 |
fmt.Println(ok) |
| 2962 |
|
| 2963 |
ok = td.CmpSmuggle(t, "123", func(numStr string) (int, error) { //nolint: gocritic |
| 2964 |
return strconv.Atoi(numStr) |
| 2965 |
}, td.Between(120, 130), |
| 2966 |
"checks that number in %#v is in [120 .. 130]") |
| 2967 |
fmt.Println(ok) |
| 2968 |
|
| 2969 |
// Short version :) |
| 2970 |
ok = td.CmpSmuggle(t, "123", strconv.Atoi, td.Between(120, 130), |
| 2971 |
"checks that number in %#v is in [120 .. 130]") |
| 2972 |
fmt.Println(ok) |
| 2973 |
|
| 2974 |
// Output: |
| 2975 |
// true |
| 2976 |
// true |
| 2977 |
// true |
| 2978 |
// true |
| 2979 |
// true |
| 2980 |
} |
| 2981 |
|
| 2982 |
func ExampleCmpSmuggle_lax() { |
| 2983 |
t := &testing.T{} |
| 2984 |
|
| 2985 |
// got is an int16 and Smuggle func input is an int64: it is OK |
| 2986 |
got := int(123) |
| 2987 |
|
| 2988 |
ok := td.CmpSmuggle(t, got, func(n int64) uint32 { return uint32(n) }, uint32(123)) |
| 2989 |
fmt.Println("got int16(123) → smuggle via int64 → uint32(123):", ok) |
| 2990 |
|
| 2991 |
// Output: |
| 2992 |
// got int16(123) → smuggle via int64 → uint32(123): true |
| 2993 |
} |
| 2994 |
|
| 2995 |
func ExampleCmpSmuggle_auto_unmarshal() { |
| 2996 |
t := &testing.T{} |
| 2997 |
|
| 2998 |
// Automatically json.Unmarshal to compare |
| 2999 |
got := []byte(`{"a":1,"b":2}`) |
| 3000 |
|
| 3001 |
ok := td.CmpSmuggle(t, got, func(b json.RawMessage) (r map[string]int, err error) { |
| 3002 |
err = json.Unmarshal(b, &r) |
| 3003 |
return |
| 3004 |
}, map[string]int{ |
| 3005 |
"a": 1, |
| 3006 |
"b": 2, |
| 3007 |
}) |
| 3008 |
fmt.Println("JSON contents is OK:", ok) |
| 3009 |
|
| 3010 |
// Output: |
| 3011 |
// JSON contents is OK: true |
| 3012 |
} |
| 3013 |
|
| 3014 |
func ExampleCmpSmuggle_cast() { |
| 3015 |
t := &testing.T{} |
| 3016 |
|
| 3017 |
// A string containing JSON |
| 3018 |
got := `{ "foo": 123 }` |
| 3019 |
|
| 3020 |
// Automatically cast a string to a json.RawMessage so td.JSON can operate |
| 3021 |
ok := td.CmpSmuggle(t, got, json.RawMessage{}, td.JSON(`{"foo":123}`)) |
| 3022 |
fmt.Println("JSON contents in string is OK:", ok) |
| 3023 |
|
| 3024 |
// Automatically read from io.Reader to a json.RawMessage |
| 3025 |
ok = td.CmpSmuggle(t, bytes.NewReader([]byte(got)), json.RawMessage{}, td.JSON(`{"foo":123}`)) |
| 3026 |
fmt.Println("JSON contents just read is OK:", ok) |
| 3027 |
|
| 3028 |
// Output: |
| 3029 |
// JSON contents in string is OK: true |
| 3030 |
// JSON contents just read is OK: true |
| 3031 |
} |
| 3032 |
|
| 3033 |
func ExampleCmpSmuggle_complex() { |
| 3034 |
t := &testing.T{} |
| 3035 |
|
| 3036 |
// No end date but a start date and a duration |
| 3037 |
type StartDuration struct { |
| 3038 |
StartDate time.Time |
| 3039 |
Duration time.Duration |
| 3040 |
} |
| 3041 |
|
| 3042 |
// Checks that end date is between 17th and 19th February both at 0h |
| 3043 |
// for each of these durations in hours |
| 3044 |
|
| 3045 |
for _, duration := range []time.Duration{48 * time.Hour, 72 * time.Hour, 96 * time.Hour} { |
| 3046 |
got := StartDuration{ |
| 3047 |
StartDate: time.Date(2018, time.February, 14, 12, 13, 14, 0, time.UTC), |
| 3048 |
Duration: duration, |
| 3049 |
} |
| 3050 |
|
| 3051 |
// Simplest way, but in case of Between() failure, error will be bound |
| 3052 |
// to DATA<smuggled>, not very clear... |
| 3053 |
ok := td.CmpSmuggle(t, got, func(sd StartDuration) time.Time { |
| 3054 |
return sd.StartDate.Add(sd.Duration) |
| 3055 |
}, td.Between( |
| 3056 |
time.Date(2018, time.February, 17, 0, 0, 0, 0, time.UTC), |
| 3057 |
time.Date(2018, time.February, 19, 0, 0, 0, 0, time.UTC))) |
| 3058 |
fmt.Println(ok) |
| 3059 |
|
| 3060 |
// Name the computed value "ComputedEndDate" to render a Between() failure |
| 3061 |
// more understandable, so error will be bound to DATA.ComputedEndDate |
| 3062 |
ok = td.CmpSmuggle(t, got, func(sd StartDuration) td.SmuggledGot { |
| 3063 |
return td.SmuggledGot{ |
| 3064 |
Name: "ComputedEndDate", |
| 3065 |
Got: sd.StartDate.Add(sd.Duration), |
| 3066 |
} |
| 3067 |
}, td.Between( |
| 3068 |
time.Date(2018, time.February, 17, 0, 0, 0, 0, time.UTC), |
| 3069 |
time.Date(2018, time.February, 19, 0, 0, 0, 0, time.UTC))) |
| 3070 |
fmt.Println(ok) |
| 3071 |
} |
| 3072 |
|
| 3073 |
// Output: |
| 3074 |
// false |
| 3075 |
// false |
| 3076 |
// true |
| 3077 |
// true |
| 3078 |
// true |
| 3079 |
// true |
| 3080 |
} |
| 3081 |
|
| 3082 |
func ExampleCmpSmuggle_interface() { |
| 3083 |
t := &testing.T{} |
| 3084 |
|
| 3085 |
gotTime, err := time.Parse(time.RFC3339, "2018-05-23T12:13:14Z") |
| 3086 |
if err != nil { |
| 3087 |
t.Fatal(err) |
| 3088 |
} |
| 3089 |
|
| 3090 |
// Do not check the struct itself, but its stringified form |
| 3091 |
ok := td.CmpSmuggle(t, gotTime, func(s fmt.Stringer) string { |
| 3092 |
return s.String() |
| 3093 |
}, "2018-05-23 12:13:14 +0000 UTC") |
| 3094 |
fmt.Println("stringified time.Time OK:", ok) |
| 3095 |
|
| 3096 |
// If got does not implement the fmt.Stringer interface, it fails |
| 3097 |
// without calling the Smuggle func |
| 3098 |
type MyTime time.Time |
| 3099 |
ok = td.CmpSmuggle(t, MyTime(gotTime), func(s fmt.Stringer) string { |
| 3100 |
fmt.Println("Smuggle func called!") |
| 3101 |
return s.String() |
| 3102 |
}, "2018-05-23 12:13:14 +0000 UTC") |
| 3103 |
fmt.Println("stringified MyTime OK:", ok) |
| 3104 |
|
| 3105 |
// Output: |
| 3106 |
// stringified time.Time OK: true |
| 3107 |
// stringified MyTime OK: false |
| 3108 |
} |
| 3109 |
|
| 3110 |
func ExampleCmpSmuggle_field_path() { |
| 3111 |
t := &testing.T{} |
| 3112 |
|
| 3113 |
type Body struct { |
| 3114 |
Name string |
| 3115 |
Value any |
| 3116 |
} |
| 3117 |
type Request struct { |
| 3118 |
Body *Body |
| 3119 |
} |
| 3120 |
type Transaction struct { |
| 3121 |
Request |
| 3122 |
} |
| 3123 |
type ValueNum struct { |
| 3124 |
Num int |
| 3125 |
} |
| 3126 |
|
| 3127 |
got := &Transaction{ |
| 3128 |
Request: Request{ |
| 3129 |
Body: &Body{ |
| 3130 |
Name: "test", |
| 3131 |
Value: &ValueNum{Num: 123}, |
| 3132 |
}, |
| 3133 |
}, |
| 3134 |
} |
| 3135 |
|
| 3136 |
// Want to check whether Num is between 100 and 200? |
| 3137 |
ok := td.CmpSmuggle(t, got, func(tr *Transaction) (int, error) { |
| 3138 |
if tr.Body == nil || |
| 3139 |
tr.Body.Value == nil { |
| 3140 |
return 0, errors.New("Request.Body or Request.Body.Value is nil") |
| 3141 |
} |
| 3142 |
if v, ok := tr.Body.Value.(*ValueNum); ok && v != nil { |
| 3143 |
return v.Num, nil |
| 3144 |
} |
| 3145 |
return 0, errors.New("Request.Body.Value isn't *ValueNum or nil") |
| 3146 |
}, td.Between(100, 200)) |
| 3147 |
fmt.Println("check Num by hand:", ok) |
| 3148 |
|
| 3149 |
// Same, but automagically generated... |
| 3150 |
ok = td.CmpSmuggle(t, got, "Request.Body.Value.Num", td.Between(100, 200)) |
| 3151 |
fmt.Println("check Num using a fields-path:", ok) |
| 3152 |
|
| 3153 |
// And as Request is an anonymous field, can be simplified further |
| 3154 |
// as it can be omitted |
| 3155 |
ok = td.CmpSmuggle(t, got, "Body.Value.Num", td.Between(100, 200)) |
| 3156 |
fmt.Println("check Num using an other fields-path:", ok) |
| 3157 |
|
| 3158 |
// Note that maps and array/slices are supported |
| 3159 |
got.Body.Value = map[string]any{ |
| 3160 |
"foo": []any{ |
| 3161 |
3: map[int]string{666: "bar"}, |
| 3162 |
}, |
| 3163 |
} |
| 3164 |
ok = td.CmpSmuggle(t, got, "Body.Value[foo][3][666]", "bar") |
| 3165 |
fmt.Println("check fields-path including maps/slices:", ok) |
| 3166 |
|
| 3167 |
// Output: |
| 3168 |
// check Num by hand: true |
| 3169 |
// check Num using a fields-path: true |
| 3170 |
// check Num using an other fields-path: true |
| 3171 |
// check fields-path including maps/slices: true |
| 3172 |
} |
| 3173 |
|
| 3174 |
func ExampleCmpSort_basic() { |
| 3175 |
t := &testing.T{} |
| 3176 |
|
| 3177 |
got := []int{-1, 1, 2, -3, 3, -2, 0} |
| 3178 |
|
| 3179 |
// Generic ascending order (≥0 or nil) |
| 3180 |
ok := td.CmpSort(t, got, 1, []int{-3, -2, -1, 0, 1, 2, 3}) |
| 3181 |
fmt.Println("asc order:", ok) |
| 3182 |
|
| 3183 |
ok = td.CmpSort(t, got, 0, []int{-3, -2, -1, 0, 1, 2, 3}) |
| 3184 |
fmt.Println("asc order:", ok) |
| 3185 |
|
| 3186 |
ok = td.CmpSort(t, got, nil, []int{-3, -2, -1, 0, 1, 2, 3}) |
| 3187 |
fmt.Println("asc order:", ok) |
| 3188 |
|
| 3189 |
// Generic descending order (< 0) |
| 3190 |
ok = td.CmpSort(t, got, -1, []int{3, 2, 1, 0, -1, -2, -3}) |
| 3191 |
fmt.Println("desc order:", ok) |
| 3192 |
|
| 3193 |
evenHigher := func(a, b int) bool { |
| 3194 |
if (a%2 == 0) != (b%2 == 0) { |
| 3195 |
return a%2 != 0 |
| 3196 |
} |
| 3197 |
return a < b |
| 3198 |
} |
| 3199 |
ok = td.CmpSort(t, got, evenHigher, []int{-3, -1, 1, 3, -2, 0, 2}) |
| 3200 |
fmt.Println("even higher order:", ok) |
| 3201 |
|
| 3202 |
// Output: |
| 3203 |
// asc order: true |
| 3204 |
// asc order: true |
| 3205 |
// asc order: true |
| 3206 |
// desc order: true |
| 3207 |
// even higher order: true |
| 3208 |
} |
| 3209 |
|
| 3210 |
func ExampleCmpSort_fields_path() { |
| 3211 |
t := &testing.T{} |
| 3212 |
|
| 3213 |
type Person struct { |
| 3214 |
Name string |
| 3215 |
Age int |
| 3216 |
} |
| 3217 |
|
| 3218 |
brian := Person{Name: "Brian", Age: 22} |
| 3219 |
bob := Person{Name: "Bob", Age: 19} |
| 3220 |
stephen := Person{Name: "Stephen", Age: 19} |
| 3221 |
alice := Person{Name: "Alice", Age: 20} |
| 3222 |
marcel := Person{Name: "Marcel", Age: 25} |
| 3223 |
got := []Person{brian, bob, stephen, alice, marcel} |
| 3224 |
|
| 3225 |
ok := td.CmpSort(t, got, "Name", []Person{alice, bob, brian, marcel, stephen}) |
| 3226 |
fmt.Println("by name asc:", ok) |
| 3227 |
|
| 3228 |
ok = td.CmpSort(t, got, "-Name", []Person{stephen, marcel, brian, bob, alice}) |
| 3229 |
fmt.Println("by name desc:", ok) |
| 3230 |
|
| 3231 |
ok = td.CmpSort(t, got, []string{"-Age", "Name"}, []Person{marcel, brian, alice, bob, stephen}) |
| 3232 |
fmt.Println("by age desc, then by name asc:", ok) |
| 3233 |
|
| 3234 |
type A struct{ props map[string]int } |
| 3235 |
p12 := A{props: map[string]int{"priority": 12}} |
| 3236 |
p23 := A{props: map[string]int{"priority": 23}} |
| 3237 |
p34 := A{props: map[string]int{"priority": 34}} |
| 3238 |
got2 := []A{p23, p12, p34} |
| 3239 |
ok = td.CmpSort(t, got2, `-props[priority]`, []A{p34, p23, p12}) |
| 3240 |
fmt.Println("by priority desc:", ok) |
| 3241 |
|
| 3242 |
ok = td.CmpSort(t, got2, `props.priority`, []A{p12, p23, p34}) |
| 3243 |
fmt.Println("by priority asc:", ok) |
| 3244 |
|
| 3245 |
// Output: |
| 3246 |
// by name asc: true |
| 3247 |
// by name desc: true |
| 3248 |
// by age desc, then by name asc: true |
| 3249 |
// by priority desc: true |
| 3250 |
// by priority asc: true |
| 3251 |
} |
| 3252 |
|
| 3253 |
func ExampleCmpSorted_basic() { |
| 3254 |
t := &testing.T{} |
| 3255 |
|
| 3256 |
got := []int{-3, -2, -1, 0, 1, 2, 3} |
| 3257 |
|
| 3258 |
ok := td.CmpSorted(t, got, nil) |
| 3259 |
fmt.Println("is asc order (default):", ok) |
| 3260 |
|
| 3261 |
ok = td.CmpSorted(t, got, 1) |
| 3262 |
fmt.Println("is asc order (1):", ok) |
| 3263 |
|
| 3264 |
ok = td.CmpSorted(t, got, 0) |
| 3265 |
fmt.Println("is asc order (0):", ok) |
| 3266 |
|
| 3267 |
ok = td.CmpSorted(t, got, nil) |
| 3268 |
fmt.Println("is asc order (nil):", ok) |
| 3269 |
|
| 3270 |
ok = td.CmpSorted(t, got, -1) |
| 3271 |
fmt.Println("is desc order:", ok) |
| 3272 |
|
| 3273 |
got = []int{-3, -1, 1, 3, -2, 0, 2} |
| 3274 |
evenHigher := func(a, b int) bool { |
| 3275 |
if (a%2 == 0) != (b%2 == 0) { |
| 3276 |
return a%2 != 0 |
| 3277 |
} |
| 3278 |
return a < b |
| 3279 |
} |
| 3280 |
ok = td.CmpSorted(t, got, evenHigher) |
| 3281 |
fmt.Println("is even higher order:", ok) |
| 3282 |
|
| 3283 |
// Output: |
| 3284 |
// is asc order (default): true |
| 3285 |
// is asc order (1): true |
| 3286 |
// is asc order (0): true |
| 3287 |
// is asc order (nil): true |
| 3288 |
// is desc order: false |
| 3289 |
// is even higher order: true |
| 3290 |
} |
| 3291 |
|
| 3292 |
func ExampleCmpSorted_fields_path() { |
| 3293 |
t := &testing.T{} |
| 3294 |
|
| 3295 |
type Person struct { |
| 3296 |
Name string |
| 3297 |
Age int |
| 3298 |
} |
| 3299 |
|
| 3300 |
alice := Person{Name: "Alice", Age: 20} |
| 3301 |
bob := Person{Name: "Bob", Age: 19} |
| 3302 |
brian := Person{Name: "Brian", Age: 22} |
| 3303 |
marcel := Person{Name: "Marcel", Age: 25} |
| 3304 |
stephen := Person{Name: "Stephen", Age: 19} |
| 3305 |
|
| 3306 |
got := []Person{alice, bob, brian, marcel, stephen} |
| 3307 |
ok := td.CmpSorted(t, got, "Name") |
| 3308 |
fmt.Println("is sorted by name asc:", ok) |
| 3309 |
|
| 3310 |
got = []Person{stephen, marcel, brian, bob, alice} |
| 3311 |
ok = td.CmpSorted(t, got, "-Name") |
| 3312 |
fmt.Println("is sorted by name desc:", ok) |
| 3313 |
|
| 3314 |
got = []Person{marcel, brian, alice, bob, stephen} |
| 3315 |
ok = td.CmpSorted(t, got, []string{"-Age", "Name"}) |
| 3316 |
fmt.Println("is sorted by age desc, then by name asc 1:", ok) |
| 3317 |
|
| 3318 |
got = []Person{marcel, brian, alice, stephen, bob} |
| 3319 |
ok = td.CmpSorted(t, got, []string{"-Age", "Name"}) |
| 3320 |
fmt.Println("is sorted by age desc, then by name asc 2:", ok) |
| 3321 |
|
| 3322 |
type A struct{ props map[string]int } |
| 3323 |
p12 := A{props: map[string]int{"priority": 12}} |
| 3324 |
p23 := A{props: map[string]int{"priority": 23}} |
| 3325 |
p34 := A{props: map[string]int{"priority": 34}} |
| 3326 |
got2 := []A{p34, p23, p12} |
| 3327 |
ok = td.CmpSorted(t, got2, `-props[priority]`) |
| 3328 |
fmt.Println("is sorted by priority desc:", ok) |
| 3329 |
|
| 3330 |
ok = td.CmpSorted(t, got2, `props.priority`) |
| 3331 |
fmt.Println("is sorted by priority asc:", ok) |
| 3332 |
|
| 3333 |
// Output: |
| 3334 |
// is sorted by name asc: true |
| 3335 |
// is sorted by name desc: true |
| 3336 |
// is sorted by age desc, then by name asc 1: true |
| 3337 |
// is sorted by age desc, then by name asc 2: false |
| 3338 |
// is sorted by priority desc: true |
| 3339 |
// is sorted by priority asc: false |
| 3340 |
} |
| 3341 |
|
| 3342 |
func ExampleCmpSStruct() { |
| 3343 |
t := &testing.T{} |
| 3344 |
|
| 3345 |
type Person struct { |
| 3346 |
Name string |
| 3347 |
Age int |
| 3348 |
NumChildren int |
| 3349 |
} |
| 3350 |
|
| 3351 |
got := Person{ |
| 3352 |
Name: "Foobar", |
| 3353 |
Age: 42, |
| 3354 |
NumChildren: 0, |
| 3355 |
} |
| 3356 |
|
| 3357 |
// NumChildren is not listed in expected fields so it must be zero |
| 3358 |
ok := td.CmpSStruct(t, got, Person{Name: "Foobar"}, td.StructFields{ |
| 3359 |
"Age": td.Between(40, 50), |
| 3360 |
}, |
| 3361 |
"checks %v is the right Person") |
| 3362 |
fmt.Println("Foobar is between 40 & 50:", ok) |
| 3363 |
|
| 3364 |
// Model can be empty |
| 3365 |
got.NumChildren = 3 |
| 3366 |
ok = td.CmpSStruct(t, got, Person{}, td.StructFields{ |
| 3367 |
"Name": "Foobar", |
| 3368 |
"Age": td.Between(40, 50), |
| 3369 |
"NumChildren": td.Not(0), |
| 3370 |
}, |
| 3371 |
"checks %v is the right Person") |
| 3372 |
fmt.Println("Foobar has some children:", ok) |
| 3373 |
|
| 3374 |
// Works with pointers too |
| 3375 |
ok = td.CmpSStruct(t, &got, &Person{}, td.StructFields{ |
| 3376 |
"Name": "Foobar", |
| 3377 |
"Age": td.Between(40, 50), |
| 3378 |
"NumChildren": td.Not(0), |
| 3379 |
}, |
| 3380 |
"checks %v is the right Person") |
| 3381 |
fmt.Println("Foobar has some children (using pointer):", ok) |
| 3382 |
|
| 3383 |
// Model does not need to be instanciated |
| 3384 |
ok = td.CmpSStruct(t, &got, (*Person)(nil), td.StructFields{ |
| 3385 |
"Name": "Foobar", |
| 3386 |
"Age": td.Between(40, 50), |
| 3387 |
"NumChildren": td.Not(0), |
| 3388 |
}, |
| 3389 |
"checks %v is the right Person") |
| 3390 |
fmt.Println("Foobar has some children (using nil model):", ok) |
| 3391 |
|
| 3392 |
// Output: |
| 3393 |
// Foobar is between 40 & 50: true |
| 3394 |
// Foobar has some children: true |
| 3395 |
// Foobar has some children (using pointer): true |
| 3396 |
// Foobar has some children (using nil model): true |
| 3397 |
} |
| 3398 |
|
| 3399 |
func ExampleCmpSStruct_overwrite_model() { |
| 3400 |
t := &testing.T{} |
| 3401 |
|
| 3402 |
type Person struct { |
| 3403 |
Name string |
| 3404 |
Age int |
| 3405 |
NumChildren int |
| 3406 |
} |
| 3407 |
|
| 3408 |
got := Person{ |
| 3409 |
Name: "Foobar", |
| 3410 |
Age: 42, |
| 3411 |
NumChildren: 3, |
| 3412 |
} |
| 3413 |
|
| 3414 |
ok := td.CmpSStruct(t, got, Person{ |
| 3415 |
Name: "Foobar", |
| 3416 |
Age: 53, |
| 3417 |
}, td.StructFields{ |
| 3418 |
">Age": td.Between(40, 50), // ">" to overwrite Age:53 in model |
| 3419 |
"NumChildren": td.Gt(2), |
| 3420 |
}, |
| 3421 |
"checks %v is the right Person") |
| 3422 |
fmt.Println("Foobar is between 40 & 50:", ok) |
| 3423 |
|
| 3424 |
ok = td.CmpSStruct(t, got, Person{ |
| 3425 |
Name: "Foobar", |
| 3426 |
Age: 53, |
| 3427 |
}, td.StructFields{ |
| 3428 |
"> Age": td.Between(40, 50), // same, ">" can be followed by spaces |
| 3429 |
"NumChildren": td.Gt(2), |
| 3430 |
}, |
| 3431 |
"checks %v is the right Person") |
| 3432 |
fmt.Println("Foobar is between 40 & 50:", ok) |
| 3433 |
|
| 3434 |
// Output: |
| 3435 |
// Foobar is between 40 & 50: true |
| 3436 |
// Foobar is between 40 & 50: true |
| 3437 |
} |
| 3438 |
|
| 3439 |
func ExampleCmpSStruct_patterns() { |
| 3440 |
t := &testing.T{} |
| 3441 |
|
| 3442 |
type Person struct { |
| 3443 |
Firstname string |
| 3444 |
Lastname string |
| 3445 |
Surname string |
| 3446 |
Nickname string |
| 3447 |
CreatedAt time.Time |
| 3448 |
UpdatedAt time.Time |
| 3449 |
DeletedAt *time.Time |
| 3450 |
id int64 |
| 3451 |
secret string |
| 3452 |
} |
| 3453 |
|
| 3454 |
now := time.Now() |
| 3455 |
got := Person{ |
| 3456 |
Firstname: "Maxime", |
| 3457 |
Lastname: "Foo", |
| 3458 |
Surname: "Max", |
| 3459 |
Nickname: "max", |
| 3460 |
CreatedAt: now, |
| 3461 |
UpdatedAt: now, |
| 3462 |
DeletedAt: nil, // not deleted yet |
| 3463 |
id: 2345, |
| 3464 |
secret: "5ecr3T", |
| 3465 |
} |
| 3466 |
|
| 3467 |
ok := td.CmpSStruct(t, got, Person{Lastname: "Foo"}, td.StructFields{ |
| 3468 |
`DeletedAt`: nil, |
| 3469 |
`= *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model |
| 3470 |
`=~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt |
| 3471 |
`! [A-Z]*`: td.Ignore(), // private fields |
| 3472 |
}, |
| 3473 |
"mix shell & regexp patterns") |
| 3474 |
fmt.Println("Patterns match only remaining fields:", ok) |
| 3475 |
|
| 3476 |
ok = td.CmpSStruct(t, got, Person{Lastname: "Foo"}, td.StructFields{ |
| 3477 |
`DeletedAt`: nil, |
| 3478 |
`1 = *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model |
| 3479 |
`2 =~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt |
| 3480 |
`3 !~ ^[A-Z]`: td.Ignore(), // private fields |
| 3481 |
}, |
| 3482 |
"ordered patterns") |
| 3483 |
fmt.Println("Ordered patterns match only remaining fields:", ok) |
| 3484 |
|
| 3485 |
// Output: |
| 3486 |
// Patterns match only remaining fields: true |
| 3487 |
// Ordered patterns match only remaining fields: true |
| 3488 |
} |
| 3489 |
|
| 3490 |
func ExampleCmpSStruct_lazy_model() { |
| 3491 |
t := &testing.T{} |
| 3492 |
|
| 3493 |
got := struct { |
| 3494 |
name string |
| 3495 |
age int |
| 3496 |
}{ |
| 3497 |
name: "Foobar", |
| 3498 |
age: 42, |
| 3499 |
} |
| 3500 |
|
| 3501 |
ok := td.CmpSStruct(t, got, nil, td.StructFields{ |
| 3502 |
"name": "Foobar", |
| 3503 |
"age": td.Between(40, 45), |
| 3504 |
}) |
| 3505 |
fmt.Println("Lazy model:", ok) |
| 3506 |
|
| 3507 |
ok = td.CmpSStruct(t, got, nil, td.StructFields{ |
| 3508 |
"name": "Foobar", |
| 3509 |
"zip": 666, |
| 3510 |
}) |
| 3511 |
fmt.Println("Lazy model with unknown field:", ok) |
| 3512 |
|
| 3513 |
// Output: |
| 3514 |
// Lazy model: true |
| 3515 |
// Lazy model with unknown field: false |
| 3516 |
} |
| 3517 |
|
| 3518 |
func ExampleCmpString() { |
| 3519 |
t := &testing.T{} |
| 3520 |
|
| 3521 |
got := "foobar" |
| 3522 |
|
| 3523 |
ok := td.CmpString(t, got, "foobar", "checks %s", got) |
| 3524 |
fmt.Println("using string:", ok) |
| 3525 |
|
| 3526 |
ok = td.Cmp(t, []byte(got), td.String("foobar"), "checks %s", got) |
| 3527 |
fmt.Println("using []byte:", ok) |
| 3528 |
|
| 3529 |
// Output: |
| 3530 |
// using string: true |
| 3531 |
// using []byte: true |
| 3532 |
} |
| 3533 |
|
| 3534 |
func ExampleCmpString_stringer() { |
| 3535 |
t := &testing.T{} |
| 3536 |
|
| 3537 |
// bytes.Buffer implements fmt.Stringer |
| 3538 |
got := bytes.NewBufferString("foobar") |
| 3539 |
|
| 3540 |
ok := td.CmpString(t, got, "foobar", "checks %s", got) |
| 3541 |
fmt.Println(ok) |
| 3542 |
|
| 3543 |
// Output: |
| 3544 |
// true |
| 3545 |
} |
| 3546 |
|
| 3547 |
func ExampleCmpString_error() { |
| 3548 |
t := &testing.T{} |
| 3549 |
|
| 3550 |
got := errors.New("foobar") |
| 3551 |
|
| 3552 |
ok := td.CmpString(t, got, "foobar", "checks %s", got) |
| 3553 |
fmt.Println(ok) |
| 3554 |
|
| 3555 |
// Output: |
| 3556 |
// true |
| 3557 |
} |
| 3558 |
|
| 3559 |
func ExampleCmpStruct() { |
| 3560 |
t := &testing.T{} |
| 3561 |
|
| 3562 |
type Person struct { |
| 3563 |
Name string |
| 3564 |
Age int |
| 3565 |
NumChildren int |
| 3566 |
} |
| 3567 |
|
| 3568 |
got := Person{ |
| 3569 |
Name: "Foobar", |
| 3570 |
Age: 42, |
| 3571 |
NumChildren: 3, |
| 3572 |
} |
| 3573 |
|
| 3574 |
// As NumChildren is zero in Struct() call, it is not checked |
| 3575 |
ok := td.CmpStruct(t, got, Person{Name: "Foobar"}, td.StructFields{ |
| 3576 |
"Age": td.Between(40, 50), |
| 3577 |
}, |
| 3578 |
"checks %v is the right Person") |
| 3579 |
fmt.Println("Foobar is between 40 & 50:", ok) |
| 3580 |
|
| 3581 |
// Model can be empty |
| 3582 |
ok = td.CmpStruct(t, got, Person{}, td.StructFields{ |
| 3583 |
"Name": "Foobar", |
| 3584 |
"Age": td.Between(40, 50), |
| 3585 |
"NumChildren": td.Not(0), |
| 3586 |
}, |
| 3587 |
"checks %v is the right Person") |
| 3588 |
fmt.Println("Foobar has some children:", ok) |
| 3589 |
|
| 3590 |
// Works with pointers too |
| 3591 |
ok = td.CmpStruct(t, &got, &Person{}, td.StructFields{ |
| 3592 |
"Name": "Foobar", |
| 3593 |
"Age": td.Between(40, 50), |
| 3594 |
"NumChildren": td.Not(0), |
| 3595 |
}, |
| 3596 |
"checks %v is the right Person") |
| 3597 |
fmt.Println("Foobar has some children (using pointer):", ok) |
| 3598 |
|
| 3599 |
// Model does not need to be instanciated |
| 3600 |
ok = td.CmpStruct(t, &got, (*Person)(nil), td.StructFields{ |
| 3601 |
"Name": "Foobar", |
| 3602 |
"Age": td.Between(40, 50), |
| 3603 |
"NumChildren": td.Not(0), |
| 3604 |
}, |
| 3605 |
"checks %v is the right Person") |
| 3606 |
fmt.Println("Foobar has some children (using nil model):", ok) |
| 3607 |
|
| 3608 |
// Output: |
| 3609 |
// Foobar is between 40 & 50: true |
| 3610 |
// Foobar has some children: true |
| 3611 |
// Foobar has some children (using pointer): true |
| 3612 |
// Foobar has some children (using nil model): true |
| 3613 |
} |
| 3614 |
|
| 3615 |
func ExampleCmpStruct_overwrite_model() { |
| 3616 |
t := &testing.T{} |
| 3617 |
|
| 3618 |
type Person struct { |
| 3619 |
Name string |
| 3620 |
Age int |
| 3621 |
NumChildren int |
| 3622 |
} |
| 3623 |
|
| 3624 |
got := Person{ |
| 3625 |
Name: "Foobar", |
| 3626 |
Age: 42, |
| 3627 |
NumChildren: 3, |
| 3628 |
} |
| 3629 |
|
| 3630 |
ok := td.CmpStruct(t, got, Person{ |
| 3631 |
Name: "Foobar", |
| 3632 |
Age: 53, |
| 3633 |
}, td.StructFields{ |
| 3634 |
">Age": td.Between(40, 50), // ">" to overwrite Age:53 in model |
| 3635 |
"NumChildren": td.Gt(2), |
| 3636 |
}, |
| 3637 |
"checks %v is the right Person") |
| 3638 |
fmt.Println("Foobar is between 40 & 50:", ok) |
| 3639 |
|
| 3640 |
ok = td.CmpStruct(t, got, Person{ |
| 3641 |
Name: "Foobar", |
| 3642 |
Age: 53, |
| 3643 |
}, td.StructFields{ |
| 3644 |
"> Age": td.Between(40, 50), // same, ">" can be followed by spaces |
| 3645 |
"NumChildren": td.Gt(2), |
| 3646 |
}, |
| 3647 |
"checks %v is the right Person") |
| 3648 |
fmt.Println("Foobar is between 40 & 50:", ok) |
| 3649 |
|
| 3650 |
// Output: |
| 3651 |
// Foobar is between 40 & 50: true |
| 3652 |
// Foobar is between 40 & 50: true |
| 3653 |
} |
| 3654 |
|
| 3655 |
func ExampleCmpStruct_patterns() { |
| 3656 |
t := &testing.T{} |
| 3657 |
|
| 3658 |
type Person struct { |
| 3659 |
Firstname string |
| 3660 |
Lastname string |
| 3661 |
Surname string |
| 3662 |
Nickname string |
| 3663 |
CreatedAt time.Time |
| 3664 |
UpdatedAt time.Time |
| 3665 |
DeletedAt *time.Time |
| 3666 |
} |
| 3667 |
|
| 3668 |
now := time.Now() |
| 3669 |
got := Person{ |
| 3670 |
Firstname: "Maxime", |
| 3671 |
Lastname: "Foo", |
| 3672 |
Surname: "Max", |
| 3673 |
Nickname: "max", |
| 3674 |
CreatedAt: now, |
| 3675 |
UpdatedAt: now, |
| 3676 |
DeletedAt: nil, // not deleted yet |
| 3677 |
} |
| 3678 |
|
| 3679 |
ok := td.CmpStruct(t, got, Person{Lastname: "Foo"}, td.StructFields{ |
| 3680 |
`DeletedAt`: nil, |
| 3681 |
`= *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model |
| 3682 |
`=~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt |
| 3683 |
}, |
| 3684 |
"mix shell & regexp patterns") |
| 3685 |
fmt.Println("Patterns match only remaining fields:", ok) |
| 3686 |
|
| 3687 |
ok = td.CmpStruct(t, got, Person{Lastname: "Foo"}, td.StructFields{ |
| 3688 |
`DeletedAt`: nil, |
| 3689 |
`1 = *name`: td.Re(`^(?i)max`), // shell pattern, matches all names except Lastname as in model |
| 3690 |
`2 =~ At\z`: td.Lte(time.Now()), // regexp, matches CreatedAt & UpdatedAt |
| 3691 |
}, |
| 3692 |
"ordered patterns") |
| 3693 |
fmt.Println("Ordered patterns match only remaining fields:", ok) |
| 3694 |
|
| 3695 |
// Output: |
| 3696 |
// Patterns match only remaining fields: true |
| 3697 |
// Ordered patterns match only remaining fields: true |
| 3698 |
} |
| 3699 |
|
| 3700 |
func ExampleCmpStruct_lazy_model() { |
| 3701 |
t := &testing.T{} |
| 3702 |
|
| 3703 |
got := struct { |
| 3704 |
name string |
| 3705 |
age int |
| 3706 |
}{ |
| 3707 |
name: "Foobar", |
| 3708 |
age: 42, |
| 3709 |
} |
| 3710 |
|
| 3711 |
ok := td.CmpStruct(t, got, nil, td.StructFields{ |
| 3712 |
"name": "Foobar", |
| 3713 |
"age": td.Between(40, 45), |
| 3714 |
}) |
| 3715 |
fmt.Println("Lazy model:", ok) |
| 3716 |
|
| 3717 |
ok = td.CmpStruct(t, got, nil, td.StructFields{ |
| 3718 |
"name": "Foobar", |
| 3719 |
"zip": 666, |
| 3720 |
}) |
| 3721 |
fmt.Println("Lazy model with unknown field:", ok) |
| 3722 |
|
| 3723 |
// Output: |
| 3724 |
// Lazy model: true |
| 3725 |
// Lazy model with unknown field: false |
| 3726 |
} |
| 3727 |
|
| 3728 |
func ExampleCmpSubBagOf() { |
| 3729 |
t := &testing.T{} |
| 3730 |
|
| 3731 |
got := []int{1, 3, 5, 8, 8, 1, 2} |
| 3732 |
|
| 3733 |
ok := td.CmpSubBagOf(t, got, []any{0, 0, 1, 1, 2, 2, 3, 3, 5, 5, 8, 8, 9, 9}, |
| 3734 |
"checks at least all items are present, in any order") |
| 3735 |
fmt.Println(ok) |
| 3736 |
|
| 3737 |
// got contains one 8 too many |
| 3738 |
ok = td.CmpSubBagOf(t, got, []any{0, 0, 1, 1, 2, 2, 3, 3, 5, 5, 8, 9, 9}, |
| 3739 |
"checks at least all items are present, in any order") |
| 3740 |
fmt.Println(ok) |
| 3741 |
|
| 3742 |
got = []int{1, 3, 5, 2} |
| 3743 |
|
| 3744 |
ok = td.CmpSubBagOf(t, got, []any{td.Between(0, 3), td.Between(0, 3), td.Between(0, 3), td.Between(0, 3), td.Gt(4), td.Gt(4)}, |
| 3745 |
"checks at least all items match, in any order with TestDeep operators") |
| 3746 |
fmt.Println(ok) |
| 3747 |
|
| 3748 |
// When expected is already a non-[]any slice, it cannot be |
| 3749 |
// flattened directly using expected... without copying it to a new |
| 3750 |
// []any slice, then use td.Flatten! |
| 3751 |
expected := []int{1, 2, 3, 5, 9, 8} |
| 3752 |
ok = td.CmpSubBagOf(t, got, []any{td.Flatten(expected)}, |
| 3753 |
"checks at least all expected items are present, in any order") |
| 3754 |
fmt.Println(ok) |
| 3755 |
|
| 3756 |
// Output: |
| 3757 |
// true |
| 3758 |
// false |
| 3759 |
// true |
| 3760 |
// true |
| 3761 |
} |
| 3762 |
|
| 3763 |
func ExampleCmpSubJSONOf_basic() { |
| 3764 |
t := &testing.T{} |
| 3765 |
|
| 3766 |
got := &struct { |
| 3767 |
Fullname string `json:"fullname"` |
| 3768 |
Age int `json:"age"` |
| 3769 |
}{ |
| 3770 |
Fullname: "Bob", |
| 3771 |
Age: 42, |
| 3772 |
} |
| 3773 |
|
| 3774 |
ok := td.CmpSubJSONOf(t, got, `{"age":42,"fullname":"Bob","gender":"male"}`, nil) |
| 3775 |
fmt.Println("check got with age then fullname:", ok) |
| 3776 |
|
| 3777 |
ok = td.CmpSubJSONOf(t, got, `{"fullname":"Bob","age":42,"gender":"male"}`, nil) |
| 3778 |
fmt.Println("check got with fullname then age:", ok) |
| 3779 |
|
| 3780 |
ok = td.CmpSubJSONOf(t, got, ` |
| 3781 |
// This should be the JSON representation of a struct |
| 3782 |
{ |
| 3783 |
// A person: |
| 3784 |
"fullname": "Bob", // The name of this person |
| 3785 |
"age": 42, /* The age of this person: |
| 3786 |
- 42 of course |
| 3787 |
- to demonstrate a multi-lines comment */ |
| 3788 |
"gender": "male" // This field is ignored as SubJSONOf |
| 3789 |
}`, nil) |
| 3790 |
fmt.Println("check got with nicely formatted and commented JSON:", ok) |
| 3791 |
|
| 3792 |
ok = td.CmpSubJSONOf(t, got, `{"fullname":"Bob","gender":"male"}`, nil) |
| 3793 |
fmt.Println("check got without age field:", ok) |
| 3794 |
|
| 3795 |
// Output: |
| 3796 |
// check got with age then fullname: true |
| 3797 |
// check got with fullname then age: true |
| 3798 |
// check got with nicely formatted and commented JSON: true |
| 3799 |
// check got without age field: false |
| 3800 |
} |
| 3801 |
|
| 3802 |
func ExampleCmpSubJSONOf_placeholders() { |
| 3803 |
t := &testing.T{} |
| 3804 |
|
| 3805 |
got := &struct { |
| 3806 |
Fullname string `json:"fullname"` |
| 3807 |
Age int `json:"age"` |
| 3808 |
}{ |
| 3809 |
Fullname: "Bob Foobar", |
| 3810 |
Age: 42, |
| 3811 |
} |
| 3812 |
|
| 3813 |
ok := td.CmpSubJSONOf(t, got, `{"age": $1, "fullname": $2, "gender": $3}`, []any{42, "Bob Foobar", "male"}) |
| 3814 |
fmt.Println("check got with numeric placeholders without operators:", ok) |
| 3815 |
|
| 3816 |
ok = td.CmpSubJSONOf(t, got, `{"age": $1, "fullname": $2, "gender": $3}`, []any{td.Between(40, 45), td.HasSuffix("Foobar"), td.NotEmpty()}) |
| 3817 |
fmt.Println("check got with numeric placeholders:", ok) |
| 3818 |
|
| 3819 |
ok = td.CmpSubJSONOf(t, got, `{"age": "$1", "fullname": "$2", "gender": "$3"}`, []any{td.Between(40, 45), td.HasSuffix("Foobar"), td.NotEmpty()}) |
| 3820 |
fmt.Println("check got with double-quoted numeric placeholders:", ok) |
| 3821 |
|
| 3822 |
ok = td.CmpSubJSONOf(t, got, `{"age": $age, "fullname": $name, "gender": $gender}`, []any{td.Tag("age", td.Between(40, 45)), td.Tag("name", td.HasSuffix("Foobar")), td.Tag("gender", td.NotEmpty())}) |
| 3823 |
fmt.Println("check got with named placeholders:", ok) |
| 3824 |
|
| 3825 |
ok = td.CmpSubJSONOf(t, got, `{"age": $^NotZero, "fullname": $^NotEmpty, "gender": $^NotEmpty}`, nil) |
| 3826 |
fmt.Println("check got with operator shortcuts:", ok) |
| 3827 |
|
| 3828 |
// Output: |
| 3829 |
// check got with numeric placeholders without operators: true |
| 3830 |
// check got with numeric placeholders: true |
| 3831 |
// check got with double-quoted numeric placeholders: true |
| 3832 |
// check got with named placeholders: true |
| 3833 |
// check got with operator shortcuts: true |
| 3834 |
} |
| 3835 |
|
| 3836 |
func ExampleCmpSubJSONOf_file() { |
| 3837 |
t := &testing.T{} |
| 3838 |
|
| 3839 |
got := &struct { |
| 3840 |
Fullname string `json:"fullname"` |
| 3841 |
Age int `json:"age"` |
| 3842 |
Gender string `json:"gender"` |
| 3843 |
}{ |
| 3844 |
Fullname: "Bob Foobar", |
| 3845 |
Age: 42, |
| 3846 |
Gender: "male", |
| 3847 |
} |
| 3848 |
|
| 3849 |
tmpDir, err := os.MkdirTemp("", "") |
| 3850 |
if err != nil { |
| 3851 |
t.Fatal(err) |
| 3852 |
} |
| 3853 |
defer os.RemoveAll(tmpDir) //nolint: errcheck // clean up |
| 3854 |
|
| 3855 |
filename := tmpDir + "/test.json" |
| 3856 |
if err = os.WriteFile(filename, []byte(` |
| 3857 |
{ |
| 3858 |
"fullname": "$name", |
| 3859 |
"age": "$age", |
| 3860 |
"gender": "$gender", |
| 3861 |
"details": { |
| 3862 |
"city": "TestCity", |
| 3863 |
"zip": 666 |
| 3864 |
} |
| 3865 |
}`), 0644); err != nil { |
| 3866 |
t.Fatal(err) |
| 3867 |
} |
| 3868 |
|
| 3869 |
// OK let's test with this file |
| 3870 |
ok := td.CmpSubJSONOf(t, got, filename, []any{td.Tag("name", td.HasPrefix("Bob")), td.Tag("age", td.Between(40, 45)), td.Tag("gender", td.Re(`^(male|female)\z`))}) |
| 3871 |
fmt.Println("Full match from file name:", ok) |
| 3872 |
|
| 3873 |
// When the file is already open |
| 3874 |
file, err := os.Open(filename) |
| 3875 |
if err != nil { |
| 3876 |
t.Fatal(err) |
| 3877 |
} |
| 3878 |
ok = td.CmpSubJSONOf(t, got, file, []any{td.Tag("name", td.HasPrefix("Bob")), td.Tag("age", td.Between(40, 45)), td.Tag("gender", td.Re(`^(male|female)\z`))}) |
| 3879 |
fmt.Println("Full match from io.Reader:", ok) |
| 3880 |
|
| 3881 |
// Output: |
| 3882 |
// Full match from file name: true |
| 3883 |
// Full match from io.Reader: true |
| 3884 |
} |
| 3885 |
|
| 3886 |
func ExampleCmpSubMapOf_map() { |
| 3887 |
t := &testing.T{} |
| 3888 |
|
| 3889 |
got := map[string]int{"foo": 12, "bar": 42} |
| 3890 |
|
| 3891 |
ok := td.CmpSubMapOf(t, got, map[string]int{"bar": 42}, td.MapEntries{"foo": td.Lt(15), "zip": 666}, |
| 3892 |
"checks map %v is included in expected keys/values", got) |
| 3893 |
fmt.Println(ok) |
| 3894 |
|
| 3895 |
// Output: |
| 3896 |
// true |
| 3897 |
} |
| 3898 |
|
| 3899 |
func ExampleCmpSubMapOf_typedMap() { |
| 3900 |
t := &testing.T{} |
| 3901 |
|
| 3902 |
type MyMap map[string]int |
| 3903 |
|
| 3904 |
got := MyMap{"foo": 12, "bar": 42} |
| 3905 |
|
| 3906 |
ok := td.CmpSubMapOf(t, got, MyMap{"bar": 42}, td.MapEntries{"foo": td.Lt(15), "zip": 666}, |
| 3907 |
"checks typed map %v is included in expected keys/values", got) |
| 3908 |
fmt.Println(ok) |
| 3909 |
|
| 3910 |
ok = td.CmpSubMapOf(t, &got, &MyMap{"bar": 42}, td.MapEntries{"foo": td.Lt(15), "zip": 666}, |
| 3911 |
"checks pointed typed map %v is included in expected keys/values", got) |
| 3912 |
fmt.Println(ok) |
| 3913 |
|
| 3914 |
// Output: |
| 3915 |
// true |
| 3916 |
// true |
| 3917 |
} |
| 3918 |
|
| 3919 |
func ExampleCmpSubSetOf() { |
| 3920 |
t := &testing.T{} |
| 3921 |
|
| 3922 |
got := []int{1, 3, 5, 8, 8, 1, 2} |
| 3923 |
|
| 3924 |
// Matches as all items are expected, ignoring duplicates |
| 3925 |
ok := td.CmpSubSetOf(t, got, []any{1, 2, 3, 4, 5, 6, 7, 8}, |
| 3926 |
"checks at least all items are present, in any order, ignoring duplicates") |
| 3927 |
fmt.Println(ok) |
| 3928 |
|
| 3929 |
// Tries its best not to raise an error when a value can be matched |
| 3930 |
// by several SubSetOf entries |
| 3931 |
ok = td.CmpSubSetOf(t, got, []any{td.Between(1, 4), 3, td.Between(2, 10), td.Gt(100)}, |
| 3932 |
"checks at least all items are present, in any order, ignoring duplicates") |
| 3933 |
fmt.Println(ok) |
| 3934 |
|
| 3935 |
// When expected is already a non-[]any slice, it cannot be |
| 3936 |
// flattened directly using expected... without copying it to a new |
| 3937 |
// []any slice, then use td.Flatten! |
| 3938 |
expected := []int{1, 2, 3, 4, 5, 6, 7, 8} |
| 3939 |
ok = td.CmpSubSetOf(t, got, []any{td.Flatten(expected)}, |
| 3940 |
"checks at least all expected items are present, in any order, ignoring duplicates") |
| 3941 |
fmt.Println(ok) |
| 3942 |
|
| 3943 |
// Output: |
| 3944 |
// true |
| 3945 |
// true |
| 3946 |
// true |
| 3947 |
} |
| 3948 |
|
| 3949 |
func ExampleCmpSuperBagOf() { |
| 3950 |
t := &testing.T{} |
| 3951 |
|
| 3952 |
got := []int{1, 3, 5, 8, 8, 1, 2} |
| 3953 |
|
| 3954 |
ok := td.CmpSuperBagOf(t, got, []any{8, 5, 8}, |
| 3955 |
"checks the items are present, in any order") |
| 3956 |
fmt.Println(ok) |
| 3957 |
|
| 3958 |
ok = td.CmpSuperBagOf(t, got, []any{td.Gt(5), td.Lte(2)}, |
| 3959 |
"checks at least 2 items of %v match", got) |
| 3960 |
fmt.Println(ok) |
| 3961 |
|
| 3962 |
// When expected is already a non-[]any slice, it cannot be |
| 3963 |
// flattened directly using expected... without copying it to a new |
| 3964 |
// []any slice, then use td.Flatten! |
| 3965 |
expected := []int{8, 5, 8} |
| 3966 |
ok = td.CmpSuperBagOf(t, got, []any{td.Flatten(expected)}, |
| 3967 |
"checks the expected items are present, in any order") |
| 3968 |
fmt.Println(ok) |
| 3969 |
|
| 3970 |
// Output: |
| 3971 |
// true |
| 3972 |
// true |
| 3973 |
// true |
| 3974 |
} |
| 3975 |
|
| 3976 |
func ExampleCmpSuperJSONOf_basic() { |
| 3977 |
t := &testing.T{} |
| 3978 |
|
| 3979 |
got := &struct { |
| 3980 |
Fullname string `json:"fullname"` |
| 3981 |
Age int `json:"age"` |
| 3982 |
Gender string `json:"gender"` |
| 3983 |
City string `json:"city"` |
| 3984 |
Zip int `json:"zip"` |
| 3985 |
}{ |
| 3986 |
Fullname: "Bob", |
| 3987 |
Age: 42, |
| 3988 |
Gender: "male", |
| 3989 |
City: "TestCity", |
| 3990 |
Zip: 666, |
| 3991 |
} |
| 3992 |
|
| 3993 |
ok := td.CmpSuperJSONOf(t, got, `{"age":42,"fullname":"Bob","gender":"male"}`, nil) |
| 3994 |
fmt.Println("check got with age then fullname:", ok) |
| 3995 |
|
| 3996 |
ok = td.CmpSuperJSONOf(t, got, `{"fullname":"Bob","age":42,"gender":"male"}`, nil) |
| 3997 |
fmt.Println("check got with fullname then age:", ok) |
| 3998 |
|
| 3999 |
ok = td.CmpSuperJSONOf(t, got, ` |
| 4000 |
// This should be the JSON representation of a struct |
| 4001 |
{ |
| 4002 |
// A person: |
| 4003 |
"fullname": "Bob", // The name of this person |
| 4004 |
"age": 42, /* The age of this person: |
| 4005 |
- 42 of course |
| 4006 |
- to demonstrate a multi-lines comment */ |
| 4007 |
"gender": "male" // The gender! |
| 4008 |
}`, nil) |
| 4009 |
fmt.Println("check got with nicely formatted and commented JSON:", ok) |
| 4010 |
|
| 4011 |
ok = td.CmpSuperJSONOf(t, got, `{"fullname":"Bob","gender":"male","details":{}}`, nil) |
| 4012 |
fmt.Println("check got with details field:", ok) |
| 4013 |
|
| 4014 |
// Output: |
| 4015 |
// check got with age then fullname: true |
| 4016 |
// check got with fullname then age: true |
| 4017 |
// check got with nicely formatted and commented JSON: true |
| 4018 |
// check got with details field: false |
| 4019 |
} |
| 4020 |
|
| 4021 |
func ExampleCmpSuperJSONOf_placeholders() { |
| 4022 |
t := &testing.T{} |
| 4023 |
|
| 4024 |
got := &struct { |
| 4025 |
Fullname string `json:"fullname"` |
| 4026 |
Age int `json:"age"` |
| 4027 |
Gender string `json:"gender"` |
| 4028 |
City string `json:"city"` |
| 4029 |
Zip int `json:"zip"` |
| 4030 |
}{ |
| 4031 |
Fullname: "Bob Foobar", |
| 4032 |
Age: 42, |
| 4033 |
Gender: "male", |
| 4034 |
City: "TestCity", |
| 4035 |
Zip: 666, |
| 4036 |
} |
| 4037 |
|
| 4038 |
ok := td.CmpSuperJSONOf(t, got, `{"age": $1, "fullname": $2, "gender": $3}`, []any{42, "Bob Foobar", "male"}) |
| 4039 |
fmt.Println("check got with numeric placeholders without operators:", ok) |
| 4040 |
|
| 4041 |
ok = td.CmpSuperJSONOf(t, got, `{"age": $1, "fullname": $2, "gender": $3}`, []any{td.Between(40, 45), td.HasSuffix("Foobar"), td.NotEmpty()}) |
| 4042 |
fmt.Println("check got with numeric placeholders:", ok) |
| 4043 |
|
| 4044 |
ok = td.CmpSuperJSONOf(t, got, `{"age": "$1", "fullname": "$2", "gender": "$3"}`, []any{td.Between(40, 45), td.HasSuffix("Foobar"), td.NotEmpty()}) |
| 4045 |
fmt.Println("check got with double-quoted numeric placeholders:", ok) |
| 4046 |
|
| 4047 |
ok = td.CmpSuperJSONOf(t, got, `{"age": $age, "fullname": $name, "gender": $gender}`, []any{td.Tag("age", td.Between(40, 45)), td.Tag("name", td.HasSuffix("Foobar")), td.Tag("gender", td.NotEmpty())}) |
| 4048 |
fmt.Println("check got with named placeholders:", ok) |
| 4049 |
|
| 4050 |
ok = td.CmpSuperJSONOf(t, got, `{"age": $^NotZero, "fullname": $^NotEmpty, "gender": $^NotEmpty}`, nil) |
| 4051 |
fmt.Println("check got with operator shortcuts:", ok) |
| 4052 |
|
| 4053 |
// Output: |
| 4054 |
// check got with numeric placeholders without operators: true |
| 4055 |
// check got with numeric placeholders: true |
| 4056 |
// check got with double-quoted numeric placeholders: true |
| 4057 |
// check got with named placeholders: true |
| 4058 |
// check got with operator shortcuts: true |
| 4059 |
} |
| 4060 |
|
| 4061 |
func ExampleCmpSuperJSONOf_file() { |
| 4062 |
t := &testing.T{} |
| 4063 |
|
| 4064 |
got := &struct { |
| 4065 |
Fullname string `json:"fullname"` |
| 4066 |
Age int `json:"age"` |
| 4067 |
Gender string `json:"gender"` |
| 4068 |
City string `json:"city"` |
| 4069 |
Zip int `json:"zip"` |
| 4070 |
}{ |
| 4071 |
Fullname: "Bob Foobar", |
| 4072 |
Age: 42, |
| 4073 |
Gender: "male", |
| 4074 |
City: "TestCity", |
| 4075 |
Zip: 666, |
| 4076 |
} |
| 4077 |
|
| 4078 |
tmpDir, err := os.MkdirTemp("", "") |
| 4079 |
if err != nil { |
| 4080 |
t.Fatal(err) |
| 4081 |
} |
| 4082 |
defer os.RemoveAll(tmpDir) //nolint: errcheck // clean up |
| 4083 |
|
| 4084 |
filename := tmpDir + "/test.json" |
| 4085 |
if err = os.WriteFile(filename, []byte(` |
| 4086 |
{ |
| 4087 |
"fullname": "$name", |
| 4088 |
"age": "$age", |
| 4089 |
"gender": "$gender" |
| 4090 |
}`), 0644); err != nil { |
| 4091 |
t.Fatal(err) |
| 4092 |
} |
| 4093 |
|
| 4094 |
// OK let's test with this file |
| 4095 |
ok := td.CmpSuperJSONOf(t, got, filename, []any{td.Tag("name", td.HasPrefix("Bob")), td.Tag("age", td.Between(40, 45)), td.Tag("gender", td.Re(`^(male|female)\z`))}) |
| 4096 |
fmt.Println("Full match from file name:", ok) |
| 4097 |
|
| 4098 |
// When the file is already open |
| 4099 |
file, err := os.Open(filename) |
| 4100 |
if err != nil { |
| 4101 |
t.Fatal(err) |
| 4102 |
} |
| 4103 |
ok = td.CmpSuperJSONOf(t, got, file, []any{td.Tag("name", td.HasPrefix("Bob")), td.Tag("age", td.Between(40, 45)), td.Tag("gender", td.Re(`^(male|female)\z`))}) |
| 4104 |
fmt.Println("Full match from io.Reader:", ok) |
| 4105 |
|
| 4106 |
// Output: |
| 4107 |
// Full match from file name: true |
| 4108 |
// Full match from io.Reader: true |
| 4109 |
} |
| 4110 |
|
| 4111 |
func ExampleCmpSuperMapOf_map() { |
| 4112 |
t := &testing.T{} |
| 4113 |
|
| 4114 |
got := map[string]int{"foo": 12, "bar": 42, "zip": 89} |
| 4115 |
|
| 4116 |
ok := td.CmpSuperMapOf(t, got, map[string]int{"bar": 42}, td.MapEntries{"foo": td.Lt(15)}, |
| 4117 |
"checks map %v contains at least all expected keys/values", got) |
| 4118 |
fmt.Println(ok) |
| 4119 |
|
| 4120 |
// Output: |
| 4121 |
// true |
| 4122 |
} |
| 4123 |
|
| 4124 |
func ExampleCmpSuperMapOf_typedMap() { |
| 4125 |
t := &testing.T{} |
| 4126 |
|
| 4127 |
type MyMap map[string]int |
| 4128 |
|
| 4129 |
got := MyMap{"foo": 12, "bar": 42, "zip": 89} |
| 4130 |
|
| 4131 |
ok := td.CmpSuperMapOf(t, got, MyMap{"bar": 42}, td.MapEntries{"foo": td.Lt(15)}, |
| 4132 |
"checks typed map %v contains at least all expected keys/values", got) |
| 4133 |
fmt.Println(ok) |
| 4134 |
|
| 4135 |
ok = td.CmpSuperMapOf(t, &got, &MyMap{"bar": 42}, td.MapEntries{"foo": td.Lt(15)}, |
| 4136 |
"checks pointed typed map %v contains at least all expected keys/values", |
| 4137 |
got) |
| 4138 |
fmt.Println(ok) |
| 4139 |
|
| 4140 |
// Output: |
| 4141 |
// true |
| 4142 |
// true |
| 4143 |
} |
| 4144 |
|
| 4145 |
func ExampleCmpSuperSetOf() { |
| 4146 |
t := &testing.T{} |
| 4147 |
|
| 4148 |
got := []int{1, 3, 5, 8, 8, 1, 2} |
| 4149 |
|
| 4150 |
ok := td.CmpSuperSetOf(t, got, []any{1, 2, 3}, |
| 4151 |
"checks the items are present, in any order and ignoring duplicates") |
| 4152 |
fmt.Println(ok) |
| 4153 |
|
| 4154 |
ok = td.CmpSuperSetOf(t, got, []any{td.Gt(5), td.Lte(2)}, |
| 4155 |
"checks at least 2 items of %v match ignoring duplicates", got) |
| 4156 |
fmt.Println(ok) |
| 4157 |
|
| 4158 |
// When expected is already a non-[]any slice, it cannot be |
| 4159 |
// flattened directly using expected... without copying it to a new |
| 4160 |
// []any slice, then use td.Flatten! |
| 4161 |
expected := []int{1, 2, 3} |
| 4162 |
ok = td.CmpSuperSetOf(t, got, []any{td.Flatten(expected)}, |
| 4163 |
"checks the expected items are present, in any order and ignoring duplicates") |
| 4164 |
fmt.Println(ok) |
| 4165 |
|
| 4166 |
// Output: |
| 4167 |
// true |
| 4168 |
// true |
| 4169 |
// true |
| 4170 |
} |
| 4171 |
|
| 4172 |
func ExampleCmpSuperSliceOf_array() { |
| 4173 |
t := &testing.T{} |
| 4174 |
|
| 4175 |
got := [4]int{42, 58, 26, 666} |
| 4176 |
|
| 4177 |
ok := td.CmpSuperSliceOf(t, got, [4]int{1: 58}, td.ArrayEntries{3: td.Gt(660)}, |
| 4178 |
"checks array %v", got) |
| 4179 |
fmt.Println("Only check items #1 & #3:", ok) |
| 4180 |
|
| 4181 |
ok = td.CmpSuperSliceOf(t, got, [4]int{}, td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4182 |
"checks array %v", got) |
| 4183 |
fmt.Println("Only check items #0 & #3:", ok) |
| 4184 |
|
| 4185 |
ok = td.CmpSuperSliceOf(t, &got, &[4]int{}, td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4186 |
"checks array %v", got) |
| 4187 |
fmt.Println("Only check items #0 & #3 of an array pointer:", ok) |
| 4188 |
|
| 4189 |
ok = td.CmpSuperSliceOf(t, &got, (*[4]int)(nil), td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4190 |
"checks array %v", got) |
| 4191 |
fmt.Println("Only check items #0 & #3 of an array pointer, using nil model:", ok) |
| 4192 |
|
| 4193 |
// Output: |
| 4194 |
// Only check items #1 & #3: true |
| 4195 |
// Only check items #0 & #3: true |
| 4196 |
// Only check items #0 & #3 of an array pointer: true |
| 4197 |
// Only check items #0 & #3 of an array pointer, using nil model: true |
| 4198 |
} |
| 4199 |
|
| 4200 |
func ExampleCmpSuperSliceOf_typedArray() { |
| 4201 |
t := &testing.T{} |
| 4202 |
|
| 4203 |
type MyArray [4]int |
| 4204 |
|
| 4205 |
got := MyArray{42, 58, 26, 666} |
| 4206 |
|
| 4207 |
ok := td.CmpSuperSliceOf(t, got, MyArray{1: 58}, td.ArrayEntries{3: td.Gt(660)}, |
| 4208 |
"checks typed array %v", got) |
| 4209 |
fmt.Println("Only check items #1 & #3:", ok) |
| 4210 |
|
| 4211 |
ok = td.CmpSuperSliceOf(t, got, MyArray{}, td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4212 |
"checks array %v", got) |
| 4213 |
fmt.Println("Only check items #0 & #3:", ok) |
| 4214 |
|
| 4215 |
ok = td.CmpSuperSliceOf(t, &got, &MyArray{}, td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4216 |
"checks array %v", got) |
| 4217 |
fmt.Println("Only check items #0 & #3 of an array pointer:", ok) |
| 4218 |
|
| 4219 |
ok = td.CmpSuperSliceOf(t, &got, (*MyArray)(nil), td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4220 |
"checks array %v", got) |
| 4221 |
fmt.Println("Only check items #0 & #3 of an array pointer, using nil model:", ok) |
| 4222 |
|
| 4223 |
// Output: |
| 4224 |
// Only check items #1 & #3: true |
| 4225 |
// Only check items #0 & #3: true |
| 4226 |
// Only check items #0 & #3 of an array pointer: true |
| 4227 |
// Only check items #0 & #3 of an array pointer, using nil model: true |
| 4228 |
} |
| 4229 |
|
| 4230 |
func ExampleCmpSuperSliceOf_slice() { |
| 4231 |
t := &testing.T{} |
| 4232 |
|
| 4233 |
got := []int{42, 58, 26, 666} |
| 4234 |
|
| 4235 |
ok := td.CmpSuperSliceOf(t, got, []int{1: 58}, td.ArrayEntries{3: td.Gt(660)}, |
| 4236 |
"checks array %v", got) |
| 4237 |
fmt.Println("Only check items #1 & #3:", ok) |
| 4238 |
|
| 4239 |
ok = td.CmpSuperSliceOf(t, got, []int{}, td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4240 |
"checks array %v", got) |
| 4241 |
fmt.Println("Only check items #0 & #3:", ok) |
| 4242 |
|
| 4243 |
ok = td.CmpSuperSliceOf(t, &got, &[]int{}, td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4244 |
"checks array %v", got) |
| 4245 |
fmt.Println("Only check items #0 & #3 of a slice pointer:", ok) |
| 4246 |
|
| 4247 |
ok = td.CmpSuperSliceOf(t, &got, (*[]int)(nil), td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4248 |
"checks array %v", got) |
| 4249 |
fmt.Println("Only check items #0 & #3 of a slice pointer, using nil model:", ok) |
| 4250 |
|
| 4251 |
// Output: |
| 4252 |
// Only check items #1 & #3: true |
| 4253 |
// Only check items #0 & #3: true |
| 4254 |
// Only check items #0 & #3 of a slice pointer: true |
| 4255 |
// Only check items #0 & #3 of a slice pointer, using nil model: true |
| 4256 |
} |
| 4257 |
|
| 4258 |
func ExampleCmpSuperSliceOf_typedSlice() { |
| 4259 |
t := &testing.T{} |
| 4260 |
|
| 4261 |
type MySlice []int |
| 4262 |
|
| 4263 |
got := MySlice{42, 58, 26, 666} |
| 4264 |
|
| 4265 |
ok := td.CmpSuperSliceOf(t, got, MySlice{1: 58}, td.ArrayEntries{3: td.Gt(660)}, |
| 4266 |
"checks typed array %v", got) |
| 4267 |
fmt.Println("Only check items #1 & #3:", ok) |
| 4268 |
|
| 4269 |
ok = td.CmpSuperSliceOf(t, got, MySlice{}, td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4270 |
"checks array %v", got) |
| 4271 |
fmt.Println("Only check items #0 & #3:", ok) |
| 4272 |
|
| 4273 |
ok = td.CmpSuperSliceOf(t, &got, &MySlice{}, td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4274 |
"checks array %v", got) |
| 4275 |
fmt.Println("Only check items #0 & #3 of a slice pointer:", ok) |
| 4276 |
|
| 4277 |
ok = td.CmpSuperSliceOf(t, &got, (*MySlice)(nil), td.ArrayEntries{0: 42, 3: td.Between(660, 670)}, |
| 4278 |
"checks array %v", got) |
| 4279 |
fmt.Println("Only check items #0 & #3 of a slice pointer, using nil model:", ok) |
| 4280 |
|
| 4281 |
// Output: |
| 4282 |
// Only check items #1 & #3: true |
| 4283 |
// Only check items #0 & #3: true |
| 4284 |
// Only check items #0 & #3 of a slice pointer: true |
| 4285 |
// Only check items #0 & #3 of a slice pointer, using nil model: true |
| 4286 |
} |
| 4287 |
|
| 4288 |
func ExampleCmpTruncTime() { |
| 4289 |
t := &testing.T{} |
| 4290 |
|
| 4291 |
dateToTime := func(str string) time.Time { |
| 4292 |
t, err := time.Parse(time.RFC3339Nano, str) |
| 4293 |
if err != nil { |
| 4294 |
panic(err) |
| 4295 |
} |
| 4296 |
return t |
| 4297 |
} |
| 4298 |
|
| 4299 |
got := dateToTime("2018-05-01T12:45:53.123456789Z") |
| 4300 |
|
| 4301 |
// Compare dates ignoring nanoseconds and monotonic parts |
| 4302 |
expected := dateToTime("2018-05-01T12:45:53Z") |
| 4303 |
ok := td.CmpTruncTime(t, got, expected, time.Second, |
| 4304 |
"checks date %v, truncated to the second", got) |
| 4305 |
fmt.Println(ok) |
| 4306 |
|
| 4307 |
// Compare dates ignoring time and so monotonic parts |
| 4308 |
expected = dateToTime("2018-05-01T11:22:33.444444444Z") |
| 4309 |
ok = td.CmpTruncTime(t, got, expected, 24*time.Hour, |
| 4310 |
"checks date %v, truncated to the day", got) |
| 4311 |
fmt.Println(ok) |
| 4312 |
|
| 4313 |
// Compare dates exactly but ignoring monotonic part |
| 4314 |
expected = dateToTime("2018-05-01T12:45:53.123456789Z") |
| 4315 |
ok = td.CmpTruncTime(t, got, expected, 0, |
| 4316 |
"checks date %v ignoring monotonic part", got) |
| 4317 |
fmt.Println(ok) |
| 4318 |
|
| 4319 |
// Output: |
| 4320 |
// true |
| 4321 |
// true |
| 4322 |
// true |
| 4323 |
} |
| 4324 |
|
| 4325 |
func ExampleCmpValues() { |
| 4326 |
t := &testing.T{} |
| 4327 |
|
| 4328 |
got := map[string]int{"foo": 1, "bar": 2, "zip": 3} |
| 4329 |
|
| 4330 |
// Values tests values in an ordered manner |
| 4331 |
ok := td.CmpValues(t, got, []int{1, 2, 3}) |
| 4332 |
fmt.Println("All sorted values are found:", ok) |
| 4333 |
|
| 4334 |
// If the expected values are not ordered, it fails |
| 4335 |
ok = td.CmpValues(t, got, []int{3, 1, 2}) |
| 4336 |
fmt.Println("All unsorted values are found:", ok) |
| 4337 |
|
| 4338 |
// To circumvent that, one can use Bag operator |
| 4339 |
ok = td.CmpValues(t, got, td.Bag(3, 1, 2)) |
| 4340 |
fmt.Println("All unsorted values are found, with the help of Bag operator:", ok) |
| 4341 |
|
| 4342 |
// Check that each value is between 1 and 3 |
| 4343 |
ok = td.CmpValues(t, got, td.ArrayEach(td.Between(1, 3))) |
| 4344 |
fmt.Println("Each value is between 1 and 3:", ok) |
| 4345 |
|
| 4346 |
// Output: |
| 4347 |
// All sorted values are found: true |
| 4348 |
// All unsorted values are found: false |
| 4349 |
// All unsorted values are found, with the help of Bag operator: true |
| 4350 |
// Each value is between 1 and 3: true |
| 4351 |
} |
| 4352 |
|
| 4353 |
func ExampleCmpZero() { |
| 4354 |
t := &testing.T{} |
| 4355 |
|
| 4356 |
ok := td.CmpZero(t, 0) |
| 4357 |
fmt.Println(ok) |
| 4358 |
|
| 4359 |
ok = td.CmpZero(t, float64(0)) |
| 4360 |
fmt.Println(ok) |
| 4361 |
|
| 4362 |
ok = td.CmpZero(t, 12) // fails, as 12 is not 0 :) |
| 4363 |
fmt.Println(ok) |
| 4364 |
|
| 4365 |
ok = td.CmpZero(t, (map[string]int)(nil)) |
| 4366 |
fmt.Println(ok) |
| 4367 |
|
| 4368 |
ok = td.CmpZero(t, map[string]int{}) // fails, as not nil |
| 4369 |
fmt.Println(ok) |
| 4370 |
|
| 4371 |
ok = td.CmpZero(t, ([]int)(nil)) |
| 4372 |
fmt.Println(ok) |
| 4373 |
|
| 4374 |
ok = td.CmpZero(t, []int{}) // fails, as not nil |
| 4375 |
fmt.Println(ok) |
| 4376 |
|
| 4377 |
ok = td.CmpZero(t, [3]int{}) |
| 4378 |
fmt.Println(ok) |
| 4379 |
|
| 4380 |
ok = td.CmpZero(t, [3]int{0, 1}) // fails, DATA[1] is not 0 |
| 4381 |
fmt.Println(ok) |
| 4382 |
|
| 4383 |
ok = td.CmpZero(t, bytes.Buffer{}) |
| 4384 |
fmt.Println(ok) |
| 4385 |
|
| 4386 |
ok = td.CmpZero(t, &bytes.Buffer{}) // fails, as pointer not nil |
| 4387 |
fmt.Println(ok) |
| 4388 |
|
| 4389 |
ok = td.Cmp(t, &bytes.Buffer{}, td.Ptr(td.Zero())) // OK with the help of Ptr() |
| 4390 |
fmt.Println(ok) |
| 4391 |
|
| 4392 |
// Output: |
| 4393 |
// true |
| 4394 |
// true |
| 4395 |
// false |
| 4396 |
// true |
| 4397 |
// false |
| 4398 |
// true |
| 4399 |
// false |
| 4400 |
// true |
| 4401 |
// false |
| 4402 |
// true |
| 4403 |
// false |
| 4404 |
// true |
| 4405 |
} |