From 4be1269bd2dcc4796c2630a9c4adb96af92d51fe Mon Sep 17 00:00:00 2001 From: jgabry Date: Fri, 22 Nov 2024 12:05:14 -0700 Subject: [PATCH] a few more cases --- tests/testthat/test-fit-laplace.R | 2 +- tests/testthat/test-fit-shared.R | 4 ++-- tests/testthat/test-model-compile.R | 2 +- tests/testthat/test-model-laplace.R | 23 +++++++++++++---------- tests/testthat/test-model-pathfinder.R | 7 ++++--- tests/testthat/test-threads.R | 10 +++++++--- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/tests/testthat/test-fit-laplace.R b/tests/testthat/test-fit-laplace.R index af94bebc8..8991a9e12 100644 --- a/tests/testthat/test-fit-laplace.R +++ b/tests/testthat/test-fit-laplace.R @@ -1,4 +1,4 @@ -context("fitted-vb") +context("fitted-laplace") set_cmdstan_path() fit_laplace <- testing_fit("logistic", method = "laplace", seed = 100) diff --git a/tests/testthat/test-fit-shared.R b/tests/testthat/test-fit-shared.R index a4593c385..0f98e64ac 100644 --- a/tests/testthat/test-fit-shared.R +++ b/tests/testthat/test-fit-shared.R @@ -462,7 +462,7 @@ test_that("sampling with inits works with include_paths", { include_paths = test_path("resources", "stan")) data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1)) - expect_no_error( + expect_no_error(utils::capture.output( fit <- mod_w_include$sample( data = data_list, seed = 123, @@ -474,7 +474,7 @@ test_that("sampling with inits works with include_paths", { list(theta = 0.25), list(theta = 0.25)) ) - ) + )) }) test_that("CmdStanModel created with exe_file works", { diff --git a/tests/testthat/test-model-compile.R b/tests/testthat/test-model-compile.R index 2be8390f5..9517aeabf 100644 --- a/tests/testthat/test-model-compile.R +++ b/tests/testthat/test-model-compile.R @@ -837,7 +837,7 @@ test_that("dirname of stan_file is used as include path if no other paths suppli mod_tmp <- cmdstan_model(stan_file, compile = FALSE) expect_true(mod_tmp$check_syntax()) - expect_true(mod_tmp$format()) + utils::capture.output(expect_true(mod_tmp$format())) expect_s3_class(mod_tmp$compile(), "CmdStanModel") }) diff --git a/tests/testthat/test-model-laplace.R b/tests/testthat/test-model-laplace.R index caaef8e48..00dc78bbf 100644 --- a/tests/testthat/test-model-laplace.R +++ b/tests/testthat/test-model-laplace.R @@ -36,7 +36,9 @@ test_that("laplace() method errors for any invalid argument before calling cmdst for (nm in names(bad_arg_values)) { args <- ok_arg_values args[[nm]] <- bad_arg_values[[nm]] - expect_error(do.call(mod$laplace, args), regexp = nm, info = nm) + utils::capture.output( + expect_error(do.call(mod$laplace, args), regexp = nm, info = nm) + ) } args <- ok_arg_values args$opt_args <- list(iter = "NOT_A_NUMBER") @@ -129,14 +131,15 @@ test_that("laplace() errors with bad combinations of arguments", { test_that("laplace() errors if optimize() fails", { mod_schools <- testing_model("schools") - expect_error( - expect_warning( - expect_message( - mod_schools$laplace(data = testing_data("schools"), refresh = 0), - "Line search failed to achieve a sufficient decrease" + utils::capture.output( + expect_error( + expect_warning( + expect_message( + mod_schools$laplace(data = testing_data("schools"), refresh = 0), + "Line search failed to achieve a sufficient decrease" + ), + "Fitting finished unexpectedly" ), - "Fitting finished unexpectedly" - ), - "Optimization failed" - ) + "Optimization failed" + )) }) diff --git a/tests/testthat/test-model-pathfinder.R b/tests/testthat/test-model-pathfinder.R index 3eba55abc..d301588b1 100644 --- a/tests/testthat/test-model-pathfinder.R +++ b/tests/testthat/test-model-pathfinder.R @@ -11,7 +11,7 @@ data_list <- testing_data("bernoulli") data_file_r <- test_path("resources", "data", "bernoulli.data.R") data_file_json <- test_path("resources", "data", "bernoulli.data.json") -# these are all valid for sample() +# these are all valid for pathfinder() ok_arg_values <- list( data = data_list, output_dir = tempdir(), @@ -32,9 +32,10 @@ ok_arg_values <- list( max_lbfgs_iters = 100, save_single_paths = FALSE, calculate_lp = TRUE, - psis_resample=TRUE) + psis_resample=TRUE +) -# using any one of these should cause sample() to error +# using any one of these should cause pathfinder() to error bad_arg_values <- list( data = "NOT_A_FILE", output_dir = "NOT_A_DIRECTORY", diff --git a/tests/testthat/test-threads.R b/tests/testthat/test-threads.R index 1a333e82c..fb5eec615 100644 --- a/tests/testthat/test-threads.R +++ b/tests/testthat/test-threads.R @@ -172,9 +172,13 @@ test_that("correct output when stan_threads not TRUE", { fixed = TRUE ) mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = FALSE), force_recompile = TRUE) - expect_warning( - mod$sample(data = data_file_json, threads_per_chain = 4), - "'threads_per_chain' is set but the model was not compiled with 'cpp_options = list(stan_threads = TRUE)' so 'threads_per_chain' will have no effect!", + expect_output( + expect_warning( + mod$sample(data = data_file_json, threads_per_chain = 4), + "'threads_per_chain' is set but the model was not compiled with 'cpp_options = list(stan_threads = TRUE)' so 'threads_per_chain' will have no effect!", + fixed = TRUE + ), + "Running MCMC with 4 sequential chains", fixed = TRUE ) })