Skip to content

Commit

Permalink
FormatAssertions: use filename to convert to input
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Feb 11, 2022
1 parent 4de41cc commit 7505d9f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ object Scalafmt {
} else
doFormatOne(code, style, file, range)

private[scalafmt] def toInput(code: String, file: String): Input.VirtualFile =
Input.VirtualFile(file, code)

private def doFormatOne(
code: String,
style: ScalafmtConfig,
Expand All @@ -159,7 +162,7 @@ object Scalafmt {
if (code.matches("\\s*")) Try("\n")
else {
val runner = style.runner
def codeToInput(srcCode: String) = Input.VirtualFile(file, srcCode)
val codeToInput: String => Input.VirtualFile = toInput(_, file)
val parsed = runner.parse(Rewrite(codeToInput(code), style, codeToInput))
parsed.fold(
_.details match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FidelityTest extends FunSuite with FormatAssertions {
ScalafmtConfig.default,
filename = example.filename
)
assertFormatPreservesAst(example.code, formatted.get)(
assertFormatPreservesAst(example.filename, example.code, formatted.get)(
scala.meta.parsers.Parse.parseSource,
Scala211
)
Expand Down
8 changes: 7 additions & 1 deletion scalafmt-tests/src/test/scala/org/scalafmt/FormatTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
!t.style.assumeStandardLibraryStripMargin &&
!FileOps.isAmmonite(t.filename) && !FileOps.isMarkdown(t.filename) &&
t.style.onTestFailure.isEmpty
) assertFormatPreservesAst(t.original, obtained, result.config.runner)
)
assertFormatPreservesAst(
t.filename,
t.original,
obtained,
result.config.runner
)
val debug2 = new Debug(onlyOne)
val result2 = Scalafmt.formatCode(
obtained,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ScalafmtProps extends FunSuite with FormatAssertions {
try {
Scalafmt.format(code, config) match {
case Formatted.Success(formatted) =>
assertFormatPreservesAst[Source](code, formatted)
assertFormatPreservesAst[Source](file.filename, code, formatted)
val formattedSecondTime = Scalafmt.format(formatted, config).get
try assertNoDiff(formattedSecondTime, formatted, "Idempotence")
catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream

import munit.internal.difflib.Diffs
import org.scalafmt.Error.{FormatterChangedAST, FormatterOutputDoesNotParse}
import org.scalafmt.Scalafmt
import org.scalafmt.config.ScalafmtRunner
import org.scalameta.logger

Expand All @@ -14,26 +15,29 @@ import scala.meta.{Dialect, Tree}
trait FormatAssertions {

def assertFormatPreservesAst(
filename: String,
original: String,
obtained: String,
runner: ScalafmtRunner
): Unit =
assertFormatPreservesAst(original, obtained)(
assertFormatPreservesAst(filename, original, obtained)(
runner.parser.parse,
runner.getDialect
)

def assertFormatPreservesAst[T <: Tree](
filename: String,
original: String,
obtained: String
)(implicit ev: Parse[T], dialect: Dialect): Unit = {
import scala.meta._
original.parse[T] match {
def toInput(code: String) = Scalafmt.toInput(code, filename)
toInput(original).parse[T] match {
case Parsed.Error(_, message, _) =>
logger.debug(original)
logger.debug(s"original does not parse $message")
case Parsed.Success(originalParsed) =>
dialect(obtained).parse[T] match {
toInput(obtained).parse[T] match {
case Parsed.Success(obtainedParsed) =>
StructurallyEqual(originalParsed, obtainedParsed) match {
case Right(_) => // OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ trait HasTests extends FormatAssertions {
)
val obtained = result.formatted.get
if (t.style.rewrite.rules.isEmpty)
assertFormatPreservesAst(t.original, obtained, result.config.runner)
assertFormatPreservesAst(
t.filename,
t.original,
obtained,
result.config.runner
)
assertNoDiff(obtained, t.expected)
}

Expand Down

0 comments on commit 7505d9f

Please sign in to comment.