@@ -107,4 +107,80 @@ test_that("enlist works",{
107
107
my_list <- enlist(x = 1 ,y = 2 ,z = 3 )
108
108
expect_equal(my_list $ x ,1 )
109
109
expect_true(inherits(my_list ," list" ))
110
- })
110
+ })
111
+
112
+ test_that(" assert_sufficient_f_args alerts if the provided f doesn't take enough args" , {
113
+ f_xg = function (x , g ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
114
+ f_xg_dots = function (x , g , ... ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
115
+
116
+ # If `regexp` is NA, asserts that there should be no errors/messages.
117
+ expect_error(assert_sufficient_f_args(f_xg ), regexp = NA )
118
+ expect_warning(assert_sufficient_f_args(f_xg ), regexp = NA )
119
+ expect_error(assert_sufficient_f_args(f_xg_dots ), regexp = NA )
120
+ expect_warning(assert_sufficient_f_args(f_xg_dots ), regexp = NA )
121
+
122
+ f_x_dots = function (x , ... ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
123
+ f_dots = function (... ) dplyr :: tibble(value = c(5 ), count = c(2 ))
124
+ f_x = function (x ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
125
+ f = function () dplyr :: tibble(value = c(5 ), count = c(2 ))
126
+
127
+ expect_warning(assert_sufficient_f_args(f_x_dots ),
128
+ regexp = " , the group key will be included" ,
129
+ class = " epiprocess__assert_sufficient_f_args__mandatory_f_args_passed_to_f_dots" )
130
+ expect_warning(assert_sufficient_f_args(f_dots ),
131
+ regexp = " , the window data and group key will be included" ,
132
+ class = " epiprocess__assert_sufficient_f_args__mandatory_f_args_passed_to_f_dots" )
133
+ expect_error(assert_sufficient_f_args(f_x ),
134
+ class = " epiprocess__assert_sufficient_f_args__f_needs_min_args" )
135
+ expect_error(assert_sufficient_f_args(f ),
136
+ class = " epiprocess__assert_sufficient_f_args__f_needs_min_args" )
137
+
138
+ f_xs_dots = function (x , setting = " a" , ... ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
139
+ f_xs = function (x , setting = " a" ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
140
+ expect_warning(assert_sufficient_f_args(f_xs_dots , setting = " b" ),
141
+ class = " epiprocess__assert_sufficient_f_args__mandatory_f_args_passed_to_f_dots" )
142
+ expect_error(assert_sufficient_f_args(f_xs , setting = " b" ),
143
+ class = " epiprocess__assert_sufficient_f_args__f_needs_min_args_plus_forwarded" )
144
+
145
+ expect_error(assert_sufficient_f_args(f_xg , " b" ),
146
+ class = " epiprocess__assert_sufficient_f_args__f_needs_min_args_plus_forwarded" )
147
+ })
148
+
149
+ test_that(" assert_sufficient_f_args alerts if the provided f has defaults for the required args" , {
150
+ f_xg = function (x , g = 1 ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
151
+ f_xg_dots = function (x = 1 , g , ... ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
152
+ f_x_dots = function (x = 1 , ... ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
153
+
154
+ expect_error(assert_sufficient_f_args(f_xg ),
155
+ regexp = " pass the group key to `f`'s g argument," ,
156
+ class = " epiprocess__assert_sufficient_f_args__required_args_contain_defaults" )
157
+ expect_error(assert_sufficient_f_args(f_xg_dots ),
158
+ regexp = " pass the window data to `f`'s x argument," ,
159
+ class = " epiprocess__assert_sufficient_f_args__required_args_contain_defaults" )
160
+ expect_error(suppressWarnings(assert_sufficient_f_args(f_x_dots )),
161
+ class = " epiprocess__assert_sufficient_f_args__required_args_contain_defaults" )
162
+
163
+ f_xsg = function (x , setting = " a" , g ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
164
+ f_xsg_dots = function (x , setting = " a" , g , ... ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
165
+ f_xs_dots = function (x = 1 , setting = " a" , ... ) dplyr :: tibble(value = mean(x $ binary ), count = length(x $ binary ))
166
+
167
+ # forwarding named dots should prevent some complaints:
168
+ expect_no_error(assert_sufficient_f_args(f_xsg , setting = " b" ))
169
+ expect_no_error(assert_sufficient_f_args(f_xsg_dots , setting = " b" ))
170
+ expect_error(suppressWarnings(assert_sufficient_f_args(f_xs_dots , setting = " b" )),
171
+ regexp = " window data to `f`'s x argument" ,
172
+ class = " epiprocess__assert_sufficient_f_args__required_args_contain_defaults" )
173
+
174
+ # forwarding unnamed dots should not:
175
+ expect_error(assert_sufficient_f_args(f_xsg , " b" ),
176
+ class = " epiprocess__assert_sufficient_f_args__required_args_contain_defaults" )
177
+ expect_error(assert_sufficient_f_args(f_xsg_dots , " b" ),
178
+ class = " epiprocess__assert_sufficient_f_args__required_args_contain_defaults" )
179
+ expect_error(assert_sufficient_f_args(f_xs_dots , " b" ),
180
+ class = " epiprocess__assert_sufficient_f_args__required_args_contain_defaults" )
181
+
182
+ # forwarding no dots should produce a different error message in some cases:
183
+ expect_error(assert_sufficient_f_args(f_xs_dots ),
184
+ regexp = " window data and group key to `f`'s x and setting argument" ,
185
+ class = " epiprocess__assert_sufficient_f_args__required_args_contain_defaults" )
186
+ })
0 commit comments