Skip to content
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

MatchError during compilation when stubbing value classes with generics #169

Closed
einholen opened this issue Oct 29, 2019 · 2 comments
Closed

Comments

@einholen
Copy link
Contributor

einholen commented Oct 29, 2019

Hi Bruno!

Thank you for all your effort on mockito-scala! We've been adopting it with great success... until running into this issue today.

We use value classes like case class Something[T](value: String) extends AnyVal, and it's been extremely hard to make them work so far - I either get an NPE, or an "invalid use of matchers" error, or even a MatchError exception during compilation!

Hope this is an easy pattern fix in the macro?

import org.mockito.scalatest.IdiomaticMockito
import org.scalatest.FlatSpec
import org.scalatest.Matchers

class GenericAnyValStubbingTest extends FlatSpec with Matchers with IdiomaticMockito {
  val svc = mock[AnyValsService]

  it should "stub generic AnyVals" in {
    svc.printTaggedValue(any[TaggedValue[String]]) returns "hello"

    svc.printTaggedValue(TaggedValue[String](1)) shouldBe "hello"
  }
}

trait AnyValsService {
  def printTaggedValue(value: TaggedValue[String]): String
}

case class TaggedValue[T](v: Int) extends AnyVal

And here's what I'm getting from the compiler:

[error] scala.MatchError: apply (of class scala.reflect.internal.Trees$Ident)
[error] 	at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.isClassTypeAccessible$1(RefChecks.scala:1504)
[error] 	at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.isSimpleCaseApply(RefChecks.scala:1520)
[error] 	at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1784)
[error] 	at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:107)
[error] 	at scala.reflect.internal.Trees.itransform(Trees.scala:1373)
[error] 	at scala.reflect.internal.Trees.itransform$(Trees.scala:1362)
... and many more lines
@einholen
Copy link
Contributor Author

So far the only way for me to overcome it was refrain from using idiomatic syntax for stubbing:

when(svc.printTaggedValue(TaggedValue(any))).thenReturn("hello")

...because if I try wrapping any into TaggedValue I get "invalid use of argument matchers" because the returns macro only supports a predefined set of matchers

@ultrasecreth
Copy link
Member

Version 1.7.0 should solve it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants