From 1a78743a43db666d12f298244cc45864b29be322 Mon Sep 17 00:00:00 2001 From: Martijn Date: Mon, 20 May 2024 18:06:17 +0200 Subject: [PATCH 1/7] only cast null for primitives before, we tried to cast applied types to the type arguments, to capture context bounds. That fails for simple generic types that then get cast to the wrong type. However, that's all unnessecary: the only nulls we need to cast are primitives, which can't be passed as null. Anyting else doesn't need a cast at all --- .../main/scala-3/org/scalamock/clazz/MockMaker.scala | 11 +++++------ .../scala/com/paulbutcher/test/mock/MockTest.scala | 9 +++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala b/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala index 95db97a5..f353e832 100644 --- a/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala +++ b/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala @@ -42,12 +42,11 @@ private[clazz] object MockMaker: val constructorFieldsFilledWithNulls: List[List[Term]] = 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 tpe = sym.info.widen + if tpe <:< TypeRepr.of[AnyRef] then '{null}.asTerm + else Select.unique('{null}.asTerm, "asInstanceOf").appliedToType(tpe) + }) if constructorFieldsFilledWithNulls.forall(_.isEmpty) then tree diff --git a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala index cdd5f31a..11cf434c 100644 --- a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala +++ b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala @@ -488,5 +488,14 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers { "stub[HasNotifyMethod]" should compile } + + "mock generic 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 + } } } From 06045055f36ae68bd62d136aae897b0a93a3931e Mon Sep 17 00:00:00 2001 From: Martijn Date: Thu, 6 Jun 2024 13:31:06 +0200 Subject: [PATCH 2/7] add failing tests --- .../test/scala/com/paulbutcher/test/mock/MockTest.scala | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala index 11cf434c..5d8e2b55 100644 --- a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala +++ b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala @@ -493,9 +493,17 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers { class WithOption(opt: Option[String]) class WithInt(i: Int) class WithString(s: String) + class WithGeneric[T](t: T) + class WithTC[TC[_]](tc: TC[Int]) + type ID[A] = A "mock[WithOption]" should compile "mock[WithInt]" should compile "mock[WithString]" should compile + "mock[WithGeneric[Int]]" should compile + "mock[WithGeneric[String]]" should compile + "mock[WithTC[ID]]" should compile + "mock[WithTC[List]]" should compile + } } } From cf3ab781939cc17a66c4173a14d7a001694ce96c Mon Sep 17 00:00:00 2001 From: Martijn Date: Thu, 6 Jun 2024 13:31:35 +0200 Subject: [PATCH 3/7] ignore failing tests --- .../scala/com/paulbutcher/test/mock/MockTest.scala | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala index 5d8e2b55..8d2974e0 100644 --- a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala +++ b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala @@ -499,11 +499,12 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers { "mock[WithOption]" should compile "mock[WithInt]" should compile "mock[WithString]" should compile - "mock[WithGeneric[Int]]" should compile - "mock[WithGeneric[String]]" should compile - "mock[WithTC[ID]]" should compile - "mock[WithTC[List]]" should compile - + pendingUntilFixed { + "mock[WithGeneric[Int]]" should compile + "mock[WithGeneric[String]]" should compile + "mock[WithTC[ID]]" should compile + "mock[WithTC[List]]" should compile + } } } } From b815deaeb5a160242acd617beb8f323f2880898c Mon Sep 17 00:00:00 2001 From: Martijn Date: Fri, 7 Jun 2024 17:54:09 +0200 Subject: [PATCH 4/7] fix class type parameters --- .../org/scalamock/clazz/MockMaker.scala | 27 ++++++++++++++++--- .../com/paulbutcher/test/mock/MockTest.scala | 19 ++++++++----- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala b/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala index f353e832..53354cea 100644 --- a/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala +++ b/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala @@ -40,12 +40,31 @@ 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{sym => - val tpe = sym.info.widen - if tpe <:< TypeRepr.of[AnyRef] then '{null}.asTerm - else Select.unique('{null}.asTerm, "asInstanceOf").appliedToType(tpe) + .map(_.map{ sym => + val classParamRef = tpe.widen match { + case AppliedType(_, params) => paramsMap.get(sym.info.typeSymbol) + case _ => None + } + val target = classParamRef.getOrElse(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 diff --git a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala index 8d2974e0..32266c66 100644 --- a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala +++ b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala @@ -493,18 +493,25 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers { class WithOption(opt: Option[String]) class WithInt(i: Int) class WithString(s: String) - class WithGeneric[T](t: T) - class WithTC[TC[_]](tc: TC[Int]) - type ID[A] = A "mock[WithOption]" should compile "mock[WithInt]" should compile "mock[WithString]" should compile + + } + + "mock type constructor arguments" in { + class WithTC[TC[_]](tc: TC[Int]) + type ID[A] = A + "mock[WithTC[List]]" should compile pendingUntilFixed { - "mock[WithGeneric[Int]]" should compile - "mock[WithGeneric[String]]" should compile "mock[WithTC[ID]]" should compile - "mock[WithTC[List]]" should compile } } + + "mock generic arguments" in { + class WithGeneric[T](t: T) + "mock[WithGeneric[String]]" should compile + "mock[WithGeneric[Int]]" should compile + } } } From 1fe7f82ca96cfd04a5701e300eaae299aa28d278 Mon Sep 17 00:00:00 2001 From: Martijn Date: Fri, 7 Jun 2024 18:04:43 +0200 Subject: [PATCH 5/7] add tests from passer-by --- .../scala/com/paulbutcher/test/mock/MockTest.scala | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala index 32266c66..ccb9372d 100644 --- a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala +++ b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala @@ -513,5 +513,15 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers { "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 + } } } From c7b23a632d2d82186368d2ae78626a788ad47f13 Mon Sep 17 00:00:00 2001 From: Martijn Date: Fri, 7 Jun 2024 20:50:17 +0200 Subject: [PATCH 6/7] move test that don't (yet) pass on scala 2 to scala 3 specific testsuite --- .../com/paulbutcher/test/MockTestScala3.scala | 62 +++++++++++++++++++ .../com/paulbutcher/test/mock/MockTest.scala | 28 +-------- 2 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 shared/src/test/scala-3/com/paulbutcher/test/MockTestScala3.scala diff --git a/shared/src/test/scala-3/com/paulbutcher/test/MockTestScala3.scala b/shared/src/test/scala-3/com/paulbutcher/test/MockTestScala3.scala new file mode 100644 index 00000000..7b1a8b4b --- /dev/null +++ b/shared/src/test/scala-3/com/paulbutcher/test/MockTestScala3.scala @@ -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 + } + } +} \ No newline at end of file diff --git a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala index ccb9372d..efdfd9c7 100644 --- a/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala +++ b/shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala @@ -489,39 +489,13 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers { "stub[HasNotifyMethod]" should compile } - "mock generic constructor arguments" in { + "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 - - } - - "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 } } } From 6d39fb49fd6d2a3b16289f5917f9f048fb7f8655 Mon Sep 17 00:00:00 2001 From: Martijn Date: Fri, 7 Jun 2024 20:54:37 +0200 Subject: [PATCH 7/7] simply getting target --- shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala b/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala index 53354cea..76930da7 100644 --- a/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala +++ b/shared/src/main/scala-3/org/scalamock/clazz/MockMaker.scala @@ -56,11 +56,7 @@ private[clazz] object MockMaker: tree.tpe.dealias.typeSymbol.primaryConstructor.paramSymss .filterNot(_.exists(_.isType)) .map(_.map{ sym => - val classParamRef = tpe.widen match { - case AppliedType(_, params) => paramsMap.get(sym.info.typeSymbol) - case _ => None - } - val target = classParamRef.getOrElse(sym.info).widen.dealias + 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