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

assert: add NotZeroThenSetZero and NotNilThenSetNil #1440

Closed
wants to merge 2 commits into from

Conversation

HaraldNordgren
Copy link
Contributor

@HaraldNordgren HaraldNordgren commented Jul 30, 2023

I have used similar versions in my own code for many years. It helps to do assertions of API responses, like this:

clientResponse := someApiRequest(request)

assert.NotZeroThenSetZero(t, &clientResponse.CreateTime)
assert.NotZeroThenSetZero(t, &clientResponse.UpdateTime)

assert.Equal(t, expected, clientResponse)

Could resolve the issue brought up in #1432.

@dolmen
Copy link
Collaborator

dolmen commented Jul 30, 2023

  1. I don't understand the use case. If this API usage is not obvious, then its place is probably not the core testify.
  2. Implantation requires generics. This is a blocking for now as we still aim to support Go versions below 1.18.

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.

I do not plan to merge this.

But anyway I choose to give you feedback that might be valuable for another contribution

Also you didn't run "go generate ./.. ".

@@ -24,7 +24,6 @@ jobs:
strategy:
matrix:
go_version:
- "1.17"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't change this. This is out of scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the minimum version to be >=1.18 in discussion with Boyan Soubachov in this PR: #1379 (comment). Maybe that was a bad change?

assert/assertions.go Outdated Show resolved Hide resolved
assert/assertions.go Outdated Show resolved Hide resolved
@dolmen dolmen added enhancement pkg-assert Change related to package testify/assert generics labels Jul 30, 2023
@dolmen dolmen changed the title Create functions 'NotZeroThenSetZero' and 'NotNilThenSetNil' assert: add NotZeroThenSetZero and NotNilThenSetNil Jul 30, 2023
@HaraldNordgren
Copy link
Contributor Author

HaraldNordgren commented Jul 30, 2023

I'm sorry you feel you don't want to merge this.

Also you didn't run "go generate ./.. ".

Yes, I noticed that the cannot run now. Seems to be because of generics as well. Should I close the PR?

@dolmen
Copy link
Collaborator

dolmen commented Jul 31, 2023

From your description of the use case:

clientResponse := someApiRequest(request)

assert.NotZeroThenSetZero(t, &clientResponse.CreateTime)
assert.NotZeroThenSetZero(t, &clientResponse.UpdateTime)

assert.Equal(t, expected, clientResponse)

This can be rewritten as:

clientResponse := someApiRequest(request)

assert.NotZero(t, clientResponse.CreateTime)
assert.NotZero(t, clientResponse.UpdateTime)
var zeroTime time.Time
clientResponse.CreateTime = zeroTime
clientResponse.UpdateTime = zeroTime

assert.Equal(t, expected, clientResponse)

And I think that is would be more clear that the proposed API.

I think that if you want than feature it is best to copy your implementation in the test package where you use it. That way, the source is immediately available to the reader who may not be familiar with the full testify API. We don't need more niche features.

@dolmen dolmen closed this Jul 31, 2023
@dolmen
Copy link
Collaborator

dolmen commented Jul 31, 2023

About the code generation not working with generics code, this is a problem that we must fix before introducing any generics-based API.

@HaraldNordgren HaraldNordgren deleted the ThenSetNil branch July 31, 2023 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement generics pkg-assert Change related to package testify/assert
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants