diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala index a8f8b99b0c..91b2774b05 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala @@ -5,6 +5,7 @@ import org.scalafmt.config._ import org.scalafmt.internal._ import org.scalafmt.rewrite.Rewrite import org.scalafmt.sysops.FileOps +import org.scalafmt.util.LoggerOps import org.scalafmt.util.MarkdownParser import scala.meta.Input @@ -90,8 +91,8 @@ object Scalafmt { val res = MarkdownParser .transformMdoc(code)(doFormatOne(_, mdocStyle, file, range)) style.lineEndings match { - case Some(LineEndings.unix) => res.map(_.replaceAll("\r*\n", "\n")) - case Some(LineEndings.windows) => res.map(_.replaceAll("\r*\n", "\r\n")) + case Some(LineEndings.unix) => res.map(LoggerOps.lf) + case Some(LineEndings.windows) => res.map(LoggerOps.crlf) case _ => res } } else doFormatOne(code, style, file, range) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/LoggerOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/LoggerOps.scala index 5e06c83bd4..1a75c7b2f1 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/LoggerOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/LoggerOps.scala @@ -199,4 +199,8 @@ object LoggerOps { logger.debug(s"Total cost: ${finalState.cost}") } + def lf(str: String): String = str.replaceAll("\r*\n", "\n") + def crlf(str: String): String = str.replaceAll("\r*\n", "\r\n") + def showCR(str: String): String = str.replace("\r", "^M") + } diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala index 0c5c0e5a30..f85e856604 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala @@ -2,6 +2,7 @@ package org.scalafmt import org.scalafmt.Error.Incomplete import org.scalafmt.Error.SearchStateExploded +import org.scalafmt.config.LineEndings import org.scalafmt.rewrite.FormatTokensRewrite import org.scalafmt.sysops.FileOps import org.scalafmt.util._ @@ -33,7 +34,15 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions { override val tests = if (onlyManual) ManualTests.tests else UnitTests.tests tests.sortBy(x => (x.loc.path, x.loc.line)).withFilter(testShouldRun) - .foreach(runTest(run)) + .foreach { t => + runTest(run)(t) + if (t.style.lineEndings.isEmpty) runTest(run)(t.copy( + name = s"${t.name} [WIN]", + original = crlf(t.original), + expected = crlf(t.expected), + style = t.style.withLineEndings(LineEndings.windows), + )) + } def run(t: DiffTest): Unit = { // @note munit assertions take an implicit Location generated by macros at compile time @@ -80,7 +89,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions { } var debug2Opt: Option[Debug] = None def assertObtained(implicit loc: munit.Location) = { - assertEquals(obtained, t.expected) + assertEquals(showCR(obtained), showCR(t.expected)) assertVisits(debug, t.stateVisits, debug2Opt, t.stateVisits2) } debugResults += saveResult(t, obtained, debug) @@ -145,7 +154,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions { val explored = Debug.explored.get() logger.debug(s"Total explored: $explored") if (!onlyUnit && !onlyManual) - assertEquals(explored, 1259874, "total explored") + assertEquals(explored, 2519748, "total explored") val results = debugResults.result() // TODO(olafur) don't block printing out test results. // I don't want to deal with scalaz's Tasks :'(