Skip to content

Commit 0119ad7

Browse files
committed
style(lint): linting
1 parent c49cc4a commit 0119ad7

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

R/validate_model_inputs.R

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,20 @@ check_allowed_params <- function(object_name, actual_names, allowed_names) {
186186
extra_names <- setdiff(actual_names, allowed_names)
187187
missing_names <- setdiff(allowed_names, actual_names)
188188
if (length(extra_names) > 0L) {
189-
stop("Unrecognised parameter(s) in ", object_name, ": ",
190-
paste(extra_names, collapse = ", "), ". Allowed: ",
191-
paste(allowed_names, collapse = ", "), call. = FALSE)
189+
stop(
190+
sprintf(
191+
"Unrecognised parameter(s) in %s: %s. Allowed: %s",
192+
object_name, toString(extra_names), toString(allowed_names)
193+
), call. = FALSE
194+
)
192195
}
193196
if (length(missing_names) > 0L) {
194-
stop("Missing required parameter(s) in ", object_name, ": ",
195-
paste(missing_names, collapse = ", "), ". Allowed: ",
196-
paste(allowed_names, collapse = ", "), call. = FALSE)
197+
stop(
198+
sprintf(
199+
"Missing required parameter(s) in %s: %s. Allowed: %s",
200+
object_name, toString(missing_names), toString(allowed_names)
201+
), call. = FALSE
202+
)
197203
}
198204
}
199205

@@ -230,7 +236,7 @@ check_param_values <- function(param) {
230236
check_allowed_params(
231237
object_name = paste0("param$dist_config$", dist_name, "$params"),
232238
actual_names = names(params),
233-
allowed_names = c("mean")
239+
allowed_names = "mean"
234240
)
235241
}
236242

