|
| 1 | +package dotty.tools.dotc.plugins |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | + |
| 5 | +import dotty.tools.dotc._ |
| 6 | +import plugins._ |
| 7 | +import transform.MegaPhase.MiniPhase |
| 8 | + |
| 9 | +class PluginsTest { |
| 10 | + class TestPhase extends PluginPhase { def phaseName = this.getClass.getName } |
| 11 | + class P1 extends TestPhase |
| 12 | + class P2 extends TestPhase |
| 13 | + class P3a extends TestPhase |
| 14 | + class P3b extends TestPhase |
| 15 | + class P3c extends TestPhase |
| 16 | + class P3d extends TestPhase |
| 17 | + class P3e extends TestPhase |
| 18 | + class P4 extends TestPhase |
| 19 | + class P5 extends TestPhase |
| 20 | + class P6a extends TestPhase |
| 21 | + class P6b extends TestPhase |
| 22 | + class P6c extends TestPhase |
| 23 | + class P6d extends TestPhase |
| 24 | + class P6e extends TestPhase |
| 25 | + class P7 extends TestPhase |
| 26 | + class P8 extends TestPhase |
| 27 | + |
| 28 | + class TestPlugin extends TestPhase with Plugin { |
| 29 | + def name = this.getClass.getName |
| 30 | + override def description = "" |
| 31 | + |
| 32 | + override def init(options: List[String]): List[PluginPhase] = this :: Nil |
| 33 | + } |
| 34 | + |
| 35 | + val basicPlan = List( |
| 36 | + List(new P1), |
| 37 | + List(new P2), |
| 38 | + List(new P3a, new P3b, new P3c, new P3d, new P3e), |
| 39 | + List(new P4), |
| 40 | + List(new P5), |
| 41 | + List(new P6a, new P6b, new P6c, new P6d, new P6e), |
| 42 | + List(new P7), |
| 43 | + List(new P8) |
| 44 | + ) |
| 45 | + |
| 46 | + @Test |
| 47 | + def insertAfter = { |
| 48 | + object M1 extends TestPlugin { |
| 49 | + override val runsAfter = Set(classOf[P3d]) |
| 50 | + } |
| 51 | + |
| 52 | + val updatedPlan = Plugins.schedule(basicPlan, M1 :: Nil, _ => Nil) |
| 53 | + assert(updatedPlan(3)(0) eq M1) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + def insertBefore = { |
| 58 | + object ConstFold extends TestPlugin { |
| 59 | + override val runsBefore = Set(classOf[P7]) |
| 60 | + } |
| 61 | + |
| 62 | + val updatedPlan = Plugins.schedule(basicPlan, ConstFold :: Nil, _ => Nil) |
| 63 | + assert(updatedPlan(6)(0) eq ConstFold) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + def insertBeforeAfter = { |
| 68 | + object ConstFold extends TestPlugin { |
| 69 | + override val runsAfter = Set(classOf[P3d]) |
| 70 | + override val runsBefore = Set(classOf[P7], classOf[P8]) |
| 71 | + } |
| 72 | + |
| 73 | + // prefers the runsBefore |
| 74 | + val updatedPlan = Plugins.schedule(basicPlan, ConstFold :: Nil, _ => Nil) |
| 75 | + assert(updatedPlan(6)(0) eq ConstFold) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + def constraintUnsatisfiable = { |
| 80 | + object ConstFold extends TestPlugin { |
| 81 | + override val runsAfter = Set(classOf[P6d]) |
| 82 | + override val runsBefore = Set(classOf[P2], classOf[P8]) |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + Plugins.schedule(basicPlan, ConstFold :: Nil, _ => Nil) |
| 87 | + assert(false, "unsatisfiable constraint should throw exception, but not") |
| 88 | + } catch { |
| 89 | + case _: Exception => |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + def orderingTwoPlugins1 = { |
| 95 | + object M1 extends TestPlugin { |
| 96 | + override val runsAfter = Set(classOf[P3d]) |
| 97 | + override val runsBefore = Set(M2.getClass, classOf[P7], classOf[P8]) |
| 98 | + } |
| 99 | + object M2 extends TestPlugin { |
| 100 | + override val runsAfter = Set(classOf[P3d]) |
| 101 | + override val runsBefore = Set(classOf[P7], classOf[P8]) |
| 102 | + } |
| 103 | + |
| 104 | + // M1 inserted to plan first |
| 105 | + val updatedPlan1 = Plugins.schedule(basicPlan, M1 :: M2 :: Nil, _ => Nil) |
| 106 | + assert(updatedPlan1(6)(0) eq M1) |
| 107 | + assert(updatedPlan1(7)(0) eq M2) |
| 108 | + |
| 109 | + // M2 inserted to plan first |
| 110 | + val updatedPlan2 = Plugins.schedule(basicPlan, M2 :: M1 :: Nil, _ => Nil) |
| 111 | + assert(updatedPlan2(6)(0) eq M1) |
| 112 | + assert(updatedPlan2(7)(0) eq M2) |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + def orderingTwoPlugins2 = { |
| 117 | + object M1 extends TestPlugin { |
| 118 | + override val runsAfter = Set(classOf[P3d], M2.getClass) |
| 119 | + } |
| 120 | + object M2 extends TestPlugin { |
| 121 | + override val runsAfter = Set(classOf[P3d]) |
| 122 | + override val runsBefore = Set(classOf[P7], classOf[P8]) |
| 123 | + } |
| 124 | + |
| 125 | + // M1 inserted to plan first |
| 126 | + val updatedPlan1 = Plugins.schedule(basicPlan, M1 :: M2 :: Nil, _ => Nil) |
| 127 | + assert(updatedPlan1(4)(0) eq M1) |
| 128 | + assert(updatedPlan1(3)(0) eq M2) |
| 129 | + |
| 130 | + // M2 inserted to plan first |
| 131 | + val updatedPlan2 = Plugins.schedule(basicPlan, M2 :: M1 :: Nil, _ => Nil) |
| 132 | + assert(updatedPlan2(7)(0) eq M1) |
| 133 | + assert(updatedPlan2(6)(0) eq M2) |
| 134 | + } |
| 135 | +} |
0 commit comments