|
| 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.{tpd, 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 | +import dotty.tools.dotc.transform.CreateCompanionObjects |
| 19 | + |
| 20 | + |
| 21 | +class CreateCompanionObjectsTest extends DottyTest { |
| 22 | + |
| 23 | + import tpd._ |
| 24 | + |
| 25 | + @Test |
| 26 | + def shouldCreateNonExistingObjectsInPackage = checkCompile("frontend", "class A{} ") { |
| 27 | + (tree, context) => |
| 28 | + implicit val ctx = context |
| 29 | + |
| 30 | + val transformer = new PostTyperTransformer { |
| 31 | + override def transformations = Array(new CreateCompanionObjects(_, _) { |
| 32 | + override def predicate(cts: TypeDef): Boolean = true |
| 33 | + }) |
| 34 | + |
| 35 | + override def name: String = "test" |
| 36 | + } |
| 37 | + val transformed = transformer.transform(tree).toString |
| 38 | + println(transformed) |
| 39 | + val classPattern = "TypeDef(Modifiers(,,List()),A," |
| 40 | + val classPos = transformed.indexOf(classPattern) |
| 41 | + val moduleClassPattern = "TypeDef(Modifiers(final module <synthetic>,,List()),A$" |
| 42 | + val modulePos = transformed.indexOf(moduleClassPattern) |
| 43 | + |
| 44 | + Assert.assertTrue("should create non-existing objects in package", |
| 45 | + classPos < modulePos |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + def shouldCreateNonExistingObjectsInBlock = checkCompile("frontend", "class D {def p = {class A{}; 1}} ") { |
| 51 | + (tree, context) => |
| 52 | + implicit val ctx = context |
| 53 | + val transformer = new PostTyperTransformer { |
| 54 | + override def transformations = Array(new CreateCompanionObjects(_, _) { |
| 55 | + override def predicate(cts: TypeDef): Boolean = true |
| 56 | + }) |
| 57 | + |
| 58 | + override def name: String = "test" |
| 59 | + } |
| 60 | + val transformed = transformer.transform(tree).toString |
| 61 | + val classPattern = "TypeDef(Modifiers(,,List()),A," |
| 62 | + val classPos = transformed.indexOf(classPattern) |
| 63 | + val moduleClassPattern = "TypeDef(Modifiers(final module <synthetic>,,List()),A$" |
| 64 | + val modulePos = transformed.indexOf(moduleClassPattern) |
| 65 | + |
| 66 | + Assert.assertTrue("should create non-existing objects in block", |
| 67 | + classPos < modulePos |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + def shouldCreateNonExistingObjectsInTemplate = checkCompile("frontend", "class D {class A{}; } ") { |
| 73 | + (tree, context) => |
| 74 | + implicit val ctx = context |
| 75 | + val transformer = new PostTyperTransformer { |
| 76 | + override def transformations = Array(new CreateCompanionObjects(_, _) { |
| 77 | + override def predicate(cts: TypeDef): Boolean = true |
| 78 | + }) |
| 79 | + |
| 80 | + override def name: String = "test" |
| 81 | + } |
| 82 | + val transformed = transformer.transform(tree).toString |
| 83 | + val classPattern = "TypeDef(Modifiers(,,List()),A," |
| 84 | + val classPos = transformed.indexOf(classPattern) |
| 85 | + val moduleClassPattern = "TypeDef(Modifiers(final module <synthetic>,,List()),A$" |
| 86 | + val modulePos = transformed.indexOf(moduleClassPattern) |
| 87 | + |
| 88 | + Assert.assertTrue("should create non-existing objects in template", |
| 89 | + classPos < modulePos |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + def shouldCreateOnlyIfAskedFor = checkCompile("frontend", "class DONT {class CREATE{}; } ") { |
| 95 | + (tree, context) => |
| 96 | + implicit val ctx = context |
| 97 | + val transformer = new PostTyperTransformer { |
| 98 | + override def transformations = Array(new CreateCompanionObjects(_, _) { |
| 99 | + override def predicate(cts: TypeDef): Boolean = cts.name.toString.contains("CREATE") |
| 100 | + }) |
| 101 | + |
| 102 | + override def name: String = "test" |
| 103 | + } |
| 104 | + val transformed = transformer.transform(tree).toString |
| 105 | + val classPattern = "TypeDef(Modifiers(,,List()),A," |
| 106 | + val classPos = transformed.indexOf(classPattern) |
| 107 | + val moduleClassPattern = "TypeDef(Modifiers(final module <synthetic>,,List()),CREATE$" |
| 108 | + val modulePos = transformed.indexOf(moduleClassPattern) |
| 109 | + |
| 110 | + val notCreatedModulePattern = "TypeDef(Modifiers(final module <synthetic>,,List()),DONT" |
| 111 | + val notCreatedPos = transformed.indexOf(notCreatedModulePattern) |
| 112 | + |
| 113 | + Assert.assertTrue("should create non-existing objects in template", |
| 114 | + classPos < modulePos && (notCreatedPos < 0) |
| 115 | + ) |
| 116 | + } |
| 117 | +} |
0 commit comments