Skip to content

Commit

Permalink
fix default param with type param
Browse files Browse the repository at this point in the history
  • Loading branch information
goshacodes committed Feb 24, 2024
1 parent a43f4de commit 10f89c6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 5 additions & 4 deletions shared/src/main/scala-3/org/scalamock/clazz/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()), "<init>"), 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@

package org.scalamock.test.scalatest

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest._
import org.scalatest.flatspec.{AnyFlatSpec, AsyncFlatSpec}
import org.scalamock.scalatest.{MockFactory, AsyncMockFactory}

/**
* Tests for issue #371
*/
@Ignore
class AsyncSyncMixinTest extends AnyFlatSpec {

"MockFactory" should "be mixed only with Any*Spec and not Async*Spec traits" in {
Expand Down

0 comments on commit 10f89c6

Please sign in to comment.