Skip to content

Commit

Permalink
Better handling mocks for methods taking implicit Tag types
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechMazur committed Apr 26, 2024
1 parent 94c7453 commit c1f09df
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
20 changes: 20 additions & 0 deletions input/src/main/scala/rewrite/mockable/ImplicitsParams.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
rule = ZIOMockableCodeGen
*/
package rewrite.mockable

import zio._
import zio.stream.ZSink
import zio.mock._
import rewrite.accessible._

@mockable[ImplicitsParamsService]
object ImplicitsParamsServiceMock

object ImplicitsParamsServiceMockTest {
implicitly[ImplicitsParamsServiceMock.type <:< Mock[ImplicitsParamsService]]
implicitly[ImplicitsParamsServiceMock.ReduceTransactions.type <:< ImplicitsParamsServiceMock.Poly.Effect.InputErrorOutput]
implicitly[
ImplicitsParamsServiceMock.EventsForGroup.type <:< ImplicitsParamsServiceMock.Method[GroupId, Throwable, List[TransactionEvent]]
]
}
33 changes: 33 additions & 0 deletions output/src/main/scala/rewrite/mockable/ImplicitsParams.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package rewrite.mockable

import zio._
import zio.stream.ZSink
import zio.mock._
import rewrite.accessible._
import zio.mock.Mock


object ImplicitsParamsServiceMock extends Mock[ImplicitsParamsService] {
// format: off
// Generated by ZIO Mockable CodeGen Scalafix Rule
case object ReduceTransactions extends Poly.Effect.InputErrorOutput
case object EventsForGroup extends Method[GroupId, Throwable, List[TransactionEvent]]
override val compose: URLayer[Proxy, ImplicitsParamsService] = ZLayer.fromZIO[Proxy, Nothing, ImplicitsParamsService](
ZIO.service[Proxy].flatMap { proxy =>
withRuntime[Proxy, ImplicitsParamsService] { rt =>
class MockImpl extends ImplicitsParamsService {
final override def reduceTransactions[R, E, A](groupId: GroupId, sink: ZSink[R, E, TransactionEvent, Nothing, A])(using Tag[R], Tag[E], Tag[A]): ZIO[R, E, A] = proxy(ReduceTransactions.of[(GroupId, ZSink[R, E, TransactionEvent, Nothing, A]), E, A], groupId, sink)
final override def eventsForGroup(id: GroupId)(using Tag[GroupId]): List[TransactionEvent] = Unsafe.unsafe { case given Unsafe => rt.unsafe.run(proxy(EventsForGroup, id)).getOrThrow() }
}
ZIO.succeed(new MockImpl())
}})
// format: on
}

object ImplicitsParamsServiceMockTest {
implicitly[ImplicitsParamsServiceMock.type <:< Mock[ImplicitsParamsService]]
implicitly[ImplicitsParamsServiceMock.ReduceTransactions.type <:< ImplicitsParamsServiceMock.Poly.Effect.InputErrorOutput]
implicitly[
ImplicitsParamsServiceMock.EventsForGroup.type <:< ImplicitsParamsServiceMock.Method[GroupId, Throwable, List[TransactionEvent]]
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ class ZIOMockableCodeGen extends SemanticRule("ZIOMockableCodeGen") with ZIOCode
}
case _ => false
}
def fromType = param.tpe.collect { case Type.Name("EnvironmentTag") => true }.nonEmpty
def fromType = param.tpe.collect {
case Type.Name("EnvironmentTag") => true
case Type.Select(Term.Select(Term.Name("zio"), _), Type.Name(name)) if name.startsWith("Tag") || name.endsWith("Tag") => true
}.nonEmpty

fromSymbol || fromType
}
Expand Down

0 comments on commit c1f09df

Please sign in to comment.