Commit Graph

195 Commits (master)

Author SHA1 Message Date
Oladapo Ajala 66eef0ef3a
fix: assert.MapSubset (or just support maps in assert.Subset) (#1178)
* WIP: added map key value check in subset

* upgraded subset & notsubset to check handle maps
2022-06-28 21:24:53 +10:00
Ruan Moolman 2fab6dffcf
Add WithinTimeRange method (#1188)
* Add WithinTimeRange method

* Run ./.ci.generate.sh

* Rename WithinTimeRange to WithinRange

* Rename WithinRange expected parameter to actual

* Capitalise start parameter at start of error message

* Improve WithinRange example
2022-06-28 21:14:39 +10:00
Adam Luzsi 840cb80149
arrays value types in a zero-initialized state are considered empty (#1126)
* fix .Empty assertion with Array types

* refactor .Empty assertion for array types
2022-06-20 20:44:27 +10:00
RmbRT 083ff1c044 Fixed didPanic to now detect panic(nil).
Previously, the function would not detect panic(nil) calls.
In didPanic, removed the anonymous function call, instead,
added named return values. Added extra test cases for the
panic(nil) call.
2022-03-16 07:08:43 +10:00
Menno ab6dc32628 fix linting errors in /assert package 2021-08-24 21:55:23 +10:00
Menno edff5a049b fix funtion name 2021-08-24 21:55:23 +10:00
Menno 5c61ef97ae fix potential nil-pointer dereference
the fix gracefully fails the assertion instead of panicking unresolved.
2021-08-24 21:55:23 +10:00
Menno fd9e1fb0e1 Fix NaN similarity checks
Plus minor alignments in code.
2021-07-09 20:35:46 +10:00
Harald Nordgren dc5c261377 Make sure time.Time comparison produces a helpful diff. closes #989 2021-04-27 21:16:43 +10:00
Alun Evans 6990a05d54 Add ErrorContains 2021-01-16 12:32:05 +11:00
Boyan ed4976c764 Revert "Fix PR comments"
This reverts commit 1ebd9c5791.
2020-07-27 22:28:46 +10:00
arseny 1ebd9c5791 Fix PR comments 2020-07-27 21:16:21 +10:00
Pal Sivertsen 95a9d909e9 Add wrapper functions for errors Is and As funcs
This commit adds wrapper functions for Is and As functions from the
errors package.
2020-07-20 20:40:44 +10:00
Ivo van der Wijk b8f7d52a4a Rewrite test loops into individual test cases 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 8a501b0fac make testcase definition local 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 4bbffeac6c table-ify TestElementsMatch 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 52b38ca424 table-ify, refactor (Not)Subset() 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 0929293466 refactor, table-ify TestContains / TestNotContains 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 67a4d91853 table-ify ObjectsAreEqual() 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 5717c498e9 table-ify Equal() 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 408bc6703a keep type definition local to function where used 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 590942c47f table-ify Exactly 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 9ffb85bbec clarify sets of tests 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 51b7cfe385 rewrite NotEqual() tests to table tests 2020-06-13 21:33:52 +10:00
Ivo van der Wijk 07d1e00890 first attempt at table-ifying NotEqualValues tests 2020-06-13 21:33:52 +10:00
Ivo van der Wijk f50e178a9f first attempt at table-ifying NotEqualValues tests 2020-06-13 21:33:52 +10:00
Ivo van der Wijk ac1463f956 Implement NotEqualValues 2020-05-27 08:39:56 +10:00
Bryan Boreham f7ef284eb4 Add a benchmark to time trivial call of NotNil 2020-05-04 21:01:24 +10:00
David Wragg ca8e08c131 Support uint in InDelta and InEpsilon
InDelta and InEpsilon assertions on uint values would fail with
the error "Parameters must be numerical".
2020-04-29 21:29:26 +10:00
hectorj 012967472b Make InEpsilon fail when given a NaN
fixes #918
2020-04-02 22:34:25 +11:00
Martin Sucha f6cbfc0d03 Print more details in ElementsMatch
It is not very helpful to print that the lengths differ when an
assertion fails, since that does not reveal what the cause of the issue
might be.

Let's print which elements are extra in each list, that should convey
the relevant information to the user. Also use spew to format the
objects, similar to what Equal does, to make the output more readable.
2020-03-14 21:17:20 +11:00
Henry Blyth cb23521296 Test verbose Contains Fail message
If asserting an error contained in a string, includeElement will fail
but Contains will confusingly print both values as strings, which can
look like a testify problem instead of an assertion failure.
2020-03-10 08:59:40 +11:00
Christian Muehlhaeuser 1c7f4ef084 Avoid unnecessary conversions
No need to convert values here.
2020-03-06 14:40:56 +11:00
Christian Muehlhaeuser 08b5acc756 Fixed tautological errors
err can't ever be not nil here, unless the Go language specs are changing.
2020-03-06 14:38:03 +11:00
Christian Muehlhaeuser fbbf8a0782 Removed unused variables
Probably just became obsolete by now.
2020-03-06 14:31:21 +11:00
Christian Muehlhaeuser c5d499e514 Simplify go code
Just reads a bit nicer, in my opinion.
2020-03-06 14:30:34 +11:00
Hiram Chirino 310548cda6 fix: error diff was not being show for large values
If you were comparing values which when formatted were longer than about 64k then the actual, expected and diff messages were not printed to the console.
2020-03-04 06:47:27 +11:00
Luan Santos afd4130c14 Disable using Stringer/error interfaces for diffing
`spew`'s default formatter will call `String()` or `Error()` in structs
that implement the `fmt.Stringer` or `error` interfaces. Depending on
the implementation of those, the diff can become quite useless to read
(see the example struct I used for the test case in this commit).

This changes `spew`'s configuration to `DisableMethods` so that it will
always use it's own pretty printer. This makes testing structs less
surprising and generally more useful, without tying the tests to the
implementation of `String()` (the user here can always chose to
`require.Equal(a.String(), b.String())` if testing those is important to
them.
2020-02-26 08:50:07 -07:00
Torkel Rogstad 17a1e1d4bf Add Never assertion
Never asserts that a conditon isn't met within a given timeframe.
2020-01-29 09:11:59 +11:00
Dinesh Kumar 8c465a0c8e changing time.Duration equality mismatch output from int64 to readable format. Fixes #626
(cherry picked from commit 637cd144ddae7a3792bcb5c74a3bf3a071c0a250)
2020-01-16 14:34:20 +11:00
Boyan Soubachov 858f37ff9b Allow nil-function Equal comparisons 2019-12-23 07:34:01 -07:00
Boyan Soubachov 28b40b159e Improve tests 2019-12-12 12:41:57 -07:00
Boyan Soubachov 4b71b28738 WIP 2019-12-12 12:41:57 -07:00
Boyan Soubachov 7b3a490010 Rename 2019-12-12 12:41:57 -07:00
Boyan Soubachov 937e12391f Added negative dir and file tests
* Added NotFileExists test
* Added NotDirExists test
* Cleaned-up some comment formatting
2019-12-12 12:41:57 -07:00
Leigh McCulloch 367102ea5a Add PanicsWithError
# What
Add `PanicsWithError` that behaves like `PanicsWithValue` but requires
the value to be an `error` and compares an error string against the
error in the same fashion as `EqualError`.

# Why
`PanicsWithValue` is very useful with the value that will panic is a
basic type that can be easily defined in a test, but is less useful for
validating an error because error values may not be easily recreated in
a test to exactly match the error that will be generated in the moment.
This can be because an error will contain fields that aren't exported
and the test may not be able to define the exact error. Most of the time
testing error objects just testing the string is sufficient. This
concept is already supported by this module in the existing `EqualError`
function.
2019-12-10 08:32:02 -07:00
Jacek Szwec f1bd0923b8 Fix panic for Eventually functions
Fixes #805, Fixes #835
2019-11-06 15:43:47 -07:00
Boyan a88bf7aab8 PR comments 2019-11-04 07:28:29 -07:00
Boyan cabedb3967 Refactor 2019-11-04 07:28:29 -07:00
Boyan 4d8751d477 Unit test typo fix 2019-11-04 07:28:29 -07:00
Boyan 7088056203 Add NotSame
* Added NotSame test for the assert package
* Added NotSame test for the require package
* Included formatted variants of NotSame for both assert and require
2019-11-04 07:28:29 -07:00
Daniel Cormier 85f2b59c44 Add panic stack to output from NotPanics assertion failures 2019-09-04 10:35:30 -06:00
Nobuhiro MIKI 0224ef258e Add YAMLEq to assert that two YAML strings are equivalent 2019-07-11 17:50:32 -06:00
Grzegorz Miejski d84e815d44 Introduce Eventually assertion. 2019-07-09 13:57:54 -06:00
Gavin Cabbage 21cb1c2932 add ptr comparison func 'Same' to assert pkg 2019-02-28 14:48:09 -07:00
Viswajith Venugopal 5b93e2dc01 Run go fmt 2018-12-12 10:17:45 -08:00
Viswajith Venugopal 6697e04e8d Support arbitrary objects in msgAndArgs 2018-12-12 10:17:45 -08:00
Harald Nordgren f2347ac6c9 Allow assert.Equal on string type alias without panicking on failure 2018-10-02 17:26:21 -06:00
timfeirg 2a15e200fd support diff display for string variables 2018-06-09 11:55:26 +01:00
Cory Bennett c679ae2cc0 add function aliases for common assert and requires function signatures to make table driven tests easier 2018-03-19 22:34:59 +00:00
Ernesto Jiménez a726187e31 Fix vim-go integration and other editors
Use Go 1.9 t.Helper() to remove testify from the output of the tests and
stop using `\r` to try to overwrite the output.

This means in Go 1.7 and Go 1.8 testify will appear as failing the test.
2018-02-02 18:39:40 +00:00
Ernesto Jiménez b57b6d191c Add FileExists and DirExists assertions
Implement FileExists and DirExists assertions to check whether file/dir
exist in a given path.

Closes #428
2017-12-30 18:26:00 +01:00
Adam Medzinski 42baa3e5a4 Nil check in Implements assertion 2017-12-30 17:32:04 +01:00
Emil Stanchev 51464dae67 Consider empty/nil arrays as matching elements 2017-12-30 16:33:25 +01:00
Emil Stanchev bf57a5dedc ElementsMatch array/slice assertion ignoring order
An assertion that compares the elements of the slices/arrays disregarding the order,
i.e. it checks whether each element in the first slice/array appears the same number of times in it
as in the second slice/array.

This name seemed like it would be easy to find.

Possible alternatives for the name:
- ContainsSameElements
- IsPermutation (C++: http://en.cppreference.com/w/cpp/algorithm/is_permutation)
- MatchArray (rspec: http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array)
- EqualSorted
- Other ideas?

This implementaiton is O(N^2), while sorting both lists first would be O(nlogn).
However, this one doesn't need to copy the lists, so it is simpler and doesn't require additional
memory and time for the copies.

I realize this was deemed as out of scope
https://github.com/stretchr/testify/issues/275
but I decided to give it a shot as I needed it also.
2017-12-30 16:33:25 +01:00
Emil Stanchev 9fb9de17de Make NotSubset actually fail the test on nil subset
The assertion function was simply returning false, which doesn't actually fail a test.
An example test that should have failed but doesn't:

  func TestNotSubset(t *testing.T) {
      assert.NotSubset(t, []string{"x"}, nil)
  }
2017-12-30 16:07:04 +01:00
odog c0f1d443b4 indent actual value for better comparison with expected value 2017-12-30 14:43:45 +01:00
Jonathan Jin 249123e03a Implement delta comparison for map values 2017-12-30 14:26:37 +01:00
Vincent Cote-Roy 88a414d072 generalize Empty assertion
Make `Empty` work against any struct and custom types, by replacing
explicit zero value comparisons with a `DeepEqual` comparison with
the type's `reflect.ZeroValue`.
2017-12-30 13:03:09 +01:00
Joshua T Corbin 2f1cd6b778 time.Duraions are numbers too 2017-07-14 14:27:08 -07:00
Tom Artale f6abca5936 Added assert.PanicsWithValue + tests 2017-06-01 22:03:22 +01:00
Adam Medzinski 46b3c8225b Simple validation of Equal/NotEqual assertion arguments 2017-05-29 17:57:31 +01:00
Ernesto Jiménez c7668ea997 Fixes #339 - Add `assertion`f assertions like Errorf and Equalf 2017-05-29 12:56:43 +01:00
Ernesto Jiménez c33f336b92 Fix vet warnings and go generate to update docs 2017-05-29 12:56:43 +01:00
decauwsemaecker.glen@gmail.com 3a59a58abc
add Subset and NotSubset assertions 2017-05-27 17:24:18 -05:00
Cameron Moore 09f61d78b8 assert: fix error reporting when error contains escape sequences
Fixes #325
2017-05-25 17:28:59 -07:00
havnesvo bc11a6e4dd Tighten language by increasing overall consistency in wording in texts and argument names: use 'actual' instead of 'received' 2017-05-25 17:16:55 -07:00
Denys Smirnov 97c0e43cd5 compare bytes with bytes.Equal instead of reflect.DeepEqual 2017-05-25 17:04:05 -07:00
Ernesto Jiménez 332ae0e18f Add Equal test comparing nil with non-nil 2017-01-30 11:25:29 +00:00
Ernesto Jiménez 2402e8e7a0 Merge pull request #364 from nmiyake/fixFailOutput
Ensure that assert.Fail properly align its output
2016-12-17 14:04:45 -06:00
Ernesto Jiménez ae4c58e530 Merge pull request #367 from packrat386/rm_numeric_check
Remove isNumericType check
2016-12-17 13:26:48 -06:00
Nick Miyake 3928f579ee Add comments for Equal and NotEqual to clarify pointer comparison
Clarify that pointer equality is determined by equality of values
rather than memory address.

Fixes #358
2016-11-19 23:31:18 -08:00
Aidan Coyle 6835870125 Remove isNumericType check
I'm not sure why this check is needed. It seems worthwhile to print the
type any time the types aren't equal. Closes #366.
2016-11-17 10:29:04 -06:00
Nick Miyake ddb91ee140 Ensure that assert.Fail properly align its output
Previous implementation depended on tab alignment. This worked
when the output was not prepended with anything, but would break
if the output was prepended with further spaces (which can occur
in environments like IntelliJ test runners). This commit fixes it
so that the output is always aligned logically.

Fixes #83
2016-10-28 19:36:52 -07:00
Ernesto Jiménez 69483b4bd1 Merge pull request #327 from jveski/issue-155
Clarify assert.Equal failure message given mismatched numeric types
2016-09-25 02:54:16 +01:00
Jordan Olshevski d2b5f58808 Add formatUnequalValues struct test coverage
This expands the TestFormatUnequalValues test case to cover the behavior
of the function when provided two struct type'd values.
2016-09-24 17:22:54 -07:00
Michael Broll 3656ffd69b write test to simulate parallel execution of diff() 2016-09-06 17:15:19 -07:00
Jordan Olshevski 3bfb674961 Print types in failed numeric equality assertions
This addresses vague and misleading output when asserting on the
equality of two values of differing numeric types.

Example: assert.Equal(t, int64(123), int32(123))

Previously, the user would have received a vague message claiming that
123 != 123. Now the message will read "int64(123) != int32(123)".
2016-07-06 18:38:55 -07:00
Ernesto Jiménez d77da356e5 Fixes #311 - Detect empty interface error gotcha 2016-06-15 11:28:44 +02:00
Ernesto Jiménez 0f6478d3a8 Fixes #257 - InEpsilon works when actual == 0
Implemented new InEpsilon by calculating the relative error and
comparing it to the expected epsilon rather than calculating the
acceptable margin and using InDelta.

While doing so we got rid of the false failure when the actual value
was zero.
2016-01-09 17:37:37 +01:00
Ernesto Jiménez 3dad6b7b59 Add tests for workaround solution to #263 2016-01-09 02:56:16 +01:00
Cyrill Schumacher cd2b6c1151 Add check for empty time.Time struct (non pointer)
The standard library returns always a non-pointer Time type.
2016-01-07 07:55:48 +01:00
Ernesto Jiménez 67106a5111 Sort map keys before diffing maps 2015-11-11 20:37:34 +01:00
Matt Joiner 7d6ace9113 Handle case where one object is nil and the other is not 2015-11-07 19:02:25 +11:00
Ernesto Jiménez a76410419d Merge pull request #232 from stretchr/issue-136
Fix #136 - Empty returns true for nil pointers
2015-11-07 04:46:19 +00:00
Ernesto Jiménez 33c4c93911 Fixes #229 - show diffs for structs/maps/arrays/slices when Equal fails 2015-11-03 01:32:38 +00:00
Ernesto Jiménez c478a808a1 Fix #136 2015-11-02 01:41:59 +00:00
Ernesto Jiménez 40b02b9eef Merge pull request #165 from tuvistavie/master
Support map keys for Contains/NotContains assertion.
2015-11-01 21:02:28 +00:00
Ernesto Jiménez a0534acc7b Merge pull request #217 from juliancooper/assert-equivalent-json
Added assertion/requirement that checks if two JSON strings represent equivalent objects
2015-11-01 20:45:50 +00:00
Julian Cooper 7f6cb13c5c split JSONEq tests into individual assertions from one monolithic test 2015-10-13 13:36:47 -07:00