diff --git a/R/sim_all.R b/R/sim_all.R index 4c3849589ad69161ed535af2b52a10c1597c0f39..b0417e69b1af94a8617ce5fa39828a388f66abfb 100644 --- a/R/sim_all.R +++ b/R/sim_all.R @@ -4,8 +4,8 @@ #' @param resps Number of respondents you want to simulate #' @param destype Is it a design created with ngene or with spdesign. Ngene desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design #' @param designpath The path to the folder where the designs are stored. For example "c:/myfancydec/Designs" -#' @param u A list with utility functions. The list can incorporate as many decision rule groups as you want. However, each group must be in a list in this list. If you just use one group (the normal), this group still has to be in a list in the u list. As a convention name betacoefficients starting with a lower case "b" -#' @param bcoefficients List of coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions +#' @param u A list with utility functions. The list can incorporate as many decision rule groups as you want. However, each group must be in a list in this list. If you just use one group (the normal), this group still has to be in a list in the u list. As a convention name beta coefficients starting with a lower case "b" +#' @param bcoefficients List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions #' #' @return A list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template. #' @export @@ -27,20 +27,15 @@ #' sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff){ + ################################################# + ########## Input Validation Tests ############### + ################################################# ########### validate the utility function ######## if (missing(u) || !(is.list(u) && any(sapply(u, is.list)))){ - stop(" 'u' must be provided and must be a list containing at least one list element.") + stop(" 'u' must be provided and must be a list containing at least one list element (list of lists).") } - if (missing(resps) || !(is.integer(resps) || (is.numeric(resps) && identical(trunc(resps), resps)))) { - stop(" 'resps' must be provided and must be an integer indicating the number of respondents per run.") - } - - if (!dir.exists(designpath)) { - stop(" The folder where your designs are stored does not exist. \n Check if designpath is correctly specified") - } - ########## validate the bcoeff list ################ # Check if bcoeff is provided if (missing(bcoeff)) { @@ -75,8 +70,18 @@ sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff){ if (length(missing_coeffs) > 0) { stop(paste("Missing coefficients in 'bcoeff':", paste(missing_coeffs, collapse = ", "), ". Perhaps there is a typo?")) } + ########## validate resps ##################### + if (missing(resps) || !(is.integer(resps) || (is.numeric(resps) && identical(trunc(resps), resps)))) { + stop(" 'resps' must be provided and must be an integer indicating the number of respondents per run.") + } + ########## validate designpath ################ + if (!dir.exists(designpath)) { + stop(" The folder where your designs are stored does not exist. \n Check if designpath is correctly specified") + } - ### end input validation tests ## + ################################################# + ########## End Validation Tests ################# + ################################################# designfile<-list.files(designpath,full.names = T) designname <- stringr::str_remove_all(list.files(designpath,full.names = F), diff --git a/tests/testthat/Rplots.pdf b/tests/testthat/Rplots.pdf index 5395fa31505edc5a9a1307ef3509f29514e401ae..63af0c06f0b595c355ed0e6ef922eee3e640309f 100644 Binary files a/tests/testthat/Rplots.pdf and b/tests/testthat/Rplots.pdf differ diff --git a/tests/testthat/test-sim_all.R b/tests/testthat/test-sim_all.R index 8c7db5a2c01b9e0ea20e3d6e094f8c075d16ab0c..962ecbe593a2489fb204e53217f7d415f9a26931 100644 --- a/tests/testthat/test-sim_all.R +++ b/tests/testthat/test-sim_all.R @@ -31,11 +31,6 @@ ul<- list(u1= list( ) ) - - - - - test_that("u is not a list of lists", { expect_error(sim_all(nosim = nosim, resps=resps, destype = destype, designpath = designpath, u=data.frame(u=" alp"), bcoeff = bcoeff), @@ -126,8 +121,31 @@ test_that("Utility functions are valid", { expect_no_error(eval(ul$u1$v2)) }) -test_that("Function behavior matches documentation", { - expect_true(sim_all %in% names(simulateDCE:::NAMESPACE)) +test_that("Design path must be a valid directory", { + # Test case: designpath is not a character string + expect_error(sim_all(nosim = nosim, resps = resps, destype = destype, designpath = 123, u = ul, bcoeff = bcoeff)) + + # Test case: designpath does not exist + expect_error(sim_all(nosim = nosim, resps = resps, destype = destype, designpath = '/nonexistent/path', u = ul, bcoeff = bcoeff)) + + # Test case: designpath is not a directory + expect_error(sim_all(nosim = nosim, resps = resps, destype = destype, designpath = 'path/to/a/file.txt', u = ul, bcoeff = bcoeff)) +}) + +test_that("Resps must be an integer", { + # Test case: resps is missing + expect_error(sim_all(nosim = nosim, destype = destype, designpath = designpath, u = ul, bcoeff = bcoeff)) + + # Test case: resps is not an integer + expect_error(sim_all(nosim = nosim, resps = "abc", destype = destype, designpath = designpath, u = ul, bcoeff = bcoeff)) + + # Test case: resps is a numeric but not an integer + expect_error(sim_all(nosim = nosim, resps = 1.5, destype = destype, designpath = designpath, u = ul, bcoeff = bcoeff)) + +}) + +test_that("Function exists in simulateDCE", { + expect_true("sim_all" %in% ls("package:simulateDCE")) }) test_that("Simulation results are reasonable", {