|
| 1 | +package dotty.tools.dotc.ast |
| 2 | + |
| 3 | +import dotty.tools.DottyTest |
| 4 | +import dotty.tools.dotc.ast.Trees._ |
| 5 | +import dotty.tools.dotc.util.Property |
| 6 | +import dotty.tools.dotc.transform.PostTyper |
| 7 | + |
| 8 | +import org.junit.Test |
| 9 | +import org.junit.Assert.{assertEquals, assertTrue, fail} |
| 10 | + |
| 11 | +class AttachmentsTests extends DottyTest { |
| 12 | + |
| 13 | + private val TestKey = new Property.Key[String] |
| 14 | + private val StickyTestKey = new Property.StickyKey[String] |
| 15 | + private val StickyTestKey2 = new Property.StickyKey[String] |
| 16 | + |
| 17 | + @Test |
| 18 | + def attachmentsAreNotCopiedOver: Unit = { |
| 19 | + checkCompile("frontend", "class A") { |
| 20 | + case (PackageDef(_, (clazz: tpd.TypeDef) :: Nil), context) => |
| 21 | + assertTrue("Attachment shouldn't be present", clazz.getAttachment(TestKey).isEmpty) |
| 22 | + |
| 23 | + val msg = "hello" |
| 24 | + clazz.putAttachment(TestKey, msg) |
| 25 | + assertEquals(Some(msg), clazz.getAttachment(TestKey)) |
| 26 | + |
| 27 | + val copy = tpd.cpy.TypeDef(clazz)(rhs = tpd.EmptyTree) |
| 28 | + assertTrue("A copy should have been returned", clazz ne copy) |
| 29 | + assertTrue("Attachment shouldn't be present", copy.getAttachment(TestKey).isEmpty) |
| 30 | + |
| 31 | + case _ => |
| 32 | + fail |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + def stickyAttachmentsAreCopiedOver: Unit = { |
| 38 | + checkCompile("frontend", "class A") { |
| 39 | + case (PackageDef(_, (clazz: tpd.TypeDef) :: Nil), context) => |
| 40 | + assertTrue("Attachment shouldn't be present", clazz.getAttachment(StickyTestKey).isEmpty) |
| 41 | + assertTrue("Attachment shouldn't be present", clazz.getAttachment(StickyTestKey2).isEmpty) |
| 42 | + assertTrue("Attachment shouldn't be present", clazz.getAttachment(TestKey).isEmpty) |
| 43 | + |
| 44 | + val msg = "hello" |
| 45 | + clazz.putAttachment(StickyTestKey, msg) |
| 46 | + clazz.putAttachment(TestKey, msg) |
| 47 | + clazz.putAttachment(StickyTestKey2, msg) |
| 48 | + assertEquals(Some(msg), clazz.getAttachment(StickyTestKey)) |
| 49 | + assertEquals(Some(msg), clazz.getAttachment(TestKey)) |
| 50 | + assertEquals(Some(msg), clazz.getAttachment(StickyTestKey)) |
| 51 | + |
| 52 | + val copy = tpd.cpy.TypeDef(clazz)(rhs = tpd.EmptyTree) |
| 53 | + assertTrue("A copy should have been returned", clazz ne copy) |
| 54 | + assertTrue("Attachment should be present", copy.getAttachment(StickyTestKey).isDefined) |
| 55 | + assertTrue("Attachment shouldn't be present", copy.getAttachment(TestKey).isEmpty) |
| 56 | + assertTrue("Attachment should be present", copy.getAttachment(StickyTestKey2).isDefined) |
| 57 | + |
| 58 | + case _ => |
| 59 | + fail |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments