-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUtilsTestMacro.test.scala
More file actions
83 lines (68 loc) · 2.52 KB
/
ArrayUtilsTestMacro.test.scala
File metadata and controls
83 lines (68 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package org.encalmo.utils
import ArrayUtils.*
import scala.quoted.*
object ArrayUtilsTestMacro {
inline def testBuildArrayLoop[A](value: Array[A]): String = {
${ testBuildArrayLoopImpl[A]('{ value }) }
}
def testBuildArrayLoopImpl[A: Type](valueExpr: Expr[Array[A]])(using Quotes): Expr[String] = {
given cache: StatementsCache = new StatementsCache
import cache.quotes.reflect.*
testBuildArrayLoop2Impl[A](valueExpr.asTerm)
}
def testBuildArrayLoop2Impl[A: Type](using
cache: StatementsCache
)(valueTerm: cache.quotes.reflect.Term): Expr[String] = {
given cache.quotes.type = cache.quotes
import cache.quotes.reflect.*
val bufferRef = cache.getValueRefOfExpr("buffer", '{ collection.mutable.ListBuffer.empty[String] })
TypeRepr.of[Array[A]] match {
case TypeReprIsArray(tpe) =>
cache.put {
buildArrayLoop(
TypeNameUtils.valueNameOf(tpe),
tpe,
valueTerm,
functionOnItem = { (tpe, valueTerm, indexTerm) =>
bufferRef.methodCall(
"append",
List(
StringUtils.concat(
StringUtils.applyToString(valueTerm),
Literal(StringConstant(" at ")),
StringUtils.applyToString(indexTerm)
)
)
)
}
)
}
}
cache.put {
bufferRef.methodCall("mkString", List(Literal(StringConstant(", "))))
}
val result = cache.asTerm
// report.warning(result.show(using Printer.TreeCode))
result.asExprOf[String]
}
inline def testCreateArrayViaList[A](inline value: List[A]): Array[A] = {
${ testCreateArrayViaListImpl[A]('{ value }) }
}
def testCreateArrayViaListImpl[A: Type](valueExpr: Expr[List[A]])(using Quotes): Expr[Array[A]] = {
given cache: StatementsCache = new StatementsCache
given cache.quotes.type = cache.quotes
testCreateArrayViaList2Impl[A](TypeUtils.extractTermsFromList[A](valueExpr))
}
def testCreateArrayViaList2Impl[A: Type](using
cache: StatementsCache
)(valueTerms: List[cache.quotes.reflect.Term]): Expr[Array[A]] = {
given cache.quotes.type = cache.quotes
val bufferRef = cache.getValueRefOfExpr("buffer", '{ collection.mutable.ListBuffer.empty[String] })
cache.put {
createArrayViaList(cache.quotes.reflect.TypeRepr.of[A], valueTerms)
}
val result = cache.asTerm
// report.warning(result.show(using Printer.TreeCode))
result.asExprOf[Array[A]]
}
}