From 5bde1e9411612c131fa48e14fce4229b425a1a12 Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Fri, 3 Apr 2026 14:51:19 -0700 Subject: [PATCH 1/6] Generated snapshot tests --- tests/testthat/_snaps/fit-gq.md | 115 ++++++++++++++++++ tests/testthat/_snaps/fit-laplace.md | 23 ++++ tests/testthat/_snaps/fit-mcmc.md | 127 ++++++++++++++++++++ tests/testthat/_snaps/fit-mle.md | 22 ++++ tests/testthat/_snaps/fit-vb.md | 88 ++++++++++++++ tests/testthat/_snaps/install.md | 38 ++++++ tests/testthat/_snaps/print-stan-file.md | 118 ++++++++++++++++++ tests/testthat/helper-custom-expectations.R | 28 +++++ tests/testthat/test-fit-gq.R | 65 ++++++---- tests/testthat/test-fit-laplace.R | 15 ++- tests/testthat/test-fit-mcmc.R | 71 +++++++---- tests/testthat/test-fit-mle.R | 15 ++- tests/testthat/test-fit-vb.R | 44 ++++--- tests/testthat/test-install.R | 43 ++++--- tests/testthat/test-print-stan-file.R | 21 ++-- 15 files changed, 736 insertions(+), 97 deletions(-) create mode 100644 tests/testthat/_snaps/fit-gq.md create mode 100644 tests/testthat/_snaps/fit-laplace.md create mode 100644 tests/testthat/_snaps/fit-mcmc.md create mode 100644 tests/testthat/_snaps/fit-mle.md create mode 100644 tests/testthat/_snaps/fit-vb.md create mode 100644 tests/testthat/_snaps/install.md create mode 100644 tests/testthat/_snaps/print-stan-file.md diff --git a/tests/testthat/_snaps/fit-gq.md b/tests/testthat/_snaps/fit-gq.md new file mode 100644 index 00000000..fb401fb0 --- /dev/null +++ b/tests/testthat/_snaps/fit-gq.md @@ -0,0 +1,115 @@ +# print() method works after gq + + Code + full_print <- fit_gq$print() + Output + variable mean median sd mad q5 q95 + y_rep[1] + y_rep[2] + y_rep[3] + y_rep[4] + y_rep[5] + y_rep[6] + y_rep[7] + y_rep[8] + y_rep[9] + y_rep[10] + + # showing 10 of 11 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit_gq$print(max_rows = 1) + Output + variable mean median sd mad q5 q95 + y_rep[1] + + # showing 1 of 11 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit_gq$print(NULL, c("mad")) + Output + variable mad + y_rep[1] + y_rep[2] + y_rep[3] + y_rep[4] + y_rep[5] + y_rep[6] + y_rep[7] + y_rep[8] + y_rep[9] + y_rep[10] + + # showing 10 of 11 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit_gq$print(max_rows = 2) + Output + variable mean median sd mad q5 q95 + y_rep[1] + y_rep[2] + + # showing 2 of 11 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit_gq$print(max_rows = 11) + Output + variable mean median sd mad q5 q95 + y_rep[1] + y_rep[2] + y_rep[3] + y_rep[4] + y_rep[5] + y_rep[6] + y_rep[7] + y_rep[8] + y_rep[9] + y_rep[10] + sum_y + +--- + + Code + fit_gq$print("y_rep", max_rows = 2) + Output + variable mean median sd mad q5 q95 + y_rep[1] + y_rep[2] + + # showing 2 of 10 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit_gq$print("y_rep") + Output + variable mean median sd mad q5 q95 + y_rep[1] + y_rep[2] + y_rep[3] + y_rep[4] + y_rep[5] + y_rep[6] + y_rep[7] + y_rep[8] + y_rep[9] + y_rep[10] + +--- + + Code + fit_gq$print(c("y_rep[1]", "sum_y", "y_rep[3]")) + Output + variable mean median sd mad q5 q95 + y_rep[1] + sum_y + y_rep[3] + diff --git a/tests/testthat/_snaps/fit-laplace.md b/tests/testthat/_snaps/fit-laplace.md new file mode 100644 index 00000000..8ea5f9a7 --- /dev/null +++ b/tests/testthat/_snaps/fit-laplace.md @@ -0,0 +1,23 @@ +# summary() and print() methods works after laplace + + Code + full_print <- fit_laplace$print() + Output + variable mean median sd mad q5 q95 + lp__ + lp_approx__ + alpha + beta[1] + beta[2] + beta[3] + +--- + + Code + fit_laplace$print(max_rows = 1) + Output + variable mean median sd mad q5 q95 + lp__ + + # showing 1 of 6 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + diff --git a/tests/testthat/_snaps/fit-mcmc.md b/tests/testthat/_snaps/fit-mcmc.md new file mode 100644 index 00000000..1905123a --- /dev/null +++ b/tests/testthat/_snaps/fit-mcmc.md @@ -0,0 +1,127 @@ +# print() method works after mcmc + + Code + basic_print <- fit_mcmc$print() + Output + variable mean median sd mad q5 q95 rhat ess_bulk ess_tail + lp__ + alpha + beta[1] + beta[2] + beta[3] + +--- + + Code + fit_mcmc$print(max_rows = 1) + Output + variable mean median sd mad q5 q95 rhat ess_bulk ess_tail + lp__ + + # showing 1 of 5 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit_mcmc$print(NULL, c("ess_sd")) + Output + variable ess_sd + lp__ + alpha + beta[1] + beta[2] + beta[3] + +--- + + Code + fit$print() + Output + variable mean median sd mad q5 q95 rhat ess_bulk ess_tail + lp__ + mu + tau + theta_raw[1] + theta_raw[2] + theta_raw[3] + theta_raw[4] + theta_raw[5] + theta_raw[6] + theta_raw[7] + + # showing 10 of 19 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit$print(max_rows = 2) + Output + variable mean median sd mad q5 q95 rhat ess_bulk ess_tail + lp__ + mu + + # showing 2 of 19 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit$print(max_rows = 19) + Output + variable mean median sd mad q5 q95 rhat ess_bulk ess_tail + lp__ + mu + tau + theta_raw[1] + theta_raw[2] + theta_raw[3] + theta_raw[4] + theta_raw[5] + theta_raw[6] + theta_raw[7] + theta_raw[8] + theta[1] + theta[2] + theta[3] + theta[4] + theta[5] + theta[6] + theta[7] + theta[8] + +--- + + Code + fit$print("theta", max_rows = 2) + Output + variable mean median sd mad q5 q95 rhat ess_bulk ess_tail + theta[1] + theta[2] + + # showing 2 of 8 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit$print("theta") + Output + variable mean median sd mad q5 q95 rhat ess_bulk ess_tail + theta[1] + theta[2] + theta[3] + theta[4] + theta[5] + theta[6] + theta[7] + theta[8] + +--- + + Code + fit$print(c("theta[1]", "tau", "mu", "theta_raw[3]")) + Output + variable mean median sd mad q5 q95 rhat ess_bulk ess_tail + theta[1] + tau + mu + theta_raw[3] + diff --git a/tests/testthat/_snaps/fit-mle.md b/tests/testthat/_snaps/fit-mle.md new file mode 100644 index 00000000..4f2874ba --- /dev/null +++ b/tests/testthat/_snaps/fit-mle.md @@ -0,0 +1,22 @@ +# print() method works after optimization + + Code + full_print <- fit_mle$print() + Output + variable estimate + lp__ + alpha + beta[1] + beta[2] + beta[3] + +--- + + Code + fit_mle$print(max_rows = 1) + Output + variable estimate + lp__ + + # showing 1 of 5 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + diff --git a/tests/testthat/_snaps/fit-vb.md b/tests/testthat/_snaps/fit-vb.md new file mode 100644 index 00000000..aef0a859 --- /dev/null +++ b/tests/testthat/_snaps/fit-vb.md @@ -0,0 +1,88 @@ +# print() method works after vb + + Code + basic_print <- fit_vb$print() + Output + variable mean median sd mad q5 q95 + lp__ + lp_approx__ + alpha + beta[1] + beta[2] + beta[3] + +--- + + Code + fit_vb$print(max_rows = 1) + Output + variable mean median sd mad q5 q95 + lp__ + + # showing 1 of 6 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit$print() + Output + variable mean median sd mad q5 q95 + lp__ + lp_approx__ + mu + tau + theta_raw[1] + theta_raw[2] + theta_raw[3] + theta_raw[4] + theta_raw[5] + theta_raw[6] + + # showing 10 of 20 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + +--- + + Code + fit$print(max_rows = 20) + Output + variable mean median sd mad q5 q95 + lp__ + lp_approx__ + mu + tau + theta_raw[1] + theta_raw[2] + theta_raw[3] + theta_raw[4] + theta_raw[5] + theta_raw[6] + theta_raw[7] + theta_raw[8] + theta[1] + theta[2] + theta[3] + theta[4] + theta[5] + theta[6] + theta[7] + theta[8] + +--- + + Code + fit$print(c("theta", "tau", "lp__", "lp_approx__")) + Output + variable mean median sd mad q5 q95 + theta[1] + theta[2] + theta[3] + theta[4] + theta[5] + theta[6] + theta[7] + theta[8] + tau + lp__ + + # showing 10 of 11 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option) + diff --git a/tests/testthat/_snaps/install.md b/tests/testthat/_snaps/install.md new file mode 100644 index 00000000..46f261c5 --- /dev/null +++ b/tests/testthat/_snaps/install.md @@ -0,0 +1,38 @@ +# toolchain checks on Unix work + + Code + check_unix_cpp_compiler() + Condition + Error: + ! C++ compiler missing. + +--- + + Code + check_unix_make() + Condition + Error: + ! make missing. + +# check_rtools4x_windows_toolchain reports missing Rtools and make + + Code + check_rtools4x_windows_toolchain() + Condition + Error: + ! + Rtools was not found but is required to run CmdStan with R version . + Please install or reinstall the appropriate Rtools version for this R installation, + restart R, and then run cmdstanr::check_cmdstan_toolchain(). + +--- + + Code + check_rtools4x_windows_toolchain() + Condition + Error: + ! + Rtools is missing the required 'make' executable in . + Please reinstall the appropriate Rtools version for this R installation, + restart R, and then run cmdstanr::check_cmdstan_toolchain(). + diff --git a/tests/testthat/_snaps/print-stan-file.md b/tests/testthat/_snaps/print-stan-file.md new file mode 100644 index 00000000..090c002a --- /dev/null +++ b/tests/testthat/_snaps/print-stan-file.md @@ -0,0 +1,118 @@ +# print_stan_file() prints plain code outside of knitr + + Code + cat(out, sep = "\n") + Output + + parameters { + real y; + } + model { + y ~ std_normal(); + } + + +# print_stan_file() outputs fenced code block in knitr with results='asis' + + Code + cat(out, sep = "\n") + Output + ```stan + + parameters { + real y; + } + model { + y ~ std_normal(); + } + + + ``` + +# print_stan_file() wraps in
when fold=TRUE + + Code + cat(out, sep = "\n") + Output +
Stan model code + + ```stan + + parameters { + real y; + } + model { + y ~ std_normal(); + } + + + ``` + +
+ +# print_stan_file() uses custom summary text + + Code + cat(out, sep = "\n") + Output +
My Stan Code + + ```stan + + parameters { + real y; + } + model { + y ~ std_normal(); + } + + + ``` + +
+ +# print_stan_file() does not fold when fold=FALSE in knitr + + Code + cat(out, sep = "\n") + Output + ```stan + + parameters { + real y; + } + model { + y ~ std_normal(); + } + + + ``` + +# print_stan_file() falls back to plain text without results='asis' + + Code + cat(out, sep = "\n") + Output + + parameters { + real y; + } + model { + y ~ std_normal(); + } + + +# print_stan_file() falls back to plain text without knitr.in.progress + + Code + cat(out, sep = "\n") + Output + + parameters { + real y; + } + model { + y ~ std_normal(); + } + + diff --git a/tests/testthat/helper-custom-expectations.R b/tests/testthat/helper-custom-expectations.R index d0128753..58046c71 100644 --- a/tests/testthat/helper-custom-expectations.R +++ b/tests/testthat/helper-custom-expectations.R @@ -127,3 +127,31 @@ expect_equal_ignore_order <- function(object, expected, ...) { } expect_not_true <- function(...) expect_false(isTRUE(...)) + +transform_print_snapshot <- function(x) { + vapply(x, function(line) { + line <- trimws(line) + if (!nzchar(line)) { + return(line) + } + if (grepl("^variable\\b", line)) { + return(gsub("\\s+", " ", line)) + } + if (grepl("^# showing", line) || + grepl("^Can't find the following variable\\(s\\):", line)) { + return(line) + } + sub("\\s+.*$", "", line) + }, character(1)) +} + +transform_unix_toolchain_snapshot <- function(x) { + x <- gsub("A suitable C\\+\\+ compiler was not found\\..*", "C++ compiler missing.", x) + x <- gsub("A C\\+\\+ compiler was not found\\..*", "C++ compiler missing.", x) + gsub("The 'make' tool was not found\\..*", "make missing.", x) +} + +transform_r_version_snapshot <- function(x) { + x <- gsub("Rtools[0-9]+", "Rtools", x) + gsub("R version [0-9]+\\.[0-9]+\\.[0-9]+", "R version ", x) +} diff --git a/tests/testthat/test-fit-gq.R b/tests/testthat/test-fit-gq.R index 4fc43f1f..c29e0a71 100644 --- a/tests/testthat/test-fit-gq.R +++ b/tests/testthat/test-fit-gq.R @@ -64,34 +64,55 @@ test_that("summary() method works after gq", { }) test_that("print() method works after gq", { - expect_output(expect_s3_class(fit_gq$print(), "CmdStanGQ"), "variable") - expect_output(fit_gq$print(max_rows = 1), "# showing 1 of 11 rows") - expect_output(fit_gq$print(NULL, c("mad")), "mad") + full_print <- NULL + expect_snapshot( + full_print <- fit_gq$print(), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_s3_class(full_print, "CmdStanGQ") + + expect_snapshot( + fit_gq$print(max_rows = 1), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit_gq$print(NULL, c("mad")), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit_gq$print(max_rows = 2), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit_gq$print(max_rows = 11), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit_gq$print("y_rep", max_rows = 2), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit_gq$print("y_rep"), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit_gq$print(c("y_rep[1]", "sum_y", "y_rep[3]")), + transform = transform_print_snapshot, + cran = TRUE + ) - expect_output(fit_gq$print(), "showing 10 of 11 rows") - expect_output(fit_gq$print(max_rows = 2), "showing 2 of 11 rows") - expect_output(fit_gq$print(max_rows = 11), "sum_y", fixed=TRUE) # last parameter - expect_output(fit_gq$print("y_rep", max_rows = 2), "showing 2 of 10 rows") expect_error( fit_gq$print(variable = "unknown", max_rows = 20), "Can't find the following variable(s): unknown", fixed = TRUE ) - - out <- capture.output(fit_gq$print("y_rep")) - expect_length(out, 11) # columns names + 1 y_rep - expect_match(out[1], "variable") - expect_match(out[2], "y_rep[1]", fixed = TRUE) - expect_match(out[9], "y_rep[8]", fixed = TRUE) - expect_false(any(grepl("sum_y|theta", out))) - - # make sure the row order is correct - out <- capture.output(fit_gq$print(c("y_rep[1]", "sum_y", "y_rep[3]"))) - expect_length(out, 4) - expect_match(out[1], " variable") - expect_match(out[2], " y_rep[1]", fixed = TRUE) - expect_match(out[3], " sum_y") - expect_match(out[4], " y_rep[3]", fixed = TRUE) }) test_that("output() method works after gq", { diff --git a/tests/testthat/test-fit-laplace.R b/tests/testthat/test-fit-laplace.R index 90415e93..d777c336 100644 --- a/tests/testthat/test-fit-laplace.R +++ b/tests/testthat/test-fit-laplace.R @@ -13,8 +13,18 @@ test_that("summary() and print() methods works after laplace", { expect_equal(x$variable, c("lp__", "lp_approx__", PARAM_NAMES)) expect_equal(colnames(x), c("variable", "mean", "sd")) - expect_output(expect_s3_class(fit_laplace$print(), "CmdStanLaplace"), "variable") - expect_output(fit_laplace$print(max_rows = 1), "# showing 1 of 6 rows") + full_print <- NULL + expect_snapshot( + full_print <- fit_laplace$print(), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_s3_class(full_print, "CmdStanLaplace") + expect_snapshot( + fit_laplace$print(max_rows = 1), + transform = transform_print_snapshot, + cran = TRUE + ) }) test_that("draws() method returns posterior sample (reading csv works)", { @@ -61,4 +71,3 @@ test_that("draws() errors if invalid format", { "The supplied draws format is not valid" ) }) - diff --git a/tests/testthat/test-fit-mcmc.R b/tests/testthat/test-fit-mcmc.R index aede1110..f1e05b7e 100644 --- a/tests/testthat/test-fit-mcmc.R +++ b/tests/testthat/test-fit-mcmc.R @@ -123,37 +123,62 @@ test_that("summary() method works after mcmc", { }) test_that("print() method works after mcmc", { - expect_output(expect_s3_class(fit_mcmc$print(), "CmdStanMCMC"), "variable") - expect_output(fit_mcmc$print(max_rows = 1), "# showing 1 of 5 rows") - expect_output(fit_mcmc$print(NULL, c("ess_sd")), "ess_sd") + basic_print <- NULL + expect_snapshot( + basic_print <- fit_mcmc$print(), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_s3_class(basic_print, "CmdStanMCMC") + expect_snapshot( + fit_mcmc$print(max_rows = 1), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit_mcmc$print(NULL, c("ess_sd")), + transform = transform_print_snapshot, + cran = TRUE + ) # test on model with more parameters fit <- cmdstanr_example("schools_ncp") - expect_output(fit$print(), "showing 10 of 19 rows") - expect_output(fit$print(max_rows = 2), "showing 2 of 19 rows") - expect_output(fit$print(max_rows = 19), "theta[8]", fixed=TRUE) # last parameter - expect_output(fit$print("theta", max_rows = 2), "showing 2 of 8 rows") + expect_snapshot( + fit$print(), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit$print(max_rows = 2), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit$print(max_rows = 19), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit$print("theta", max_rows = 2), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit$print("theta"), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit$print(c("theta[1]", "tau", "mu", "theta_raw[3]")), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_error( fit$print(variable = "unknown", max_rows = 20), "Can't find the following variable(s): unknown", fixed = TRUE ) - - out <- capture.output(fit$print("theta")) - expect_length(out, 9) # columns names + 8 thetas - expect_match(out[1], "variable") - expect_match(out[2], "theta[1]", fixed = TRUE) - expect_match(out[9], "theta[8]", fixed = TRUE) - expect_false(any(grepl("mu|tau|theta_raw", out))) - - # make sure the row order is correct - out <- capture.output(fit$print(c("theta[1]", "tau", "mu", "theta_raw[3]"))) - expect_length(out, 5) - expect_match(out[1], " variable") - expect_match(out[2], " theta[1]", fixed = TRUE) - expect_match(out[3], " tau") - expect_match(out[4], " mu") - expect_match(out[5], " theta_raw[3]", fixed = TRUE) }) test_that("output() method works after mcmc", { diff --git a/tests/testthat/test-fit-mle.R b/tests/testthat/test-fit-mle.R index c868a37c..1c709a83 100644 --- a/tests/testthat/test-fit-mle.R +++ b/tests/testthat/test-fit-mle.R @@ -22,8 +22,19 @@ test_that("summary method works after optimization", { }) test_that("print() method works after optimization", { - expect_output(expect_s3_class(fit_mle$print(), "CmdStanMLE"), "estimate") - expect_output(fit_mle$print(max_rows = 1), "# showing 1 of 5 rows") + full_print <- NULL + expect_snapshot( + full_print <- fit_mle$print(), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_s3_class(full_print, "CmdStanMLE") + expect_snapshot( + fit_mle$print(max_rows = 1), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_error( fit_mle$print(variable = "unknown", max_rows = 20), "Can't find the following variable(s): unknown", diff --git a/tests/testthat/test-fit-vb.R b/tests/testthat/test-fit-vb.R index f5d2f2c9..1379255c 100644 --- a/tests/testthat/test-fit-vb.R +++ b/tests/testthat/test-fit-vb.R @@ -18,29 +18,43 @@ test_that("summary() method works after vb", { }) test_that("print() method works after vb", { - expect_output(expect_s3_class(fit_vb$print(), "CmdStanVB"), "variable") - expect_output(fit_vb$print(max_rows = 1), "# showing 1 of 6 rows") + basic_print <- NULL + expect_snapshot( + basic_print <- fit_vb$print(), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_s3_class(basic_print, "CmdStanVB") + + expect_snapshot( + fit_vb$print(max_rows = 1), + transform = transform_print_snapshot, + cran = TRUE + ) # test on model with more parameters fit <- cmdstanr_example("schools_ncp", method = "variational", seed = 123) - expect_output(fit$print(), "lp_approx__") - expect_output(fit$print(), "showing 10 of 20 rows") - expect_output(fit$print(max_rows = 20), "theta[8]", fixed = TRUE) # last parameter + expect_snapshot( + fit$print(), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit$print(max_rows = 20), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_snapshot( + fit$print(c("theta", "tau", "lp__", "lp_approx__")), + transform = transform_print_snapshot, + cran = TRUE + ) + expect_error( fit$print(variable = "unknown", max_rows = 20), "Can't find the following variable(s): unknown", fixed = TRUE ) # unknown parameter - - out <- capture.output(fit$print(c("theta", "tau", "lp__", "lp_approx__"))) - expect_length(out, 13) # columns names + 8 thetas + tau + lp__ + lp_approx__ + empty + message - expect_match(out[1], " variable") - expect_match(out[2], " theta[1]", fixed = TRUE) - expect_match(out[9], " theta[8]", fixed = TRUE) - expect_match(out[10], " tau") - expect_match(out[11], " lp__") - expect_false(nzchar(out[12])) # empty line - expect_match(out[13], "10 of 11 rows") }) test_that("draws() method returns posterior sample (reading csv works)", { diff --git a/tests/testthat/test-install.R b/tests/testthat/test-install.R index ccb781cc..5968409b 100644 --- a/tests/testthat/test-install.R +++ b/tests/testthat/test-install.R @@ -123,22 +123,17 @@ test_that("install_cmdstan() works with version and release_url", { test_that("toolchain checks on Unix work", { skip_if(os_is_windows()) withr::local_envvar(c("PATH" = "")) - if (os_is_macos()) { - err_msg_cpp <- "A suitable C++ compiler was not found. Please install the command line tools for Mac with 'xcode-select --install' or install Xcode from the app store. Then restart R and run cmdstanr::check_cmdstan_toolchain()." - err_msg_make <- "The 'make' tool was not found. Please install the command line tools for Mac with 'xcode-select --install' or install Xcode from the app store. Then restart R and run cmdstanr::check_cmdstan_toolchain()." - } else { - err_msg_cpp <- "A C++ compiler was not found. Please install the 'clang++' or 'g++' compiler, restart R, and run cmdstanr::check_cmdstan_toolchain()." - err_msg_make <- "The 'make' tool was not found. Please install 'make', restart R, and then run cmdstanr::check_cmdstan_toolchain()." - } - expect_error( + expect_snapshot( check_unix_cpp_compiler(), - err_msg_cpp, - fixed = TRUE + error = TRUE, + transform = transform_unix_toolchain_snapshot, + cran = TRUE ) - expect_error( + expect_snapshot( check_unix_make(), - err_msg_make, - fixed = TRUE + error = TRUE, + transform = transform_unix_toolchain_snapshot, + cran = TRUE ) }) @@ -444,10 +439,11 @@ test_that("check_rtools4x_windows_toolchain reports missing Rtools and make", { rtools4x_home_path = function() "", rtools4x_version = function() "44" ) - expect_error( + expect_snapshot( check_rtools4x_windows_toolchain(), - "restart R, and then run cmdstanr::check_cmdstan_toolchain()", - fixed = TRUE + error = TRUE, + transform = transform_r_version_snapshot, + cran = TRUE ) }) @@ -458,10 +454,19 @@ test_that("check_rtools4x_windows_toolchain reports missing Rtools and make", { rtools4x_home_path = function() fake_rtools_home, rtools4x_version = function() "44" ) - expect_error( + expect_snapshot( check_rtools4x_windows_toolchain(), - "restart R, and then run cmdstanr::check_cmdstan_toolchain()", - fixed = TRUE + error = TRUE, + transform = function(x) { + x <- transform_r_version_snapshot(x) + gsub( + repair_path(file.path(fake_rtools_home, "usr", "bin")), + "", + x, + fixed = TRUE + ) + }, + cran = TRUE ) }) }) diff --git a/tests/testthat/test-print-stan-file.R b/tests/testthat/test-print-stan-file.R index cc7d6940..cbc1c6d3 100644 --- a/tests/testthat/test-print-stan-file.R +++ b/tests/testthat/test-print-stan-file.R @@ -7,11 +7,10 @@ model { } " stan_file <- write_stan_file(stan_code) -stan_lines <- readLines(stan_file) test_that("print_stan_file() prints plain code outside of knitr", { out <- capture.output(print_stan_file(stan_file)) - expect_identical(out, stan_lines) + expect_snapshot(cat(out, sep = "\n"), cran = TRUE) }) test_that("print_stan_file() returns file path invisibly", { @@ -21,12 +20,10 @@ test_that("print_stan_file() returns file path invisibly", { test_that("print_stan_file() outputs fenced code block in knitr with results='asis'", { out <- with_mocked_bindings( - paste(capture.output(print_stan_file(stan_file)), collapse = "\n"), + capture.output(print_stan_file(stan_file)), is_knitr_asis_output = function() TRUE ) - expect_match(out, "^```stan\n") - expect_match(out, "\n```$") - expect_match(out, paste(stan_lines, collapse = "\n"), fixed = TRUE) + expect_snapshot(cat(out, sep = "\n"), cran = TRUE) }) test_that("print_stan_file() wraps in
when fold=TRUE", { @@ -34,8 +31,7 @@ test_that("print_stan_file() wraps in
when fold=TRUE", { capture.output(print_stan_file(stan_file, fold = TRUE)), is_knitr_asis_output = function() TRUE ) - expect_match(out[1], "
Stan model code") - expect_match(out[length(out)], "
") + expect_snapshot(cat(out, sep = "\n"), cran = TRUE) }) test_that("print_stan_file() uses custom summary text", { @@ -43,7 +39,7 @@ test_that("print_stan_file() uses custom summary text", { capture.output(print_stan_file(stan_file, fold = TRUE, summary = "My Stan Code")), is_knitr_asis_output = function() TRUE ) - expect_match(out[1], "
My Stan Code") + expect_snapshot(cat(out, sep = "\n"), cran = TRUE) }) test_that("print_stan_file() does not fold when fold=FALSE in knitr", { @@ -51,8 +47,7 @@ test_that("print_stan_file() does not fold when fold=FALSE in knitr", { capture.output(print_stan_file(stan_file, fold = FALSE)), is_knitr_asis_output = function() TRUE ) - expect_no_match(paste(out, collapse = "\n"), "
", fixed = TRUE) - expect_no_match(paste(out, collapse = "\n"), "
", fixed = TRUE) + expect_snapshot(cat(out, sep = "\n"), cran = TRUE) }) test_that("print_stan_file() falls back to plain text without results='asis'", { @@ -60,11 +55,11 @@ test_that("print_stan_file() falls back to plain text without results='asis'", { capture.output(print_stan_file(stan_file)), is_knitr_asis_output = function() FALSE ) - expect_identical(out, stan_lines) + expect_snapshot(cat(out, sep = "\n"), cran = TRUE) }) test_that("print_stan_file() falls back to plain text without knitr.in.progress", { withr::local_options(knitr.in.progress = FALSE) out <- capture.output(print_stan_file(stan_file)) - expect_identical(out, stan_lines) + expect_snapshot(cat(out, sep = "\n"), cran = TRUE) }) From 69d74e7ec2360f8bfdce4dbd270792045e192f6e Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Fri, 3 Apr 2026 18:53:47 -0700 Subject: [PATCH 2/6] Eerror tests instead of snapshots for install --- tests/testthat/_snaps/install.md | 38 ------------------ tests/testthat/helper-custom-expectations.R | 11 ------ tests/testthat/test-install.R | 43 +++++++++------------ 3 files changed, 19 insertions(+), 73 deletions(-) delete mode 100644 tests/testthat/_snaps/install.md diff --git a/tests/testthat/_snaps/install.md b/tests/testthat/_snaps/install.md deleted file mode 100644 index 46f261c5..00000000 --- a/tests/testthat/_snaps/install.md +++ /dev/null @@ -1,38 +0,0 @@ -# toolchain checks on Unix work - - Code - check_unix_cpp_compiler() - Condition - Error: - ! C++ compiler missing. - ---- - - Code - check_unix_make() - Condition - Error: - ! make missing. - -# check_rtools4x_windows_toolchain reports missing Rtools and make - - Code - check_rtools4x_windows_toolchain() - Condition - Error: - ! - Rtools was not found but is required to run CmdStan with R version . - Please install or reinstall the appropriate Rtools version for this R installation, - restart R, and then run cmdstanr::check_cmdstan_toolchain(). - ---- - - Code - check_rtools4x_windows_toolchain() - Condition - Error: - ! - Rtools is missing the required 'make' executable in . - Please reinstall the appropriate Rtools version for this R installation, - restart R, and then run cmdstanr::check_cmdstan_toolchain(). - diff --git a/tests/testthat/helper-custom-expectations.R b/tests/testthat/helper-custom-expectations.R index 58046c71..74400d77 100644 --- a/tests/testthat/helper-custom-expectations.R +++ b/tests/testthat/helper-custom-expectations.R @@ -144,14 +144,3 @@ transform_print_snapshot <- function(x) { sub("\\s+.*$", "", line) }, character(1)) } - -transform_unix_toolchain_snapshot <- function(x) { - x <- gsub("A suitable C\\+\\+ compiler was not found\\..*", "C++ compiler missing.", x) - x <- gsub("A C\\+\\+ compiler was not found\\..*", "C++ compiler missing.", x) - gsub("The 'make' tool was not found\\..*", "make missing.", x) -} - -transform_r_version_snapshot <- function(x) { - x <- gsub("Rtools[0-9]+", "Rtools", x) - gsub("R version [0-9]+\\.[0-9]+\\.[0-9]+", "R version ", x) -} diff --git a/tests/testthat/test-install.R b/tests/testthat/test-install.R index 5968409b..ccb781cc 100644 --- a/tests/testthat/test-install.R +++ b/tests/testthat/test-install.R @@ -123,17 +123,22 @@ test_that("install_cmdstan() works with version and release_url", { test_that("toolchain checks on Unix work", { skip_if(os_is_windows()) withr::local_envvar(c("PATH" = "")) - expect_snapshot( + if (os_is_macos()) { + err_msg_cpp <- "A suitable C++ compiler was not found. Please install the command line tools for Mac with 'xcode-select --install' or install Xcode from the app store. Then restart R and run cmdstanr::check_cmdstan_toolchain()." + err_msg_make <- "The 'make' tool was not found. Please install the command line tools for Mac with 'xcode-select --install' or install Xcode from the app store. Then restart R and run cmdstanr::check_cmdstan_toolchain()." + } else { + err_msg_cpp <- "A C++ compiler was not found. Please install the 'clang++' or 'g++' compiler, restart R, and run cmdstanr::check_cmdstan_toolchain()." + err_msg_make <- "The 'make' tool was not found. Please install 'make', restart R, and then run cmdstanr::check_cmdstan_toolchain()." + } + expect_error( check_unix_cpp_compiler(), - error = TRUE, - transform = transform_unix_toolchain_snapshot, - cran = TRUE + err_msg_cpp, + fixed = TRUE ) - expect_snapshot( + expect_error( check_unix_make(), - error = TRUE, - transform = transform_unix_toolchain_snapshot, - cran = TRUE + err_msg_make, + fixed = TRUE ) }) @@ -439,11 +444,10 @@ test_that("check_rtools4x_windows_toolchain reports missing Rtools and make", { rtools4x_home_path = function() "", rtools4x_version = function() "44" ) - expect_snapshot( + expect_error( check_rtools4x_windows_toolchain(), - error = TRUE, - transform = transform_r_version_snapshot, - cran = TRUE + "restart R, and then run cmdstanr::check_cmdstan_toolchain()", + fixed = TRUE ) }) @@ -454,19 +458,10 @@ test_that("check_rtools4x_windows_toolchain reports missing Rtools and make", { rtools4x_home_path = function() fake_rtools_home, rtools4x_version = function() "44" ) - expect_snapshot( + expect_error( check_rtools4x_windows_toolchain(), - error = TRUE, - transform = function(x) { - x <- transform_r_version_snapshot(x) - gsub( - repair_path(file.path(fake_rtools_home, "usr", "bin")), - "", - x, - fixed = TRUE - ) - }, - cran = TRUE + "restart R, and then run cmdstanr::check_cmdstan_toolchain()", + fixed = TRUE ) }) }) From cdbea48fa86d9bd21354a2c6fde6653391992f88 Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Mon, 6 Apr 2026 08:25:08 -0700 Subject: [PATCH 3/6] Cleaned up code slightly --- tests/testthat/_snaps/fit-gq.md | 2 +- tests/testthat/_snaps/fit-laplace.md | 2 +- tests/testthat/_snaps/fit-mcmc.md | 2 +- tests/testthat/_snaps/fit-mle.md | 2 +- tests/testthat/_snaps/fit-vb.md | 2 +- tests/testthat/test-fit-gq.R | 4 +--- tests/testthat/test-fit-laplace.R | 4 +--- tests/testthat/test-fit-mcmc.R | 4 +--- tests/testthat/test-fit-mle.R | 4 +--- tests/testthat/test-fit-vb.R | 4 +--- tests/testthat/test-model-compile-user_header.R | 5 ++--- tests/testthat/test-model-recompile-logic.R | 10 +++------- 12 files changed, 15 insertions(+), 30 deletions(-) diff --git a/tests/testthat/_snaps/fit-gq.md b/tests/testthat/_snaps/fit-gq.md index fb401fb0..77e09252 100644 --- a/tests/testthat/_snaps/fit-gq.md +++ b/tests/testthat/_snaps/fit-gq.md @@ -1,7 +1,7 @@ # print() method works after gq Code - full_print <- fit_gq$print() + expect_s3_class(fit_gq$print(), "CmdStanGQ") Output variable mean median sd mad q5 q95 y_rep[1] diff --git a/tests/testthat/_snaps/fit-laplace.md b/tests/testthat/_snaps/fit-laplace.md index 8ea5f9a7..4effe964 100644 --- a/tests/testthat/_snaps/fit-laplace.md +++ b/tests/testthat/_snaps/fit-laplace.md @@ -1,7 +1,7 @@ # summary() and print() methods works after laplace Code - full_print <- fit_laplace$print() + expect_s3_class(fit_laplace$print(), "CmdStanLaplace") Output variable mean median sd mad q5 q95 lp__ diff --git a/tests/testthat/_snaps/fit-mcmc.md b/tests/testthat/_snaps/fit-mcmc.md index 1905123a..004cc382 100644 --- a/tests/testthat/_snaps/fit-mcmc.md +++ b/tests/testthat/_snaps/fit-mcmc.md @@ -1,7 +1,7 @@ # print() method works after mcmc Code - basic_print <- fit_mcmc$print() + expect_s3_class(fit_mcmc$print(), "CmdStanMCMC") Output variable mean median sd mad q5 q95 rhat ess_bulk ess_tail lp__ diff --git a/tests/testthat/_snaps/fit-mle.md b/tests/testthat/_snaps/fit-mle.md index 4f2874ba..2e5b14e6 100644 --- a/tests/testthat/_snaps/fit-mle.md +++ b/tests/testthat/_snaps/fit-mle.md @@ -1,7 +1,7 @@ # print() method works after optimization Code - full_print <- fit_mle$print() + expect_s3_class(fit_mle$print(), "CmdStanMLE") Output variable estimate lp__ diff --git a/tests/testthat/_snaps/fit-vb.md b/tests/testthat/_snaps/fit-vb.md index aef0a859..cfd3a703 100644 --- a/tests/testthat/_snaps/fit-vb.md +++ b/tests/testthat/_snaps/fit-vb.md @@ -1,7 +1,7 @@ # print() method works after vb Code - basic_print <- fit_vb$print() + expect_s3_class(fit_vb$print(), "CmdStanVB") Output variable mean median sd mad q5 q95 lp__ diff --git a/tests/testthat/test-fit-gq.R b/tests/testthat/test-fit-gq.R index c29e0a71..972eea1f 100644 --- a/tests/testthat/test-fit-gq.R +++ b/tests/testthat/test-fit-gq.R @@ -64,13 +64,11 @@ test_that("summary() method works after gq", { }) test_that("print() method works after gq", { - full_print <- NULL expect_snapshot( - full_print <- fit_gq$print(), + expect_s3_class(fit_gq$print(), "CmdStanGQ"), transform = transform_print_snapshot, cran = TRUE ) - expect_s3_class(full_print, "CmdStanGQ") expect_snapshot( fit_gq$print(max_rows = 1), diff --git a/tests/testthat/test-fit-laplace.R b/tests/testthat/test-fit-laplace.R index d777c336..d598a8c4 100644 --- a/tests/testthat/test-fit-laplace.R +++ b/tests/testthat/test-fit-laplace.R @@ -13,13 +13,11 @@ test_that("summary() and print() methods works after laplace", { expect_equal(x$variable, c("lp__", "lp_approx__", PARAM_NAMES)) expect_equal(colnames(x), c("variable", "mean", "sd")) - full_print <- NULL expect_snapshot( - full_print <- fit_laplace$print(), + expect_s3_class(fit_laplace$print(), "CmdStanLaplace"), transform = transform_print_snapshot, cran = TRUE ) - expect_s3_class(full_print, "CmdStanLaplace") expect_snapshot( fit_laplace$print(max_rows = 1), transform = transform_print_snapshot, diff --git a/tests/testthat/test-fit-mcmc.R b/tests/testthat/test-fit-mcmc.R index f1e05b7e..29338a80 100644 --- a/tests/testthat/test-fit-mcmc.R +++ b/tests/testthat/test-fit-mcmc.R @@ -123,13 +123,11 @@ test_that("summary() method works after mcmc", { }) test_that("print() method works after mcmc", { - basic_print <- NULL expect_snapshot( - basic_print <- fit_mcmc$print(), + expect_s3_class(fit_mcmc$print(), "CmdStanMCMC"), transform = transform_print_snapshot, cran = TRUE ) - expect_s3_class(basic_print, "CmdStanMCMC") expect_snapshot( fit_mcmc$print(max_rows = 1), transform = transform_print_snapshot, diff --git a/tests/testthat/test-fit-mle.R b/tests/testthat/test-fit-mle.R index 1c709a83..bad5df7a 100644 --- a/tests/testthat/test-fit-mle.R +++ b/tests/testthat/test-fit-mle.R @@ -22,13 +22,11 @@ test_that("summary method works after optimization", { }) test_that("print() method works after optimization", { - full_print <- NULL expect_snapshot( - full_print <- fit_mle$print(), + expect_s3_class(fit_mle$print(), "CmdStanMLE"), transform = transform_print_snapshot, cran = TRUE ) - expect_s3_class(full_print, "CmdStanMLE") expect_snapshot( fit_mle$print(max_rows = 1), transform = transform_print_snapshot, diff --git a/tests/testthat/test-fit-vb.R b/tests/testthat/test-fit-vb.R index 1379255c..52ee652b 100644 --- a/tests/testthat/test-fit-vb.R +++ b/tests/testthat/test-fit-vb.R @@ -18,13 +18,11 @@ test_that("summary() method works after vb", { }) test_that("print() method works after vb", { - basic_print <- NULL expect_snapshot( - basic_print <- fit_vb$print(), + expect_s3_class(fit_vb$print(), "CmdStanVB"), transform = transform_print_snapshot, cran = TRUE ) - expect_s3_class(basic_print, "CmdStanVB") expect_snapshot( fit_vb$print(max_rows = 1), diff --git a/tests/testthat/test-model-compile-user_header.R b/tests/testthat/test-model-compile-user_header.R index 8f549953..1d5f7546 100644 --- a/tests/testthat/test-model-compile-user_header.R +++ b/tests/testthat/test-model-compile-user_header.R @@ -1,8 +1,7 @@ skip_if(os_is_macos()) -file_that_exists <- "placeholder_exists" -file_that_doesnt_exist <- "placeholder_doesnt_exist" -withr::local_file(file_that_exists) +file_that_exists <- withr::local_tempfile(pattern = "placeholder_exists") +file_that_doesnt_exist <- withr::local_tempfile(pattern = "placeholder_doesnt_exist") w_path <- function(f) { x <- sapply(f, function(fi) wsl_safe_path(absolute_path(fi))) diff --git a/tests/testthat/test-model-recompile-logic.R b/tests/testthat/test-model-recompile-logic.R index caac4f13..c9d7a385 100644 --- a/tests/testthat/test-model-recompile-logic.R +++ b/tests/testthat/test-model-recompile-logic.R @@ -1,11 +1,7 @@ stan_program <- cmdstan_example_file() -file_that_doesnt_exist <- "placeholder_doesnt_exist" -file_that_exists <- "placeholder_exists" +file_that_doesnt_exist <- withr::local_tempfile(pattern = "placeholder_doesnt_exist") +file_that_exists <- withr::local_tempfile(pattern = "placeholder_exists") file.create(file_that_exists) -withr::defer( - if (file.exists(file_that_exists)) file.remove(file_that_exists), - teardown_env() -) skip_message <- "To be fixed in a later version." @@ -106,4 +102,4 @@ test_that("recompile when cpp args don't match binary", { ) }) ) -}) \ No newline at end of file +}) From f37f5fbcca777a2d144865d95d987b3a4bda16bc Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Tue, 7 Apr 2026 18:19:41 -0700 Subject: [PATCH 4/6] Don't run some snapshots on CRAN --- tests/testthat/test-fit-gq.R | 16 ++++++++-------- tests/testthat/test-fit-laplace.R | 4 ++-- tests/testthat/test-fit-mcmc.R | 18 +++++++++--------- tests/testthat/test-fit-mle.R | 4 ++-- tests/testthat/test-fit-vb.R | 10 +++++----- tests/testthat/test-model-code-print.R | 4 ++-- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/tests/testthat/test-fit-gq.R b/tests/testthat/test-fit-gq.R index 972eea1f..bf7cfa33 100644 --- a/tests/testthat/test-fit-gq.R +++ b/tests/testthat/test-fit-gq.R @@ -67,43 +67,43 @@ test_that("print() method works after gq", { expect_snapshot( expect_s3_class(fit_gq$print(), "CmdStanGQ"), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_gq$print(max_rows = 1), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_gq$print(NULL, c("mad")), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_gq$print(max_rows = 2), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_gq$print(max_rows = 11), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_gq$print("y_rep", max_rows = 2), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_gq$print("y_rep"), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_gq$print(c("y_rep[1]", "sum_y", "y_rep[3]")), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_error( diff --git a/tests/testthat/test-fit-laplace.R b/tests/testthat/test-fit-laplace.R index d598a8c4..54933eef 100644 --- a/tests/testthat/test-fit-laplace.R +++ b/tests/testthat/test-fit-laplace.R @@ -16,12 +16,12 @@ test_that("summary() and print() methods works after laplace", { expect_snapshot( expect_s3_class(fit_laplace$print(), "CmdStanLaplace"), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_laplace$print(max_rows = 1), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) }) diff --git a/tests/testthat/test-fit-mcmc.R b/tests/testthat/test-fit-mcmc.R index 29338a80..09507ba8 100644 --- a/tests/testthat/test-fit-mcmc.R +++ b/tests/testthat/test-fit-mcmc.R @@ -126,17 +126,17 @@ test_that("print() method works after mcmc", { expect_snapshot( expect_s3_class(fit_mcmc$print(), "CmdStanMCMC"), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_mcmc$print(max_rows = 1), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_mcmc$print(NULL, c("ess_sd")), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) # test on model with more parameters @@ -144,32 +144,32 @@ test_that("print() method works after mcmc", { expect_snapshot( fit$print(), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit$print(max_rows = 2), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit$print(max_rows = 19), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit$print("theta", max_rows = 2), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit$print("theta"), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit$print(c("theta[1]", "tau", "mu", "theta_raw[3]")), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_error( diff --git a/tests/testthat/test-fit-mle.R b/tests/testthat/test-fit-mle.R index bad5df7a..e65b9205 100644 --- a/tests/testthat/test-fit-mle.R +++ b/tests/testthat/test-fit-mle.R @@ -25,12 +25,12 @@ test_that("print() method works after optimization", { expect_snapshot( expect_s3_class(fit_mle$print(), "CmdStanMLE"), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_mle$print(max_rows = 1), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_error( diff --git a/tests/testthat/test-fit-vb.R b/tests/testthat/test-fit-vb.R index 52ee652b..bac2d079 100644 --- a/tests/testthat/test-fit-vb.R +++ b/tests/testthat/test-fit-vb.R @@ -21,13 +21,13 @@ test_that("print() method works after vb", { expect_snapshot( expect_s3_class(fit_vb$print(), "CmdStanVB"), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit_vb$print(max_rows = 1), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) # test on model with more parameters @@ -35,17 +35,17 @@ test_that("print() method works after vb", { expect_snapshot( fit$print(), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit$print(max_rows = 20), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_snapshot( fit$print(c("theta", "tau", "lp__", "lp_approx__")), transform = transform_print_snapshot, - cran = TRUE + cran = FALSE ) expect_error( diff --git a/tests/testthat/test-model-code-print.R b/tests/testthat/test-model-code-print.R index 16fdaa81..81621893 100644 --- a/tests/testthat/test-model-code-print.R +++ b/tests/testthat/test-model-code-print.R @@ -3,8 +3,8 @@ stan_program <- testing_stan_file("bernoulli") mod <- testing_model("bernoulli") test_that("code() and print() methods work", { - expect_snapshot_output(mod$print(), cran = TRUE) - expect_snapshot_value(mod$code(), style = "deparse", cran = TRUE) + expect_snapshot_output(mod$print(), cran = FALSE) + expect_snapshot_value(mod$code(), style = "deparse", cran = FALSE) }) test_that("code() and print() still work if file is removed", { From 7cc635b8336afac3d6da5032d3fe6718c4db2f59 Mon Sep 17 00:00:00 2001 From: Visruth Srimath Kandali Date: Wed, 8 Apr 2026 22:59:59 -0700 Subject: [PATCH 5/6] Update tests/testthat/helper-custom-expectations.R Co-authored-by: Jonah Gabry --- tests/testthat/helper-custom-expectations.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/helper-custom-expectations.R b/tests/testthat/helper-custom-expectations.R index 74400d77..5223581b 100644 --- a/tests/testthat/helper-custom-expectations.R +++ b/tests/testthat/helper-custom-expectations.R @@ -128,6 +128,8 @@ expect_equal_ignore_order <- function(object, expected, ...) { expect_not_true <- function(...) expect_false(isTRUE(...)) +# strips numeric values (which may change slightly with different hardware or compilers) +# allowing us to still verify names, ordering, column headers, row counts, etc. transform_print_snapshot <- function(x) { vapply(x, function(line) { line <- trimws(line) From 6201b5638a348a520f097c11df10f74a233d5b34 Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Wed, 8 Apr 2026 23:17:06 -0700 Subject: [PATCH 6/6] Actually create existing file --- tests/testthat/test-model-compile-user_header.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-model-compile-user_header.R b/tests/testthat/test-model-compile-user_header.R index 1d5f7546..1002b866 100644 --- a/tests/testthat/test-model-compile-user_header.R +++ b/tests/testthat/test-model-compile-user_header.R @@ -1,6 +1,7 @@ skip_if(os_is_macos()) file_that_exists <- withr::local_tempfile(pattern = "placeholder_exists") +file.create(file_that_exists) file_that_doesnt_exist <- withr::local_tempfile(pattern = "placeholder_doesnt_exist") w_path <- function(f) {