Exception when matching on Box

83 views
Skip to first unread message

Jeppe Nejsum Madsen

unread,
Jul 8, 2011, 9:39:18 AM7/8/11
to specs2...@googlegroups.com
Hi

Trying to test some code that uses Lift's Box, I get an exception:

val x: Box[X] = ...something that returns Failure

x must beNone // Fails with exception

There should be an implicit from Box to Option. And if I do this,
everything works:

val x:Option[X] = ....

x must beNone // Works

Any hints?

/Jeppe

etorreborre

unread,
Jul 8, 2011, 9:58:32 AM7/8/11
to specs2...@googlegroups.com
I'm very surprised that the form box must beNone compiles actually.

What's the exact exception that you have?

E.

etorreborre

unread,
Jul 8, 2011, 10:25:00 AM7/8/11
to specs2...@googlegroups.com
Yes, I tried to reproduce your case on a simple example and I get a compilation error:

found   : java.lang.Object with org.specs2.matcher.Matcher[Option[Any]]
required: org.specs2.matcher.Matcher[examples.Box[Int]]
     b must beNone

There must be something else at play.

E.

Jeppe Nejsum Madsen

unread,
Jul 11, 2011, 4:32:14 AM7/11/11
to specs2...@googlegroups.com
etorreborre <etorr...@gmail.com> writes:

Seems to be something with Mockito:

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import org.specs2.mutable._
import org.specs2.mutable._

scala> import org.specs2.mock._
import org.specs2.mock._

scala> import net.liftweb.common._
import net.liftweb.common._

scala> class Spec extends Specification with Mockito {"test" should {"parse" in { val b:Box[Int] = Full(1); b must beNone}}}
defined class Spec

/Jeppe

etorreborre

unread,
Jul 11, 2011, 4:53:02 AM7/11/11
to specs2...@googlegroups.com
There's indeed an implicit conversion from the Mockito trait which is causing this issue, you can deactivate it locally like that:

   override def argThat[T](m: org.specs2.matcher.Matcher[T]): T = org.mockito.Matchers.argThat(new org.specs2.mock.HamcrestMatcherAdapter(m))

I'll think about that for a while to see how to fix this issue for the long term.

Eric.

Jeppe Nejsum Madsen

unread,
Jul 11, 2011, 6:35:54 AM7/11/11
to specs2...@googlegroups.com

For me, the best solution seems to just force conversion to Option. At
least now I know what look out for :-)

/Jeppe

etorreborre

unread,
Jul 11, 2011, 6:40:03 AM7/11/11
to specs2...@googlegroups.com
Yes you're right.

I'm actually amazed that this conversion even works because it returns a T where we expect a Matcher[T].

For example this compiles:

 val b: Box[Int] = Full(1)
 b must  argThat(beNone)

and this doesn't:

 val b: Box[Int] = Full(1)
 val m = argThat(beNone) 
 b must m

 found   : Option[Any]
 required: org.specs2.matcher.Matcher[examples.Box[Int]]
          b must m

E.

etorreborre

unread,
Jul 12, 2011, 2:04:31 AM7/12/11
to specs2...@googlegroups.com
Hi Jeppe,

After a few trials I get the impression that this is a compiler error w.r.t type inference. I asked for confirmation on the mailing-list:


Eric.

Jeppe Nejsum Madsen

unread,
Jul 12, 2011, 4:01:24 AM7/12/11
to specs2...@googlegroups.com
etorreborre <etorr...@gmail.com> writes:

Thanks for following through on this :-)

/Jeppe

etorreborre

unread,
Jul 12, 2011, 6:21:17 PM7/12/11
to specs2...@googlegroups.com
And thanks for raising the issue. It is very troubling indeed: https://groups.google.com/d/topic/scala-user/Cbpmvq09HhE/discussion.

For the time being, I'm going to raise an exception when this situation happens and provide a link to the discussion in the exception message so that the issue can be understood and fixed quickly.

Eric.

etorreborre

unread,
Jul 13, 2011, 5:14:44 AM7/13/11
to specs2...@googlegroups.com
Yay! Thanks to Florian Hars on the scala-user mailing-list I was able to publish a new snapshot where the compiler will tell you exactly what's wrong without having to deal with an exception at runtime. I feel much better. Being able to write "1 must beNone" with the Mockito trait really was enough to give me terrifying nightmares.

E.
Reply all
Reply to author
Forward
0 new messages