Skip to content

Commit efa51f8

Browse files
committed
Handle reflect invalid and nil cases
1 parent 8b7c2e0 commit efa51f8

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

assert/assertions.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,19 @@ func Kind(t TestingT, expectedKind reflect.Kind, object interface{}, msgAndArgs
495495
h.Helper()
496496
}
497497

498+
if expectedKind == reflect.Invalid {
499+
return Fail(t, "reflect.Invalid must not be used as expected kind", msgAndArgs...)
500+
}
501+
502+
if object == nil {
503+
return Fail(t, "Object must not be nil", msgAndArgs...)
504+
}
505+
498506
objectKind := reflect.TypeOf(object).Kind()
499507
if objectKind == expectedKind {
500508
return true
501509
}
510+
502511
return Fail(t, fmt.Sprintf("Object expected to be of kind %v, but was %v", expectedKind, objectKind), msgAndArgs...)
503512
}
504513

@@ -510,10 +519,19 @@ func NotKind(t TestingT, unexpectedKind reflect.Kind, object interface{}, msgAnd
510519
h.Helper()
511520
}
512521

522+
if unexpectedKind == reflect.Invalid {
523+
return Fail(t, "reflect.Invalid must not be used as unexpected kind", msgAndArgs...)
524+
}
525+
526+
if object == nil {
527+
return Fail(t, "Object must not be nil", msgAndArgs...)
528+
}
529+
513530
objectKind := reflect.TypeOf(object).Kind()
514531
if objectKind != unexpectedKind {
515532
return true
516533
}
534+
517535
return Fail(t, fmt.Sprintf("Object expected NOT to be of kind %v, but was %v", unexpectedKind, objectKind), msgAndArgs...)
518536
}
519537

assert/assertions_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ func TestKind(t *testing.T) {
594594
result bool
595595
remark string
596596
}{
597+
// True cases
597598
{reflect.String, "Hello World", true, "is string"},
598599
{reflect.Int, 123, true, "is int"},
599600
{reflect.Array, [6]int{2, 3, 5, 7, 11, 13}, true, "is array"},
@@ -603,11 +604,15 @@ func TestKind(t *testing.T) {
603604
{reflect.Bool, true, true, "is bool"},
604605
{reflect.Ptr, new(int), true, "is pointer"},
605606

606-
// Not expected to be equal
607+
// False cases
607608
{reflect.String, 13, false, "not string"},
608609
{reflect.Int, [6]int{2, 3, 5, 7, 11, 13}, false, "not int"},
609610
{reflect.Float64, 12, false, "not float64"},
610611
{reflect.Bool, make(map[string]int), false, "not bool"},
612+
613+
// Invalid inputs
614+
{reflect.Invalid, "string", false, "reflect.Invalid must not be used as expected kind"},
615+
{reflect.Ptr, nil, false, "Object must not be nil"},
611616
}
612617

613618
for _, c := range cases {
@@ -630,14 +635,19 @@ func TestNotKind(t *testing.T) {
630635
result bool
631636
remark string
632637
}{
638+
// True cases
633639
{reflect.String, 123, true, "not string"},
634640
{reflect.Int, "hi", true, "not int"},
635641
{reflect.Map, []int{1, 2}, true, "not map"},
636642
{reflect.Ptr, 99, true, "not pointer"},
637643

638-
// Should fail when kinds match
644+
// False cases
639645
{reflect.Func, func() {}, false, "is func"},
640646
{reflect.Bool, false, false, "is bool"},
647+
648+
// Invalid inputs
649+
{reflect.Invalid, "string", false, "reflect.Invalid must not be used as unexpected kind"},
650+
{reflect.Ptr, nil, false, "Object must not be nil"},
641651
}
642652

643653
for _, c := range cases {

0 commit comments

Comments
 (0)