Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,8 @@ func isEmptyValue(objValue reflect.Value) bool {
// Note: array types are empty when they match their zero-initialized state.
case reflect.Chan, reflect.Map, reflect.Slice:
return objValue.Len() == 0
// non-nil pointers are empty if the value they point to is empty
case reflect.Ptr:
// non-nil pointers and interfaces are empty if the value they hold is empty
case reflect.Ptr, reflect.Interface:
return isEmptyValue(objValue.Elem())
}
return false
Expand Down
44 changes: 44 additions & 0 deletions assert/issue1955_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package assert

import (
"testing"
)

type issue1955Shape interface{ Area() int }

type issue1955Square struct{ Side int }

func (s *issue1955Square) Area() int { return s.Side * s.Side }

func TestIssue1955EmptyPointerToInterface(t *testing.T) {
var s issue1955Shape = &issue1955Square{}
var nilShape issue1955Shape
var nonEmpty issue1955Shape = &issue1955Square{Side: 2}

tests := []struct {
name string
value interface{}
empty bool
}{
{"unwrapped value", issue1955Square{}, true},
{"pointer to value", &issue1955Square{}, true},
{"interface", s, true},
{"pointer to interface holding empty value", &s, true},
{"pointer to empty interface", &nilShape, true},
{"pointer to interface holding non-empty value", &nonEmpty, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockT := new(testing.T)
got := Empty(mockT, tt.value)
if got != tt.empty {
t.Errorf("Empty(%T) = %v, want %v", tt.value, got, tt.empty)
}
notGot := NotEmpty(mockT, tt.value)
if notGot == tt.empty {
t.Errorf("NotEmpty(%T) = %v, want %v", tt.value, notGot, !tt.empty)
}
})
}
}