Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor assertions to use generics #1308

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

TheAndrew2115
Copy link

Summary

Changes

Motivation

Related issues

@dolmen dolmen added this to the v2.0.0 milestone Jul 11, 2023
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.

This implementation puts much more compile-time constraints on types. I expect this will break existing code.

// assert.InDelta(t, math.Pi, 22/7.0, 0.01)
func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool {
// assert.InDelta(t, math.Pi, 22/7.0, 0.01)
func InDelta[T ConvertibleToFloat64](t TestingT, expected, actual T, delta float64, msgAndArgs ...interface{}) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

InDelta should only take ~float32 | ~float64. ConvertibleToFloat64 is too wide.

return Fail(t, fmt.Sprintf("Should be zero, but was %v", i), msgAndArgs...)
}
return true
}

// NotZero asserts that i is not the zero value for its type.
func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool {
func NotZero[T any](t TestingT, i T, msgAndArgs ...interface{}) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Inconsistency with Zero where i is ìnterface{}`.

@dolmen dolmen added enhancement Breaking Change pkg-assert Change related to package testify/assert go1.19+ Change applies when running with go1.19+ generics labels Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change enhancement generics go1.19+ Change applies when running with go1.19+ pkg-assert Change related to package testify/assert
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants