(t *testing.T)
| 340 | } |
| 341 | |
| 342 | func TestValueBinder_MustCustomFunc(t *testing.T) { |
| 343 | var testCases = []struct { |
| 344 | expectValue any |
| 345 | name string |
| 346 | whenURL string |
| 347 | givenFuncErrors []error |
| 348 | expectParamValues []string |
| 349 | expectErrors []string |
| 350 | givenFailFast bool |
| 351 | }{ |
| 352 | { |
| 353 | name: "ok, binds value", |
| 354 | whenURL: "/search?nr=en&id=1&id=100", |
| 355 | expectParamValues: []string{"1", "100"}, |
| 356 | expectValue: int64(1000), |
| 357 | }, |
| 358 | { |
| 359 | name: "nok, params values empty, returns error, value is not changed", |
| 360 | whenURL: "/search?nr=en", |
| 361 | expectParamValues: []string{}, |
| 362 | expectValue: int64(99), |
| 363 | expectErrors: []string{"code=400, message=required field value is empty, field=id"}, |
| 364 | }, |
| 365 | { |
| 366 | name: "nok, previous errors fail fast without binding value", |
| 367 | givenFailFast: true, |
| 368 | whenURL: "/search?nr=en&id=1&id=100", |
| 369 | expectParamValues: []string{"1", "100"}, |
| 370 | expectValue: int64(99), |
| 371 | expectErrors: []string{"previous error"}, |
| 372 | }, |
| 373 | { |
| 374 | name: "nok, func returns errors", |
| 375 | givenFuncErrors: []error{ |
| 376 | errors.New("first error"), |
| 377 | errors.New("second error"), |
| 378 | }, |
| 379 | whenURL: "/search?nr=en&id=1&id=100", |
| 380 | expectParamValues: []string{"1", "100"}, |
| 381 | expectValue: int64(99), |
| 382 | expectErrors: []string{"first error", "second error"}, |
| 383 | }, |
| 384 | } |
| 385 | |
| 386 | for _, tc := range testCases { |
| 387 | t.Run(tc.name, func(t *testing.T) { |
| 388 | c := createTestContext(tc.whenURL, nil, nil) |
| 389 | b := QueryParamsBinder(c).FailFast(tc.givenFailFast) |
| 390 | if tc.givenFailFast { |
| 391 | b.errors = []error{errors.New("previous error")} |
| 392 | } |
| 393 | |
| 394 | id := int64(99) |
| 395 | givenCustomFunc := func(values []string) []error { |
| 396 | assert.Equal(t, tc.expectParamValues, values) |
| 397 | if tc.givenFuncErrors == nil { |
| 398 | id = 1000 // emulated conversion and setting value |
| 399 | return nil |
nothing calls this directly
no test coverage detected
searching dependent graphs…