Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test curried method using PrivateMethodTester

Tags:

I'm trying to test a curried private method (actually function) using PrivateMethodTester but can't to figure out how to invoke it

Consider following curried method:

object MyObject {
  ..
  private def curriedAdder(augend: Int)(addend: Int): Int = augend + addend
  ..
 }

Following gives compilation error

assert(9, MyObject.invokePrivate(PrivateMethod[Int]('curriedAdder)(2)(7)))

..
[error] /path/to/MyObject.scala:76:80: org.scalatest.PrivateMethodTester.Invocation[Int] does not take parameters
[error]     assert(9, MyObject.invokePrivate(PrivateMethod[Int]('curriedAdder)(2)(7)))
[error]                                                                                ^
..

While this one fails to find the function

assert(9, MyObject.invokePrivate(PrivateMethod[Int => Int]('curriedAdder)(2))(7))

..
[info] - my-test-name *** FAILED ***
[info]   java.lang.IllegalArgumentException: Can't find a private method named: curriedAdder
[info]   at org.scalatest.PrivateMethodTester$Invoker.invokePrivate(PrivateMethodTester.scala:247)
..

  • Is it at all possible to test curried private methods using PrivateMethodTester?
  • If yes, where am I going wrong?

Framework versions:

  • "org.scalatest" %% "scalatest" % "3.0.3" % Test
  • scalaVersion := "2.11.11"
  • sbt.version=1.0.3
like image 312
y2k-shubham Avatar asked Jun 11 '18 11:06

y2k-shubham


1 Answers

The following worked for me with ScalaTest 3.0.1:

val curriedAdder = PrivateMethod[Int]('curriedAdder)
val actualResult = (MyObject invokePrivate curriedAdder(2, 7))
assert(9 == actualResult)

According to docs:

The private method is invoked dynamically via reflection

When I hear reflection I think of Object.getClass(), so calling MyObject.getClass.getDeclaredMethods.foreach(println) the method showed up as

private int example.HelloSpec$MyObject$.curriedAdder(int,int)

and so curriedAdder(int,int) was a hint for me on how to call this curried method.

like image 166
Mario Galic Avatar answered Sep 28 '22 18:09

Mario Galic