(t *testing.T)
| 41 | } |
| 42 | |
| 43 | func TestPathParam(t *testing.T) { |
| 44 | var testCases = []struct { |
| 45 | name string |
| 46 | givenKey string |
| 47 | givenValue string |
| 48 | expect bool |
| 49 | expectErr string |
| 50 | }{ |
| 51 | { |
| 52 | name: "ok", |
| 53 | givenValue: "true", |
| 54 | expect: true, |
| 55 | }, |
| 56 | { |
| 57 | name: "nok, non existent key", |
| 58 | givenKey: "missing", |
| 59 | givenValue: "true", |
| 60 | expect: false, |
| 61 | expectErr: ErrNonExistentKey.Error(), |
| 62 | }, |
| 63 | { |
| 64 | name: "nok, invalid value", |
| 65 | givenValue: "can_parse_me", |
| 66 | expect: false, |
| 67 | expectErr: `code=400, message=path value, err=failed to parse value, err: strconv.ParseBool: parsing "can_parse_me": invalid syntax, field=key`, |
| 68 | }, |
| 69 | } |
| 70 | for _, tc := range testCases { |
| 71 | t.Run(tc.name, func(t *testing.T) { |
| 72 | c := NewContext(nil, nil) |
| 73 | c.SetPathValues(PathValues{{ |
| 74 | Name: cmp.Or(tc.givenKey, "key"), |
| 75 | Value: tc.givenValue, |
| 76 | }}) |
| 77 | |
| 78 | v, err := PathParam[bool](c, "key") |
| 79 | if tc.expectErr != "" { |
| 80 | assert.EqualError(t, err, tc.expectErr) |
| 81 | } else { |
| 82 | assert.NoError(t, err) |
| 83 | } |
| 84 | assert.Equal(t, tc.expect, v) |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestPathParam_UnsupportedType(t *testing.T) { |
| 90 | c := NewContext(nil, nil) |
nothing calls this directly
no test coverage detected
searching dependent graphs…