Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set nml with vectors #245

Merged
merged 5 commits into from Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion R/set_nml.R
Expand Up @@ -17,7 +17,7 @@
#'print(glm_nml)
#'@seealso \link{get_nml_value}, \link{read_nml}
#'@export
set_nml <- function(glm_nml,arg_name,arg_val,arg_list=NULL){
set_nml <- function(glm_nml, arg_name, arg_val, arg_list = NULL){

if (missing(arg_name) & missing(arg_val)){
return(setnmlList(glm_nml,arg_list))
Expand Down Expand Up @@ -46,6 +46,9 @@ set_nml <- function(glm_nml,arg_name,arg_val,arg_list=NULL){
# get appropriate block to place val within ** assumes no duplicate param names in other blocks **
blck <- get_block(glm_nml,arg_name)
arg_name <- get_arg_name(arg_name)
if(length(arg_val) > 1 & is.character(arg_val)){
arg_val <- paste0(arg_val, collapse = ",")
}
glm_nml[[blck]][[arg_name]] <- arg_val
return(glm_nml)
}
30 changes: 30 additions & 0 deletions tests/testthat/test-nml.R
Expand Up @@ -28,6 +28,36 @@ test_that("can read in nml with vector for logicals", {
expect_is(get_nml_value(nml, 'flt_off_sw'), 'logical')
})

test_that("can read and write nml with vectors", {
# read character vectors
glm_nml <- read_nml()
glm_nml <- set_nml(glm_nml, arg_list = list(
'inflow_fl' = c("yahara.csv", "yahara2.csv")))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a small test to verify that setting it the old way has an identical result? So that we stay backwards compatible with this change. I expect it is the same, but better to have a test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think the new test I've added checks for this.

expect_equal(
length(get_nml_value(glm_nml, arg_name = "inflow_fl")),
1)

# read numeric vectors
glm_nml <- read_nml()
glm_nml <- set_nml(glm_nml, arg_list = list(
'A' = c(1, 2, 3)))
expect_true(
length(get_nml_value(glm_nml, arg_name = "A")) > 1)

# write character vectors
write_path <- paste0(tempdir(), 'glm2.nml')
write_nml(glm_nml, file = write_path)
expect_equal(
length(get_nml_value(read_nml(write_path), arg_name = "inflow_fl")),
1)

# write numeric vectors
write_path <- paste0(tempdir(), 'glm2.nml')
write_nml(glm_nml, file = write_path)
expect_true(
length(get_nml_value(read_nml(write_path), arg_name = "A")) > 1)
})

test_that("can read values from an nml file", {
nml <- read_nml()
nml <- set_nml(nml, "sim_name", "test")
Expand Down