@@ -4,49 +4,48 @@ import utest.framework.{GoldenFix}
4
4
import utest .{AssertionError , TestValue }
5
5
import java .nio .file .{Files , Path }
6
6
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),
20
15
)
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))
24
27
}
25
28
}
26
29
}
27
30
28
- def assertGoldenLiteral (testValue : Any , golden : GoldenFix .Span [Any ])
31
+ def assertGoldenLiteral (actualValue : Any , goldenLiteral : GoldenFix .Span [Any ])
29
32
(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)
43
42
} else {
44
43
reporter.apply(
45
44
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
50
49
)
51
50
)
52
51
}
0 commit comments