| Title: | Convenient Snapshot Testing Functions for Packages |
|---|---|
| Description: | Provides convenient snapshot testing functions for packages, including expect_snapshot_data() for data.frames and expect_snapshot_object() for any R object. |
| Authors: | Douglas Ezra Morrison [aut, cre] |
| Maintainer: | Douglas Ezra Morrison <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-06-03 18:40:51 UTC |
| Source: | https://github.com/d-morrison/snapr |
Provides convenient snapshot testing functions for packages, including expect_snapshot_data() for data.frames and expect_snapshot_object() for any R object.
Maintainer: Douglas Ezra Morrison [email protected]
Authors:
Douglas Ezra Morrison [email protected]
Useful links:
Report bugs at https://github.com/d-morrison/snapr/issues
This comparison function loads RDS files and compares the deserialized R objects rather than raw bytes. This enables better visualization in snapshot_review() by comparing the actual object structure rather than binary serialization.
For meaningful diffs in testthat's snapshot_review(), this function
compares the R objects after deserialization using waldo::compare().
compare_file_object(old, new, print = FALSE, ...)compare_file_object(old, new, print = FALSE, ...)
old |
Path to the old (reference) RDS file |
new |
Path to the new RDS file to compare |
print |
logical whether to print waldo::compare output to R console; can become very long for complex objects like lms |
... |
Arguments passed on to
|
logical TRUE if objects are identical, FALSE otherwise
old_obj <- list(a = 1, b = 2) new_obj <- list(a = 1, b = 3) old_path <- tempfile(fileext = ".rds") new_path <- tempfile(fileext = ".rds") saveRDS(old_obj, old_path) saveRDS(new_obj, new_path) compare_file_object(old_path, new_path)old_obj <- list(a = 1, b = 2) new_obj <- list(a = 1, b = 3) old_path <- tempfile(fileext = ".rds") new_path <- tempfile(fileext = ".rds") saveRDS(old_obj, old_path) saveRDS(new_obj, new_path) compare_file_object(old_path, new_path)
Returns "darwin" for macOS, NULL for other platforms (Linux/Windows).
darwin_variant()darwin_variant()
This is a specialized variant function for cases where only macOS
produces different results while Linux and Windows are identical.
Use this instead of system_os() when:
macOS produces unique output (e.g., platform-specific math libraries)
Linux and Windows produce identical results
You want to maintain a single snapshot for Linux/Windows
Common use cases:
JAGS MCMC output (macOS uses different floating-point arithmetic)
Platform-specific numerical computations
"darwin" on macOS, NULL on other platforms
darwin_variant()darwin_variant()
copied from https://github.com/bcgov/ssdtools with permission (https://github.com/bcgov/ssdtools/issues/379)
expect_snapshot_data(x, name, digits = 6, ...)expect_snapshot_data(x, name, digits = 6, ...)
x |
a data.frame to snapshot |
name |
character snapshot name |
digits |
|
... |
Arguments passed on to
|
NULL (from testthat::expect_snapshot_file())
# expect_snapshot_data() must be called inside a test_that() block with # testthat 3rd edition active. Outside a test suite, the snapshot is # skipped because there is no reference file to compare against. withr::with_tempdir({ testthat::test_that("iris snapshot", { testthat::local_edition(3) expect_snapshot_data(iris, name = "iris") }) })# expect_snapshot_data() must be called inside a test_that() block with # testthat 3rd edition active. Outside a test suite, the snapshot is # skipped because there is no reference file to compare against. withr::with_tempdir({ testthat::test_that("iris snapshot", { testthat::local_edition(3) expect_snapshot_data(iris, name = "iris") }) })
A flexible wrapper around testthat::expect_snapshot_file() that allows
snapshotting any R object, not just data.frames. This function provides
a convenient interface for snapshot testing with sensible defaults for
serialization.
When using RDS format (the default), snapshots are compared using
waldo::compare() which provides rich, visual diffs in
testthat::snapshot_review(). This makes it much easier to review
changes to complex R objects.
expect_snapshot_object( x, name, writer = save_rds, print = FALSE, tolerance = NULL, ... )expect_snapshot_object( x, name, writer = save_rds, print = FALSE, tolerance = NULL, ... )
x |
An R object to snapshot. Can be any R object including lists, models, data.frames, vectors, etc. |
name |
character snapshot name (file extension added automatically) |
writer |
function Function to write the object to a file.
Default is |
print |
logical whether to print waldo::compare output to R console; can become very long for complex objects like lms |
tolerance |
If non- |
... |
Arguments passed on to
|
When using RDS format (the default), snapshots can vary across R versions
and platforms even with fixed serialization versions. Consider using
variant = platform_variant() for RDS snapshots to handle these
differences, or use text-based formats like JSON or deparse for more
stable snapshots across platforms and versions.
The RDS comparison uses waldo::compare() internally, which provides
rich visual diffs in testthat::snapshot_review(). This is particularly
useful for complex objects like models, nested lists, or data structures
where byte-level comparison would be difficult to interpret.
NULL (from testthat::expect_snapshot_file())
# expect_snapshot_object() must be called inside a test_that() block with # testthat 3rd edition active. Outside a test suite, the snapshot is # skipped because there is no reference file to compare against. withr::with_tempdir({ testthat::test_that("snapshot examples", { testthat::local_edition(3) # Snapshot a list (RDS format with platform/version variant) expect_snapshot_object( list(a = 1, b = 2), name = "config", variant = platform_variant() ) # Snapshot a model model <- lm(mpg ~ wt, data = mtcars) expect_snapshot_object( model, name = "model", variant = platform_variant() ) # Snapshot with JSON format (for human-readable diffs) # Text formats don't need variants expect_snapshot_object(iris[1:5, ], name = "iris", writer = save_json) # Snapshot with deparse format expect_snapshot_object( list(x = 1:5), name = "simple_list", writer = save_deparse ) }) })# expect_snapshot_object() must be called inside a test_that() block with # testthat 3rd edition active. Outside a test suite, the snapshot is # skipped because there is no reference file to compare against. withr::with_tempdir({ testthat::test_that("snapshot examples", { testthat::local_edition(3) # Snapshot a list (RDS format with platform/version variant) expect_snapshot_object( list(a = 1, b = 2), name = "config", variant = platform_variant() ) # Snapshot a model model <- lm(mpg ~ wt, data = mtcars) expect_snapshot_object( model, name = "model", variant = platform_variant() ) # Snapshot with JSON format (for human-readable diffs) # Text formats don't need variants expect_snapshot_object(iris[1:5, ], name = "iris", writer = save_json) # Snapshot with deparse format expect_snapshot_object( list(x = 1:5), name = "simple_list", writer = save_deparse ) }) })
Returns a variant string combining OS and R major.minor version. This is useful for RDS snapshots that vary by both platform and R version.
platform_variant()platform_variant()
RDS files can produce different binary output across R versions even when using the same serialization version. This function creates variant strings like "linux-4.4", "darwin-4.5", "windows-4.4" to handle these differences.
Use this function with testthat::expect_snapshot_file() when testing
RDS files or other formats that vary by both OS and R version.
A character string like "linux-4.4", "darwin-4.5", or "windows-4.4"
platform_variant()platform_variant()
Returns the name of the current operating system in lowercase.
This is a simple wrapper around Sys.info()[["sysname"]].
system_os()system_os()
This function is used with testthat's variant parameter in
testthat::expect_snapshot_file() to create OS-specific snapshots
when file formats produce different output across platforms.
Common use cases:
RDS files: Binary serialization format that can differ across OS
Platform-specific numeric computations (e.g., MCMC algorithms)
Files with platform-specific line endings or encodings
A character string: "linux", "darwin" (macOS), or "windows"
system_os()system_os()