## 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
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.
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
1. Isolate tests that use the "unsafe" package in a separate package
assert/internal/unsafetests. That way the assert package is not
tainted with unsafe.
2. Remove one reference to the private assert.isNil() in assert tests.
3. Add more tests of assert.Nil and assert.NotNil with unsafe.Pointer.
Fix TestEventuallyIssue805 which was flaky because of the use of a timer
that has exactly the same duration as the Eventually timout. But time is
not a synchronization primitive.
Now we use channels to ensure that the condition is still running and
Eventually times out before checking its return value.
The test is also renamed to TestEventuallyTimeout to more clearly show
its purpose.
Reverse order of return values of internal function getLen() to be more
consistent with stdlib (ex: os.LookupEnv).
Old: func (any) (ok bool, length int)
New: func (any) (length int, ok bool)
* EqualExportedValues: Handle pointer and slice fields
* Update assert/assertions.go
Co-authored-by: Michael Pu <michael.pu123@gmail.com>
* Reduce redundant calls to 'copyExportedFields'
* Update comments
* Add support for maps
* Update Go version support to 1.19 and onward
* Re-generate after rebasing
---------
Co-authored-by: Michael Pu <michael.pu123@gmail.com>
* add EventuallyWithT assertion
* Change "EventuallyWithT" condition acceptance to no-errors raised
This change updates the "EventuallyWithT" assertion variants (regular, formatted,
requirement) to consider a condition as "met" if no assertion errors were raised
in a tick.
This allows to write easier conditions which simply contain assertions, without
needing to return a bool. The equivalent of a condition returning true in the
previous implementation would be a a condition with a single "assert.True(..)" call.
* Declare the "Collect.Copy(T)" method as a testing helper
* run go generate
---------
Co-authored-by: Arik Kfir <arik@kfirs.com>
* assert: rename and refactor TestContainsFailMessage
I've renamed it to TestContainsNotContains and added a test case
structure so that I can use this test to validate the failure messages
from both Contains and NotContains,
There are no functional changes here, strictly refactoring. A new test
case will be added in a subsequent change.
* assert: fix error message formatting for NotContains
It was using "%s" to format the s and contains arguments, but these
could be any type that supports iteration such as a map. In this case
of NotContains failing against a map, it would print something like:
"map[one:%!s(int=1)]" should not contain "one"
Fix this using "%#v" as was already done for Contains and added test
cases covering both the "len()" / iterable failure messages as well as
the Contains/NotContains failure case.
The new message for this example map would be something like:
map[string]int{"one":1} should not contain "one"
* Implement checking only exported fields
Co-authored-by: Anthony Chang <anthony-chang@users.noreply.github.com>
* Update comment
* Run go generate
* Make compatiable with Go 1.16.5
* Fix go generate files
* Fix white space changes
* Fix whitespace changes
* Fix whitespace changes in gogenerate files
---------
Co-authored-by: Anthony Chang <anthony-chang@users.noreply.github.com>
`MapIndex` returns a zero `reflect.Value` if the map value does not
exist. This now aligns more closely with `InDeltaMapValues` by
checking `reflect/Value.IsValid`.
The use of `recover()` was hiding this error by setting the result of
the test to be "false" despite the test suite passing. This led to
flapping tests where they would succeed if the panic occurred on a
missing key before comparing key values that didn't match!
I've ensured the test suite now asserts on the expected error message
and added another example where the subset has keys not found on the
map under test.