## Summary
Reduces the confusion assosciated with NotSame that previously would
check nothing if any of the values is not a pointer. The changes made
were tested using TestSame, TestNotSame, and Test_samePointers tests,
and the changes did clear the tests.
## Changes
1. Modified samePointers to return another bool value(ok) that would
determine if the 2 values are of pointer type or not, while the returned
"same" bool value would determine if the 2 pointers are same.
2. Modified assert.NotSame to call Fail() if the 2 values are either
i)pointers pointing to the same address or ii)either/both of the values
are not pointers.
3. Modified assert.Same to call Fail() if the 2 values are either
i)pointers not pointing to the same address or ii)either/both of the
values are not pointers.
4. Modified Test_samePointers to handle the new behavior of samePointers
by checking both if the inputs are pointers (ok) and if they point to
the same object (same).
## Motivation
Ensure NotSame accurately verifies pointer sameness by handling
non-pointer inputs explicitly, improving clarity and reducing potential
misuse.
## Related issues
Closes#1661
---------
Co-authored-by: Bracken <abdawson@gmail.com>
The library already had assertions for `ErrorIs`, `NotErrorIs` and
`ErrorAs`. This commit adds the `NotErrorAs` assertion which is the
inverse of `ErrorAs`.
## Summary
`collect.FailNow()` should exit goroutine without a panic to be usable
with `require` package.
## Changes
`collect.FailNow()` just does `runtime.Goexit()` instead of `panic()`.
For example `FailNow()` from `testing` package [behaves
similarly](https://cs.opensource.google/go/go/+/refs/tags/go1.21.2:src/testing/testing.go;l=973).
## Motivation
I just want `require` package to be usable with `EventuallyWithT` e.g. I
want this example to pass but it panics:
```go
package main
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRequireEventuallyWithT(t *testing.T) {
state := 0
require.EventuallyWithT(t, func(c *assert.CollectT) {
defer func() { state += 1 }()
require.True(c, state == 2)
}, 100*time.Millisecond, 10*time.Millisecond)
}
```
See https://go.dev/play/p/Oqd95IT7qxQ
## Related issues
Fixes https://github.com/stretchr/testify/issues/1396
Fixes https://github.com/stretchr/testify/issues/1457
Grammar fixes in doc: _asserts that at none_ -> _asserts that none_
$ grep 'that at none' */*.go
assert/assertion_format.go:// NotErrorIsf asserts that at none of the errors in err's chain matches target.
assert/assertion_forward.go:// NotErrorIs asserts that at none of the errors in err's chain matches target.
assert/assertion_forward.go:// NotErrorIsf asserts that at none of the errors in err's chain matches target.
assert/assertions.go:// NotErrorIs asserts that at none of the errors in err's chain matches target.
require/require.go:// NotErrorIs asserts that at none of the errors in err's chain matches target.
require/require.go:// NotErrorIsf asserts that at none of the errors in err's chain matches target.
require/require_forward.go:// NotErrorIs asserts that at none of the errors in err's chain matches target.
require/require_forward.go:// NotErrorIsf asserts that at none of the errors in err's chain matches target.
The regexp package works more efficiently on bytes; if you have bytes,
it is easier to pass these directly to assert.Regexp than to convert
them to a string first.
In addition, FindIndex/FindStringIndex are unnecessary here because we
immediately throw away the result - let's just call Match() instead.
Make the YAML dependency required for {assert,require}.YAMLEq{,f} pluggable.
The implementation can be selected using build tags:
- testify_yaml_default (default): gopkg.in/yaml.v3 is used, like before
- testify_yaml_fail: YAML deserialization is not implemented and always
fails. So assert.YAMLEq always fails. This is useful if the test suite
package doesn't use assert.YAMLEq (very common case).
- testify_yaml_custom: the github.com/stretchr/testify/assert/yaml
package exposes an Unmarshal variable of type func([]byte, any) error
(same as gopkg.in/yaml.v3) that allows to plug any alternate
implementation. For example github.com/goccy/go-yaml.Unmarshal.
This allows to avoid the link constraints of the license of
gopkg.in/yaml.v3 (see PR stretchr/testify#1120).
Usage: go test -tags testify_yaml_fail
To install the alternate implementation with testify_yaml_custom:
//go:build testify_yaml_custom
package my_pkg_test
import (
goyaml "github.com/goccy/go-yaml"
"github.com/stretchr/testify/assert/yaml"
)
func init() {
yaml.Unmarshal = goyaml.Unmarshal
}
Rename CompareType to compareResult to make it private. This type is
used only in internal functions and is of no use for external users and
should never have been made public.
CompareType has been introduced by 0b4ff03cda
and appeared in releases since v1.6.0.
Note: strictly speaking, this is a breaking change, but no external user
should have ever used assert.CompareType as it made no sense in the assert
API. A search on GitHub has revealed no use of it.
https://github.com/search?q=assert.CompareType+language%3AGo&type=Repositories&ref=advsearch&l=Go&l=
The tHelper interface is defined only for internal usage. We don't need
a "hard" type for it as we don't define methods on it. Let's make is
just a alias to the anonymous interface it represents.
Note: we are already using type aliases elswhere, and type aliases were
introduced a long time ago in Go 1.9.
This reverts commit 34763e0df3.
time.Time.Equal only tests that the two instances refer to the same instant, but time.Time also carries zone information, so this caused two non-equal instances to be considered equal.
Previously, the use of %s with array objects meant you would get an
error like this:
"[%!s(int=1) %!s(int=2) %!s(int=3)]\" should have 4 item(s), but has 3
Use %v instead, which provides a much nicer error.
"[1 2 3]" should have 4 item(s), but has 3
Fixes#1482.
The underlying function ObjectsAreEqualValues did not handle
overflow/underflow of values while converting one type to another
for comparison. For example:
EqualValues(t, int(270), int8(14))
would return true, even though the values are not equal. Because, when
you convert int(270) to int8, it overflows and becomes 14 (270 % 256 = 14)
This commit fixes that by making sure that the conversion always happens
from the smaller type to the larger type, and then comparing the values.
Additionally, this commit also seperates out the test cases of
ObjectsAreEqualValues from TestObjectsAreEqual.
Fixes#1462