Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit f50f477

Browse files
Added new test cases for type classes
1 parent c8ae142 commit f50f477

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

modules/pulp/src/main/scala/io/scalaland/pulp/internals/WiredImpl.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ private[pulp] class WiredImpl(wiredType: WiredImpl.Type)(val c: Context)(annotte
1313
extends { ..$_ }
1414
with ..$_ { $_ => ..$_ }""" =>
1515
val providerArgs = ctorParams.flatten.map(p => q"${p.name}: _root_.io.scalaland.pulp.Provider[${p.tpt}]")
16-
val ctorArgs =
16+
val ctorArgsPreFix =
1717
if (wiredType != WiredImpl.Type.Singleton) ctorParams.map(_.map(p => q"${p.name}.get"))
1818
else ctorParams.map(_.map(p => q"_root_.io.scalaland.pulp.Provider.get[${p.tpt}]"))
19+
val startWithImplicit = ctorParams.flatten.headOption.exists { case ValDef(mods, _, _, _) =>
20+
mods.hasFlag(Flag.IMPLICIT)
21+
}
22+
val ctorArgs = if (startWithImplicit) Nil +: ctorArgsPreFix else ctorArgsPreFix
1923

2024
withTraceLog("Provider implicit expanded") {
2125
wiredType match {

modules/pulp/src/test/scala/io/scalaland/pulp/WiredSpec.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class WiredSpec extends Specification {
2121
@Wired class ComplexCase[T](t: T, name: String, size: Int)
2222
@Wired class ComplexCase2[T](t: T)(name: String)(size: Int)
2323

24+
trait TestTC[T]
25+
@Wired class TCCase1[T: TestTC](implicit i: Int)
26+
@Wired class TCCase2[T: TestTC]
27+
@Wired class TCCase3[T: TestTC](d: Double)(implicit i: Int)
28+
2429
"@Wired annotation" should {
2530

2631
"generate implicit Provider def for a monomorphic class without a companion" in {
@@ -56,5 +61,15 @@ class WiredSpec extends Specification {
5661

5762
Provider.get[ComplexCase2[Double]] must not(beNull)
5863
}
64+
65+
"generate implicit Provider def for a class with type class constraint" in {
66+
implicit val stringTCProvider: Provider[TestTC[String]] = Provider.const(new TestTC[String] {})
67+
implicit val doubleProvider: Provider[Double] = Provider.const(1.0)
68+
implicit val intProvider: Provider[Int] = Provider.const(10)
69+
70+
Provider.get[TCCase1[String]] must not(beNull)
71+
Provider.get[TCCase2[String]] must not(beNull)
72+
Provider.get[TCCase3[String]] must not(beNull)
73+
}
5974
}
6075
}

0 commit comments

Comments
 (0)