320 Commits

Author SHA1 Message Date
Olivier Mengué
6c516f8b1d assert.CallerInfo: cleanup
Move the stackFrameBufferSize const which was in package scope but used
only by CallerInfo, into CallerInfo body.
2025-06-30 17:54:06 +02:00
Olivier Mengué
5c949551ee assert.CallerInfo: micro optimization by using LastIndexByte
Use strings.LastIndexByte instead of strings.Split to extract the
function name in CallerInfo. This reduces memory allocations.
2025-06-20 10:10:22 +02:00
ccoVeille
7127b60999 assert.ErrorAs: simplify retrieving the type name 2025-06-04 20:00:40 +02:00
Olivier Mengué
acd15f6053 assert.Empty: refactor using reflect.Value.IsZero()
Simplify isEmptyValue (used by assert.Empty) by checking early if the
value is the zero value of the type using reflect.Value.IsZero
(available since Go 1.13, so after the initial assert.Empty implementation).
isEmpty is now faster.

go test -bench Benchmark_isEmpty
goos: darwin
goarch: arm64
pkg: github.com/stretchr/testify/assert
cpu: Apple M2

Before:
  Benchmark_isEmpty-8   	15841243	       77.27 ns/op	      8 B/op	      1 allocs/op
After
  Benchmark_isEmpty-8   	50665512	       21.08 ns/op	      0 B/op	      0 allocs/op
2025-06-04 12:19:48 +02:00
Olivier Mengué
890082edf2 assert.Empty: refactor isEmpty (1)
Refactor isEmpty to extract func isEmptyValue. This allows to avoid
unwrapping/wrapping when checking pointer values.
2025-06-04 12:19:48 +02:00
Olivier Mengué
c519b7942b assert.Empty: comprehensive doc of "Empty"-ness rules
Document the assert.Empty rules more comprehensively. This exposes our
quirks to the user to avoid wrong expectations.

