Skip to content

Commit

Permalink
improve assertion function signature
Browse files Browse the repository at this point in the history
Based on community feedback in `testify`, it was observed that users often misuse assertion functions.
Specifically, they mistakenly use an assertion function designed for a single parameter with two parameters, as shown below:

	assert.Error(t, expectedErr, actualErr, "bla bla bla")

This leads to issues because the third argument for these functions is not meant for comparison but is a variadic assertion message argument.
As a result, the `expectedErr` is checked if it's an error (which it is), and `actualErr` is treated as a failure assertion message.

To address this, the function's signature has been modified to accept a fixed string-based type.
This change ensures that such mistakes are caught during compilation due to type mismatches, making it harder for users to misuse the function.
  • Loading branch information
adamluzsi committed Aug 26, 2023
1 parent 8f97729 commit afba958
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 154 deletions.
2 changes: 1 addition & 1 deletion Race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestRace(t *testing.T) {
it.Must.Equal(int32(4), total)
it.Log(`counter:`, counter, `total:`, total)
it.Must.True(counter < total,
fmt.Sprintf(`counter was expected to be less that the total block run during race`))
assert.Message(fmt.Sprintf(`counter was expected to be less that the total block run during race`)))
})
})

Expand Down
6 changes: 3 additions & 3 deletions assert/AnyOf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// OneOf function checks a list of values and matches an expectation against each element of the list.
// If any of the elements pass the assertion, then the assertion helper function does not fail the test.
func OneOf[V any](tb testing.TB, vs []V, blk func(it It, got V), msg ...any) {
func OneOf[V any](tb testing.TB, vs []V, blk func(it It, got V), msg ...Message) {
tb.Helper()
Must(tb).AnyOf(func(a *AnyOf) {
a.name = "OneOf"
Expand Down Expand Up @@ -71,7 +71,7 @@ func (ao *AnyOf) Test(blk func(t It)) {
}

// Finish will check if any of the assertion succeeded.
func (ao *AnyOf) Finish(msg ...interface{}) {
func (ao *AnyOf) Finish(msg ...Message) {
ao.TB.Helper()
if ao.OK() {
return
Expand All @@ -89,7 +89,7 @@ func (ao *AnyOf) Finish(msg ...interface{}) {
}
return "None of the .Test succeeded"
}(),
Message: msg,
Message: toMsg(msg),
Values: nil,
})
ao.Fail()
Expand Down
2 changes: 1 addition & 1 deletion assert/AnyOf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestAnyOf(t *testing.T) {

s.Then(`AnyOf yields failure on .Finish`, func(t *testcase.T) {
anyOf.Get(t).Finish()
t.Must.True(true, stub.Get(t).IsFailed)
t.Must.True(stub.Get(t).IsFailed)
})

s.Then("AnyOf.OK will yield false due to no passing test", func(t *testcase.T) {
Expand Down
Loading

0 comments on commit afba958

Please sign in to comment.