Skip to content

Implement assertGoldenFile and assertGoldenLiteral tests #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 28, 2025
Merged

Conversation

lihaoyi
Copy link
Member

@lihaoyi lihaoyi commented Jul 28, 2025

This PR implements assertGoldenLiteral and assertGoldenFile, which are assertions that not only compare the given values (literal v.s. literal or string v.s. file contents respectively), but can take the UTEST_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 new utest.framework.GoldenFix.Span[T] implicit conversion similar to sourcecode.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 use utest.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 preserved

Inspired by Jane Street's https://blog.janestreet.com/the-joy-of-expect-tests/[What if writing tests was a joyful experience?] blog post

@lihaoyi lihaoyi merged commit 674f82d into master Jul 28, 2025
3 of 4 checks passed
@lihaoyi lihaoyi deleted the golden-tests branch July 28, 2025 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant