Skip to content

Commit 07b4f61

Browse files
committed
improvements to golden testing
1 parent cc0228f commit 07b4f61

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

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

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,48 @@ import utest.framework.{GoldenFix}
44
import utest.{AssertionError, TestValue}
55
import java.nio.file.{Files, Path}
66
trait AssertsPlatformSpecific {
7-
def assertGoldenFile(testValue: String, path: Path)(implicit reporter: GoldenFix.Reporter): Unit = {
8-
val goldenFileContents = if (Files.exists(path)) Files.readString(path) else ""
9-
if (goldenFileContents != testValue) {
10-
if (!sys.env.contains("UTEST_UPDATE_GOLDEN_TESTS")) {
11-
throw new AssertionError(
12-
"Value does not match golden file contents\n" +
13-
"Run UTEST_UPDATE_GOLDEN_TESTS=1 to update golden file " + path,
14-
Seq(
15-
TestValue.Equality(
16-
TestValue.Single("goldenFileContents", None, goldenFileContents),
17-
TestValue.Single("testValue", None, testValue),
18-
)
19-
)
7+
private def throwAssertionError(path: String, goldenValue: Any, actualValue: Any): Unit = {
8+
throw new AssertionError(
9+
s"Actual value does not match golden data in file $path\n" +
10+
"Run tests with UTEST_UPDATE_GOLDEN_TESTS=1 to apply the following patch to update the golden value",
11+
Seq(
12+
TestValue.Equality(
13+
TestValue.Single("goldenValue", None, goldenValue),
14+
TestValue.Single("actualValue", None, actualValue),
2015
)
21-
}
22-
else {
23-
reporter.apply(GoldenFix(path, testValue, 0, goldenFileContents.length))
16+
)
17+
)
18+
19+
}
20+
def assertGoldenFile(actualValue: String, goldenFilePath: Path)(implicit reporter: GoldenFix.Reporter): Unit = {
21+
val goldenFileContents = if (Files.exists(goldenFilePath)) Files.readString(goldenFilePath) else ""
22+
if (goldenFileContents != actualValue) {
23+
if (!sys.env.get("UTEST_UPDATE_GOLDEN_TESTS").exists(_.nonEmpty)) {
24+
throwAssertionError(goldenFilePath.toString, goldenFileContents, actualValue)
25+
} else {
26+
reporter.apply(GoldenFix(goldenFilePath, actualValue, 0, goldenFileContents.length))
2427
}
2528
}
2629
}
2730

28-
def assertGoldenLiteral(testValue: Any, golden: GoldenFix.Span[Any])
31+
def assertGoldenLiteral(actualValue: Any, goldenLiteral: GoldenFix.Span[Any])
2932
(implicit reporter: GoldenFix.Reporter): Unit = {
30-
val goldenValue = golden.value
31-
if (testValue != goldenValue) {
32-
if (!sys.env.contains("UTEST_UPDATE_GOLDEN_TESTS")) {
33-
throw new AssertionError(
34-
"Value does not match golden literal contents\n" +
35-
"Run UTEST_UPDATE_GOLDEN_TESTS=1 to update golden literal in " + golden.sourceFile,
36-
Seq(
37-
TestValue.Equality(
38-
TestValue.Single("testValue", None, testValue),
39-
TestValue.Single("goldenValue", None, goldenValue),
40-
)
41-
)
42-
)
33+
Predef.assert(
34+
goldenLiteral != null,
35+
"assertGoldenLiteral does not allow `null` as the golden literal," +
36+
"please use `()` if you want a placeholder for `UTEST_UPDATE_GOLDEN_TESTS=1` to fill in"
37+
)
38+
val goldenValue = goldenLiteral.value
39+
if (actualValue != goldenValue) {
40+
if (!sys.env.get("UTEST_UPDATE_GOLDEN_TESTS").exists(_.nonEmpty)) {
41+
throwAssertionError(goldenLiteral.sourceFile, goldenValue, actualValue)
4342
} else {
4443
reporter.apply(
4544
GoldenFix(
46-
Path.of(golden.sourceFile),
47-
utest.shaded.pprint.PPrinter.BlackWhite.apply(golden.value).plainText,
48-
golden.startOffset,
49-
golden.endOffset
45+
Path.of(goldenLiteral.sourceFile),
46+
utest.shaded.pprint.PPrinter.BlackWhite.apply(actualValue).plainText,
47+
goldenLiteral.startOffset,
48+
goldenLiteral.endOffset
5049
)
5150
)
5251
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ object GoldenFix {
4141
lineStarts.toArray
4242
}
4343

44-
def columnLookup(index: Int, lineNumberLookup: Array[Int]): Int = {
44+
def lineColumnLookup(index: Int, lineNumberLookup: Array[Int]): (Int, Int) = {
4545
val line = lineNumberLookup.indexWhere(_ > index) match {
4646
case -1 => lineNumberLookup.length - 1
4747
case n => math.max(0, n - 1)
4848
}
49-
index - lineNumberLookup(line)
49+
(line, index - lineNumberLookup(line))
5050
}
5151

5252
trait Reporter {
@@ -55,10 +55,8 @@ object GoldenFix {
5555

5656
def applyAll(fixes: Seq[GoldenFix]): Unit = {
5757
for((path, group) <- fixes.groupBy(_.path)){
58-
println(s"Applying ${group.size} golden fixes to file $path")
59-
58+
println(s"UTEST_UPDATE_GOLDEN_TESTS detected, uTest applying ${group.size} golden fixes to file $path")
6059
val text = java.nio.file.Files.readString(path)
61-
6260
java.nio.file.Files.writeString(path, applyToText(text, group))
6361
}
6462
}
@@ -73,8 +71,10 @@ object GoldenFix {
7371
val lineNumberLookupTable = lineNumberLookup(text)
7472
var lengthOffset = 0
7573
for (fix <- sorted) {
76-
val col = columnLookup(fix.startOffset, lineNumberLookupTable)
77-
val indentedContents = fix.contents.linesWithSeparators.mkString(" " * col)
74+
val (startLine, startCol) = lineColumnLookup(fix.startOffset, lineNumberLookupTable)
75+
val (endLine, endCol) = lineColumnLookup(fix.endOffset, lineNumberLookupTable)
76+
println(s"Updating line:column $startLine:$startCol to $endLine:$endCol")
77+
val indentedContents = fix.contents.linesWithSeparators.mkString(" " * startCol)
7878
text = text.patch(
7979
fix.startOffset + lengthOffset, indentedContents,
8080
fix.endOffset - fix.startOffset

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ trait TestSuitePlatformSpecific {
99

1010
object TestSuitePlatformSpecific {
1111
def processGolden(allSuites: Seq[utest.TestSuite]): Unit = {
12-
println("TestSuitePlatformSpecific.processGolden: " + allSuites.length)
1312
if (sys.env.contains("UTEST_UPDATE_GOLDEN_TESTS")) {
1413
val goldenFixes = allSuites.flatMap { suite =>
1514
suite.utestGoldenReports.synchronized {

0 commit comments

Comments
 (0)