@@ -36,7 +36,9 @@ test_that("laplace() method errors for any invalid argument before calling cmdst
3636 for (nm in names(bad_arg_values )) {
3737 args <- ok_arg_values
3838 args [[nm ]] <- bad_arg_values [[nm ]]
39- expect_error(do.call(mod $ laplace , args ), regexp = nm , info = nm )
39+ utils :: capture.output(
40+ expect_error(do.call(mod $ laplace , args ), regexp = nm , info = nm )
41+ )
4042 }
4143 args <- ok_arg_values
4244 args $ opt_args <- list (iter = " NOT_A_NUMBER" )
@@ -63,10 +65,12 @@ test_that("laplace() runs when all arguments specified validly", {
6365})
6466
6567test_that(" laplace() all valid 'mode' inputs give same results" , {
66- mode <- mod $ optimize(data = data_list , jacobian = TRUE , seed = 100 , refresh = 0 )
67- fit1 <- mod $ laplace(data = data_list , mode = mode , seed = 100 , refresh = 0 )
68- fit2 <- mod $ laplace(data = data_list , mode = mode $ output_files(), seed = 100 , refresh = 0 )
69- fit3 <- mod $ laplace(data = data_list , mode = NULL , seed = 100 , refresh = 0 )
68+ utils :: capture.output({
69+ mode <- mod $ optimize(data = data_list , jacobian = TRUE , seed = 100 , refresh = 0 )
70+ fit1 <- mod $ laplace(data = data_list , mode = mode , seed = 100 , refresh = 0 )
71+ fit2 <- mod $ laplace(data = data_list , mode = mode $ output_files(), seed = 100 , refresh = 0 )
72+ fit3 <- mod $ laplace(data = data_list , mode = NULL , seed = 100 , refresh = 0 )
73+ })
7074
7175 expect_is(fit1 , " CmdStanLaplace" )
7276 expect_is(fit2 , " CmdStanLaplace" )
@@ -85,17 +89,22 @@ test_that("laplace() all valid 'mode' inputs give same results", {
8589})
8690
8791test_that(" laplace() allows choosing number of draws" , {
88- fit <- mod $ laplace(data = data_list , draws = 10 , refresh = 0 )
92+ utils :: capture.output({
93+ fit <- mod $ laplace(data = data_list , draws = 10 , refresh = 0 )
94+ fit2 <- mod $ laplace(data = data_list , draws = 100 , refresh = 0 )
95+ })
96+
8997 expect_equal(fit $ metadata()$ draws , 10 )
9098 expect_equal(posterior :: ndraws(fit $ draws()), 10 )
9199
92- fit2 <- mod $ laplace(data = data_list , draws = 100 , refresh = 0 )
93100 expect_equal(fit2 $ metadata()$ draws , 100 )
94101 expect_equal(posterior :: ndraws(fit2 $ draws()), 100 )
95102})
96103
97104test_that(" laplace() errors if jacobian arg doesn't match what optimize used" , {
98- fit <- mod $ optimize(data = data_list , jacobian = FALSE , refresh = 0 )
105+ utils :: capture.output(
106+ fit <- mod $ optimize(data = data_list , jacobian = FALSE , refresh = 0 )
107+ )
99108 expect_error(
100109 mod $ laplace(data = data_list , mode = fit , jacobian = TRUE ),
101110 " 'jacobian' argument to optimize and laplace must match"
@@ -107,7 +116,9 @@ test_that("laplace() errors if jacobian arg doesn't match what optimize used", {
107116})
108117
109118test_that(" laplace() errors with bad combinations of arguments" , {
110- fit <- mod $ optimize(data = data_list , jacobian = TRUE , refresh = 0 )
119+ utils :: capture.output(
120+ fit <- mod $ optimize(data = data_list , jacobian = TRUE , refresh = 0 )
121+ )
111122 expect_error(
112123 mod $ laplace(data = data_list , mode = mod , opt_args = list (iter = 10 )),
113124 " Cannot specify both 'opt_args' and 'mode' arguments."
@@ -120,14 +131,15 @@ test_that("laplace() errors with bad combinations of arguments", {
120131
121132test_that(" laplace() errors if optimize() fails" , {
122133 mod_schools <- testing_model(" schools" )
123- expect_error(
124- expect_warning(
125- expect_message(
126- mod_schools $ laplace(data = testing_data(" schools" ), refresh = 0 ),
127- " Line search failed to achieve a sufficient decrease"
134+ utils :: capture.output(
135+ expect_error(
136+ expect_warning(
137+ expect_message(
138+ mod_schools $ laplace(data = testing_data(" schools" ), refresh = 0 ),
139+ " Line search failed to achieve a sufficient decrease"
140+ ),
141+ " Fitting finished unexpectedly"
128142 ),
129- " Fitting finished unexpectedly"
130- ),
131- " Optimization failed"
132- )
143+ " Optimization failed"
144+ ))
133145})
0 commit comments