Specs 2 and mocking partial functions

521 views
Skip to first unread message

Thomas Sant'ana

unread,
Jul 4, 2011, 8:11:33 AM7/4/11
to specs2...@googlegroups.com
Hi,

    I tried to no avail to mock a PartialFunction. The isDefinedAt works fine. But the apply does not work. Is there something special I should do?

Thomas

etorreborre

unread,
Jul 4, 2011, 9:09:34 PM7/4/11
to specs2...@googlegroups.com
Hi Thomas,

Could you please post some code?

Is it along the lines of what you tried to do before: https://groups.google.com/d/topic/specs2-users/R3DS_ZPe29w/discussion?

E.

Thomas Sant'ana

unread,
Jul 5, 2011, 11:44:41 AM7/5/11
to specs2...@googlegroups.com
This is a subset of the test that I can make work:

import org.specs2.SpecificationWithJUnit
import org.specs2.mock.Mockito

class PartialFunctionCommandStream[A, S](pf: PartialFunction[S, A]) {

  def next(s: S): (A, PartialFunctionCommandStream[A, S]) = {
    if (pf.isDefinedAt(s)) (pf.apply(s), this)
    else throw new NoSuchElementException("CommandStream not defined at " + s)
  }

  def hasNext(s: S): Boolean = pf.isDefinedAt(s)
}


class PFTest extends SpecificationWithJUnit with Mockito {

   def is =
   "Must have next " ! e2 ^
   "Must mock a PF " ! e1 ^
   end
  
   def e1 = {
      val pf = mock[PartialFunction[Int,Int]]
      val pfcs = new PartialFunctionCommandStream(pf)
      pf.isDefinedAt(1) returns true
      pf.apply(1) returns 10
     
      (pfcs.next(1) must_== 10) and (there was one(pf).isDefinedAt(1) then one(pf).apply(1))
   }

   def e2 = {
      val pf = mock[PartialFunction[Int,Int]]
      val pfcs = new PartialFunctionCommandStream(pf)
      pf.isDefinedAt(1) returns true
      pf.apply(1) returns 10
     
      (pfcs.hasNext(1) must beTrue) and (there was one(pf).isDefinedAt(1))
   }
}

e2 works fine. e1 does not.


On Mon, Jul 4, 2011 at 10:09 PM, etorreborre <etorr...@gmail.com> wrote:
Hi Thomas,

Could you please post some code?



No I actually have not evolved that approach much more, tests changed a bit :(

Thomas

etorreborre

unread,
Jul 6, 2011, 8:49:25 AM7/6/11
to specs2...@googlegroups.com
Hi Thomas,

I did a bit of experimentation and I think that this has to do with some boxing of primitives running back the scene.

The workaround I've found to be working is to use the "Integer" type. The only issue is that you get a deprecation warning:

      type I = Integer
      val pf = mock[PartialFunction[Int, I]]

The other easy alternative which makes sense in your case is to use String:

   def e1 = {
      val pf = mock[PartialFunction[Int, String]]
      val pfcs = new PartialFunctionCommandStream(pf)
      pf.isDefinedAt(1) returns true
      pf.apply(1) returns "10"
      (pfcs.next(1) must_== "10") and (there was one(pf).isDefinedAt(1) then one(pf).apply(1))
   }

That's an interesting use case for the use of Mockito with Scala.

Eric.

Thomas Sant'ana

unread,
Jul 6, 2011, 11:19:37 AM7/6/11
to specs2...@googlegroups.com
On Wed, Jul 6, 2011 at 9:49 AM, etorreborre <etorr...@gmail.com> wrote:
Hi Thomas,

I did a bit of experimentation and I think that this has to do with some boxing of primitives running back the scene.



Very true, I forgot about auto-boxing. I really dont need this to be based on Int. It was just to make life easier.
 
The workaround I've found to be working is to use the "Integer" type. The only issue is that you get a deprecation warning:

      type I = Integer
      val pf = mock[PartialFunction[Int, I]]

The other easy alternative which makes sense in your case is to use String:

   def e1 = {
      val pf = mock[PartialFunction[Int, String]]
      val pfcs = new PartialFunctionCommandStream(pf)
      pf.isDefinedAt(1) returns true
      pf.apply(1) returns "10"
      (pfcs.next(1) must_== "10") and (there was one(pf).isDefinedAt(1) then one(pf).apply(1))
   }

That's an interesting use case for the use of Mockito with Scala.

 
Maybe we could put a note on the Wiki. Can I edit it?

Thomas
 
Reply all
Reply to author
Forward
0 new messages