Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only cast null constructor args for primitives #520

Merged
merged 7 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,28 @@ private[clazz] object MockMaker:

def asParent(tree: TypeTree): TypeTree | Term =
val constructorFieldsFilledWithNulls: List[List[Term]] =
// we don't care about any of the parameters, and just want to pass null, since all interesting parts will be mocked.
// that's simpler said than done, because if type of the argument is a primitive
// then null needs to be cast to that primitive type.
// the type could be a generic type though, which could resolve to a primitive, so we need to find the type the param
// should resolve to, and cast it if needed.
val paramsMap = {
val abstractParams = tree.tpe.dealias.typeSymbol.primaryConstructor.paramSymss.filter(_.exists(_.isType)).flatten
val resolvedParams = tpe.widen match
case AppliedType(_, params) => params
case _ => Nil
abstractParams.zip(resolvedParams).toMap
}

tree.tpe.dealias.typeSymbol.primaryConstructor.paramSymss
.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))
}))
.map(_.map{ sym =>
val target = paramsMap.getOrElse(sym.info.typeSymbol, sym.info).widen.dealias
if target <:< TypeRepr.of[AnyVal] then
Select.unique('{null}.asTerm, "asInstanceOf").appliedToType(target)
else
'{null}.asTerm
})

if constructorFieldsFilledWithNulls.forall(_.isEmpty) then
tree
Expand Down
62 changes: 62 additions & 0 deletions shared/src/test/scala-3/com/paulbutcher/test/MockTestScala3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.paulbutcher.test

// Copyright (c) 2011-2015 ScalaMock Contributors (https://github.com/paulbutcher/ScalaMock/graphs/contributors)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.paulbutcher.test.mock

import org.scalamock.scalatest.MockFactory

import com.paulbutcher.test._
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers

class MockTestScala3 extends AnyFreeSpec with MockFactory with Matchers {

autoVerify = false

"In Scala 3, Mocks should" - {

"mock type constructor arguments" in {
class WithTC[TC[_]](tc: TC[Int])
type ID[A] = A
"mock[WithTC[List]]" should compile
pendingUntilFixed {
"mock[WithTC[ID]]" should compile
}
}

"mock generic arguments" in {
class WithGeneric[T](t: T)
"mock[WithGeneric[String]]" should compile
"mock[WithGeneric[Int]]" should compile
}

"mock type constructor context bounds" in {
trait Async[F[_]]
class A[F[_]: Async](val b: B[F])
class B[F[_]: Async](val c: C[F])
trait C[F[_]]
"mock[A[List]]" should compile
"mock[B[List]]" should compile
"mock[C[List]]" should compile
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -488,5 +488,14 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers {

"stub[HasNotifyMethod]" should compile
}

"mock constructor arguments" in {
class WithOption(opt: Option[String])
class WithInt(i: Int)
class WithString(s: String)
"mock[WithOption]" should compile
"mock[WithInt]" should compile
"mock[WithString]" should compile
}
}
}
Loading