Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatTests: ease check when test fails to parse #3757

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
Loading