Commit Graph

125 Commits (f36bfe3c337aa95c86f04c721acdbafb5ffb1611)

Author SHA1 Message Date
lisitsky 2b00d33aec
Fix Call.Unset() panic (issue #1236) (#1250)
Unset changes len of a `ExpectedCalls` slice during iteration while using index from original slice, and under some conditions it tries to reslice element beyond of the slice boundaries. That causes panic.

The proposed solution uses independent write index to count elements kept in output slice.

Tests (the simplest and more complicated cases) and comments are included.
2023-01-03 20:44:42 +10:00
Stavros Ntentos 181cea6eab
impr: `CallerInfo` should print full paths to the terminal (#1201)
* impr: `CallerInfo` should print full paths to the terminal

I am proposing this simple change, which changes this output
```
--- FAIL: TestABC (0.00s)
    --- FAIL: TestABC/C (0.00s)
        /this/is/a/path/to/file_test.go:258:
            	Error Trace:	file_test.go:258
            	            				file_test.go:748
            	Error:      	Not equal:
...
```
to this:
```
--- FAIL: TestABC (0.00s)
    --- FAIL: TestABC/C (0.00s)
        /this/is/a/path/to/file_test.go:258:
            	Error Trace:	/this/is/a/path/to/file_test.go:258
            	            				/this/is/a/path/to/file_test.go:748
            	Error:      	Not equal:
...
```

With the latter output, it is much more straightforward to find the file
you are looking for, even though in the displayed case, the file is the same.

However, for VSCodium, the case is a little more helpful, since VSCodium's
terminal is smart enough to recognize the output, and make links out of that input.

Signed-off-by: Stavros Ntentos <133706+stdedos@users.noreply.github.com>

* test: fix the tests that depended on the previous behavior

Signed-off-by: Stavros Ntentos <133706+stdedos@users.noreply.github.com>

Co-authored-by: Stavros Ntentos <11300730-stavros-relex@users.noreply.gitlab.com>
2022-06-29 20:56:06 +10:00
Bracken cf1284f8dd
Allow mock expectations to be ordered (#1106)
* Allow mock expectations to be ordered

* Only say another call if it has been called before
2022-06-28 22:13:45 +10:00
Edward Raigosa b5ce165710
fixing panic in calls to assertion with nil m.mutex (#1212)
* fixing panic in calls to assertion with nil m.mutex

This reverts a change that was made in https://github.com/stretchr/testify/pull/1182
The PR makes m.mutex a pointer which now needs to be checked but it's not checked for nil everywhere.

This should also help with these issues:
- https://github.com/stretchr/testify/issues/1208
- https://github.com/stretchr/testify/issues/1210

* Revert throwing out the lock because other concurrent calls can already have it locked

* fix go vet copy lock by using pointer

* fix obj assignment for passing test
2022-06-24 10:11:59 +10:00
Bracken c206b2e823
Mock can be deadlocked by a panic (#1157)
If an argumentMatcher function panics and AssertExpectations is deferred then the test would deadlock.
2022-06-23 19:42:21 +10:00
Paul Dufour ba1076d8b3
Add .Unset method to mock (#982)
* Add .Off method to mock

* Update README.md

* Update mock.go

* Update mock_test.go

* Update README.md

* Fix tests

* Add unset test

* remove prints

* fix test

* update readme
2022-06-22 19:31:35 +10:00
Tony Abboud 48391ba5eb
Fix panic in AssertExpectations for mocks without expectations (#1207)
Co-authored-by: Tony Abboud <tabboud@palantir.com>
2022-06-21 07:08:00 +10:00
Boyan Soubachov 3c33e07c4c
Added Go 1.18.1 as a build/supported version (#1182)
* Added Go 1.18.1 as a build/supported version
Removed Go 1.15.13 as a build version as it's no longer supported

* Fix mutex passed by value for the Mock struct

* Add mutex initialisation for Mock

Co-authored-by: Boyan Soubachov <bsoubachov@atlassian.com>
2022-06-14 20:50:25 +10:00
perrydunn e209ca88af Improve mock.MatchedBy failed comparison Diff message 2021-08-24 20:26:52 +10:00
neilisaac 6241f9ab99 Add String method to Mock to fix #625 2021-04-27 21:38:32 +10:00
Bo Sunesen acba37e5db Only use repeatability if no repeatability left 2021-01-13 20:54:11 +11:00
Bo Sunesen eb8c41ec07 Add more tests to mock package 2021-01-13 20:54:11 +11:00
Bo Sunesen a5830c56d3 Extract method to evaluate closest match 2021-01-13 20:54:11 +11:00
Bo Sunesen 1962448488 Use Repeatability as tie-breaker for closest match 2021-01-13 20:54:11 +11:00
Yaroslav Kolomiiets 3bf8d0aa5e callString not to panic on nil
Fixes stretchr/testify#936.
2020-04-29 21:10:54 +10:00
sarathsp06 35d4bf5bd4 panic: add .Panic() to mock.Call
Panic mocks a function call that panics with th specfied message
2020-04-27 19:52:16 +10:00
YukinoKamei dfba5a4e3a add ) 2020-03-30 15:55:14 +11:00
Christian Muehlhaeuser 02b2656991 Handle ineffectual assignments
Removed ineffectual assignments.
2020-03-06 16:18:13 +11:00
Christian Muehlhaeuser 8bb674980e Simplify code
- Direct boolean comparisons
- Don't use Sprintf when we can use the String() function
2020-03-06 14:36:53 +11:00
Ogoyukin 9f1c28b404 The mock.IsMethodCallable function is refactored 2020-02-26 16:09:38 +11:00
Ogoyukin 3a72ffb6d8 Refactored Test_Mock_IsMethodCallable 2020-02-26 16:09:38 +11:00
Ogoyukin 6bf5d94027 Update comment 2020-02-26 16:09:38 +11:00
Ogoyukin 4a3d527660 Refactored 'isArgsEqual' 2020-02-26 16:09:38 +11:00
Ogoyukin 36e077c09a Refactored 'IsMethodCallable' 2020-02-26 16:09:38 +11:00
Ogoyukin 42aafee8d7 Added function 'IsMethodCallable', which checking that the method can be called. 2020-02-26 16:09:38 +11:00
Olaf Alders 2aadfe8adc Grammatical change in documentation for Run()
There was a switch between singular and plural which made this line a
little bit hard to parse.
2020-02-19 07:03:40 +11:00
Daniel Cormier 518a1491c7 IsType: A type-safe way of checking argument type
Like `assert.IsType(...)`, `mock.IsType` is used to check that the
type of an argument is the expected type. This is an alternative
to `AnythingOfType`.
2020-01-31 08:08:47 +11:00
xiaolei 221dbe5ed4 fix mutex 2019-07-11 18:01:36 -06:00
xiaolei 1bb3d5a619 improve find expected call 2019-07-11 18:01:36 -06:00
Tai f1df803a70 Preserve stack frame for mock parent method call
Go tip contains following commmit, that inlines function with single
call bodies.

13baf4b2cd

`(*Call).On()` is the exact target for the improvement in Go repo. Due to
the inlining, assert.CallerInfo() can't not detect the file and line
number of the call to `(*Mock).On()` from `(*Call).On()`. Thus, the test
fails.

Adding the compiler directive `go:noinline` prevent this effect and make
mock package works with go tip as before.
2019-01-03 11:36:43 -07:00
Tony Jiang 26d4a37fb4 Fix typo ("PASS" -> "FAIL")
Fixes #669
2019-01-03 10:41:29 -07:00
gz-c f35b8ab0b5 Restore type information to matched output message 2018-06-09 12:55:18 +01:00
gz-c e4944078a3 Use %v to print matched argument values 2018-06-09 12:55:18 +01:00
Eyal Posener 33951ec724 Add T object and Test method to Mock
This makes is possible to fail the test instead of panicing in case
that the method was called with unexpected arguments.

Fixes #489

mock: change field 'T' to be private field 'test'
mock_test: MockTestingT not using Mock anymore
2018-03-19 07:42:13 +00:00
Eyal Posener 20dae58180 mock_test: remove unecessary code 2018-03-18 18:59:09 +00:00
Eyal Posener 4c1331b42c Anything: remove unecessary type 2018-03-18 18:59:09 +00:00
Dinesh Kumar 921da254ef Displaying mismatch information of arguments diff while panic for closest function call. closes #556 2018-03-18 18:58:37 +00:00
Ernesto Jiménez b89eecf5ca Improve errors from mock assertions
Mark the assert helpers as helpers for Go Versions that support
`t.Helper()`
2018-03-03 14:28:11 +00:00
Jonathan ES Lin 0bfbef4e58 Modify AssertCalled and AssertNotCalled to give better error messages
Co-authored-by: Giuseppe Landolfi <giuseppe.landolfi@ricardo.ch>
Co-authored-by: Ernesto Jiménez <me@ernesto-jimenez.com>
2018-03-03 14:28:11 +00:00
Dinesh Kumar be8372ae8e Adding logging when mock assertions fails 2018-02-06 08:25:39 +00:00
Ruben de Vries 8824eb48ce Store CallerInfo when On() is called and print for missing calls during AssertExpectations. 2018-02-02 18:36:05 +00:00
Ruben de Vries 5f831d47b7 Print missing FAIL during AssertExpectation 2018-01-31 22:19:04 +00:00
Adam Medzinski 2c9035a78f Changed mock assertions text output to be non-unicode friendly 2017-12-31 12:25:00 +01:00
Dinesh Kumar b3596e9279 Fxinng After(time.Duration) to wait properly
commit 5b0291d47dc3a70cc6c6be3bba3c2f934a8e933e
Merge: 1f324ec 8ccf48a
Author: Dinesh Kumar <dineshkumar-cse@users.noreply.github.com>
Date:   Sat Dec 30 19:55:13 2017 +0530

    Merge branch 'master' into master

commit 1f324ec8cbe892a2c5364904643aa1b710121824
Author: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date:   Sat Dec 30 19:07:16 2017 +0530

    Fixing comments: reduced test time, locking in after, unexported waitTime

    - WaitUntil/After overrides each other value
    - currently if channel is set that takes the priority, if not the
      waitTime is used to Sleep

commit a7101ec14224918fc06532acae76418d1b4f04c5
Author: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date:   Fri Dec 29 13:02:14 2017 +0530

    Using if else instead of switch and pulling out the commone one

commit 936f63dd689acacfd051867c5c700489caa1dbc2
Author: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date:   Fri Dec 29 12:41:40 2017 +0530

    After - making it wait during method call

    After was using call.WaitUntil(time.After(duration)),
    <-time.After(duration) returns a channel with the timer immediately
    started
2017-12-30 17:53:34 +01:00
Joel Low cbf22d8d28 Support nil arguments to Argument Matchers 2017-12-30 17:48:55 +01:00
Maxim Kolganov 6e494f9834 put closestCall.Arguments to the expected side of the diff 2017-12-30 16:53:32 +01:00
Peter Hsu 8ccf48a064 Allow a method call to be optional 2017-12-30 15:23:32 +01:00
Dan Heller 2aa2c176b9 Fix unprotected call fields access in MethodCalled()
This change fixes a race condition I discovered when a multithreaded
test in a service I work on failed under -race. The included test case
simulates that failure (concurrent mutation of a Call with invocations
on the mock). The test will fail with a data race if run under the
race detector; the new locking ensures that call fields are not
accessed without the protection of the parent mutex.
2017-10-17 22:22:57 -07:00
Yimin Chen e964b172ca add MethodCalled to the mock
remove IsCallExpected method

add comment
2017-05-30 21:11:52 +01:00
Ernesto Jiménez 345898142b Run go vet in travis 2017-05-29 13:40:41 +01:00