1
1
2
- file_that_exists <- ' placeholder_exists'
3
- file_that_doesnt_exist <- ' placeholder_doesnt_exist'
2
+ file_that_exists <- " placeholder_exists"
3
+ file_that_doesnt_exist <- " placeholder_doesnt_exist"
4
4
file.create(file_that_exists )
5
- on.exit(if (file.exists(file_that_exists )) file.remove(file_that_exists ), add = TRUE , after = FALSE )
5
+ on.exit(
6
+ if (file.exists(file_that_exists )) file.remove(file_that_exists ),
7
+ add = TRUE ,
8
+ after = FALSE
9
+ )
6
10
7
11
make_local_orig <- cmdstan_make_local()
8
- cmdstan_make_local(cpp_options = list (" PRECOMPILED_HEADERS" = " false" ))
9
- on.exit(cmdstan_make_local(cpp_options = make_local_orig , append = FALSE ), add = TRUE , after = FALSE )
10
- hpp <-
11
- "
12
+ cmdstan_make_local(cpp_options = list (" PRECOMPILED_HEADERS" = " false" ))
13
+ on.exit(
14
+ cmdstan_make_local(cpp_options = make_local_orig , append = FALSE ),
15
+ add = TRUE ,
16
+ after = FALSE
17
+ )
18
+ hpp <- "
12
19
#include <stan/math.hpp>
13
20
#include <boost/math/tools/promotion.hpp>
14
21
#include <ostream>
@@ -17,9 +24,10 @@ namespace bernoulli_external_model_namespace
17
24
{
18
25
template <typename T0__,
19
26
stan::require_all_t<stan::is_stan_scalar<T0__>>* = nullptr>
20
- inline typename boost::math::tools::promote_args<T0__>::type make_odds(const T0__ &
21
- theta,
22
- std::ostream *pstream__)
27
+ inline typename boost::math::tools::promote_args<T0__>::type make_odds(
28
+ const T0__ & theta,
29
+ std::ostream *pstream__
30
+ )
23
31
{
24
32
return theta / (1 - theta);
25
33
}
@@ -30,77 +38,112 @@ test_that("cmdstan_model works with user_header with mock", {
30
38
tmpfile <- tempfile(fileext = " .hpp" )
31
39
cat(hpp , file = tmpfile , sep = " \n " )
32
40
33
- with_mocked_cli(compile_ret = list (status = 0 ), info_ret = list (), code = expect_mock_compile(
34
- expect_warning(
35
- expect_no_warning({
36
- mod <- cmdstan_model(
37
- stan_file = testing_stan_file(" bernoulli_external" ),
38
- exe_file = file_that_exists ,
39
- user_header = tmpfile
40
- )
41
- }, message = ' Recompiling is recommended' ), # this warning should not occur because recompile happens automatically
42
- ' Retrieving exe_file info failed' # this warning should occur
43
- )
44
- ))
45
-
46
- with_mocked_cli(compile_ret = list (status = 0 ), info_ret = list (), code = expect_mock_compile({
47
- mod_2 <- cmdstan_model(
48
- stan_file = testing_stan_file(" bernoulli_external" ),
49
- exe_file = file_that_doesnt_exist ,
50
- cpp_options = list (USER_HEADER = tmpfile ),
51
- stanc_options = list (" allow-undefined" )
41
+ with_mocked_cli(
42
+ compile_ret = list (status = 0 ),
43
+ info_ret = list (),
44
+ code = expect_mock_compile(
45
+ expect_warning(
46
+ expect_no_warning({
47
+ mod <- cmdstan_model(
48
+ stan_file = testing_stan_file(" bernoulli_external" ),
49
+ exe_file = file_that_exists ,
50
+ user_header = tmpfile
51
+ )
52
+ }, message = " Recompiling is recommended" ),
53
+ # ^ this warning should not occur because recompile happens automatically
54
+ " Retrieving exe_file info failed"
55
+ # ^ this warning should occur
56
+ )
52
57
)
53
- }))
58
+ )
59
+
60
+ with_mocked_cli(
61
+ compile_ret = list (status = 0 ),
62
+ info_ret = list (),
63
+ code = expect_mock_compile({
64
+ mod_2 <- cmdstan_model(
65
+ stan_file = testing_stan_file(" bernoulli_external" ),
66
+ exe_file = file_that_doesnt_exist ,
67
+ cpp_options = list (USER_HEADER = tmpfile ),
68
+ stanc_options = list (" allow-undefined" )
69
+ )
70
+ })
71
+ )
54
72
55
73
# Check recompilation upon changing header
56
74
file.create(file_that_exists )
57
- with_mocked_cli(compile_ret = list (status = 0 ), info_ret = list (), code = expect_no_mock_compile({
58
- mod $ compile(quiet = TRUE , user_header = tmpfile )
59
- }))
75
+ with_mocked_cli(
76
+ compile_ret = list (status = 0 ),
77
+ info_ret = list (),
78
+ code = expect_no_mock_compile({
79
+ mod $ compile(quiet = TRUE , user_header = tmpfile )
80
+ })
81
+ )
60
82
61
83
Sys.setFileTime(tmpfile , Sys.time() + 1 ) # touch file to trigger recompile
62
- with_mocked_cli(compile_ret = list (status = 0 ), info_ret = list (), code = expect_mock_compile({
63
- mod $ compile(quiet = TRUE , user_header = tmpfile )
64
- }))
84
+ with_mocked_cli(
85
+ compile_ret = list (status = 0 ),
86
+ info_ret = list (),
87
+ code = expect_mock_compile({
88
+ mod $ compile(quiet = TRUE , user_header = tmpfile )
89
+ })
90
+ )
65
91
66
92
# mock does not automatically update file mtime
67
93
Sys.setFileTime(mod $ exe_file(), Sys.time() + 1 ) # touch file to trigger recompile
68
94
69
95
# Alternative spec of user header
70
- with_mocked_cli(compile_ret = list (status = 0 ), info_ret = list (), code = expect_no_mock_compile({
71
- mod $ compile(
72
- quiet = TRUE ,
73
- cpp_options = list (user_header = tmpfile ),
74
- dry_run = TRUE
75
- )}))
96
+ with_mocked_cli(
97
+ compile_ret = list (status = 0 ),
98
+ info_ret = list (),
99
+ code = expect_no_mock_compile({
100
+ mod $ compile(
101
+ quiet = TRUE ,
102
+ cpp_options = list (user_header = tmpfile ),
103
+ dry_run = TRUE
104
+ )
105
+ })
106
+ )
76
107
77
108
# Error/warning messages
78
- with_mocked_cli(compile_ret = list (status = 1 ), info_ret = list (), code = expect_error(
79
- cmdstan_model(
80
- stan_file = testing_stan_file(" bernoulli_external" ),
81
- cpp_options = list (USER_HEADER = " non_existent.hpp" ),
82
- stanc_options = list (" allow-undefined" )
83
- ),
84
- " header file '[^']*' does not exist"
85
- ))
86
-
87
- with_mocked_cli(compile_ret = list (status = 1 ), info_ret = list (), code = expect_warning(
88
- cmdstan_model(
89
- stan_file = testing_stan_file(" bernoulli_external" ),
90
- cpp_options = list (USER_HEADER = tmpfile , user_header = tmpfile ),
91
- dry_run = TRUE
92
- ),
93
- " User header specified both"
94
- ))
95
- with_mocked_cli(compile_ret = list (status = 1 ), info_ret = list (), code = expect_warning(
96
- cmdstan_model(
97
- stan_file = testing_stan_file(" bernoulli_external" ),
98
- user_header = tmpfile ,
99
- cpp_options = list (USER_HEADER = tmpfile ),
100
- dry_run = TRUE
101
- ),
102
- " User header specified both"
103
- ))
109
+ with_mocked_cli(
110
+ compile_ret = list (status = 1 ),
111
+ info_ret = list (),
112
+ code = expect_error(
113
+ cmdstan_model(
114
+ stan_file = testing_stan_file(" bernoulli_external" ),
115
+ cpp_options = list (USER_HEADER = " non_existent.hpp" ),
116
+ stanc_options = list (" allow-undefined" )
117
+ ),
118
+ " header file '[^']*' does not exist"
119
+ )
120
+ )
121
+
122
+ with_mocked_cli(
123
+ compile_ret = list (status = 1 ),
124
+ info_ret = list (),
125
+ code = expect_warning(
126
+ cmdstan_model(
127
+ stan_file = testing_stan_file(" bernoulli_external" ),
128
+ cpp_options = list (USER_HEADER = tmpfile , user_header = tmpfile ),
129
+ dry_run = TRUE
130
+ ),
131
+ " User header specified both"
132
+ )
133
+ )
134
+ with_mocked_cli(
135
+ compile_ret = list (status = 1 ),
136
+ info_ret = list (),
137
+ code = expect_warning(
138
+ cmdstan_model(
139
+ stan_file = testing_stan_file(" bernoulli_external" ),
140
+ user_header = tmpfile ,
141
+ cpp_options = list (USER_HEADER = tmpfile ),
142
+ dry_run = TRUE
143
+ ),
144
+ " User header specified both"
145
+ )
146
+ )
104
147
})
105
148
106
149
test_that(" user_header precedence order is correct" , {
@@ -112,35 +155,53 @@ test_that("user_header precedence order is correct", {
112
155
add = TRUE
113
156
)
114
157
115
- with_mocked_cli(compile_ret = list (status = 1 ), info_ret = list (), code = expect_warning(
116
- {mod <- cmdstan_model(
117
- stan_file = testing_stan_file(" bernoulli_external" ),
118
- user_header = tmp_files [[1 ]],
119
- cpp_options = list (USER_HEADER = tmp_files [[2 ]], user_header = tmp_files [[3 ]]),
120
- dry_run = TRUE
121
- )},
122
- " User header specified both"
123
- ))
158
+ with_mocked_cli(
159
+ compile_ret = list (status = 1 ),
160
+ info_ret = list (),
161
+ code = expect_warning({
162
+ mod <- cmdstan_model(
163
+ stan_file = testing_stan_file(" bernoulli_external" ),
164
+ user_header = tmp_files [[1 ]],
165
+ cpp_options = list (
166
+ USER_HEADER = tmp_files [[2 ]],
167
+ user_header = tmp_files [[3 ]]
168
+ ),
169
+ dry_run = TRUE
170
+ )
171
+ }, " User header specified both" )
172
+ )
124
173
expect_equal(mod $ precompile_cpp_options()$ user_header , tmp_files [[1 ]])
125
174
126
- with_mocked_cli(compile_ret = list (status = 1 ), info_ret = list (), code = expect_warning(
127
- {mod <- cmdstan_model(
128
- stan_file = testing_stan_file(" bernoulli_external" ),
129
- cpp_options = list (USER_HEADER = tmp_files [[2 ]], user_header = tmp_files [[3 ]]),
130
- dry_run = TRUE
131
- )},
132
- " User header specified both"
133
- ))
175
+ with_mocked_cli(
176
+ compile_ret = list (status = 1 ),
177
+ info_ret = list (),
178
+ code = expect_warning({
179
+ mod <- cmdstan_model(
180
+ stan_file = testing_stan_file(" bernoulli_external" ),
181
+ cpp_options = list (
182
+ USER_HEADER = tmp_files [[2 ]],
183
+ user_header = tmp_files [[3 ]]
184
+ ),
185
+ dry_run = TRUE
186
+ )
187
+ }, " User header specified both" )
188
+ )
134
189
expect_equal(mod $ precompile_cpp_options()$ user_header , tmp_files [[2 ]])
135
190
136
- with_mocked_cli(compile_ret = list (status = 1 ), info_ret = list (), code = expect_warning(
137
- {mod <- cmdstan_model(
138
- stan_file = testing_stan_file(" bernoulli_external" ),
139
- cpp_options = list (user_header = tmp_files [[3 ]], USER_HEADER = tmp_files [[2 ]] ),
140
- dry_run = TRUE
141
- )},
142
- " User header specified both"
143
- ))
191
+ with_mocked_cli(
192
+ compile_ret = list (status = 1 ),
193
+ info_ret = list (),
194
+ code = expect_warning({
195
+ mod <- cmdstan_model(
196
+ stan_file = testing_stan_file(" bernoulli_external" ),
197
+ cpp_options = list (
198
+ user_header = tmp_files [[3 ]],
199
+ USER_HEADER = tmp_files [[2 ]]
200
+ ),
201
+ dry_run = TRUE
202
+ )
203
+ }, " User header specified both" )
204
+ )
144
205
expect_equal(mod $ precompile_cpp_options()$ user_header , tmp_files [[3 ]])
145
206
146
207
})
0 commit comments