Skip to content

Commit

Permalink
FormatTests: ease check when test fails to parse
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 7, 2024
1 parent cf477fe commit eee73a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
15 changes: 10 additions & 5 deletions scalafmt-tests/src/test/scala/org/scalafmt/FormatTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.scalafmt.util._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.meta.parsers.ParseException

// TODO(olafur) property test: same solution without optimization or timeout.

Expand Down Expand Up @@ -76,16 +77,20 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
t.style.copy(runner = scalafmtRunner(runner, debug2)),
filename = t.filename
)
val formattedAgain = result2.formatted match {
case Formatted.Failure(e) => throw FormatException(e, obtained)
case Formatted.Success(code) => code
}
debug2.printTest()
val result2Either = result2.formatted.toEither
def getFormattedAgain(failOK: Boolean) = result2Either match {
case Left(e: ParseException) if !failOK =>
"test does not parse: " + parseException2Message(e, obtained)
case Left(e) => throw FormatException(e, obtained)
case Right(code) => code
}
val formattedAgain = getFormattedAgain(onlyManual)
if (onlyManual)
assertEquals(formattedAgain, obtained, "Idempotency violated")
else {
assertEquals(
if (formattedAgain == obtained) formattedAgain
if (formattedAgain == obtained || result2Either.isLeft) formattedAgain
else "Idempotency violated\n" + formattedAgain,
t.expected
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ trait FormatAssertions {

def parseException2Message(e: ParseException, obtained: String): String = {
val range = 3
val lines = obtained.linesIterator
lines.drop(e.pos.startLine - range)
val pre = lines.take(range).mkString("\n")
val post = lines.take(range).mkString("\n")
val arrow = (" " * (e.pos.startColumn - 2)) + "^"
s"""$pre
|$arrow
|${e.getMessage}
|$post
|$obtained
|""".stripMargin
val lines = obtained.linesIterator.drop(e.pos.startLine - range)
Seq(
e.shortMessage,
lines.take(range).mkString("\n"), // pre
(" " * e.pos.startColumn) + "^", // arrow
lines.take(range).mkString("\n"), // post
"====== full result: ======",
obtained.stripTrailing()
).filter(_.nonEmpty).mkString("", "\n", "\n")
}
}

0 comments on commit eee73a0

Please sign in to comment.