Skip to content

Commit b3c251d

Browse files
committed
test: refactor string to remaining net tests
Signed-off-by: Mark Sagi-Kazar <[email protected]>
1 parent a513ebf commit b3c251d

File tree

1 file changed

+42
-124
lines changed

1 file changed

+42
-124
lines changed

decode_hooks_test.go

Lines changed: 42 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -401,32 +401,19 @@ func TestStringToTimeDurationHookFunc(t *testing.T) {
401401
}
402402

403403
func TestStringToURLHookFunc(t *testing.T) {
404-
f := StringToURLHookFunc()
405-
406404
urlSample, _ := url.Parse("http://example.com")
407-
urlValue := reflect.ValueOf(urlSample)
408-
strValue := reflect.ValueOf("http://example.com")
409-
cases := []struct {
410-
f, t reflect.Value
411-
result any
412-
err bool
413-
}{
414-
{reflect.ValueOf("http://example.com"), urlValue, urlSample, false},
415-
{reflect.ValueOf("http ://example.com"), urlValue, (*url.URL)(nil), true},
416-
{reflect.ValueOf("http://example.com"), strValue, "http://example.com", false},
417-
}
418405

419-
for i, tc := range cases {
420-
actual, err := DecodeHookExec(f, tc.f, tc.t)
421-
if tc.err != (err != nil) {
422-
t.Fatalf("case %d: expected err %#v", i, tc.err)
423-
}
424-
if !reflect.DeepEqual(actual, tc.result) {
425-
t.Fatalf(
426-
"case %d: expected %#v, got %#v",
427-
i, tc.result, actual)
428-
}
406+
suite := decodeHookTestSuite[string, *url.URL]{
407+
fn: StringToURLHookFunc(),
408+
ok: []decodeHookTestCase[string, *url.URL]{
409+
{"http://example.com", urlSample},
410+
},
411+
fail: []decodeHookFailureTestCase[string, *url.URL]{
412+
{"http ://example.com"},
413+
},
429414
}
415+
416+
suite.Run(t)
430417
}
431418

432419
func TestStringToTimeHookFunc(t *testing.T) {
@@ -461,33 +448,17 @@ func TestStringToTimeHookFunc(t *testing.T) {
461448
}
462449

463450
func TestStringToIPHookFunc(t *testing.T) {
464-
strValue := reflect.ValueOf("5")
465-
ipValue := reflect.ValueOf(net.IP{})
466-
cases := []struct {
467-
f, t reflect.Value
468-
result any
469-
err bool
470-
}{
471-
{
472-
reflect.ValueOf("1.2.3.4"), ipValue,
473-
net.IPv4(0x01, 0x02, 0x03, 0x04), false,
451+
suite := decodeHookTestSuite[string, net.IP]{
452+
fn: StringToIPHookFunc(),
453+
ok: []decodeHookTestCase[string, net.IP]{
454+
{"1.2.3.4", net.IPv4(0x01, 0x02, 0x03, 0x04)},
455+
},
456+
fail: []decodeHookFailureTestCase[string, net.IP]{
457+
{"5"},
474458
},
475-
{strValue, ipValue, net.IP{}, true},
476-
{strValue, strValue, "5", false},
477459
}
478460

479-
for i, tc := range cases {
480-
f := StringToIPHookFunc()
481-
actual, err := DecodeHookExec(f, tc.f, tc.t)
482-
if tc.err != (err != nil) {
483-
t.Fatalf("case %d: expected err %#v", i, tc.err)
484-
}
485-
if !reflect.DeepEqual(actual, tc.result) {
486-
t.Fatalf(
487-
"case %d: expected %#v, got %#v",
488-
i, tc.result, actual)
489-
}
490-
}
461+
suite.Run(t)
491462
}
492463

493464
func TestStringToIPNetHookFunc(t *testing.T) {
@@ -733,99 +704,46 @@ func TestTextUnmarshallerHookFunc(t *testing.T) {
733704
}
734705

735706
func TestStringToNetIPAddrHookFunc(t *testing.T) {
736-
strValue := reflect.ValueOf("5")
737-
addrValue := reflect.ValueOf(netip.Addr{})
738-
cases := []struct {
739-
f, t reflect.Value
740-
result any
741-
err bool
742-
}{
743-
{
744-
reflect.ValueOf("192.0.2.1"), addrValue,
745-
netip.AddrFrom4([4]byte{0xc0, 0x00, 0x02, 0x01}), false,
707+
suite := decodeHookTestSuite[string, netip.Addr]{
708+
fn: StringToNetIPAddrHookFunc(),
709+
ok: []decodeHookTestCase[string, netip.Addr]{
710+
{"192.0.2.1", netip.AddrFrom4([4]byte{0xc0, 0x00, 0x02, 0x01})},
711+
},
712+
fail: []decodeHookFailureTestCase[string, netip.Addr]{
713+
{"5"},
746714
},
747-
{strValue, addrValue, netip.Addr{}, true},
748-
{strValue, strValue, "5", false},
749715
}
750716

751-
for i, tc := range cases {
752-
f := StringToNetIPAddrHookFunc()
753-
actual, err := DecodeHookExec(f, tc.f, tc.t)
754-
if tc.err != (err != nil) {
755-
t.Fatalf("case %d: expected err %#v", i, tc.err)
756-
}
757-
if !reflect.DeepEqual(actual, tc.result) {
758-
t.Fatalf(
759-
"case %d: expected %#v, got %#v",
760-
i, tc.result, actual)
761-
}
762-
}
717+
suite.Run(t)
763718
}
764719

765720
func TestStringToNetIPAddrPortHookFunc(t *testing.T) {
766-
strValue := reflect.ValueOf("5")
767-
addrPortValue := reflect.ValueOf(netip.AddrPort{})
768-
cases := []struct {
769-
f, t reflect.Value
770-
result any
771-
err bool
772-
}{
773-
{
774-
reflect.ValueOf("192.0.2.1:80"), addrPortValue,
775-
netip.AddrPortFrom(netip.AddrFrom4([4]byte{0xc0, 0x00, 0x02, 0x01}), 80), false,
721+
suite := decodeHookTestSuite[string, netip.AddrPort]{
722+
fn: StringToNetIPAddrPortHookFunc(),
723+
ok: []decodeHookTestCase[string, netip.AddrPort]{
724+
{"192.0.2.1:80", netip.AddrPortFrom(netip.AddrFrom4([4]byte{0xc0, 0x00, 0x02, 0x01}), 80)},
725+
},
726+
fail: []decodeHookFailureTestCase[string, netip.AddrPort]{
727+
{"5"},
776728
},
777-
{strValue, addrPortValue, netip.AddrPort{}, true},
778-
{strValue, strValue, "5", false},
779729
}
780730

781-
for i, tc := range cases {
782-
f := StringToNetIPAddrPortHookFunc()
783-
actual, err := DecodeHookExec(f, tc.f, tc.t)
784-
if tc.err != (err != nil) {
785-
t.Fatalf("case %d: expected err %#v", i, tc.err)
786-
}
787-
if !reflect.DeepEqual(actual, tc.result) {
788-
t.Fatalf(
789-
"case %d: expected %#v, got %#v",
790-
i, tc.result, actual)
791-
}
792-
}
731+
suite.Run(t)
793732
}
794733

795734
func TestStringToNetIPPrefixHookFunc(t *testing.T) {
796-
strValue := reflect.ValueOf("5")
797-
prefixValue := reflect.ValueOf(netip.Prefix{})
798-
cases := []struct {
799-
f, t reflect.Value
800-
result any
801-
err bool
802-
}{
803-
{
804-
reflect.ValueOf("192.0.2.1/24"), prefixValue,
805-
netip.PrefixFrom(netip.AddrFrom4([4]byte{0xc0, 0x00, 0x02, 0x01}), 24),
806-
false,
735+
suite := decodeHookTestSuite[string, netip.Prefix]{
736+
fn: StringToNetIPPrefixHookFunc(),
737+
ok: []decodeHookTestCase[string, netip.Prefix]{
738+
{"192.0.2.1/24", netip.PrefixFrom(netip.AddrFrom4([4]byte{0xc0, 0x00, 0x02, 0x01}), 24)},
739+
{"fd7a:115c::626b:430b/118", netip.PrefixFrom(netip.AddrFrom16([16]byte{0xfd, 0x7a, 0x11, 0x5c, 12: 0x62, 0x6b, 0x43, 0x0b}), 118)},
807740
},
808-
{
809-
reflect.ValueOf("fd7a:115c::626b:430b/118"), prefixValue,
810-
netip.PrefixFrom(netip.AddrFrom16([16]byte{0xfd, 0x7a, 0x11, 0x5c, 12: 0x62, 0x6b, 0x43, 0x0b}), 118),
811-
false,
741+
fail: []decodeHookFailureTestCase[string, netip.Prefix]{
742+
{"5"},
812743
},
813-
{strValue, prefixValue, netip.Prefix{}, true},
814-
{strValue, strValue, "5", false},
815744
}
816745

817-
for i, tc := range cases {
818-
f := StringToNetIPPrefixHookFunc()
819-
actual, err := DecodeHookExec(f, tc.f, tc.t)
820-
if tc.err != (err != nil) {
821-
t.Fatalf("case %d: expected err %#v", i, tc.err)
822-
}
823-
if !reflect.DeepEqual(actual, tc.result) {
824-
t.Fatalf(
825-
"case %d:\nexpected %#v,\ngot %#v",
826-
i, tc.result, actual)
827-
}
828-
}
746+
suite.Run(t)
829747
}
830748

831749
func TestStringToBasicTypeHookFunc(t *testing.T) {

0 commit comments

Comments
 (0)