Commit Graph

74 Commits (716de8dff46ed7ae3c6ebb7a6124db741ba7c018)

Author SHA1 Message Date
sikehish 716de8dff4 Increase timeouts in Test_Mock_Called_blocks to reduce flakiness in CI 2024-10-29 00:00:56 +05:30
Bracken 85a526818c
Merge branch 'master' into master 2024-10-02 17:21:55 +01:00
spirin d62ca68bf5 tests 2024-10-01 02:21:38 +03:00
Reynier Ortiz f17409f81f mock: in order mock calls
(requested changes applied)
2024-09-25 12:03:55 -04:00
Reynier Ortiz bdb1271ed8 mock: in order mock calls 2024-09-25 11:26:01 -04:00
Bracken c719de3088
Merge pull request #1331 from ianrose14/ianrose/assertexpectations-skipped
Ensure AssertExpectations does not fail in skipped tests
2024-02-17 17:50:13 +00:00
Ian Rose 1837f62a5f Ensure AssertExpectations does not fail in skipped tests 2023-10-31 00:00:55 +01:00
Olivier Mengué c740480fc1 mock: refactor TestIsArgsEqual
Refactor tests to use 'append' to build copies of slices instead of
manual loops.

Thanks golangci-lint.
2023-07-30 20:51:12 +02:00
Oleksandr Redko 37814a1755 docs: Fix typos in tests and comments 2023-07-06 13:14:39 +03:00
Harald Nordgren 4c93d8f201
EqualExportedValues: Handle nested pointer, slice and map fields (#1379)
* 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>
2023-05-11 07:42:04 +10:00
Nisheeth Barthwal b3106d772c
allow testing for functional options (#1023)
* allow testing for functional options

* add linting docs

* use reflect magic to obtain FunctionalOption name

Co-authored-by: dillonstreator <dillonstreator@gmail.com>

---------

Co-authored-by: dillonstreator <dillonstreator@gmail.com>
2023-05-10 06:55:48 +10:00
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
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
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 eb8c41ec07 Add more tests to mock package 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
Christian Muehlhaeuser 02b2656991 Handle ineffectual assignments
Removed ineffectual assignments.
2020-03-06 16:18:13 +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 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
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 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
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
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
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
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
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
Ernesto Jiménez f712be9266 Revert "add mock.MethodCalled(methodName, args...)"
This reverts commit 17a0bd50e9.

Should avoid simply exporting an internal method.
2017-05-26 07:47:23 +01:00
Ernesto Jiménez 2b76a9702e Revert "Added goconvey style assertions to the mock package"
This reverts commit faf0710ff2.
2017-05-26 07:46:02 +01:00
Richard Knop 18cfa6871f Added extra unit test for function with mixed variadic arguments. 2017-05-25 17:38:37 -07:00
Andy McCall faf0710ff2 Added goconvey style assertions to the mock package 2017-05-25 17:05:23 -07:00
Yimin Chen 17a0bd50e9 add mock.MethodCalled(methodName, args...) 2017-05-25 17:03:17 -07:00
Ernesto Jiménez 598ab58e60 Merge pull request #171 from ANPez/master
Release lock before .WaitUntil
2016-12-17 13:32:40 -06:00
Ernesto Jiménez 92787b8c71 Fixes #342 - AssertExpectationsForObjects takes *Mock 2016-09-24 18:04:24 +01:00