Skip to content

Commit

Permalink
Improve varargs handling (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultrasecreth authored Feb 1, 2021
1 parent 11ad429 commit 726d9ec
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ScalaMockHandler[T](mockSettings: MockCreationSettings[T], methodsToProces
.getOrElse(args)

//For some border cases, we can't extract the varargs in the nice way, so we try the brute force one
if (args.length != transformed.length) transformed
if (methodsToProcess.isEmpty || args.length != transformed.length) transformed
else unwrapVarargs(transformed)
}
}
Expand Down
34 changes: 34 additions & 0 deletions scalatest/src/test/scala-2.13/user/org/mockito/Issue352_213.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package user.org.mockito

import org.mockito.scalatest.MockitoSugar
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers

import scala.collection.immutable.ArraySeq

class Issue352_213 extends AnyFunSpec with Matchers with MockitoSugar {

trait IndexedSeqService {
def testMethod(str: String, args: IndexedSeq[String]): IndexedSeq[String]
}
trait ArraySeqService {
def testMethod(str: String, args: ArraySeq[String]): IndexedSeq[String]
}

describe("Mockito") {
it("should allow ArraySeq as a last parameter") {
val arraySeq = mock[ArraySeqService]
when(arraySeq.testMethod(*, *)).thenAnswer[String, ArraySeq[String]] { (fst, rst) =>
fst +: rst
}
arraySeq.testMethod("a", ArraySeq("b", "c")).should(contain).allOf("a", "b", "c")
}
it("should allow ArraySeq as a last parameter to IndexedSeq method") {
val indexedSeq = mock[IndexedSeqService]
when(indexedSeq.testMethod(*, *)).thenAnswer[String, IndexedSeq[String]] { (fst, rst) =>
fst +: rst
}
indexedSeq.testMethod("a", ArraySeq("b", "c")).should(contain).allOf("a", "b", "c")
}
}
}
46 changes: 46 additions & 0 deletions scalatest/src/test/scala/user/org/mockito/Issue352.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package user.org.mockito

import org.mockito.scalatest.MockitoSugar
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers

class Issue352 extends AnyFunSpec with Matchers with MockitoSugar {

trait IndexedSeqService {
def testMethod(str: String, args: IndexedSeq[String]): IndexedSeq[String]
}
trait VarArgsService {
def testMethod(str: String, args: String*): IndexedSeq[String]
}

describe("Mockito") {
it("should allow Vector as a last parameter to IndexedSeq method") {
val indexedSeq = mock[IndexedSeqService]
when(indexedSeq.testMethod(*, *)).thenAnswer[String, IndexedSeq[String]] { (fst, rst) =>
fst +: rst
}
indexedSeq.testMethod("a", Vector("b", "c")).should(contain).allOf("a", "b", "c")
}
it("should allow varargs as a last parameter to IndexedSeq method 2 varargs") {
val varargSeq = mock[VarArgsService]
when(varargSeq.testMethod(*, *)).thenAnswer[String, String, String] { (v0, v1, v2) =>
IndexedSeq(v0, v1, v2)
}
varargSeq.testMethod("a", "b", "c").should(contain).allOf("a", "b", "c")
}
it("should allow varargs as a last parameter to IndexedSeq method 1 vararg") {
val varargSeq = mock[VarArgsService]
when(varargSeq.testMethod(*, *)).thenAnswer[String, String] { (v0, v1) =>
IndexedSeq(v0, v1)
}
varargSeq.testMethod("a", "b").should(contain).allOf("a", "b")
}
it("should allow varargs as a last parameter to IndexedSeq method no vararg") {
val varargSeq = mock[VarArgsService]
when(varargSeq.testMethod(*, *)).thenAnswer[String] { v0 =>
IndexedSeq(v0)
}
varargSeq.testMethod("a").should(contain).only("a")
}
}
}

0 comments on commit 726d9ec

Please sign in to comment.