-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Using generics #1147
Comments
I'm not sure how much value we would get from generics. I suppose some of the assertions would benefit. But I doubt any of the maintainers is going to put much effort into it any time soon. Pull requests or further discussion are both always welcome, of course! |
Testify already supports generics. Is there a specific test case you've been unable to write? |
I think the assertions would benefit from it. It reduces turn-around time if the compiler already tells you that |
I agree with @jan-xyz, sometimes it would be helpful if the compiler would prevent me to do assertions between different types. |
@brackendawson I noticed that you down voted my reply, I'd be interested to hear your reasoning for it? Do you think it's not useful and if so why? In the meantime, I played around with go generics today and implemented some assertions from testify how I imagine them to be. |
I think this feature would require releasing a Side note: For sake of "more type-safe" assertions and evaluating a slightly different design, I started experimenting here fluentassert/verify#26. I would appreciate any feedback. |
The main reason is that it would require all users of testify to be on Go 1.18 and to have >= 1.18 in their go.mod's go directive, as pellared says it would have to be a v2 release. But there are other things I'd like to see in a v2 release (like a suite package that can handle parallel subtests) that I wouldn't like to restrict to only those on 1.18, at least for now. Maybe this situation is different in a couple of years. On the actual use of generics for some assertions where testify currently uses And also on your |
Sorry for my ignorance as I'm new to Go and testify, but would support for generics allow the caveat described at https://pkg.go.dev/github.com/stretchr/testify/mock#hdr-Example_Usage to be voided? That is, would it prevent us from having to do a
|
@jordaniversen I think you're asking if Arguments[any]{"seven", 7}
[]Argument[any]{{"seven"}, {7}} So I don't think compile time type safety can be achieved here. #967 solves your problem though. |
Ah thanks for that @brackendawson. Have you had any update on this? |
As a compromise, is it possible to write generic 'more type-safe' wrappers around |
That would be possible and would give you a safer Assert for most purposes, it would have to have a different name ( |
@brackendawson by 'backward compatibility' I meant that |
I had a stab at refactoring some of the assertion APIs using generics but there are a couple of roadblocks that make me feel like this is impossible without some major changes to testify and/or Golang itself.
func Contains[K comparable, V any, T []V | map[K]V | string](t TestingT, s T, contains V, msgAndArgs ...interface{}) But now when you call arr1 := []int{1, 2, 3}
arr2 := []int{1, 2, 3}
Contains(arr1, arr2) // error message: "cannot infer K" So you would need to always call it like |
An example where having a generic a := uint(1)
assert.Equal(t, 1, a) This is equivalent to being allowed to compare e.g. uint and int but only when one value is a literal -- something which can't be achieved using reflection. |
FWIW https://github.com/shoenig/test was lifted out of the HashiCorp Nomad project as a generics based re-imagination of testify. It leverages Google's |
Is this still being considered as a potential improvement for the next version of testify? (For anyone looking for a typesafe alternative using generics, with go-cmp for equality, zero other dependencies, and a much more reasonable set of assertion methods, check out my project https://github.com/peterldowns/testy.) |
@peterldowns a v2 of testify is currently not being considered. I do recognise that there may be enhancements possible by making some of the assertions generic, but we won't be doing that in in v1. |
@brackendawson got it, makes sense. Do you know if/when v2 will be considered? The line at the top of the README about it has been there for 4 years, I remember submitting the form myself ages ago, it seems a little misleading to keep it up there with the current wording if there are no plans to work on v2. |
@peterldowns yes, agreed. I also submitted a note to that form many years ago. I don't have access to the results, I asked one of our emeritus but didn't get a reply. #1518 will remove the note. @hendrywiranto what do you think of those words? |
@brackendawson thanks Bracken, I appreciate the link to #1518. I appreciate your maintainership and I hope you and the rest of the team keep up the great work. |
The words you suggested looks good to me @brackendawson, I have applied the changes in #1518 |
Closing this one as not planned for now. |
nice work |
Are you planning to update project to use generics and also support go 1.18?
The text was updated successfully, but these errors were encountered: