Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
21 changes: 21 additions & 0 deletions assert/assertion_format.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions assert/assertion_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,36 @@ func IsNotType(t TestingT, theType, object interface{}, msgAndArgs ...interface{
return Fail(t, fmt.Sprintf("Object type expected to be different than %T", theType), msgAndArgs...)
}

// Kind asserts that the given object's kind matches the expected kind.
//
// assert.Kind(t, reflect.String, "Hello World")
func Kind(t TestingT, expectedKind reflect.Kind, object interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case where expectedKind == reflect.Invalid must be rejected (and added to test cases).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this.
I've updated to handle this

objectKind := reflect.TypeOf(object).Kind()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object must be checked for nil and rejected before calling reflect.TypeOf (another missing test case).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this.
I've updated to handle this

if objectKind == expectedKind {
return true
}
return Fail(t, fmt.Sprintf("Object expected to be of kind %v, but was %v", expectedKind, objectKind), msgAndArgs...)
}

// NotKind asserts that the given object's kind does not match the unexpected kind.
//
// assert.NotKind(t, reflect.Int, "Hello World")
func NotKind(t TestingT, unexpectedKind reflect.Kind, object interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issues as in Kind:

  • check for reflect.Invalid expectedKind
  • check for nil object

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thank you.

objectKind := reflect.TypeOf(object).Kind()
if objectKind != unexpectedKind {
return true
}
return Fail(t, fmt.Sprintf("Object expected NOT to be of kind %v, but was %v", unexpectedKind, objectKind), msgAndArgs...)
}

// Equal asserts that two objects are equal.
//
// assert.Equal(t, 123, 123)
Expand Down
65 changes: 65 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,71 @@ func TestNotIsType(t *testing.T) {
}
}

func TestKind(t *testing.T) {
mockT := new(testing.T)

cases := []struct {
expected reflect.Kind
object interface{}
result bool
remark string
}{
{reflect.String, "Hello World", true, "is string"},
{reflect.Int, 123, true, "is int"},
{reflect.Array, [6]int{2, 3, 5, 7, 11, 13}, true, "is array"},
{reflect.Func, Kind, true, "is func"},
{reflect.Float64, 0.0345, true, "is float64"},
{reflect.Map, make(map[string]int), true, "is map"},
{reflect.Bool, true, true, "is bool"},
{reflect.Ptr, new(int), true, "is pointer"},

// Not expected to be equal
{reflect.String, 13, false, "not string"},
{reflect.Int, [6]int{2, 3, 5, 7, 11, 13}, false, "not int"},
{reflect.Float64, 12, false, "not float64"},
{reflect.Bool, make(map[string]int), false, "not bool"},
}

for _, c := range cases {
t.Run(fmt.Sprintf("Kind(%#v, %#v)", c.expected, c.object), func(t *testing.T) {
res := Kind(mockT, c.expected, c.object)

if res != c.result {
t.Errorf("Kind(%#v, %#v) should return %#v: %s", c.expected, c.object, c.result, c.remark)
}
})
}
}

func TestNotKind(t *testing.T) {
mockT := new(testing.T)

cases := []struct {
unexpected reflect.Kind
object interface{}
result bool
remark string
}{
{reflect.String, 123, true, "not string"},
{reflect.Int, "hi", true, "not int"},
{reflect.Map, []int{1, 2}, true, "not map"},
{reflect.Ptr, 99, true, "not pointer"},

// Should fail when kinds match
{reflect.Func, func() {}, false, "is func"},
{reflect.Bool, false, false, "is bool"},
}

for _, c := range cases {
t.Run(fmt.Sprintf("NotKind(%#v, %#v)", c.unexpected, c.object), func(t *testing.T) {
res := NotKind(mockT, c.unexpected, c.object)
if res != c.result {
t.Errorf("NotKind(%#v, %#v) should return %#v: %s", c.unexpected, c.object, c.result, c.remark)
}
})
}
}

func TestEqual(t *testing.T) {
t.Parallel()

Expand Down
53 changes: 53 additions & 0 deletions require/require.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions require/require_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.