Skip to content

Commit

Permalink
action_age: Explicit error if age dimension missing #134
Browse files Browse the repository at this point in the history
Without an age dimension, the stock_ss() calls will fail, but it's not
a sensible thing to do in the first place.

Fail with an informative error message.
  • Loading branch information
lentinj committed May 7, 2024
1 parent ce05843 commit e63649b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions R/action_age.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ g3a_age <- function(
run_f = ~cur_step_final,
run_at = g3_action_order$age,
transition_at = g3_action_order$age ) {
if (is.null(tryCatch(g3_stock_def(stock, 'minage'), error = function(x) NULL))) stop("stock missing age ", stock$name)

out <- new.env(parent = emptyenv())

stock__num <- g3_stock_instance(stock, 0)
Expand Down
6 changes: 6 additions & 0 deletions tests/test-action_age.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ library(unittest)

library(gadget3)

ok_group("g3a_age:no_age")
ok(ut_cmp_error({
g3a_age(g3_stock('prey_a', seq(20, 40, 4)))
}, "age.*prey_a"), "Cannot use g3a_age() without an age dimension")
#### g3a_age:no_age

ok_group("g3a_age:single_age", {
# prey_a, with single age, outputs into b & c
prey_a <- g3_stock('prey_a', seq(20, 40, 4)) %>% g3s_age(11, 11)
Expand Down

0 comments on commit e63649b

Please sign in to comment.