|
| 1 | +package test.transform |
| 2 | + |
| 3 | + |
| 4 | +import org.junit.{Assert, Test} |
| 5 | +import test.DottyTest |
| 6 | +import dotty.tools.dotc.core._ |
| 7 | +import dotty.tools.dotc.ast.Trees |
| 8 | +import Contexts._ |
| 9 | +import Flags._ |
| 10 | +import Denotations._ |
| 11 | +import NameOps._ |
| 12 | +import Symbols._ |
| 13 | +import Types._ |
| 14 | +import Decorators._ |
| 15 | +import Trees._ |
| 16 | +import dotty.tools.dotc.transform.TreeTransforms.{TreeTransform, TreeTransformer} |
| 17 | +import dotty.tools.dotc.transform.PostTyperTransformers.PostTyperTransformer |
| 18 | + |
| 19 | +class PostTyperTransformerTest extends DottyTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + def shouldStripImports = checkCompile("frontend", "class A{ import scala.collection.mutable._; val d = 1}") { |
| 23 | + (tree, context) => |
| 24 | + implicit val ctx = context |
| 25 | + class EmptyTransform(group: TreeTransformer, idx: Int) extends TreeTransform(group, idx) {} |
| 26 | + val transformer = new PostTyperTransformer { |
| 27 | + override def transformations = Array(new EmptyTransform(_, _)) |
| 28 | + |
| 29 | + override def name: String = "test" |
| 30 | + } |
| 31 | + val transformed = transformer.transform(tree) |
| 32 | + |
| 33 | + Assert.assertTrue("should strip imports", |
| 34 | + !transformed.toString.toLowerCase.contains("import") |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + def shouldStripNamedArgs = checkCompile("frontend", "class A{ def p(x:Int, y:Int= 2) = 1; p(1, y = 2)}") { |
| 40 | + (tree, context) => |
| 41 | + implicit val ctx = context |
| 42 | + class EmptyTransform(group: TreeTransformer, idx: Int) extends TreeTransform(group, idx) {} |
| 43 | + val transformer = new PostTyperTransformer { |
| 44 | + override def transformations = Array(new EmptyTransform(_, _)) |
| 45 | + |
| 46 | + override def name: String = "test" |
| 47 | + } |
| 48 | + val transformed = transformer.transform(tree) |
| 49 | + |
| 50 | + Assert.assertTrue("should string named arguments", |
| 51 | + !transformed.toString.contains("NamedArg") |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + def shouldReorderExistingObjectsInPackage = checkCompile("frontend", "object A{}; class A{} ") { |
| 57 | + (tree, context) => |
| 58 | + implicit val ctx = context |
| 59 | + class EmptyTransform(group: TreeTransformer, idx: Int) extends TreeTransform(group, idx) {} |
| 60 | + val transformer = new PostTyperTransformer { |
| 61 | + override def transformations = Array(new EmptyTransform(_, _)) |
| 62 | + |
| 63 | + override def name: String = "test" |
| 64 | + } |
| 65 | + val transformed = transformer.transform(tree).toString |
| 66 | + val classPattern = "TypeDef(Modifiers(,,List()),A," |
| 67 | + val classPos = transformed.indexOf(classPattern) |
| 68 | + val moduleClassPattern = "TypeDef(Modifiers(final module,,List()),A$," |
| 69 | + val modulePos = transformed.indexOf(moduleClassPattern) |
| 70 | + |
| 71 | + Assert.assertTrue("should reorder existing objects in package", |
| 72 | + classPos < modulePos |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + def shouldReorderExistingObjectsInBlock = checkCompile("frontend", "class D {def p = {object A{}; class A{}; 1}} ") { |
| 78 | + (tree, context) => |
| 79 | + implicit val ctx = context |
| 80 | + class EmptyTransform(group: TreeTransformer, idx: Int) extends TreeTransform(group, idx) {} |
| 81 | + val transformer = new PostTyperTransformer { |
| 82 | + override def transformations = Array(new EmptyTransform(_, _)) |
| 83 | + |
| 84 | + override def name: String = "test" |
| 85 | + } |
| 86 | + val transformed = transformer.transform(tree).toString |
| 87 | + val classPattern = "TypeDef(Modifiers(,,List()),A," |
| 88 | + val classPos = transformed.indexOf(classPattern) |
| 89 | + val moduleClassPattern = "TypeDef(Modifiers(final module,,List()),A$," |
| 90 | + val modulePos = transformed.indexOf(moduleClassPattern) |
| 91 | + |
| 92 | + Assert.assertTrue("should reorder existing objects in block", |
| 93 | + classPos < modulePos |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + def shouldReorderExistingObjectsInTemplate = checkCompile("frontend", "class D {object A{}; class A{}; } ") { |
| 99 | + (tree, context) => |
| 100 | + implicit val ctx = context |
| 101 | + class EmptyTransform(group: TreeTransformer, idx: Int) extends TreeTransform(group, idx) {} |
| 102 | + val transformer = new PostTyperTransformer { |
| 103 | + override def transformations = Array(new EmptyTransform(_, _)) |
| 104 | + |
| 105 | + override def name: String = "test" |
| 106 | + } |
| 107 | + val transformed = transformer.transform(tree).toString |
| 108 | + val classPattern = "TypeDef(Modifiers(,,List()),A," |
| 109 | + val classPos = transformed.indexOf(classPattern) |
| 110 | + val moduleClassPattern = "TypeDef(Modifiers(final module,,List()),A$," |
| 111 | + val modulePos = transformed.indexOf(moduleClassPattern) |
| 112 | + |
| 113 | + Assert.assertTrue("should reorder existing objects in template", |
| 114 | + classPos < modulePos |
| 115 | + ) |
| 116 | + } |
| 117 | +} |
0 commit comments