Implement assertGoldenFile
and assertGoldenLiteral
tests
#394
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements
assertGoldenLiteral
andassertGoldenFile
, which are assertions that not only compare the given values (literal v.s. literal or string v.s. file contents respectively), but can take theUTEST_UPDATE_GOLDEN_TESTS
environment variable to automatically update the source code and golden files to new values.assertGoldenFile
is straightforward: if the contents of the specified file does not match the given string,UTEST_UPDATE_GOLDEN_TESTS
makes utest over-write that file with the given string.assertGoldenLiteral
is more tricky: this takes a newutest.framework.GoldenFix.Span[T]
implicit conversion similar tosourcecode.Text
, but instead of just capturing the text contents of the expression it captures the sourcefile and the start/end offset of the literal within it. If the two values do not match,UTEST_UPDATE_GOLDEN_TESTS
makes utest useutest.shaded.pprint.apply
to pretty-print the given expression and the sourcefile/start/end metadata to over-write the original source file in-place. Some extra book-keeping is necessary to make sure the offsets line up even when there are multiple edits in the same file, and to ensure the indentation of start of the expression is preservedInspired by Jane Street's https://blog.janestreet.com/the-joy-of-expect-tests/[What if writing tests was a joyful experience?] blog post