Skip to content

assert: add Kind and NotKind#1803

Open
segogoreng wants to merge 6 commits intostretchr:masterfrom
segogoreng:continue_pr_1049
Open

assert: add Kind and NotKind#1803
segogoreng wants to merge 6 commits intostretchr:masterfrom
segogoreng:continue_pr_1049

Conversation

@segogoreng
Copy link

Summary

Continues work from #1049 by @matinFT.
This PR rebases with the latest master, fixes according the original PR's comments by @dolmen , and also add NotKind to adhere to the API convention.
This fixes #633

Changes

Add Kind and NotKind function in assert package.

Motivation

See #633

Related issues

Closes #633

@segogoreng segogoreng marked this pull request as draft September 29, 2025 03:50
@segogoreng segogoreng marked this pull request as ready for review September 29, 2025 03:56
@segogoreng segogoreng mentioned this pull request Sep 29, 2025
Copy link
Collaborator

@dolmen dolmen left a comment

Choose a reason for hiding this comment

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

Incomplete test suite.

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

h.Helper()
}

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 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.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @dolmen
Could you re-review, please?

@dolmen dolmen added enhancement pkg-assert Change related to package testify/assert enhancement: extend API An enhancement that grows the API surface pkg-require Change related to package testify/require labels Oct 20, 2025
@segogoreng segogoreng requested a review from dolmen December 9, 2025 10:06
fredbi added a commit to fredbi/testify that referenced this pull request Jan 11, 2026
This adapts stretchr#1803.

We have adopted slightly different design choices:
* we think checking for nil and reflect.Invalid is a legit use case
* we prefer to use [reflect.Value.Kind] over [reflect.Type.Kind] so as
  to reflect the concrete type of the object.

This way  we don't have to handle the case when [reflect.TypeOf] is nil,
which again may well be a legitimate assertion.

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement: extend API An enhancement that grows the API surface enhancement pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Method to check if return is a function

3 participants