Skip to content

Commit

Permalink
Allow only some processes to be named and not others.
Browse files Browse the repository at this point in the history
It was using `is.null` to detect unnamed processes, however in the case
of a list of processes where some are named and some are not, the name
is actually an empty string.
  • Loading branch information
plietar committed Jul 25, 2024
1 parent 76496ec commit fe41a9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ prepare_process <- function(p, name = NULL) {
ptr <- p
p <- function(t) execute_process(ptr, t)
}
if (!is.null(name)) {
if (!is.null(name) && name != "") {
env <- new.env()
assign(name, p, envir=env)
p <- function(t) eval(call(name, t), env)
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-simulation-e2e.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,20 @@ test_that("Can give two processes the same name", {

expect_equal(names, c("foo", "foo"))
})

test_that("Can give a name to just one process", {
names <- NULL

simulation_loop(
processes = list(
function(t) {
names <<- c(names, deparse(sys.call()[[1]]))
},
foo = function(t) {
names <<- c(names, deparse(sys.call()[[1]]))
}
),
timesteps = 1)

expect_equal(names[[2]], "foo")
})

0 comments on commit fe41a9e

Please sign in to comment.