forked from scalameta/scalameta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectionSuite.scala
144 lines (138 loc) · 4.49 KB
/
ReflectionSuite.scala
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package scala.meta.tests
package trees
import munit._
import org.scalameta.internal.ScalaCompat.EOL
import scala.collection.mutable.ListBuffer
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{universe => ru}
class ReflectionSuite extends FunSuite {
object TreeReflection
extends {
val u: ru.type = ru
val mirror: u.Mirror = u.runtimeMirror(classOf[scala.meta.Tree].getClassLoader)
}
with scala.meta.internal.trees.Reflection
import TreeReflection._
// NOTE: These counts are important because our TreeReflection infrastructure is quite fragile.
// Therefore we do need additional safeguards in place to prevent silent failures.
// I understand that it's inconvenient to update these numbers every time something changes,
// but please deal with that (or come up with a more effective way of testing TreeReflection)
test("root") {
val sym = symbolOf[scala.meta.Tree]
assert(sym.isRoot)
val root = sym.asRoot
assertEquals((root.allBranches.length, root.allLeafs.length), (50, 410))
}
test("If") {
val iff = symbolOf[scala.meta.Term.If].asLeaf
assertEquals(
iff.fields.map(_.toString),
List(
"field Term.If.cond: scala.meta.Term",
"field Term.If.thenp: scala.meta.Term",
"field Term.If.elsep: scala.meta.Term",
"field Term.If.mods: List[scala.meta.Mod]"
)
)
assertEquals(
iff.allFields.map(_.toString),
List(
"field Term.If.origin: scala.meta.trees.Origin",
"field Term.If.cond: scala.meta.Term",
"field Term.If.thenp: scala.meta.Term",
"field Term.If.elsep: scala.meta.Term",
"field Term.If.mods: List[scala.meta.Mod]"
)
)
}
test("Term.Name") {
val iff = symbolOf[scala.meta.Term.Name].asLeaf
assertEquals(
iff.fields.map(_.toString),
List(
"field Term.Name.value: String @org.scalameta.invariants.nonEmpty"
)
)
assertEquals(
iff.allFields.map(_.toString),
List(
"field Term.Name.origin: scala.meta.trees.Origin",
"field Term.Name.value: String @org.scalameta.invariants.nonEmpty"
)
)
}
test("allFields") {
val allRelevantFields = symbolOf[scala.meta.Tree].asRoot.allLeafs
.filter(!_.sym.fullName.endsWith(".Quasi"))
.flatMap(_.fields)
val duplicateRelevantFieldTpes =
allRelevantFields.map(_.tpe).map { case AnnotatedType(_, tpe) => tpe; case tpe => tpe }
// NOTE: we can't just do `duplicateRelevantFieldTpes.distinct`, because that doesn't account for `=:=`
val distinctRelevantFieldTpes = ListBuffer[Type]()
duplicateRelevantFieldTpes.foreach(tpe =>
if (!distinctRelevantFieldTpes.exists(_ =:= tpe)) distinctRelevantFieldTpes += tpe
)
val obtained = distinctRelevantFieldTpes.sortBy(_.toString).mkString(EOL)
assertNoDiff(
obtained,
"""
|Boolean
|Byte
|Char
|Int
|List[meta.Term.ParamClause]
|List[scala.meta.Case]
|List[scala.meta.Enumerator]
|List[scala.meta.Importee]
|List[scala.meta.Importer]
|List[scala.meta.Init]
|List[scala.meta.Lit]
|List[scala.meta.Member.ParamClauseGroup]
|List[scala.meta.Mod.Annot]
|List[scala.meta.Mod]
|List[scala.meta.Pat]
|List[scala.meta.Source]
|List[scala.meta.Stat.TypeDef]
|List[scala.meta.Stat]
|List[scala.meta.Term.Name]
|List[scala.meta.Term.Param]
|List[scala.meta.Term]
|List[scala.meta.Type.Param]
|List[scala.meta.TypeCase]
|List[scala.meta.Type]
|Long
|Option[scala.meta.Member.ParamClauseGroup]
|Option[scala.meta.Mod.ArgsType]
|Option[scala.meta.Mod.ParamsType]
|Option[scala.meta.Mod.Variant]
|Option[scala.meta.Term]
|Option[scala.meta.Type]
|Seq[scala.meta.Term.ArgClause]
|Seq[scala.meta.Term.ParamClause]
|Short
|String
|Symbol
|meta.Type.ParamClause
|scala.meta.Ctor.Primary
|scala.meta.Init
|scala.meta.Name
|scala.meta.Pat
|scala.meta.Pat.ArgClause
|scala.meta.Ref
|scala.meta.Self
|scala.meta.Stat
|scala.meta.Template
|scala.meta.Term
|scala.meta.Term.ArgClause
|scala.meta.Term.Name
|scala.meta.Term.ParamClause
|scala.meta.Term.Ref
|scala.meta.Type
|scala.meta.Type.ArgClause
|scala.meta.Type.Bounds
|scala.meta.Type.FuncParamClause
|scala.meta.Type.Name
""".trim.stripMargin.split('\n').mkString(EOL)
)
}
}