|
| 1 | +package dotty.tools.dotc.reporting |
| 2 | + |
| 3 | +import dotty.tools.DottyTest |
| 4 | +import dotty.tools.dotc.rewrites.Rewrites |
| 5 | +import dotty.tools.dotc.rewrites.Rewrites.ActionPatch |
| 6 | +import dotty.tools.dotc.util.SourceFile |
| 7 | + |
| 8 | +import scala.annotation.tailrec |
| 9 | +import scala.jdk.CollectionConverters.* |
| 10 | +import scala.runtime.Scala3RunTime.assertFailed |
| 11 | + |
| 12 | +import org.junit.Test |
| 13 | + |
| 14 | +/** This is a test suite that is meant to test the actions acttatched to the |
| 15 | + * diagnostic for a given code snippet. |
| 16 | + */ |
| 17 | +class CodeActionTest extends DottyTest { |
| 18 | + |
| 19 | + @Test def convertToFunctionValue = |
| 20 | + checkCodeAction( |
| 21 | + """|object Test: |
| 22 | + | def x: Int = 3 |
| 23 | + | val test = x _ |
| 24 | + |""".stripMargin, |
| 25 | + "Make function value", |
| 26 | + """|object Test: |
| 27 | + | def x: Int = 3 |
| 28 | + | val test = (() => x) |
| 29 | + |""".stripMargin |
| 30 | + ) |
| 31 | + |
| 32 | + private def checkCodeAction(code: String, title: String, expected: String) = |
| 33 | + val source = SourceFile.virtual("test", code).content |
| 34 | + val result = checkCompile("typer", code): |
| 35 | + case (_, ctx) => |
| 36 | + val diagnostics = ctx.reporter.pendingMessages |
| 37 | + assert(diagnostics.size == 1, s"Only expecting a single diagnostic here, but got ${diagnostics.size}") |
| 38 | + |
| 39 | + val diagnostic = diagnostics.head |
| 40 | + val actions = diagnostic.msg.actions.asScala.toList |
| 41 | + assert(actions.size == 1, "Right now only a signle action is being tested for. This needs to be updated this fails.") |
| 42 | + |
| 43 | + // TODO account for more than 1 action |
| 44 | + val action = actions.head |
| 45 | + assert(action.title == title) |
| 46 | + val patches = action.patches.asScala.toList |
| 47 | + if patches.nonEmpty then |
| 48 | + patches.reduceLeft {(p1, p2) => |
| 49 | + assert(p1.srcPos.span.end <= p2.srcPos.span.start, s"overlapping patches $p1 and $p2") |
| 50 | + p2 |
| 51 | + } |
| 52 | + else assertFailed("Expected a patch attatched to this action, but it was empty") |
| 53 | + |
| 54 | + val result = new Array[Char](source.length) |
| 55 | + |
| 56 | + @tailrec def loop(ps: List[ActionPatch], inIdx: Int, outIdx: Int): Unit = |
| 57 | + def copy(upTo: Int): Int = |
| 58 | + val untouched = upTo - inIdx |
| 59 | + System.arraycopy(source, inIdx, result, outIdx, untouched) |
| 60 | + outIdx + untouched |
| 61 | + |
| 62 | + ps match |
| 63 | + case patch @ ActionPatch(srcPos, replacement) :: ps1 => |
| 64 | + val outNew = copy(srcPos.start) |
| 65 | + replacement.copyToArray(result, outNew) |
| 66 | + loop(ps1, srcPos.end, outNew + replacement.length) |
| 67 | + case Nil => |
| 68 | + val outNew = copy(source.length) |
| 69 | + assert(outNew == result.length, s"$outNew != ${result.length}") |
| 70 | + |
| 71 | + loop(patches, 0, 0) |
| 72 | + assert(result.mkString == expected) |
| 73 | +} |
0 commit comments