|
6 | 6 | "math/big" |
7 | 7 | "net" |
8 | 8 | "net/netip" |
| 9 | + "net/url" |
9 | 10 | "reflect" |
10 | 11 | "strings" |
11 | 12 | "testing" |
@@ -286,6 +287,35 @@ func TestStringToTimeDurationHookFunc(t *testing.T) { |
286 | 287 | } |
287 | 288 | } |
288 | 289 |
|
| 290 | +func TestStringToURLHookFunc(t *testing.T) { |
| 291 | + f := StringToURLHookFunc() |
| 292 | + |
| 293 | + urlSample, _ := url.Parse("http://example.com") |
| 294 | + urlValue := reflect.ValueOf(urlSample) |
| 295 | + strValue := reflect.ValueOf("http://example.com") |
| 296 | + cases := []struct { |
| 297 | + f, t reflect.Value |
| 298 | + result interface{} |
| 299 | + err bool |
| 300 | + }{ |
| 301 | + {reflect.ValueOf("http://example.com"), urlValue, urlSample, false}, |
| 302 | + {reflect.ValueOf("http ://example.com"), urlValue, (*url.URL)(nil), true}, |
| 303 | + {reflect.ValueOf("http://example.com"), strValue, "http://example.com", false}, |
| 304 | + } |
| 305 | + |
| 306 | + for i, tc := range cases { |
| 307 | + actual, err := DecodeHookExec(f, tc.f, tc.t) |
| 308 | + if tc.err != (err != nil) { |
| 309 | + t.Fatalf("case %d: expected err %#v", i, tc.err) |
| 310 | + } |
| 311 | + if !reflect.DeepEqual(actual, tc.result) { |
| 312 | + t.Fatalf( |
| 313 | + "case %d: expected %#v, got %#v", |
| 314 | + i, tc.result, actual) |
| 315 | + } |
| 316 | + } |
| 317 | +} |
| 318 | + |
289 | 319 | func TestStringToTimeHookFunc(t *testing.T) { |
290 | 320 | strValue := reflect.ValueOf("5") |
291 | 321 | timeValue := reflect.ValueOf(time.Time{}) |
|
0 commit comments