| 1 |
// Copyright (c) 2022, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/internal/test" |
| 13 |
"github.com/maxatome/go-testdeep/td" |
| 14 |
) |
| 15 |
|
| 16 |
func TestGrep(t *testing.T) { |
| 17 |
t.Run("basic", func(t *testing.T) { |
| 18 |
got := [...]int{-3, -2, -1, 0, 1, 2, 3} |
| 19 |
sgot := got[:] |
| 20 |
|
| 21 |
testCases := []struct { |
| 22 |
name string |
| 23 |
got any |
| 24 |
}{ |
| 25 |
{"slice", sgot}, |
| 26 |
{"array", got}, |
| 27 |
{"*slice", &sgot}, |
| 28 |
{"*array", &got}, |
| 29 |
} |
| 30 |
for _, tc := range testCases { |
| 31 |
t.Run(tc.name, func(t *testing.T) { |
| 32 |
checkOK(t, tc.got, td.Grep(td.Gt(0), []int{1, 2, 3})) |
| 33 |
checkOK(t, tc.got, td.Grep(td.Not(td.Between(-2, 2)), []int{-3, 3})) |
| 34 |
|
| 35 |
checkOK(t, tc.got, td.Grep( |
| 36 |
func(x int) bool { return (x & 1) != 0 }, |
| 37 |
[]int{-3, -1, 1, 3})) |
| 38 |
|
| 39 |
checkOK(t, tc.got, td.Grep( |
| 40 |
func(x int64) bool { return (x & 1) != 0 }, |
| 41 |
[]int{-3, -1, 1, 3}), |
| 42 |
"int64 filter vs int items") |
| 43 |
|
| 44 |
checkOK(t, tc.got, td.Grep( |
| 45 |
func(x any) bool { return (x.(int) & 1) != 0 }, |
| 46 |
[]int{-3, -1, 1, 3}), |
| 47 |
"any filter vs int items") |
| 48 |
}) |
| 49 |
} |
| 50 |
}) |
| 51 |
|
| 52 |
t.Run("struct", func(t *testing.T) { |
| 53 |
type person struct { |
| 54 |
ID int64 |
| 55 |
Name string |
| 56 |
} |
| 57 |
got := [...]person{ |
| 58 |
{ID: 1, Name: "Joe"}, |
| 59 |
{ID: 2, Name: "Bob"}, |
| 60 |
{ID: 3, Name: "Alice"}, |
| 61 |
{ID: 4, Name: "Brian"}, |
| 62 |
{ID: 5, Name: "Britt"}, |
| 63 |
} |
| 64 |
sgot := got[:] |
| 65 |
|
| 66 |
testCases := []struct { |
| 67 |
name string |
| 68 |
got any |
| 69 |
}{ |
| 70 |
{"slice", sgot}, |
| 71 |
{"array", got}, |
| 72 |
{"*slice", &sgot}, |
| 73 |
{"*array", &got}, |
| 74 |
} |
| 75 |
for _, tc := range testCases { |
| 76 |
t.Run(tc.name, func(t *testing.T) { |
| 77 |
checkOK(t, tc.got, td.Grep( |
| 78 |
td.JSONPointer("/Name", td.HasPrefix("Br")), |
| 79 |
[]person{{ID: 4, Name: "Brian"}, {ID: 5, Name: "Britt"}})) |
| 80 |
|
| 81 |
checkOK(t, tc.got, td.Grep( |
| 82 |
func(p person) bool { return p.ID < 3 }, |
| 83 |
[]person{{ID: 1, Name: "Joe"}, {ID: 2, Name: "Bob"}})) |
| 84 |
}) |
| 85 |
} |
| 86 |
}) |
| 87 |
|
| 88 |
t.Run("interfaces", func(t *testing.T) { |
| 89 |
got := [...]any{-3, -2, -1, 0, 1, 2, 3} |
| 90 |
sgot := got[:] |
| 91 |
|
| 92 |
testCases := []struct { |
| 93 |
name string |
| 94 |
got any |
| 95 |
}{ |
| 96 |
{"slice", sgot}, |
| 97 |
{"array", got}, |
| 98 |
{"*slice", &sgot}, |
| 99 |
{"*array", &got}, |
| 100 |
} |
| 101 |
for _, tc := range testCases { |
| 102 |
t.Run(tc.name, func(t *testing.T) { |
| 103 |
checkOK(t, tc.got, td.Grep(td.Gt(0), []any{1, 2, 3})) |
| 104 |
checkOK(t, tc.got, td.Grep(td.Not(td.Between(-2, 2)), []any{-3, 3})) |
| 105 |
|
| 106 |
checkOK(t, tc.got, td.Grep( |
| 107 |
func(x int) bool { return (x & 1) != 0 }, |
| 108 |
[]any{-3, -1, 1, 3})) |
| 109 |
|
| 110 |
checkOK(t, tc.got, td.Grep( |
| 111 |
func(x int64) bool { return (x & 1) != 0 }, |
| 112 |
[]any{-3, -1, 1, 3}), |
| 113 |
"int64 filter vs any/int items") |
| 114 |
|
| 115 |
checkOK(t, tc.got, td.Grep( |
| 116 |
func(x any) bool { return (x.(int) & 1) != 0 }, |
| 117 |
[]any{-3, -1, 1, 3}), |
| 118 |
"any filter vs any/int items") |
| 119 |
}) |
| 120 |
} |
| 121 |
}) |
| 122 |
|
| 123 |
t.Run("interfaces error", func(t *testing.T) { |
| 124 |
got := [...]any{123, "foo"} |
| 125 |
sgot := got[:] |
| 126 |
|
| 127 |
testCases := []struct { |
| 128 |
name string |
| 129 |
got any |
| 130 |
}{ |
| 131 |
{"slice", sgot}, |
| 132 |
{"array", got}, |
| 133 |
{"*slice", &sgot}, |
| 134 |
{"*array", &got}, |
| 135 |
} |
| 136 |
for _, tc := range testCases { |
| 137 |
t.Run(tc.name, func(t *testing.T) { |
| 138 |
checkError(t, tc.got, |
| 139 |
td.Grep(func(x int) bool { return true }, []string{"never reached"}), |
| 140 |
expectedError{ |
| 141 |
Message: mustBe("incompatible parameter type"), |
| 142 |
Path: mustBe("DATA[1]"), |
| 143 |
Got: mustBe("string"), |
| 144 |
Expected: mustBe("int"), |
| 145 |
}) |
| 146 |
}) |
| 147 |
} |
| 148 |
}) |
| 149 |
|
| 150 |
t.Run("nil slice", func(t *testing.T) { |
| 151 |
var got []int |
| 152 |
testCases := []struct { |
| 153 |
name string |
| 154 |
got any |
| 155 |
}{ |
| 156 |
{"slice", got}, |
| 157 |
{"*slice", &got}, |
| 158 |
} |
| 159 |
for _, tc := range testCases { |
| 160 |
t.Run(tc.name, func(t *testing.T) { |
| 161 |
checkOK(t, tc.got, td.Grep(td.Gt(666), ([]int)(nil))) |
| 162 |
}) |
| 163 |
} |
| 164 |
}) |
| 165 |
|
| 166 |
t.Run("nil pointer", func(t *testing.T) { |
| 167 |
checkError(t, (*[]int)(nil), td.Grep(td.Ignore(), []int{33}), |
| 168 |
expectedError{ |
| 169 |
Message: mustBe("nil pointer"), |
| 170 |
Path: mustBe("DATA"), |
| 171 |
Got: mustBe("nil *slice (*[]int type)"), |
| 172 |
Expected: mustBe("non-nil *slice OR *array"), |
| 173 |
}) |
| 174 |
}) |
| 175 |
|
| 176 |
t.Run("JSON", func(t *testing.T) { |
| 177 |
got := map[string]any{ |
| 178 |
"values": []int{1, 2, 3, 4}, |
| 179 |
} |
| 180 |
checkOK(t, got, td.JSON(`{"values": Grep(Gt(2), [3, 4])}`)) |
| 181 |
}) |
| 182 |
|
| 183 |
t.Run("errors", func(t *testing.T) { |
| 184 |
for _, filter := range []any{nil, 33} { |
| 185 |
checkError(t, "never tested", |
| 186 |
td.Grep(filter, 42), |
| 187 |
expectedError{ |
| 188 |
Message: mustBe("bad usage of Grep operator"), |
| 189 |
Path: mustBe("DATA"), |
| 190 |
Summary: mustBe("usage: Grep(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must be a function or FILTER_TESTDEEP_OPERATOR a TestDeep operator"), |
| 191 |
}, |
| 192 |
"filter:", filter) |
| 193 |
} |
| 194 |
|
| 195 |
for _, filter := range []any{ |
| 196 |
func() bool { return true }, |
| 197 |
func(a, b int) bool { return true }, |
| 198 |
func(a ...int) bool { return true }, |
| 199 |
} { |
| 200 |
checkError(t, "never tested", |
| 201 |
td.Grep(filter, 42), |
| 202 |
expectedError{ |
| 203 |
Message: mustBe("bad usage of Grep operator"), |
| 204 |
Path: mustBe("DATA"), |
| 205 |
Summary: mustBe("usage: Grep(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must take only one non-variadic argument"), |
| 206 |
}, |
| 207 |
"filter:", filter) |
| 208 |
} |
| 209 |
|
| 210 |
for _, filter := range []any{ |
| 211 |
func(a int) {}, |
| 212 |
func(a int) int { return 0 }, |
| 213 |
func(a int) (bool, bool) { return true, true }, |
| 214 |
} { |
| 215 |
checkError(t, "never tested", |
| 216 |
td.Grep(filter, 42), |
| 217 |
expectedError{ |
| 218 |
Message: mustBe("bad usage of Grep operator"), |
| 219 |
Path: mustBe("DATA"), |
| 220 |
Summary: mustBe("usage: Grep(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must return bool"), |
| 221 |
}, |
| 222 |
"filter:", filter) |
| 223 |
} |
| 224 |
|
| 225 |
checkError(t, "never tested", td.Grep(td.Ignore(), 42), |
| 226 |
expectedError{ |
| 227 |
Message: mustBe("bad usage of Grep operator"), |
| 228 |
Path: mustBe("DATA"), |
| 229 |
Summary: mustBe("usage: Grep(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), EXPECTED_VALUE must be a slice not a int"), |
| 230 |
}) |
| 231 |
|
| 232 |
checkError(t, &struct{}{}, td.Grep(td.Ignore(), []int{33}), |
| 233 |
expectedError{ |
| 234 |
Message: mustBe("bad kind"), |
| 235 |
Path: mustBe("DATA"), |
| 236 |
Got: mustBe("*struct (*struct {} type)"), |
| 237 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 238 |
}) |
| 239 |
|
| 240 |
checkError(t, nil, td.Grep(td.Ignore(), []int{33}), |
| 241 |
expectedError{ |
| 242 |
Message: mustBe("bad kind"), |
| 243 |
Path: mustBe("DATA"), |
| 244 |
Got: mustBe("nil"), |
| 245 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 246 |
}) |
| 247 |
|
| 248 |
checkError(t, []any{1, 2, 3}, td.Grep(td.JSON("{"), td.Ignore()), |
| 249 |
expectedError{ |
| 250 |
Message: mustBe("bad usage of JSON operator"), |
| 251 |
Path: mustBe(`DATA`), |
| 252 |
Summary: mustContain("JSON unmarshal error"), |
| 253 |
Under: mustContain("under operator JSON at "), |
| 254 |
}, |
| 255 |
"erroneous operator expected") |
| 256 |
|
| 257 |
checkError(t, []any{1, 2, 3}, td.Grep(td.Ignore(), td.JSON("{")), |
| 258 |
expectedError{ |
| 259 |
Message: mustBe("bad usage of JSON operator"), |
| 260 |
Path: mustMatch(`DATA<grepped>`), // always without .Iface |
| 261 |
Summary: mustContain("JSON unmarshal error"), |
| 262 |
Under: mustContain("under operator JSON at "), |
| 263 |
}, |
| 264 |
"erroneous operator expected") |
| 265 |
}) |
| 266 |
} |
| 267 |
|
| 268 |
func TestGrepTypeBehind(t *testing.T) { |
| 269 |
equalTypes(t, td.Grep(func(n int) bool { return true }, []int{33}), []int{}) |
| 270 |
equalTypes(t, td.Grep(td.Gt("0"), []string{"33"}), []string{}) |
| 271 |
|
| 272 |
// Erroneous op |
| 273 |
equalTypes(t, td.Grep(42, 33), nil) |
| 274 |
} |
| 275 |
|
| 276 |
func TestGrepString(t *testing.T) { |
| 277 |
test.EqualStr(t, |
| 278 |
td.Grep(func(n int) bool { return true }, []int{}).String(), |
| 279 |
"Grep(func(int) bool, ([]int) {\n})") |
| 280 |
|
| 281 |
test.EqualStr(t, td.Grep(td.Gt(0), []int{}).String(), "Grep(> 0, ([]int) {\n})") |
| 282 |
|
| 283 |
// Erroneous op |
| 284 |
test.EqualStr(t, td.Grep(42, []int{}).String(), "Grep(<ERROR>)") |
| 285 |
} |
| 286 |
|
| 287 |
func TestFirst(t *testing.T) { |
| 288 |
t.Run("basic", func(t *testing.T) { |
| 289 |
got := [...]int{-3, -2, -1, 0, 1, 2, 3} |
| 290 |
sgot := got[:] |
| 291 |
|
| 292 |
testCases := []struct { |
| 293 |
name string |
| 294 |
got any |
| 295 |
}{ |
| 296 |
{"slice", sgot}, |
| 297 |
{"array", got}, |
| 298 |
{"*slice", &sgot}, |
| 299 |
{"*array", &got}, |
| 300 |
} |
| 301 |
for _, tc := range testCases { |
| 302 |
t.Run(tc.name, func(t *testing.T) { |
| 303 |
checkOK(t, tc.got, td.First(td.Gt(0), 1)) |
| 304 |
checkOK(t, tc.got, td.First(td.Not(td.Between(-3, 2)), 3)) |
| 305 |
|
| 306 |
checkOK(t, tc.got, td.First( |
| 307 |
func(x int) bool { return (x & 1) == 0 }, |
| 308 |
-2)) |
| 309 |
|
| 310 |
checkOK(t, tc.got, td.First( |
| 311 |
func(x int64) bool { return (x & 1) != 0 }, |
| 312 |
-3), |
| 313 |
"int64 filter vs int items") |
| 314 |
|
| 315 |
checkOK(t, tc.got, td.First( |
| 316 |
func(x any) bool { return (x.(int) & 1) == 0 }, |
| 317 |
-2), |
| 318 |
"any filter vs int items") |
| 319 |
|
| 320 |
checkError(t, tc.got, |
| 321 |
td.First(td.Gt(666), "never reached"), |
| 322 |
expectedError{ |
| 323 |
Message: mustBe("item not found"), |
| 324 |
Path: mustBe("DATA"), |
| 325 |
Got: mustContain(`]int) (len=7)`), |
| 326 |
Expected: mustBe(`First(> 666, "never reached")`), |
| 327 |
}) |
| 328 |
}) |
| 329 |
} |
| 330 |
}) |
| 331 |
|
| 332 |
t.Run("struct", func(t *testing.T) { |
| 333 |
type person struct { |
| 334 |
ID int64 |
| 335 |
Name string |
| 336 |
} |
| 337 |
got := [...]person{ |
| 338 |
{ID: 1, Name: "Joe"}, |
| 339 |
{ID: 2, Name: "Bob"}, |
| 340 |
{ID: 3, Name: "Alice"}, |
| 341 |
{ID: 4, Name: "Brian"}, |
| 342 |
{ID: 5, Name: "Britt"}, |
| 343 |
} |
| 344 |
sgot := got[:] |
| 345 |
|
| 346 |
testCases := []struct { |
| 347 |
name string |
| 348 |
got any |
| 349 |
}{ |
| 350 |
{"slice", sgot}, |
| 351 |
{"array", got}, |
| 352 |
{"*slice", &sgot}, |
| 353 |
{"*array", &got}, |
| 354 |
} |
| 355 |
for _, tc := range testCases { |
| 356 |
t.Run(tc.name, func(t *testing.T) { |
| 357 |
checkOK(t, tc.got, td.First( |
| 358 |
td.JSONPointer("/Name", td.HasPrefix("Br")), |
| 359 |
person{ID: 4, Name: "Brian"})) |
| 360 |
|
| 361 |
checkOK(t, tc.got, td.First( |
| 362 |
func(p person) bool { return p.ID < 3 }, |
| 363 |
person{ID: 1, Name: "Joe"})) |
| 364 |
}) |
| 365 |
} |
| 366 |
}) |
| 367 |
|
| 368 |
t.Run("interfaces", func(t *testing.T) { |
| 369 |
got := [...]any{-3, -2, -1, 0, 1, 2, 3} |
| 370 |
sgot := got[:] |
| 371 |
|
| 372 |
testCases := []struct { |
| 373 |
name string |
| 374 |
got any |
| 375 |
}{ |
| 376 |
{"slice", sgot}, |
| 377 |
{"array", got}, |
| 378 |
{"*slice", &sgot}, |
| 379 |
{"*array", &got}, |
| 380 |
} |
| 381 |
for _, tc := range testCases { |
| 382 |
t.Run(tc.name, func(t *testing.T) { |
| 383 |
checkOK(t, tc.got, td.First(td.Gt(0), 1)) |
| 384 |
checkOK(t, tc.got, td.First(td.Not(td.Between(-3, 2)), 3)) |
| 385 |
|
| 386 |
checkOK(t, tc.got, td.First( |
| 387 |
func(x int) bool { return (x & 1) == 0 }, |
| 388 |
-2)) |
| 389 |
|
| 390 |
checkOK(t, tc.got, td.First( |
| 391 |
func(x int64) bool { return (x & 1) != 0 }, |
| 392 |
-3), |
| 393 |
"int64 filter vs any/int items") |
| 394 |
|
| 395 |
checkOK(t, tc.got, td.First( |
| 396 |
func(x any) bool { return (x.(int) & 1) == 0 }, |
| 397 |
-2), |
| 398 |
"any filter vs any/int items") |
| 399 |
}) |
| 400 |
} |
| 401 |
}) |
| 402 |
|
| 403 |
t.Run("interfaces error", func(t *testing.T) { |
| 404 |
got := [...]any{123, "foo"} |
| 405 |
sgot := got[:] |
| 406 |
|
| 407 |
testCases := []struct { |
| 408 |
name string |
| 409 |
got any |
| 410 |
}{ |
| 411 |
{"slice", sgot}, |
| 412 |
{"array", got}, |
| 413 |
{"*slice", &sgot}, |
| 414 |
{"*array", &got}, |
| 415 |
} |
| 416 |
for _, tc := range testCases { |
| 417 |
t.Run(tc.name, func(t *testing.T) { |
| 418 |
checkError(t, tc.got, |
| 419 |
td.First(func(x int) bool { return false }, "never reached"), |
| 420 |
expectedError{ |
| 421 |
Message: mustBe("incompatible parameter type"), |
| 422 |
Path: mustBe("DATA[1]"), |
| 423 |
Got: mustBe("string"), |
| 424 |
Expected: mustBe("int"), |
| 425 |
}) |
| 426 |
}) |
| 427 |
} |
| 428 |
}) |
| 429 |
|
| 430 |
t.Run("nil slice", func(t *testing.T) { |
| 431 |
var got []int |
| 432 |
testCases := []struct { |
| 433 |
name string |
| 434 |
got any |
| 435 |
}{ |
| 436 |
{"slice", got}, |
| 437 |
{"*slice", &got}, |
| 438 |
} |
| 439 |
for _, tc := range testCases { |
| 440 |
t.Run(tc.name, func(t *testing.T) { |
| 441 |
checkError(t, tc.got, |
| 442 |
td.First(td.Gt(666), "never reached"), |
| 443 |
expectedError{ |
| 444 |
Message: mustBe("item not found"), |
| 445 |
Path: mustBe("DATA"), |
| 446 |
Got: mustBe("([]int) <nil>"), |
| 447 |
Expected: mustBe(`First(> 666, "never reached")`), |
| 448 |
}) |
| 449 |
}) |
| 450 |
} |
| 451 |
}) |
| 452 |
|
| 453 |
t.Run("nil pointer", func(t *testing.T) { |
| 454 |
checkError(t, (*[]int)(nil), td.First(td.Ignore(), 33), |
| 455 |
expectedError{ |
| 456 |
Message: mustBe("nil pointer"), |
| 457 |
Path: mustBe("DATA"), |
| 458 |
Got: mustBe("nil *slice (*[]int type)"), |
| 459 |
Expected: mustBe("non-nil *slice OR *array"), |
| 460 |
}) |
| 461 |
}) |
| 462 |
|
| 463 |
t.Run("JSON", func(t *testing.T) { |
| 464 |
got := map[string]any{ |
| 465 |
"values": []int{1, 2, 3, 4}, |
| 466 |
} |
| 467 |
checkOK(t, got, td.JSON(`{"values": First(Gt(2), 3)}`)) |
| 468 |
}) |
| 469 |
|
| 470 |
t.Run("errors", func(t *testing.T) { |
| 471 |
for _, filter := range []any{nil, 33} { |
| 472 |
checkError(t, "never tested", |
| 473 |
td.First(filter, 42), |
| 474 |
expectedError{ |
| 475 |
Message: mustBe("bad usage of First operator"), |
| 476 |
Path: mustBe("DATA"), |
| 477 |
Summary: mustBe("usage: First(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must be a function or FILTER_TESTDEEP_OPERATOR a TestDeep operator"), |
| 478 |
}, |
| 479 |
"filter:", filter) |
| 480 |
} |
| 481 |
|
| 482 |
for _, filter := range []any{ |
| 483 |
func() bool { return true }, |
| 484 |
func(a, b int) bool { return true }, |
| 485 |
func(a ...int) bool { return true }, |
| 486 |
} { |
| 487 |
checkError(t, "never tested", |
| 488 |
td.First(filter, 42), |
| 489 |
expectedError{ |
| 490 |
Message: mustBe("bad usage of First operator"), |
| 491 |
Path: mustBe("DATA"), |
| 492 |
Summary: mustBe("usage: First(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must take only one non-variadic argument"), |
| 493 |
}, |
| 494 |
"filter:", filter) |
| 495 |
} |
| 496 |
|
| 497 |
for _, filter := range []any{ |
| 498 |
func(a int) {}, |
| 499 |
func(a int) int { return 0 }, |
| 500 |
func(a int) (bool, bool) { return true, true }, |
| 501 |
} { |
| 502 |
checkError(t, "never tested", |
| 503 |
td.First(filter, 42), |
| 504 |
expectedError{ |
| 505 |
Message: mustBe("bad usage of First operator"), |
| 506 |
Path: mustBe("DATA"), |
| 507 |
Summary: mustBe("usage: First(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must return bool"), |
| 508 |
}, |
| 509 |
"filter:", filter) |
| 510 |
} |
| 511 |
|
| 512 |
checkError(t, &struct{}{}, td.First(td.Ignore(), 33), |
| 513 |
expectedError{ |
| 514 |
Message: mustBe("bad kind"), |
| 515 |
Path: mustBe("DATA"), |
| 516 |
Got: mustBe("*struct (*struct {} type)"), |
| 517 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 518 |
}) |
| 519 |
|
| 520 |
checkError(t, nil, td.First(td.Ignore(), 33), |
| 521 |
expectedError{ |
| 522 |
Message: mustBe("bad kind"), |
| 523 |
Path: mustBe("DATA"), |
| 524 |
Got: mustBe("nil"), |
| 525 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 526 |
}) |
| 527 |
}) |
| 528 |
} |
| 529 |
|
| 530 |
func TestFirstString(t *testing.T) { |
| 531 |
test.EqualStr(t, |
| 532 |
td.First(func(n int) bool { return true }, 33).String(), |
| 533 |
"First(func(int) bool, 33)") |
| 534 |
|
| 535 |
test.EqualStr(t, td.First(td.Gt(0), 33).String(), "First(> 0, 33)") |
| 536 |
|
| 537 |
// Erroneous op |
| 538 |
test.EqualStr(t, td.First(42, 33).String(), "First(<ERROR>)") |
| 539 |
} |
| 540 |
|
| 541 |
func TestFirstTypeBehind(t *testing.T) { |
| 542 |
equalTypes(t, td.First(func(n int) bool { return true }, 33), []int{}) |
| 543 |
equalTypes(t, td.First(td.Gt("x"), "x"), []string{}) |
| 544 |
|
| 545 |
// Erroneous op |
| 546 |
equalTypes(t, td.First(42, 33), nil) |
| 547 |
} |
| 548 |
|
| 549 |
func TestLast(t *testing.T) { |
| 550 |
t.Run("basic", func(t *testing.T) { |
| 551 |
got := [...]int{-3, -2, -1, 0, 1, 2, 3} |
| 552 |
sgot := got[:] |
| 553 |
|
| 554 |
testCases := []struct { |
| 555 |
name string |
| 556 |
got any |
| 557 |
}{ |
| 558 |
{"slice", sgot}, |
| 559 |
{"array", got}, |
| 560 |
{"*slice", &sgot}, |
| 561 |
{"*array", &got}, |
| 562 |
} |
| 563 |
for _, tc := range testCases { |
| 564 |
t.Run(tc.name, func(t *testing.T) { |
| 565 |
checkOK(t, tc.got, td.Last(td.Lt(0), -1)) |
| 566 |
checkOK(t, tc.got, td.Last(td.Not(td.Between(1, 3)), 0)) |
| 567 |
|
| 568 |
checkOK(t, tc.got, td.Last( |
| 569 |
func(x int) bool { return (x & 1) == 0 }, |
| 570 |
2)) |
| 571 |
|
| 572 |
checkOK(t, tc.got, td.Last( |
| 573 |
func(x int64) bool { return (x & 1) != 0 }, |
| 574 |
3), |
| 575 |
"int64 filter vs int items") |
| 576 |
|
| 577 |
checkOK(t, tc.got, td.Last( |
| 578 |
func(x any) bool { return (x.(int) & 1) == 0 }, |
| 579 |
2), |
| 580 |
"any filter vs int items") |
| 581 |
|
| 582 |
checkError(t, tc.got, |
| 583 |
td.Last(td.Gt(666), "never reached"), |
| 584 |
expectedError{ |
| 585 |
Message: mustBe("item not found"), |
| 586 |
Path: mustBe("DATA"), |
| 587 |
Got: mustContain(`]int) (len=7)`), |
| 588 |
Expected: mustBe(`Last(> 666, "never reached")`), |
| 589 |
}) |
| 590 |
}) |
| 591 |
} |
| 592 |
}) |
| 593 |
|
| 594 |
t.Run("struct", func(t *testing.T) { |
| 595 |
type person struct { |
| 596 |
ID int64 |
| 597 |
Name string |
| 598 |
} |
| 599 |
got := [...]person{ |
| 600 |
{ID: 1, Name: "Joe"}, |
| 601 |
{ID: 2, Name: "Bob"}, |
| 602 |
{ID: 3, Name: "Alice"}, |
| 603 |
{ID: 4, Name: "Brian"}, |
| 604 |
{ID: 5, Name: "Britt"}, |
| 605 |
} |
| 606 |
sgot := got[:] |
| 607 |
|
| 608 |
testCases := []struct { |
| 609 |
name string |
| 610 |
got any |
| 611 |
}{ |
| 612 |
{"slice", sgot}, |
| 613 |
{"array", got}, |
| 614 |
{"*slice", &sgot}, |
| 615 |
{"*array", &got}, |
| 616 |
} |
| 617 |
for _, tc := range testCases { |
| 618 |
t.Run(tc.name, func(t *testing.T) { |
| 619 |
checkOK(t, tc.got, td.Last( |
| 620 |
td.JSONPointer("/Name", td.HasPrefix("Br")), |
| 621 |
person{ID: 5, Name: "Britt"})) |
| 622 |
|
| 623 |
checkOK(t, tc.got, td.Last( |
| 624 |
func(p person) bool { return p.ID < 3 }, |
| 625 |
person{ID: 2, Name: "Bob"})) |
| 626 |
}) |
| 627 |
} |
| 628 |
}) |
| 629 |
|
| 630 |
t.Run("interfaces", func(t *testing.T) { |
| 631 |
got := [...]any{-3, -2, -1, 0, 1, 2, 3} |
| 632 |
sgot := got[:] |
| 633 |
|
| 634 |
testCases := []struct { |
| 635 |
name string |
| 636 |
got any |
| 637 |
}{ |
| 638 |
{"slice", sgot}, |
| 639 |
{"array", got}, |
| 640 |
{"*slice", &sgot}, |
| 641 |
{"*array", &got}, |
| 642 |
} |
| 643 |
for _, tc := range testCases { |
| 644 |
t.Run(tc.name, func(t *testing.T) { |
| 645 |
checkOK(t, tc.got, td.Last(td.Lt(0), -1)) |
| 646 |
checkOK(t, tc.got, td.Last(td.Not(td.Between(1, 3)), 0)) |
| 647 |
|
| 648 |
checkOK(t, tc.got, td.Last( |
| 649 |
func(x int) bool { return (x & 1) == 0 }, |
| 650 |
2)) |
| 651 |
|
| 652 |
checkOK(t, tc.got, td.Last( |
| 653 |
func(x int64) bool { return (x & 1) != 0 }, |
| 654 |
3), |
| 655 |
"int64 filter vs any/int items") |
| 656 |
|
| 657 |
checkOK(t, tc.got, td.Last( |
| 658 |
func(x any) bool { return (x.(int) & 1) == 0 }, |
| 659 |
2), |
| 660 |
"any filter vs any/int items") |
| 661 |
}) |
| 662 |
} |
| 663 |
}) |
| 664 |
|
| 665 |
t.Run("interfaces error", func(t *testing.T) { |
| 666 |
got := [...]any{123, "foo", 456} |
| 667 |
sgot := got[:] |
| 668 |
|
| 669 |
testCases := []struct { |
| 670 |
name string |
| 671 |
got any |
| 672 |
}{ |
| 673 |
{"slice", sgot}, |
| 674 |
{"array", got}, |
| 675 |
{"*slice", &sgot}, |
| 676 |
{"*array", &got}, |
| 677 |
} |
| 678 |
for _, tc := range testCases { |
| 679 |
t.Run(tc.name, func(t *testing.T) { |
| 680 |
checkError(t, tc.got, |
| 681 |
td.Last(func(x int) bool { return false }, "never reached"), |
| 682 |
expectedError{ |
| 683 |
Message: mustBe("incompatible parameter type"), |
| 684 |
Path: mustBe("DATA[1]"), |
| 685 |
Got: mustBe("string"), |
| 686 |
Expected: mustBe("int"), |
| 687 |
}) |
| 688 |
}) |
| 689 |
} |
| 690 |
}) |
| 691 |
|
| 692 |
t.Run("nil slice", func(t *testing.T) { |
| 693 |
var got []int |
| 694 |
testCases := []struct { |
| 695 |
name string |
| 696 |
got any |
| 697 |
}{ |
| 698 |
{"slice", got}, |
| 699 |
{"*slice", &got}, |
| 700 |
} |
| 701 |
for _, tc := range testCases { |
| 702 |
t.Run(tc.name, func(t *testing.T) { |
| 703 |
checkError(t, tc.got, |
| 704 |
td.Last(td.Gt(666), "never reached"), |
| 705 |
expectedError{ |
| 706 |
Message: mustBe("item not found"), |
| 707 |
Path: mustBe("DATA"), |
| 708 |
Got: mustBe("([]int) <nil>"), |
| 709 |
Expected: mustBe(`Last(> 666, "never reached")`), |
| 710 |
}) |
| 711 |
}) |
| 712 |
} |
| 713 |
}) |
| 714 |
|
| 715 |
t.Run("nil pointer", func(t *testing.T) { |
| 716 |
checkError(t, (*[]int)(nil), td.Last(td.Ignore(), 33), |
| 717 |
expectedError{ |
| 718 |
Message: mustBe("nil pointer"), |
| 719 |
Path: mustBe("DATA"), |
| 720 |
Got: mustBe("nil *slice (*[]int type)"), |
| 721 |
Expected: mustBe("non-nil *slice OR *array"), |
| 722 |
}) |
| 723 |
}) |
| 724 |
|
| 725 |
t.Run("JSON", func(t *testing.T) { |
| 726 |
got := map[string]any{ |
| 727 |
"values": []int{1, 2, 3, 4}, |
| 728 |
} |
| 729 |
checkOK(t, got, td.JSON(`{"values": Last(Lt(3), 2)}`)) |
| 730 |
}) |
| 731 |
|
| 732 |
t.Run("errors", func(t *testing.T) { |
| 733 |
for _, filter := range []any{nil, 33} { |
| 734 |
checkError(t, "never tested", |
| 735 |
td.Last(filter, 42), |
| 736 |
expectedError{ |
| 737 |
Message: mustBe("bad usage of Last operator"), |
| 738 |
Path: mustBe("DATA"), |
| 739 |
Summary: mustBe("usage: Last(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must be a function or FILTER_TESTDEEP_OPERATOR a TestDeep operator"), |
| 740 |
}, |
| 741 |
"filter:", filter) |
| 742 |
} |
| 743 |
|
| 744 |
for _, filter := range []any{ |
| 745 |
func() bool { return true }, |
| 746 |
func(a, b int) bool { return true }, |
| 747 |
func(a ...int) bool { return true }, |
| 748 |
} { |
| 749 |
checkError(t, "never tested", |
| 750 |
td.Last(filter, 42), |
| 751 |
expectedError{ |
| 752 |
Message: mustBe("bad usage of Last operator"), |
| 753 |
Path: mustBe("DATA"), |
| 754 |
Summary: mustBe("usage: Last(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must take only one non-variadic argument"), |
| 755 |
}, |
| 756 |
"filter:", filter) |
| 757 |
} |
| 758 |
|
| 759 |
for _, filter := range []any{ |
| 760 |
func(a int) {}, |
| 761 |
func(a int) int { return 0 }, |
| 762 |
func(a int) (bool, bool) { return true, true }, |
| 763 |
} { |
| 764 |
checkError(t, "never tested", |
| 765 |
td.Last(filter, 42), |
| 766 |
expectedError{ |
| 767 |
Message: mustBe("bad usage of Last operator"), |
| 768 |
Path: mustBe("DATA"), |
| 769 |
Summary: mustBe("usage: Last(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must return bool"), |
| 770 |
}, |
| 771 |
"filter:", filter) |
| 772 |
} |
| 773 |
|
| 774 |
checkError(t, &struct{}{}, td.Last(td.Ignore(), 33), |
| 775 |
expectedError{ |
| 776 |
Message: mustBe("bad kind"), |
| 777 |
Path: mustBe("DATA"), |
| 778 |
Got: mustBe("*struct (*struct {} type)"), |
| 779 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 780 |
}) |
| 781 |
|
| 782 |
checkError(t, nil, td.Last(td.Ignore(), 33), |
| 783 |
expectedError{ |
| 784 |
Message: mustBe("bad kind"), |
| 785 |
Path: mustBe("DATA"), |
| 786 |
Got: mustBe("nil"), |
| 787 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 788 |
}) |
| 789 |
}) |
| 790 |
} |
| 791 |
|
| 792 |
func TestLastString(t *testing.T) { |
| 793 |
test.EqualStr(t, |
| 794 |
td.Last(func(n int) bool { return true }, 33).String(), |
| 795 |
"Last(func(int) bool, 33)") |
| 796 |
|
| 797 |
test.EqualStr(t, td.Last(td.Gt(0), 33).String(), "Last(> 0, 33)") |
| 798 |
|
| 799 |
// Erroneous op |
| 800 |
test.EqualStr(t, td.Last(42, 33).String(), "Last(<ERROR>)") |
| 801 |
} |
| 802 |
|
| 803 |
func TestLastTypeBehind(t *testing.T) { |
| 804 |
equalTypes(t, td.Last(func(n int) bool { return true }, 33), []int{}) |
| 805 |
equalTypes(t, td.Last(td.Gt("x"), "x"), []string{}) |
| 806 |
|
| 807 |
// Erroneous op |
| 808 |
equalTypes(t, td.Last(42, 33), nil) |
| 809 |
} |