Skip to content

Commit 606e7d4

Browse files
ckipp01Kordyjan
authored andcommitted
refactor(test): simplify the code action testing
Instead of keeping track of the delta, we simply reverse the patches and apply them. This shouldn't be problematic because we do a check beforehand ensuring that there aren't overlapping patches. This greatly simplifies the way these are applied for the tests while keeping the same behavior. [Cherry-picked 1cd8007]
1 parent 7d2078e commit 606e7d4

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala

+6-23
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,10 @@ class CodeActionTest extends DottyTest:
8282
p2
8383
else assertFailed("Expected a patch attatched to this action, but it was empty")
8484

85-
val delta = patches
86-
.map: patch =>
87-
patch.replacement.length - (patch.srcPos.end - patch.srcPos.start)
88-
.sum
85+
val result = patches.reverse.foldLeft(code): (newCode, patch) =>
86+
import scala.language.unsafeNulls
87+
val start = newCode.substring(0, patch.srcPos.start)
88+
val ending = newCode.substring(patch.srcPos.end, newCode.length)
89+
start + patch.replacement + ending
8990

90-
val result = new Array[Char](source.length + delta)
91-
92-
@tailrec def loop(ps: List[ActionPatch], inIdx: Int, outIdx: Int): Unit =
93-
def copy(upTo: Int): Int =
94-
val untouched = upTo - inIdx
95-
System.arraycopy(source, inIdx, result, outIdx, untouched)
96-
outIdx + untouched
97-
98-
ps match
99-
case patch @ ActionPatch(srcPos, replacement) :: ps1 =>
100-
val outNew = copy(srcPos.start)
101-
replacement.copyToArray(result, outNew)
102-
loop(ps1, srcPos.end, outNew + replacement.length)
103-
case Nil =>
104-
val outNew = copy(source.length)
105-
assert(outNew == result.length, s"$outNew != ${result.length}")
106-
107-
loop(patches, 0, 0)
108-
assertEquals(expected, result.mkString)
91+
assertEquals(expected, result)

0 commit comments

Comments
 (0)