MCPcopy
hub / github.com/labstack/echo / TestValueBinder_CustomFunc

Function TestValueBinder_CustomFunc

binder_test.go:267–340  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

265}
266
267func TestValueBinder_CustomFunc(t *testing.T) {
268 var testCases = []struct {
269 expectValue any
270 name string
271 whenURL string
272 givenFuncErrors []error
273 expectParamValues []string
274 expectErrors []string
275 givenFailFast bool
276 }{
277 {
278 name: "ok, binds value",
279 whenURL: "/search?nr=en&id=1&id=100",
280 expectParamValues: []string{"1", "100"},
281 expectValue: int64(1000),
282 },
283 {
284 name: "ok, params values empty, value is not changed",
285 whenURL: "/search?nr=en",
286 expectParamValues: []string{},
287 expectValue: int64(99),
288 },
289 {
290 name: "nok, previous errors fail fast without binding value",
291 givenFailFast: true,
292 whenURL: "/search?nr=en&id=1&id=100",
293 expectParamValues: []string{"1", "100"},
294 expectValue: int64(99),
295 expectErrors: []string{"previous error"},
296 },
297 {
298 name: "nok, func returns errors",
299 givenFuncErrors: []error{
300 errors.New("first error"),
301 errors.New("second error"),
302 },
303 whenURL: "/search?nr=en&id=1&id=100",
304 expectParamValues: []string{"1", "100"},
305 expectValue: int64(99),
306 expectErrors: []string{"first error", "second error"},
307 },
308 }
309
310 for _, tc := range testCases {
311 t.Run(tc.name, func(t *testing.T) {
312 c := createTestContext(tc.whenURL, nil, nil)
313 b := QueryParamsBinder(c).FailFast(tc.givenFailFast)
314 if tc.givenFailFast {
315 b.errors = []error{errors.New("previous error")}
316 }
317
318 id := int64(99)
319 givenCustomFunc := func(values []string) []error {
320 assert.Equal(t, tc.expectParamValues, values)
321 if tc.givenFuncErrors == nil {
322 id = 1000 // emulated conversion and setting value
323 return nil
324 }

Callers

nothing calls this directly

Calls 6

createTestContextFunction · 0.85
QueryParamsBinderFunction · 0.85
FailFastMethod · 0.80
BindErrorsMethod · 0.80
CustomFuncMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…