Skip to content

Avoid wrapping captor.capture into eqTo #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The most popular mocking framework for Java, now in Scala!!!
[![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/mockito-scala/)
## Why separate project?

The library has independent developers, release cycle and versioning from core mockito library (https://github.com/mockito/mockito). This is intentional because core Mockito developers don't use Scala and cannot confidently review PRs, and set the vision for the Scala library.
The library has independent developers, release cycle and versioning from core mockito library (<https://github.com/mockito/mockito>). This is intentional because core Mockito developers don't use Scala and cannot confidently review PRs, and set the vision for the Scala library.

## Dependency

Expand Down Expand Up @@ -145,7 +145,13 @@ verify(aMock).stringArgument(captor)
captor hasCaptured "it worked!"
```

As you can see there is no need to call `capture()` nor `getValue` anymore (although they're still there if you need them)
As you can see there is no need to call `capture()` nor `getValue` anymore (although they're still there if you need them as `capture` and `value` respectively)

The only scenario where you still have to call `capture` by hand is where the argument you want to capture is `Any` on the method signature, in that case the `implicit` conversion that automatically does the capture
```scala
implicit def asCapture[T](c: Captor[T]): T = c.capture
```
is not called as the compiler finds no need to convert `Captor[Any]` into `Any`, as it is already an instance of `Any`, given that `Any` is the parent of every type in Scala. Because of that, the type does not need any transformation to be passed in.

There is another constructor `ValCaptor[T]` that should be used to capture value classes
**NOTE: Since version 1.0.2 `ValCaptor[T]` has been deprecated as `ArgCaptor[T]` now support both, standard and value classes**
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/scala/user/org/mockito/IdiomaticMockitoTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,21 @@ class IdiomaticMockitoTest extends WordSpec with Matchers with IdiomaticMockito
}
}

"work with a captor when calling capture explicitly" in {
val org = orgDouble()
val argCaptor = ArgCaptor[Int]

org.doSomethingWithThisIntAndString(42, "test")

org.doSomethingWithThisIntAndString(argCaptor.capture, "test") was called

argCaptor hasCaptured 42

an[ArgumentsAreDifferent] should be thrownBy {
argCaptor hasCaptured 43
}
}

"check invocation order" in {
val mock1 = orgDouble()
val mock2 = mock[Bar]
Expand Down
1 change: 1 addition & 0 deletions macro/src/main/scala/org/mockito/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ object Utils {
case q"$_.n.=~[$_]($_)" => true

case q"$_.Captor.asCapture[$_]($_)" => true
case q"$_.capture" => true

case q"($_.MacroMatchers_211.eqTo[$_](...$_): $_)" => true
case q"($_($_.MacroMatchers_211.eqTo[$_](...$_)): $_)" => true
Expand Down