Skip to content

Commit

Permalink
fix context bounded classes
Browse files Browse the repository at this point in the history
  • Loading branch information
goshacodes committed Feb 25, 2024
1 parent 7a0d04f commit 2894754
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
14 changes: 10 additions & 4 deletions shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package org.scalamock.clazz

import org.scalamock.context.MockContext

import scala.quoted.*
import scala.reflect.Selectable

Expand All @@ -42,16 +41,23 @@ private[clazz] object MockMaker:
def asParent(tree: TypeTree): TypeTree | Term =
val constructorFieldsFilledWithNulls: List[List[Term]] =
tree.tpe.dealias.typeSymbol.primaryConstructor.paramSymss
.filter(_.exists(!_.isType))
.map(_.map(_.typeRef.asType match { case '[t] => '{ null.asInstanceOf[t] }.asTerm }))
.filterNot(_.exists(_.isType))
.map(_.map(_.info.widen match {
case t@AppliedType(inner, applied) =>
Select.unique('{null}.asTerm, "asInstanceOf").appliedToTypes(List(inner.appliedTo(tpe.typeArgs)))
case other =>
Select.unique('{null}.asTerm, "asInstanceOf").appliedToTypes(List(other))
}))

if constructorFieldsFilledWithNulls.forall(_.isEmpty) then
tree
else
Select(
New(TypeIdent(tree.tpe.typeSymbol)),
tree.tpe.typeSymbol.primaryConstructor
).appliedToArgss(constructorFieldsFilledWithNulls)
).appliedToTypes(tree.tpe.typeArgs)
.appliedToArgss(constructorFieldsFilledWithNulls)



val parents =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.paulbutcher.test

import org.scalamock.scalatest.MockFactory
import org.scalatest.funspec.AnyFunSpec

import scala.reflect.ClassTag

class ClassWithContextBoundSpec extends AnyFunSpec with MockFactory {

it("compile without args") {
class ContextBounded[T: ClassTag] {
def method(x: Int): Unit = ()
}

val m = mock[ContextBounded[String]]

}

it("compile with args") {
class ContextBounded[T: ClassTag](x: Int) {
def method(x: Int): Unit = ()
}

val m = mock[ContextBounded[String]]

}

it("compile with provided explicitly type class") {
class ContextBounded[T](x: ClassTag[T]) {
def method(x: Int): Unit = ()
}

val m = mock[ContextBounded[String]]

}

}

0 comments on commit 2894754

Please sign in to comment.