diff --git a/tests/testthat/_snaps/fit-gq.md b/tests/testthat/_snaps/fit-gq.md new file mode 100644 index 00000000..77e09252 --- /dev/null +++ b/tests/testthat/_snaps/fit-gq.md @@ -0,0 +1,115 @@ +# print() method works after gq + + Code + expect_s3_class(fit_gq$print(), "CmdStanGQ") + 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..4effe964 --- /dev/null +++ b/tests/testthat/_snaps/fit-laplace.md @@ -0,0 +1,23 @@ +# summary() and print() methods works after laplace + + Code + expect_s3_class(fit_laplace$print(), "CmdStanLaplace") + 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..004cc382 --- /dev/null +++ b/tests/testthat/_snaps/fit-mcmc.md @@ -0,0 +1,127 @@ +# print() method works after mcmc + + Code + expect_s3_class(fit_mcmc$print(), "CmdStanMCMC") + 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..2e5b14e6 --- /dev/null +++ b/tests/testthat/_snaps/fit-mle.md @@ -0,0 +1,22 @@ +# print() method works after optimization + + Code + expect_s3_class(fit_mle$print(), "CmdStanMLE") + 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..cfd3a703 --- /dev/null +++ b/tests/testthat/_snaps/fit-vb.md @@ -0,0 +1,88 @@ +# print() method works after vb + + Code + expect_s3_class(fit_vb$print(), "CmdStanVB") + 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/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..5223581b 100644 --- a/tests/testthat/helper-custom-expectations.R +++ b/tests/testthat/helper-custom-expectations.R @@ -127,3 +127,22 @@ 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) + 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)) +} diff --git a/tests/testthat/test-fit-gq.R b/tests/testthat/test-fit-gq.R index 4fc43f1f..bf7cfa33 100644 --- a/tests/testthat/test-fit-gq.R +++ b/tests/testthat/test-fit-gq.R @@ -64,34 +64,53 @@ 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") + expect_snapshot( + expect_s3_class(fit_gq$print(), "CmdStanGQ"), + transform = transform_print_snapshot, + cran = FALSE + ) + + expect_snapshot( + fit_gq$print(max_rows = 1), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_gq$print(NULL, c("mad")), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_gq$print(max_rows = 2), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_gq$print(max_rows = 11), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_gq$print("y_rep", max_rows = 2), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_gq$print("y_rep"), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_gq$print(c("y_rep[1]", "sum_y", "y_rep[3]")), + transform = transform_print_snapshot, + cran = FALSE + ) - 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..54933eef 100644 --- a/tests/testthat/test-fit-laplace.R +++ b/tests/testthat/test-fit-laplace.R @@ -13,8 +13,16 @@ 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") + expect_snapshot( + expect_s3_class(fit_laplace$print(), "CmdStanLaplace"), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_laplace$print(max_rows = 1), + transform = transform_print_snapshot, + cran = FALSE + ) }) test_that("draws() method returns posterior sample (reading csv works)", { @@ -61,4 +69,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..09507ba8 100644 --- a/tests/testthat/test-fit-mcmc.R +++ b/tests/testthat/test-fit-mcmc.R @@ -123,37 +123,60 @@ 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") + expect_snapshot( + expect_s3_class(fit_mcmc$print(), "CmdStanMCMC"), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_mcmc$print(max_rows = 1), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_mcmc$print(NULL, c("ess_sd")), + transform = transform_print_snapshot, + cran = FALSE + ) # 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 = FALSE + ) + expect_snapshot( + fit$print(max_rows = 2), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit$print(max_rows = 19), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit$print("theta", max_rows = 2), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit$print("theta"), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit$print(c("theta[1]", "tau", "mu", "theta_raw[3]")), + transform = transform_print_snapshot, + cran = FALSE + ) + 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..e65b9205 100644 --- a/tests/testthat/test-fit-mle.R +++ b/tests/testthat/test-fit-mle.R @@ -22,8 +22,17 @@ 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") + expect_snapshot( + expect_s3_class(fit_mle$print(), "CmdStanMLE"), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit_mle$print(max_rows = 1), + transform = transform_print_snapshot, + cran = FALSE + ) + 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..bac2d079 100644 --- a/tests/testthat/test-fit-vb.R +++ b/tests/testthat/test-fit-vb.R @@ -18,29 +18,41 @@ 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") + expect_snapshot( + expect_s3_class(fit_vb$print(), "CmdStanVB"), + transform = transform_print_snapshot, + cran = FALSE + ) + + expect_snapshot( + fit_vb$print(max_rows = 1), + transform = transform_print_snapshot, + cran = FALSE + ) # 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 = FALSE + ) + expect_snapshot( + fit$print(max_rows = 20), + transform = transform_print_snapshot, + cran = FALSE + ) + expect_snapshot( + fit$print(c("theta", "tau", "lp__", "lp_approx__")), + transform = transform_print_snapshot, + cran = FALSE + ) + 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-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", { diff --git a/tests/testthat/test-model-compile-user_header.R b/tests/testthat/test-model-compile-user_header.R index 8f549953..1002b866 100644 --- a/tests/testthat/test-model-compile-user_header.R +++ b/tests/testthat/test-model-compile-user_header.R @@ -1,8 +1,8 @@ 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.create(file_that_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 +}) 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) })