From eee73a0f010544395fdf5e0146c0240290197298 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 7 Jan 2024 10:30:07 -0800 Subject: [PATCH] FormatTests: ease check when test fails to parse --- .../test/scala/org/scalafmt/FormatTests.scala | 15 +++++++++----- .../org/scalafmt/util/FormatAssertions.scala | 20 +++++++++---------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/FormatTests.scala b/scalafmt-tests/src/test/scala/org/scalafmt/FormatTests.scala index 25c5da04e6..772e2dce9b 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/FormatTests.scala @@ -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. @@ -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 ) diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/util/FormatAssertions.scala b/scalafmt-tests/src/test/scala/org/scalafmt/util/FormatAssertions.scala index d37646e1b3..f85bcbfaad 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/util/FormatAssertions.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/util/FormatAssertions.scala @@ -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") } }