Add many many many more test cases that document edges cases and will allow
to catch breaking changes and avoid regressions.
2025-06-03 00:11:08 +02:00
Olivier Mengué
4f71159ca8 assert.YAMLEq: shortcut if same strings
Shortcut in assert.YAMLEq once we have validated that 'expected' is
valid YAML, and 'actual' is the exact same string.
2025-05-30 16:08:10 +02:00
Olivier Mengué
4eb688ba0c assert.JSONEq: shortcut if same strings
Shortcut in assert.JSONEq once we have validated that 'expected' is
valid JSON, and 'actual' is the exact same string.
2025-05-30 16:05:08 +02:00
ccoVeille
3c1541a3b4 Improve usage of Sprintf with Same/NotSame
Co-authored-by: Olivier Mengué <dolmen@cpan.org>
2025-05-27 23:42:48 +02:00
Olivier Mengué
8f73f15d69 assert.CollectT: add Helper() method
Add Helper() method to CollectT like testing.T as we intend to add
Helper() to the assert.TestingT interface.
2025-05-25 21:56:50 +02:00
Bart Venter
9fc264e324
assert: add IsNotType (#1730)
Add the `IsNotType` assertion, which is the inverse
of the existing `IsType` assertion. It allows users to assert that an
object is not of a specific type.

Additionally, minor documentation improvements were made.

## Summary
This PR adds a new assertion function, `IsNotType`, to the
`testify/assert` package. It complements the existing `IsType` function
by providing a way to assert that an object is not of a specific type.

## Changes
* Added the `IsNotType` function to the `assert` package.
* Wrote unit tests for `IsNotType` to ensure correctness.
* Updated documentation to include examples and usage for `IsNotType`.

## Motivation
The `testify/assert` package already provides an `IsType` function to
check if an object is of a specific type. However, there was no built-in
way to assert that an object is **not** of a specific type. This PR
addresses that gap by introducing the `IsNotType` function, improving
the library's completeness and usability.

## Example usage
```go
assert.IsNotType(t, &MyStruct{}, actualObject)
```

## Related issues
_N/A_
2025-05-22 11:45:11 +02:00
ccoVeille
9bcca2f950
Format assertions files with gofumpt 2025-05-13 14:17:27 +02:00
ccoVeille
c2116b4194
Improve ErrorAs failure message when error is nil
Before:

    Should be in error chain:
    expected: *assert.customError
    in chain:

After:
    An error is expected but got nil.
    expected: *assert.customError

The message `An error is expected but got nil.` is the one already
reported by `assert.Error`
2025-05-08 20:48:57 +02:00
Bracken
111716d6f9
Merge pull request #1729 from siliconbrain/fix-subset-notsubset-mixed-type
fix Subset/NotSubset when calling with mixed input types
2025-05-08 13:42:27 +01:00
Dudás Ádám
121ddb9b0e clarify behavior of Subset/NotSubset when passing maps 2025-05-08 11:16:40 +00:00
Bracken
d338e951cf
Merge pull request #1681 from tsioftas/tsioftas/erroris-improve
Improve ErrorIs message when error is nil but an error was expected
2025-05-08 11:40:41 +01:00
Archit Agarwal
01b9a87c30
Merge branch 'master' into doc-update-for-error-fn 2025-05-07 17:26:11 +05:30
Bracken
f4244f1680
Merge pull request #1427 from cszczepaniak/cs/quicker_eventually
assert: check early in Eventually, EventuallyWithT, and Never
2025-05-06 14:28:41 +01:00
Dudás Ádám
2a9c44b1d8 fix Subset/NotSubset when calling with mixed input types (array/slice list with map subset) 2025-04-23 14:31:40 +00:00
tsioftas
e7b1880349 update error message based on suggestion 2025-04-03 14:20:14 +03:00
Archit Agarwal
500cb17e16
Merge branch 'master' into doc-update-for-error-fn 2025-03-26 00:05:52 +05:30
Bracken
c3915e850a
Merge branch 'master' into refactor/simplify-with-sprintf-q 2025-03-23 14:22:18 +01:00
Bracken
3b8bd9bf7d
Merge pull request #1614 from DevotedHealth/mauclair-call-stack-perf
Call stack perf change for CallerInfo
2025-03-22 23:13:13 +01:00
Craig Davison
c60c3bd7fb dereference target 2025-01-03 14:34:16 -07:00
Craig Davison
ccb5e7f656 Remove redundant returns 2024-12-31 15:26:24 -07:00
Craig Davison
ca6698b8a1 assert.ErrorAs: log target type 2024-12-31 15:09:40 -07:00
Mike Auclair
cfee2346d7 review feedback 2024-12-17 18:18:56 +00:00
Oleksandr Redko
30f3cef5ad Apply suggestions from code review
Co-authored-by: Olivier Mengué <dolmen@cpan.org>
2024-12-10 14:08:46 +02:00
Oleksandr Redko
d57bac8721 refactor: use %q to simplify fmt.Sprintf 2024-11-24 18:28:03 +02:00
tsioftas
7434b149f3 Improve ErrorIs message when error is nil but an error was expected 2024-11-18 11:48:33 +00:00
Archit Agarwal
2eca2b1976 update documentation for the Error function in assert or require package #1609 2024-11-03 02:19:05 +05:30
Hisham Akmal
118fb83466
NotSame should fail if args are not pointers #1661 (#1664)
## 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>
2024-10-28 11:08:04 +00:00
Bracken
8302de98b1
Merge branch 'master' into master 2024-10-26 19:41:18 +01:00
Bracken
dce9e58ee3
Merge branch 'master' into fix/equal-exported-values-accepts-everything 2024-10-23 23:37:57 +01:00
Yaroslav Brustinov
6555fd4da6 Fix issue #1662 (comparing infs should fail) 2024-10-16 17:25:23 +03:00
Pål Sivertsen
f844b269df Review: Expand NotErrorAs func docs
https://github.com/stretchr/testify/pull/1129#discussion_r1787490770
2024-10-04 12:43:45 +02:00
Pål Sivertsen
dc100b1be3 Review: Drop doc line and fix typo
Review feedback:
https://github.com/stretchr/testify/pull/1129#discussion_r1786495803
2024-10-04 12:43:45 +02:00
Pal Sivertsen
3380867632 Add NotErrorAs assertion
The library already had assertions for `ErrorIs`, `NotErrorIs` and
`ErrorAs`. This commit adds the `NotErrorAs` assertion which is the
inverse of `ErrorAs`.
2024-10-04 12:43:45 +02:00
Connor Szczepaniak
e4e93dd77c
update Never to also check the condition initially 2024-07-09 08:44:03 -05:00
cszczepaniak
cd4dc2864c
respect the timeout on the initial condition check 2024-07-09 08:31:19 -05:00
cszczepaniak
68f35d2640
return early in Eventually and EventuallyWithT 2024-07-09 08:29:08 -05:00
Mike Auclair
7f10816c93 review feedback 2024-06-25 11:50:49 +00:00
Mike Auclair
176474a4c9 cleanup 2024-06-24 20:42:26 +00:00
Mike Auclair
28e0be5092 refill stack frame buffer after it's exhausted 2024-06-24 18:11:07 +00:00
Mike Auclair
4a90eff4ae fix 2024-06-24 16:50:27 +00:00
Mike Auclair
3ca01f4bc3 Stop querying for stack frames multiple times on CallerInfo() 2024-06-24 16:39:18 +00:00
Vladimir Kochnev
b074924938
assert: collect.FailNow() should not panic (#1481)
## 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
2024-06-13 22:09:51 +02:00
Maria Ines Parnisari
84619f5c3c
fix: grammar in godoc for NotErrorIs(f) functions (#1607)
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.
2024-06-13 14:57:39 +02:00
hendry.wiranto
cb4e70cf8d review: match fail msg with params 2024-05-28 21:36:34 +07:00
hendry.wiranto
7af3ed34c2 feat: new assertion NotElementsMatch 2024-05-28 21:36:34 +07:00