|
| 1 | +package test |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import org.junit.Assert._ |
| 5 | + |
| 6 | +import dotty.tools.dotc.ast.untpd.modsDeco |
| 7 | +import dotty.tools.dotc.ast.untpd._ |
| 8 | +import dotty.tools.dotc.ast.{ Trees => d } |
| 9 | +import dotty.tools.dotc.parsing.Parsers.Parser |
| 10 | +import dotty.tools.dotc.util.SourceFile |
| 11 | +import dotty.tools.dotc.core.Contexts.ContextBase |
| 12 | + |
| 13 | +object ModifiersParsingTest { |
| 14 | + implicit val ctx = (new ContextBase).initialCtx |
| 15 | + |
| 16 | + implicit def parse(code: String): Tree = { |
| 17 | + val (_, stats) = new Parser(new SourceFile("<meta>", code.toCharArray)).templateStatSeq() |
| 18 | + stats match { case List(stat) => stat; case stats => Thicket(stats) } |
| 19 | + } |
| 20 | + |
| 21 | + implicit class TreeDeco(val code: Tree) extends AnyVal { |
| 22 | + def firstConstrValDef: ValDef = code match { |
| 23 | + case d.TypeDef(_, d.Template(constr, _, _, _)) => |
| 24 | + constr.vparamss.head.head |
| 25 | + } |
| 26 | + |
| 27 | + def firstTypeParam: TypeDef = code match { |
| 28 | + case d.TypeDef(_, d.Template(constr, _, _, _)) => |
| 29 | + constr.tparams.head |
| 30 | + } |
| 31 | + |
| 32 | + def defParam(i: Int): ValDef = code match { |
| 33 | + case d.DefDef(_, _, vparamss, _, _) => |
| 34 | + vparamss.head.toArray.apply(i) |
| 35 | + } |
| 36 | + |
| 37 | + def defParam(i: Int, j: Int): ValDef = code match { |
| 38 | + case d.DefDef(_, _, vparamss, _, _) => |
| 39 | + vparamss.toArray.apply(i).toArray.apply(j) |
| 40 | + } |
| 41 | + |
| 42 | + def funParam(i: Int): Tree = code match { |
| 43 | + case Function(params, _) => |
| 44 | + params.toArray.apply(i) |
| 45 | + } |
| 46 | + |
| 47 | + def field(i: Int): Tree = code match { |
| 48 | + case d.TypeDef(_, t: Template) => |
| 49 | + t.body.toArray.apply(i) |
| 50 | + } |
| 51 | + |
| 52 | + def field(name: String): Tree = code match { |
| 53 | + case d.TypeDef(_, t: Template) => |
| 54 | + t.body.find({ |
| 55 | + case m: MemberDef => m.name.show == name |
| 56 | + case _ => false |
| 57 | + }).get |
| 58 | + } |
| 59 | + |
| 60 | + def stat(i: Int): Tree = code match { |
| 61 | + case d.Block(stats, expr) => |
| 62 | + if (i < stats.length) stats.toArray.apply(i) |
| 63 | + else expr |
| 64 | + } |
| 65 | + |
| 66 | + def modifiers: List[Mod] = code match { |
| 67 | + case t: MemberDef => t.mods.mods |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +class ModifiersParsingTest { |
| 74 | + import ModifiersParsingTest._ |
| 75 | + |
| 76 | + |
| 77 | + @Test def valDef = { |
| 78 | + var source: Tree = "class A(var a: Int)" |
| 79 | + assert(source.firstConstrValDef.modifiers == List(Mod.Var())) |
| 80 | + |
| 81 | + source = "class A(val a: Int)" |
| 82 | + assert(source.firstConstrValDef.modifiers == List(Mod.Val())) |
| 83 | + |
| 84 | + source = "class A(private val a: Int)" |
| 85 | + assert(source.firstConstrValDef.modifiers == List(Mod.Private(), Mod.Val())) |
| 86 | + |
| 87 | + source = "class A(protected var a: Int)" |
| 88 | + assert(source.firstConstrValDef.modifiers == List(Mod.Protected(), Mod.Var())) |
| 89 | + |
| 90 | + source = "class A(protected implicit val a: Int)" |
| 91 | + assert(source.firstConstrValDef.modifiers == List(Mod.Protected(), Mod.Implicit(), Mod.Val())) |
| 92 | + |
| 93 | + source = "class A[T]" |
| 94 | + assert(source.firstTypeParam.modifiers == List()) |
| 95 | + |
| 96 | + source = "class A[type T]" |
| 97 | + assert(source.firstTypeParam.modifiers == List(Mod.Type())) |
| 98 | + } |
| 99 | + |
| 100 | + @Test def typeDef = { |
| 101 | + var source: Tree = "class A" |
| 102 | + assert(source.modifiers == List()) |
| 103 | + |
| 104 | + source = "sealed class A" |
| 105 | + assert(source.modifiers == List(Mod.Sealed())) |
| 106 | + |
| 107 | + source = "implicit class A" |
| 108 | + assert(source.modifiers == List(Mod.Implicit())) |
| 109 | + |
| 110 | + source = "abstract sealed class A" |
| 111 | + assert(source.modifiers == List(Mod.Abstract(), Mod.Sealed())) |
| 112 | + } |
| 113 | + |
| 114 | + @Test def fieldDef = { |
| 115 | + val source: Tree = |
| 116 | + """ |
| 117 | + | class A { |
| 118 | + | lazy var a = ??? |
| 119 | + | lazy private val b = ??? |
| 120 | + | final val c = ??? |
| 121 | + | |
| 122 | + | abstract override def f: Boolean |
| 123 | + | inline def g(n: Int) = ??? |
| 124 | + | } |
| 125 | + """.stripMargin |
| 126 | + |
| 127 | + assert(source.field("a").modifiers == List(Mod.Lazy(), Mod.Var())) |
| 128 | + assert(source.field("b").modifiers == List(Mod.Lazy(), Mod.Private(), Mod.Val())) |
| 129 | + assert(source.field("c").modifiers == List(Mod.Final(), Mod.Val())) |
| 130 | + assert(source.field("f").modifiers == List(Mod.Abstract(), Mod.Override())) |
| 131 | + assert(source.field("g").modifiers == List(Mod.Inline())) |
| 132 | + } |
| 133 | + |
| 134 | + @Test def paramDef = { |
| 135 | + var source: Tree = "def f(inline a: Int) = ???" |
| 136 | + assert(source.defParam(0).modifiers == List(Mod.Inline())) |
| 137 | + |
| 138 | + source = "def f(implicit a: Int, b: Int) = ???" |
| 139 | + assert(source.defParam(0).modifiers == List(Mod.Implicit())) |
| 140 | + assert(source.defParam(1).modifiers == List(Mod.Implicit())) |
| 141 | + |
| 142 | + source = "def f(x: Int, y: Int)(implicit a: Int, b: Int) = ???" |
| 143 | + assert(source.defParam(0, 0).modifiers == List()) |
| 144 | + assert(source.defParam(1, 0).modifiers == List(Mod.Implicit())) |
| 145 | + } |
| 146 | + |
| 147 | + @Test def blockDef = { |
| 148 | + var source: Tree = "implicit val x : A = ???" |
| 149 | + assert(source.modifiers == List(Mod.Implicit(), Mod.Val())) |
| 150 | + |
| 151 | + source = "implicit var x : A = ???" |
| 152 | + assert(source.modifiers == List(Mod.Implicit(), Mod.Var())) |
| 153 | + |
| 154 | + source = "{ implicit var x : A = ??? }" |
| 155 | + assert(source.stat(0).modifiers == List(Mod.Implicit(), Mod.Var())) |
| 156 | + |
| 157 | + source = "{ implicit x => x * x }" |
| 158 | + assert(source.stat(0).funParam(0).modifiers == List(Mod.Implicit())) |
| 159 | + } |
| 160 | +} |
0 commit comments