-
Notifications
You must be signed in to change notification settings - Fork 50
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: use type constraints (generics) #255
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,13 +100,15 @@ import ( | |
|
||
// BoolOrComparison can be a bool, cmp.Comparison, or error. See Assert for | ||
// details about how this type is used. | ||
type BoolOrComparison interface{} | ||
type BoolOrComparison interface { | ||
bool | func() (bool, string) | ~func() cmp.Result | ||
} | ||
|
||
// TestingT is the subset of testing.T used by the assert package. | ||
type TestingT interface { | ||
FailNow() | ||
Fail() | ||
Log(args ...interface{}) | ||
Log(args ...any) | ||
} | ||
|
||
type helperT interface { | ||
|
@@ -138,7 +140,7 @@ type helperT interface { | |
// Assert uses t.FailNow to fail the test. Like t.FailNow, Assert must be called | ||
// from the goroutine running the test function, not from other | ||
// goroutines created during the test. Use Check from other goroutines. | ||
func Assert(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) { | ||
func Assert[C BoolOrComparison](t TestingT, comparison C, msgAndArgs ...any) { | ||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
|
@@ -152,7 +154,7 @@ func Assert(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) | |
// is successful Check returns true. Check may be called from any goroutine. | ||
// | ||
// See Assert for details about the comparison arg and failure messages. | ||
func Check(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) bool { | ||
func Check[C BoolOrComparison](t TestingT, comparison C, msgAndArgs ...any) bool { | ||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
|
@@ -169,7 +171,7 @@ func Check(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) b | |
// NilError uses t.FailNow to fail the test. Like t.FailNow, NilError must be | ||
// called from the goroutine running the test function, not from other | ||
// goroutines created during the test. Use Check from other goroutines. | ||
func NilError(t TestingT, err error, msgAndArgs ...interface{}) { | ||
func NilError(t TestingT, err error, msgAndArgs ...any) { | ||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
|
@@ -197,7 +199,7 @@ func NilError(t TestingT, err error, msgAndArgs ...interface{}) { | |
// called from the goroutine running the test function, not from other | ||
// goroutines created during the test. Use Check with cmp.Equal from other | ||
// goroutines. | ||
func Equal(t TestingT, x, y interface{}, msgAndArgs ...interface{}) { | ||
func Equal[ANY any](t TestingT, x, y ANY, msgAndArgs ...any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this one could/should be the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question! I did attempt to use |
||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
|
@@ -216,7 +218,7 @@ func Equal(t TestingT, x, y interface{}, msgAndArgs ...interface{}) { | |
// called from the goroutine running the test function, not from other | ||
// goroutines created during the test. Use Check with cmp.DeepEqual from other | ||
// goroutines. | ||
func DeepEqual(t TestingT, x, y interface{}, opts ...gocmp.Option) { | ||
func DeepEqual[ANY any](t TestingT, x, y ANY, opts ...gocmp.Option) { | ||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
|
@@ -235,7 +237,7 @@ func DeepEqual(t TestingT, x, y interface{}, opts ...gocmp.Option) { | |
// called from the goroutine running the test function, not from other | ||
// goroutines created during the test. Use Check with cmp.Error from other | ||
// goroutines. | ||
func Error(t TestingT, err error, expected string, msgAndArgs ...interface{}) { | ||
func Error(t TestingT, err error, expected string, msgAndArgs ...any) { | ||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
|
@@ -252,7 +254,7 @@ func Error(t TestingT, err error, expected string, msgAndArgs ...interface{}) { | |
// must be called from the goroutine running the test function, not from other | ||
// goroutines created during the test. Use Check with cmp.ErrorContains from other | ||
// goroutines. | ||
func ErrorContains(t TestingT, err error, substring string, msgAndArgs ...interface{}) { | ||
func ErrorContains(t TestingT, err error, substring string, msgAndArgs ...any) { | ||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
|
@@ -286,7 +288,7 @@ func ErrorContains(t TestingT, err error, substring string, msgAndArgs ...interf | |
// goroutines. | ||
// | ||
// Deprecated: Use ErrorIs | ||
func ErrorType(t TestingT, err error, expected interface{}, msgAndArgs ...interface{}) { | ||
func ErrorType(t TestingT, err error, expected any, msgAndArgs ...any) { | ||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
|
@@ -303,7 +305,7 @@ func ErrorType(t TestingT, err error, expected interface{}, msgAndArgs ...interf | |
// must be called from the goroutine running the test function, not from other | ||
// goroutines created during the test. Use Check with cmp.ErrorIs from other | ||
// goroutines. | ||
func ErrorIs(t TestingT, err error, expected error, msgAndArgs ...interface{}) { | ||
func ErrorIs(t TestingT, err error, expected error, msgAndArgs ...any) { | ||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module gotest.tools/v3 | ||
|
||
go 1.17 | ||
go 1.18 | ||
|
||
require ( | ||
github.com/google/go-cmp v0.5.9 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might need to include
error
. I believe it was previously possible to useassert.Assert(t, err)
.