Skip to content
Snippets Groups Projects
Commit 6be6c256 authored by samuelsmock's avatar samuelsmock
Browse files

added unit tests and validations for all arguments

parent 2de588a0
No related branches found
No related tags found
No related merge requests found
......@@ -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),
......
No preview for this file type
......@@ -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", {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment