From ed71b10129dac2801160e6fbbfbf8168a040b283 Mon Sep 17 00:00:00 2001 From: Georgii Kovalev Date: Sat, 24 Feb 2024 16:39:18 +0600 Subject: [PATCH] fix default param with type param --- .../src/main/scala-3/org/scalamock/clazz/Utils.scala | 9 +++++---- .../test/mock/MethodsWithDefaultParamsTest.scala | 10 ++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/shared/src/main/scala-3/org/scalamock/clazz/Utils.scala b/shared/src/main/scala-3/org/scalamock/clazz/Utils.scala index a966a499..52a5cf21 100644 --- a/shared/src/main/scala-3/org/scalamock/clazz/Utils.scala +++ b/shared/src/main/scala-3/org/scalamock/clazz/Utils.scala @@ -128,10 +128,11 @@ private[clazz] class Utils(using val quotes: Quotes): def apply(tpe: TypeRepr): List[MockableDefinition] = val methods = (tpe.typeSymbol.methodMembers.toSet -- TypeRepr.of[Object].typeSymbol.methodMembers).toList - .filter(sym => !sym.flags.is(Flags.Private) && !sym.flags.is(Flags.Final) && !sym.flags.is(Flags.Mutable)) - .filterNot(sym => tpe.memberType(sym) match - case defaultParam @ ByNameType(AnnotatedType(_, Apply(Select(New(Inferred()), ""), Nil))) => true - case _ => false + .filter(sym => + !sym.flags.is(Flags.Private) && + !sym.flags.is(Flags.Final) && + !sym.flags.is(Flags.Mutable) && + !sym.name.contains("$default$") ) .zipWithIndex .map((sym, idx) => MockableDefinition(idx, sym, tpe)) diff --git a/shared/src/test/scala/com/paulbutcher/test/mock/MethodsWithDefaultParamsTest.scala b/shared/src/test/scala/com/paulbutcher/test/mock/MethodsWithDefaultParamsTest.scala index 67d6e479..b9693513 100644 --- a/shared/src/test/scala/com/paulbutcher/test/mock/MethodsWithDefaultParamsTest.scala +++ b/shared/src/test/scala/com/paulbutcher/test/mock/MethodsWithDefaultParamsTest.scala @@ -34,6 +34,8 @@ class MethodsWithDefaultParamsTest extends IsolatedSpec { trait TraitHavingMethodsWithDefaultParams { def withAllDefaultParams(a: String = "default", b: CaseClass = CaseClass(42)): String + + def withDefaultParamAndTypeParam[T](a: String = "default", b: Int = 5): T } behavior of "Mocks" @@ -84,5 +86,13 @@ class MethodsWithDefaultParamsTest extends IsolatedSpec { m.withAllDefaultParams("other", CaseClass(99)) } + they should "mock trait methods with type param and default parameters" in { + val m = mock[TraitHavingMethodsWithDefaultParams] + + (m.withDefaultParamAndTypeParam[Int] _).expects("default", 5).returns(5) + + m.withDefaultParamAndTypeParam[Int]("default", 5) shouldBe 5 + } + override def newInstance = new MethodsWithDefaultParamsTest }