tests/testthat/test-functionaltest.R

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ update_routing_prob <- function(param, routing_name, updates) {
2727

2828
params_list <- param$dist_config[[routing_name]]$params
2929

30-
if (is.null(names(updates)) || any(names(updates) == "")) {
30+
if (is.null(names(updates)) || !all(nzchar(names(updates)))) {
3131
stop("'updates' must be a named vector or list", call. = FALSE)
3232
}
3333

@@ -94,15 +94,15 @@ test_that("model errors for invalid asu_los values", {
9494
test_that("model errors for invalid asu_routing probabilities", {
9595
param <- parameters()
9696
# Non-numeric value
97-
param <- update_routing_prob(param, "asu_routing_stroke", c("rehab" = "a"))
97+
param <- update_routing_prob(param, "asu_routing_stroke", c(rehab = "a"))
9898
expect_error(
9999
model(param = param, run_number = 1L),
100100
'Routing vector "asu_routing_stroke$params$prob" must be numeric.',
101101
fixed = TRUE
102102
)
103103
# Probability out of bounds
104104
param <- parameters()
105-
param <- update_routing_prob(param, "asu_routing_stroke", c("rehab" = -0.1))
105+
param <- update_routing_prob(param, "asu_routing_stroke", c(rehab = -0.1))
106106
expect_error(
107107
model(param = param, run_number = 1L),
108108
'All values in routing vector "asu_routing_stroke$params$prob" must be between 0 and 1.', # nolint: line_length_linter
@@ -111,7 +111,7 @@ test_that("model errors for invalid asu_routing probabilities", {
111111
# Probabilities do not sum to 1
112112
param <- parameters()
113113
param <- update_routing_prob(param, "asu_routing_stroke",
114-
c("rehab" = 0.5, "esd" = 0.5, "other" = 0.5))
114+
c(rehab = 0.5, esd = 0.5, other = 0.5))
115115
expect_error(
116116
model(param = param, run_number = 1L),
117117
'Values in routing vector "asu_routing_stroke$params$prob" must sum to 1 (+-0.01).', # nolint: line_length_linter
@@ -123,7 +123,7 @@ test_that("model errors for invalid asu_routing probabilities", {
123123
test_that("model errors for invalid rehab_routing probabilities", {
124124
# Probabilities should be within 0 and 1
125125
param <- parameters()
126-
param <- update_routing_prob(param, "rehab_routing_other", c("esd" = 1.5))
126+
param <- update_routing_prob(param, "rehab_routing_other", c(esd = 1.5))
127127
expect_error(
128128
model(param = param, run_number = 1L),
129129
'All values in routing vector "rehab_routing_other$params$prob" must be between 0 and 1.', # nolint: line_length_linter
@@ -133,7 +133,7 @@ test_that("model errors for invalid rehab_routing probabilities", {
133133
# Probabilities should sum to 1
134134
param <- parameters()
135135
param <- update_routing_prob(param, "rehab_routing_stroke",
136-
c("esd"=0.8, "other"=0.3))
136+
c(esd = 0.8, other = 0.3))
137137
expect_error(
138138
model(param = param, run_number = 1L),
139139
'Values in routing vector "rehab_routing_stroke$params$prob" must sum to 1 (+-0.01)', # nolint: line_length_linter
@@ -143,40 +143,58 @@ test_that("model errors for invalid rehab_routing probabilities", {
143143

144144

145145
patrick::with_parameters_test_that(
146-
"model errors for invalid/missing/extra keys in parameters",
146+
"model errors for invalid, missing, and extra keys in parameters",
147147
{
148148
param <- parameters()
149149
param <- mod(param)
150150
expect_error(model(run_number = 0L, param = param), msg, fixed = TRUE)
151151
},
152152
patrick::cases(
153153
missing_number_of_runs = list(
154-
mod = function(p) { p$number_of_runs <- NULL; p },
154+
mod = function(p) {
155+
p$number_of_runs <- NULL
156+
p
157+
},
155158
msg = "Problem in param. Missing: number_of_runs. Extra: ."
156159
),
157160
# Missing key in param$dist_config
158161
missing_rehab_arrival_neuro = list(
159-
mod = function(p) { p$dist_config$rehab_arrival_neuro <- NULL; p },
162+
mod = function(p) {
163+
p$dist_config$rehab_arrival_neuro <- NULL
164+
p
165+
},
160166
msg = "Problem in param$dist_config. Missing: rehab_arrival_neuro. Extra: ." # nolint: line_length_linter
161167
),
162168
# Missing specific dist_config key
163169
missing_rehab_los_tia = list(
164-
mod = function(p) { p$dist_config$rehab_los_tia$params <- NULL; p },
170+
mod = function(p) {
171+
p$dist_config$rehab_los_tia$params <- NULL
172+
p
173+
},
165174
msg = "Missing required parameter(s) in param$dist_configrehab_los_tia: params. Allowed: class_name, params" # nolint: line_length_linter
166175
),
167176
# Extra key in top-level param
168177
extra_top_level = list(
169-
mod = function(p) { p$extra_key <- 5L; p },
178+
mod = function(p) {
179+
p$extra_key <- 5L
180+
p
181+
},
170182
msg = "Problem in param. Missing: . Extra: extra_key."
171183
),
172184
# Extra key in param$dist_config
173185
extra_in_dist_config = list(
174-
mod = function(p) { p$dist_config$extra_key <- 5L; p },
186+
mod = function(p) {
187+
p$dist_config$extra_key <- 5L
188+
p
189+
},
175190
msg = "Problem in param$dist_config. Missing: . Extra: extra_key."
176191
),
177192
# Extra key in nested dist_config entry
178193
extra_in_asu_arrival_stroke = list(
179-
mod = function(p) { p$dist_config$asu_arrival_stroke$extra_key <- 5L; p },
194+
mod = function(p) {
195+
p$dist_config$asu_arrival_stroke$extra_key <- 5L
196+
p
197+
},
180198
msg = "Unrecognised parameter(s) in param$dist_configasu_arrival_stroke: extra_key. Allowed: class_name, params" # nolint: line_length_linter
181199
)
182200
)

0 commit comments

Comments
 (0)