Skip to content

Commit 88ee0e0

Browse files
committed
make golden literal printing configurable
1 parent 07b4f61 commit 88ee0e0

File tree

8 files changed

+34
-20
lines changed

8 files changed

+34
-20
lines changed

utest/src-js/utest/framework/TestSuitePlatformSpecific.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ trait TestSuitePlatformSpecific {
55
}
66

77
object TestSuitePlatformSpecific {
8-
def processGolden(allSuites: Seq[utest.TestSuite]): Unit = ()
8+
def processGolden(allSuites: Seq[utest.TestSuite], goldenLiteralPrinter: Any => String): Unit = ()
99
}

utest/src-jvm/utest/asserts/AssertsPlatformSpecific.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ trait AssertsPlatformSpecific {
2323
if (!sys.env.get("UTEST_UPDATE_GOLDEN_TESTS").exists(_.nonEmpty)) {
2424
throwAssertionError(goldenFilePath.toString, goldenFileContents, actualValue)
2525
} else {
26-
reporter.apply(GoldenFix(goldenFilePath, actualValue, 0, goldenFileContents.length))
26+
reporter.apply(GoldenFix(goldenFilePath, new GoldenFix.Literal(actualValue), 0, goldenFileContents.length))
2727
}
2828
}
2929
}
@@ -43,7 +43,7 @@ trait AssertsPlatformSpecific {
4343
reporter.apply(
4444
GoldenFix(
4545
Path.of(goldenLiteral.sourceFile),
46-
utest.shaded.pprint.PPrinter.BlackWhite.apply(actualValue).plainText,
46+
actualValue,
4747
goldenLiteral.startOffset,
4848
goldenLiteral.endOffset
4949
)

utest/src-jvm/utest/framework/GoldenFix.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package utest.framework
33
import scala.collection.mutable.ArrayBuffer
44

55
case class GoldenFix(path: java.nio.file.Path,
6-
contents: String,
6+
contents: Any,
77
startOffset: Int,
88
endOffset: Int)
99

1010
object GoldenFix {
11+
class Literal(s: String) {
12+
override def toString = s
13+
}
1114
// vendored from
1215
// https://github.com/com-lihaoyi/fastparse/blob/d8f95daef21d6e6f9734624237f993f4cebfa881/fastparse/src/fastparse/internal/Util.scala
1316
//
@@ -53,15 +56,15 @@ object GoldenFix {
5356
def apply(v: GoldenFix): Unit
5457
}
5558

56-
def applyAll(fixes: Seq[GoldenFix]): Unit = {
59+
def applyAll(fixes: Seq[GoldenFix], goldenLiteralPrinter: Any => String): Unit = {
5760
for((path, group) <- fixes.groupBy(_.path)){
5861
println(s"UTEST_UPDATE_GOLDEN_TESTS detected, uTest applying ${group.size} golden fixes to file $path")
5962
val text = java.nio.file.Files.readString(path)
60-
java.nio.file.Files.writeString(path, applyToText(text, group))
63+
java.nio.file.Files.writeString(path, applyToText(text, group, goldenLiteralPrinter))
6164
}
6265
}
6366

64-
def applyToText(text0: String, fixes: Seq[GoldenFix]): String = {
67+
def applyToText(text0: String, fixes: Seq[GoldenFix], goldenLiteralPrinter: Any => String): String = {
6568
var text = text0
6669
val sorted = fixes.map(t => t.startOffset -> t).toMap.map(_._2).toSeq.sortBy(_.startOffset)
6770
sorted.sliding(2).collect{ case Seq(prev, next) =>
@@ -74,7 +77,7 @@ object GoldenFix {
7477
val (startLine, startCol) = lineColumnLookup(fix.startOffset, lineNumberLookupTable)
7578
val (endLine, endCol) = lineColumnLookup(fix.endOffset, lineNumberLookupTable)
7679
println(s"Updating line:column $startLine:$startCol to $endLine:$endCol")
77-
val indentedContents = fix.contents.linesWithSeparators.mkString(" " * startCol)
80+
val indentedContents = goldenLiteralPrinter(fix.contents).linesWithSeparators.mkString(" " * startCol)
7881
text = text.patch(
7982
fix.startOffset + lengthOffset, indentedContents,
8083
fix.endOffset - fix.startOffset

utest/src-jvm/utest/framework/TestSuitePlatformSpecific.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ trait TestSuitePlatformSpecific {
88
}
99

1010
object TestSuitePlatformSpecific {
11-
def processGolden(allSuites: Seq[utest.TestSuite]): Unit = {
11+
def processGolden(allSuites: Seq[utest.TestSuite], goldenLiteralPrinter: Any => String): Unit = {
1212
if (sys.env.contains("UTEST_UPDATE_GOLDEN_TESTS")) {
1313
val goldenFixes = allSuites.flatMap { suite =>
1414
suite.utestGoldenReports.synchronized {
1515
suite.utestGoldenReports.toList
1616
}
1717
}
18-
GoldenFix.applyAll(goldenFixes)
18+
GoldenFix.applyAll(goldenFixes, goldenLiteralPrinter)
1919
}
2020
}
2121
}

utest/src-native/utest/framework/TestSuitePlatformSpecific.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ trait TestSuitePlatformSpecific {
66

77

88
object TestSuitePlatformSpecific {
9-
def processGolden(allSuites: Seq[utest.TestSuite]): Unit = ()
9+
def processGolden(allSuites: Seq[utest.TestSuite], goldenLiteralPrinter: Any => String): Unit = ()
1010
}

utest/src/utest/framework/Formatter.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ trait Formatter {
3333
else fansi.Color.Red
3434
)
3535

36+
def goldenLiteralPrinter(x: Any): String = pprint.PPrinter.BlackWhite.apply(x).plainText
37+
3638
def formatMillisColor = toggledColor(fansi.Bold.Faint)
3739

3840
def exceptionStackFrameHighlighter(s: StackTraceElement): Boolean = true

utest/src/utest/runner/MasterRunner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class MasterRunner(args: Array[String],
4545
val allSuites = collection.mutable.Buffer.empty[TestSuite]
4646
override def registerSuite(x: TestSuite) = allSuites.synchronized { allSuites.append(x) }
4747
def done(): String = {
48-
utest.framework.TestSuitePlatformSpecific.processGolden(allSuites.toSeq)
48+
utest.framework.TestSuitePlatformSpecific.processGolden(allSuites.toSeq, formatter.goldenLiteralPrinter)
4949

5050
teardown()
5151
val total = success.get() + failure.get()

utest/test/src-jvm/test/utest/GoldenFixTests.scala

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,35 @@ object GoldenFixTests extends utest.TestSuite {
2525
test("single") {
2626
val replaced = GoldenFix.applyToText(
2727
"0123456789",
28-
Seq(GoldenFix(null, "Hello", 0, 0))
28+
Seq(GoldenFix(null, new GoldenFix.Literal("Hello"), 0, 0)),
29+
utest.shaded.pprint.PPrinter.BlackWhite.apply(_).plainText
2930
)
3031
Predef.assert(replaced == "Hello0123456789")
3132
}
3233
test("middle") {
3334
val replaced = GoldenFix.applyToText(
3435
"0123456789",
35-
Seq(GoldenFix(null, "Hello", 5, 5))
36+
Seq(GoldenFix(null, new GoldenFix.Literal("Hello"), 5, 5)),
37+
utest.shaded.pprint.PPrinter.BlackWhite.apply(_).plainText
3638
)
3739
Predef.assert(replaced == "01234Hello56789")
3840
}
3941
test("replace") {
4042
val replaced = GoldenFix.applyToText(
4143
"0123456789",
42-
Seq(GoldenFix(null, "Hello", 4, 6))
44+
Seq(GoldenFix(null, new GoldenFix.Literal("Hello"), 4, 6)),
45+
utest.shaded.pprint.PPrinter.BlackWhite.apply(_).plainText
4346
)
4447
Predef.assert(replaced == "0123Hello6789")
4548
}
4649
test("replaceTwice") {
4750
val replaced = GoldenFix.applyToText(
4851
"0123456789",
49-
Seq(GoldenFix(null, "Hello", 0, 1), GoldenFix(null, "World", 5, 6))
52+
Seq(
53+
GoldenFix(null, new GoldenFix.Literal("Hello"), 0, 1),
54+
GoldenFix(null, new GoldenFix.Literal("World"), 5, 6)
55+
),
56+
utest.shaded.pprint.PPrinter.BlackWhite.apply(_).plainText
5057
)
5158
Predef.assert(replaced == "Hello1234World6789")
5259
}
@@ -55,7 +62,8 @@ object GoldenFixTests extends utest.TestSuite {
5562
"""Hello
5663
|World
5764
|""".stripMargin,
58-
Seq(GoldenFix(null, "I am\nCow", 2, 4))
65+
Seq(GoldenFix(null, new GoldenFix.Literal("I am\nCow"), 2, 4)),
66+
utest.shaded.pprint.PPrinter.BlackWhite.apply(_).plainText
5967
)
6068
Predef.assert(
6169
replaced ==
@@ -71,9 +79,10 @@ object GoldenFixTests extends utest.TestSuite {
7179
|World
7280
|""".stripMargin,
7381
Seq(
74-
GoldenFix(null, "I am\nCow", 2, 4),
75-
GoldenFix(null, "Hear\nMe\nMoo", 7, 8)
76-
)
82+
GoldenFix(null, new GoldenFix.Literal("I am\nCow"), 2, 4),
83+
GoldenFix(null, new GoldenFix.Literal("Hear\nMe\nMoo"), 7, 8)
84+
),
85+
utest.shaded.pprint.PPrinter.BlackWhite.apply(_).plainText
7786
)
7887
Predef.assert(
7988
replaced ==

0 commit comments

Comments
 (0)