Skip to content

Commit 09dec99

Browse files
committed
Add regression tests
1 parent 506bb4c commit 09dec99

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Int(1) Str(abc)
2+
Int(1) Str(abc)
3+
xyz
4+
Int(1) Str(xyz)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import scala.quoted._
2+
import scala.quoted.matching._
3+
4+
inline def (sc: StringContext) showMe(args: =>Any*): String = ${ showMeExpr('sc, 'args) }
5+
6+
private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(given qctx: QuoteContext): Expr[String] = {
7+
argsExpr match {
8+
case ExprSeq(argExprs) =>
9+
val argShowedExprs = argExprs.map {
10+
case '{ $arg: $tp } =>
11+
val showTp = '[Show[$tp]]
12+
summonExpr(given showTp, summon[QuoteContext]) match {
13+
case Some(showExpr) => '{ $showExpr.show($arg) }
14+
case None => qctx.error(s"could not find implicit for ${showTp.show}", arg); '{???}
15+
}
16+
}
17+
val newArgsExpr = Expr.ofSeq(argShowedExprs)
18+
'{ $sc.s($newArgsExpr: _*) }
19+
case _ =>
20+
// `new StringContext(...).showMeExpr(args: _*)` not an explicit `showMeExpr"..."`
21+
qctx.error(s"Args must be explicit", argsExpr)
22+
'{???}
23+
}
24+
}
25+
26+
trait Show[-T] {
27+
def show(x: T): String
28+
}
29+
30+
given Show[Int] = x => s"Int($x)"
31+
given Show[String] = x => s"Str($x)"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Test {
2+
3+
def main(args: Array[String]): Unit = {
4+
println(showMe"${1: Int} ${"abc": String}")
5+
println(showMe"${1} ${"abc"}")
6+
println(showMe"${1} ${println("xyz"); "xyz"}")
7+
}
8+
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Int(1) Str(abc)
2+
Int(1) Str(abc)
3+
xyz
4+
Int(1) Str(xyz)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
object Lib {
3+
def (sc: StringContext) showMe(args: Showed*): String = sc.s(args: _*)
4+
5+
opaque type Showed = String
6+
7+
given [T](given show: Show[T]): Conversion[T, Showed] = x => show(x)
8+
9+
trait Show[T] {
10+
def apply(x: T): String
11+
}
12+
13+
given Show[Int] = x => s"Int($x)"
14+
given Show[String] = x => s"Str($x)"
15+
}
16+
object Test {
17+
import Lib._
18+
def main(args: Array[String]): Unit = {
19+
println(showMe"${1: Int} ${"abc": String}")
20+
println(showMe"${1} ${"abc"}")
21+
println(showMe"${1} ${println("xyz"); "xyz"}")
22+
}
23+
24+
}

0 commit comments

Comments
 (0)