Skip to content

Commit 3cd45e9

Browse files
committed
added tests for selector constructors
1 parent 2693590 commit 3cd45e9

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Diff for: tests/run-macros/i21225.check

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bar codec
2+
foo codec

Diff for: tests/run-macros/i21225/Macro_1.scala

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//> using options -experimental
2+
3+
import scala.quoted.*
4+
5+
trait Codec[-T] { def print(): Unit }
6+
object Codec {
7+
inline def derivedWithDeps[T](deps: Any): Codec[T] = ${derivedWithDepsImpl[T]('deps)}
8+
9+
private def derivedWithDepsImpl[T](deps: Expr[Any])(using q: Quotes)(using Type[T]): Expr[Codec[T]] = {
10+
import q.reflect.*
11+
12+
val givenSelector: Selector = GivenSelector(None)
13+
val theImport = Import(deps.asTerm, List(givenSelector))
14+
Block(List(theImport), '{scala.compiletime.summonInline[Codec[T]]}.asTerm).asExprOf[Codec[T]]
15+
/* import deps.given
16+
* summonInline[Codec[T]]
17+
*/
18+
}
19+
20+
inline def derivedWithDepsWithNamedOmitted[T](deps: Any): Codec[T] = ${derivedWithDepsWithNamedOmittedImpl[T]('deps)}
21+
22+
private def derivedWithDepsWithNamedOmittedImpl[T](deps: Expr[Any])(using q: Quotes)(using Type[T]): Expr[Codec[T]] = {
23+
import q.reflect.*
24+
25+
val givenSelector: Selector = GivenSelector(None)
26+
val omitSelector: Selector = OmitSelector("named")
27+
val theImport = Import(deps.asTerm, List(givenSelector, omitSelector))
28+
Block(List(theImport), '{scala.compiletime.summonInline[Codec[T]]}.asTerm).asExprOf[Codec[T]]
29+
/* import deps.{given, named => _}
30+
* summonInline[Codec[T]]
31+
*/
32+
}
33+
}

Diff for: tests/run-macros/i21225/Test_2.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//> using options -experimental
2+
3+
import scala.quoted.*
4+
5+
sealed trait Foo
6+
class Bar extends Foo
7+
object CustomCodecs {
8+
given named: Codec[Bar] = new Codec[Bar] { def print(): Unit = println("bar codec")}
9+
given Codec[Foo] = new Codec[Foo] { def print(): Unit = println("foo codec") }
10+
}
11+
12+
@main def Test =
13+
Codec.derivedWithDeps[Bar](CustomCodecs).print()
14+
Codec.derivedWithDepsWithNamedOmitted[Bar](CustomCodecs).print()

0 commit comments

Comments
 (0)