diff --git a/DESCRIPTION b/DESCRIPTION index d219e992..2c0ebc62 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: admiraldev Title: Utility Functions and Development Tools for the Admiral Package Family -Version: 1.1.0.9006 +Version: 1.1.0.9007 Authors@R: c( person("Ben", "Straub", , "ben.x.straub@gsk.com", role = c("aut", "cre")), person("Stefan", "Bundfuss", role = "aut", diff --git a/NEWS.md b/NEWS.md index d4b0367d..046ab884 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,9 @@ used in the glue expression specified for the `message_text` argument. (#469) possible to specify more than one unit or not specify it at all. In the latter case only the uniqueness of the unit is checked. (#468) +- The `assert_numeric_vector()` function gained an optional `length` argument to +check whether the vector has a specific length. (#473) + ## Breaking Changes - The following functions are entering the next phase of the deprecation process: (#459) diff --git a/R/assertions.R b/R/assertions.R index 6203df4f..a92c8f91 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -687,6 +687,10 @@ assert_integer_scalar <- function(arg, #' Checks if an argument is a numeric vector #' #' @param arg A function argument to be checked +#' @param length Expected length +#' +#' If the argument is not specified or set to `NULL`, any length is accepted. +#' #' @param optional Is the checked argument optional? If set to `FALSE` and `arg` #' is `NULL` then an error is thrown #' @inheritParams assert_logical_scalar @@ -708,12 +712,20 @@ assert_integer_scalar <- function(arg, #' example_fun(1:10) #' #' try(example_fun(letters)) +#' +#' example_fun <- function(num) { +#' assert_numeric_vector(num, length = 2) +#' } +#' +#' try(example_fun(1:10)) assert_numeric_vector <- function(arg, + length = NULL, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_numeric_vector", call = parent.frame()) { + assert_integer_scalar(length, subset = "positive", optional = TRUE) assert_logical_scalar(optional) if (optional && is.null(arg)) { @@ -730,6 +742,20 @@ assert_numeric_vector <- function(arg, ) } + if (!is.null(length)) { + if (length(arg) != length) { + cli_abort( + message = message %||% + paste( + "Argument {.arg {arg_name}} must be a vector of length {.val {length}},", + "but has length {.val {length(arg)}}." + ), + class = c(class, "assert-admiraldev"), + call = call + ) + } + } + invisible(arg) } diff --git a/man/assert_numeric_vector.Rd b/man/assert_numeric_vector.Rd index 3438afcf..f0743cd9 100644 --- a/man/assert_numeric_vector.Rd +++ b/man/assert_numeric_vector.Rd @@ -6,6 +6,7 @@ \usage{ assert_numeric_vector( arg, + length = NULL, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, @@ -16,6 +17,10 @@ assert_numeric_vector( \arguments{ \item{arg}{A function argument to be checked} +\item{length}{Expected length + +If the argument is not specified or set to \code{NULL}, any length is accepted.} + \item{optional}{Is the checked argument optional? If set to \code{FALSE} and \code{arg} is \code{NULL} then an error is thrown} @@ -56,6 +61,12 @@ example_fun <- function(num) { example_fun(1:10) try(example_fun(letters)) + +example_fun <- function(num) { + assert_numeric_vector(num, length = 2) +} + +try(example_fun(1:10)) } \seealso{ Checks for valid input and returns warning or errors messages: diff --git a/tests/testthat/_snaps/assertions.md b/tests/testthat/_snaps/assertions.md index 00b01cd3..fdd6048d 100644 --- a/tests/testthat/_snaps/assertions.md +++ b/tests/testthat/_snaps/assertions.md @@ -212,7 +212,7 @@ Error in `example_fun()`: ! Argument `arg` must be an integer scalar. -# assert_numeric_vector Test 37: error if `arg` is not a numeric vector +# assert_numeric_vector Test 38: error if `arg` is not a numeric vector Code example_fun(TRUE) @@ -236,7 +236,15 @@ Error in `example_fun()`: ! Argument `arg` must be a numeric vector, but it is a string. -# assert_s3_class Test 38: error if `arg` is not an object of a specific class S3 +# assert_numeric_vector Test 39: error if length is not as expected + + Code + assert_numeric_vector(numbers, length = 2) + Condition + Error: + ! Argument `numbers` must be a vector of length 2, but has length 3. + +# assert_s3_class Test 40: error if `arg` is not an object of a specific class S3 Code example_fun("test") @@ -244,7 +252,7 @@ Error in `example_fun()`: ! Argument `arg` must be class , but is a string. -# assert_s3_class Test 40: error if `arg` is NULL and optional is FALSE +# assert_s3_class Test 42: error if `arg` is NULL and optional is FALSE Code example_fun(NULL) @@ -252,7 +260,7 @@ Error in `example_fun()`: ! Argument `arg` must be class , but is NULL. -# assert_list_of Test 42: error if `arg` is not a list of specific class S3 objects +# assert_list_of Test 44: error if `arg` is not a list of specific class S3 objects Code example_fun(list("test")) @@ -261,7 +269,7 @@ ! Each element of the list in argument `arg` must be class/type . i But, element 1 is a string -# assert_list_of Test 44: error if `arg` is NULL and optional is FALSE +# assert_list_of Test 46: error if `arg` is NULL and optional is FALSE Code example_fun(NULL) @@ -269,7 +277,7 @@ Error in `example_fun()`: ! Argument `arg` must be class , but is NULL. -# assert_list_of Test 46: error if `arg` is not a named list (no elements named) +# assert_list_of Test 48: error if `arg` is not a named list (no elements named) Code mylist <- list(1, 2, 3) @@ -279,7 +287,7 @@ ! All elements of `mylist` argument must be named. i The indices of the unnamed elements are 1, 2, and 3 -# assert_list_of Test 47: error if `arg` is not a named list (some elements named) +# assert_list_of Test 49: error if `arg` is not a named list (some elements named) Code mylist <- list(1, 2, 3, d = 4) @@ -289,7 +297,7 @@ ! All elements of `mylist` argument must be named. i The indices of the unnamed elements are 1, 2, and 3 -# assert_named Test 50: error if no elements are named +# assert_named Test 52: error if no elements are named Code arg <- c(1, 2) @@ -299,7 +307,7 @@ ! All elements of `arg` argument must be named. i The indices of the unnamed elements are 1 and 2 -# assert_function Test 51: error if `arg` is not a function +# assert_function Test 53: error if `arg` is not a function Code example_fun(5) @@ -307,7 +315,7 @@ Error in `example_fun()`: ! Argument `arg` must be a function, but is a number. -# assert_function Test 54: error if `params` is missing with no default +# assert_function Test 56: error if `params` is missing with no default Code example_fun(sum) @@ -323,7 +331,7 @@ Error in `example_fun()`: ! "x" and "y" are not arguments of the function specified for `arg`. -# assert_unit Test 59: error if multiple units in the input dataset +# assert_unit Test 61: error if multiple units in the input dataset Code assert_unit(advs, param = "WEIGHT", get_unit_expr = VSSTRESU) @@ -331,7 +339,7 @@ Error: ! Multiple units "kg" and "lb" found for "WEIGHT". Please review and update the units. -# assert_unit Test 60: error if unexpected unit in the input dataset +# assert_unit Test 62: error if unexpected unit in the input dataset Code assert_unit(advs, param = "WEIGHT", required_unit = "lb", get_unit_expr = VSSTRESU) @@ -339,7 +347,7 @@ Error: ! It is expected that "WEIGHT" has unit of "lb". In the input dataset the unit is "kg". -# assert_unit Test 61: error if get_unit_expr invalid +# assert_unit Test 63: error if get_unit_expr invalid Code assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSTRESU) @@ -349,7 +357,7 @@ See error message below: i In argument: `_unit = VSTRESU`. Caused by error: ! object 'VSTRESU' not found -# assert_param_does_not_exist Test 62: error if parameter exists in the input dataset +# assert_param_does_not_exist Test 64: error if parameter exists in the input dataset Code assert_param_does_not_exist(advs, param = "WEIGHT") @@ -357,7 +365,7 @@ Error: ! The parameter code "WEIGHT" already exists in dataset `advs`. -# assert_varval_list Test 64: error if `arg` is not a list of var-value expressions +# assert_varval_list Test 66: error if `arg` is not a list of expressions Code example_fun(c("USUBJID", "PARAMCD", "VISIT")) @@ -366,7 +374,7 @@ ! Argument `arg` must be a named list of expressions where each element is a symbol, character scalar, numeric scalar, an expression, or NA, but is a character vector. i To create a list of expressions use `exprs()`. -# assert_varval_list Test 65: error if `arg` is not a list of var-value expressions +# assert_varval_list Test 67: error if not all elements are variables Code example_fun(exprs(USUBJID, PARAMCD, NULL)) @@ -375,7 +383,7 @@ ! Argument `arg` must be a list of expressions where each element is a symbol, character scalar, numeric scalar, an expression, or NA, but is a list. i To create a list of expressions use `exprs()`. -# assert_varval_list Test 66: error if `required_elements` are missing from `arg` +# assert_varval_list Test 68: error if `required_elements` are missing from `arg` Code example_fun(exprs(DTHSEQ = AESEQ)) @@ -383,7 +391,7 @@ Error in `example_fun()`: ! The following required elements are missing from argument `arg`: "DTHDOM". -# assert_varval_list Test 68: error if `accept_expr` is TRUE and value is invalid +# assert_varval_list Test 70: error if `accept_expr` is TRUE and value is invalid Code example_fun(exprs(DTHSEQ = TRUE)) @@ -392,7 +400,7 @@ ! The elements of the list in argument `arg` must be a symbol, character scalar, numeric scalar, an expression, or NA. i "DTHSEQ" = `TRUE` is of type -# assert_varval_list Test 69: error if `accept_expr` is FALSE and value is invalid +# assert_varval_list Test 71: error if `accept_expr` is FALSE and value is invalid Code example_fun(exprs(DTHSEQ = exprs())) @@ -401,7 +409,7 @@ ! The elements of the list in argument `arg` must be a symbol, character scalar, numeric scalar, or NA. i "DTHSEQ" = `exprs()` is of type -# assert_list_element Test 78: error if the elements do not fulfill the condition +# assert_list_element Test 80: error if the elements do not fulfill the condition Code assert_list_element(list(list(var = expr(DTHDT), val = 1), list(var = expr( @@ -425,7 +433,7 @@ ! List element "val" must one of 0 and 1 in argument `input`. i But, `input[[2]]$val = -1`, and `input[[3]]$val = -2` -# assert_one_to_one Test 79: error if there is a one to many mapping +# assert_one_to_one Test 81: error if there is a one to many mapping Code assert_one_to_one(dm, exprs(DOMAIN), exprs(USUBJID)) @@ -434,7 +442,7 @@ ! For some values of "DOMAIN" there is more than one value of "USUBJID" i Call `admiral::get_one_to_many_dataset()` to get all one-to-many values. -# assert_date_var Test 82: error if variable is not a date or datetime variable +# assert_date_var Test 84: error if variable is not a date or datetime variable Code example_fun(dataset = my_data, var = USUBJID) @@ -442,7 +450,7 @@ Error in `example_fun()`: ! Column "USUBJID" in dataset `dataset` must be a date or datetime, but is a character vector. -# assert_date_vector Test 86: error if `arg` is NULL and optional is FALSE +# assert_date_vector Test 88: error if `arg` is NULL and optional is FALSE Code example_fun(NULL) @@ -450,7 +458,7 @@ Error in `example_fun()`: ! Argument `arg` must be a date or datetime, but is NULL. -# assert_atomic_vector Test 87: error if input is not atomic vector +# assert_atomic_vector Test 89: error if input is not atomic vector Code assert_atomic_vector(x) @@ -458,7 +466,7 @@ Error: ! Argument `x` must be an atomic vector, but is a list. -# assert_same_type Test 89: error if different type +# assert_same_type Test 91: error if different type Code assert_same_type(true_value, false_value, missing_value) diff --git a/tests/testthat/test-assertions.R b/tests/testthat/test-assertions.R index 2723d5f4..79ae6b42 100644 --- a/tests/testthat/test-assertions.R +++ b/tests/testthat/test-assertions.R @@ -685,8 +685,16 @@ test_that("assert_integer_scalar Test 35: error if `arg` is not an integer scala }) # assert_numeric_vector ---- -## Test 36: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_numeric_vector Test 36: no error if `arg` is NULL and optional is TRUE", { + +## Test 36: no error for expected input ---- +test_that("assert_numeric_vector Test 36: no error for expected input", { + expect_invisible( + assert_numeric_vector(c(0, 1), length = 2) + ) +}) + +## Test 37: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_numeric_vector Test 37: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_numeric_vector(arg, optional = TRUE) } @@ -696,8 +704,8 @@ test_that("assert_numeric_vector Test 36: no error if `arg` is NULL and optional ) }) -## Test 37: error if `arg` is not a numeric vector ---- -test_that("assert_numeric_vector Test 37: error if `arg` is not a numeric vector", { +## Test 38: error if `arg` is not a numeric vector ---- +test_that("assert_numeric_vector Test 38: error if `arg` is not a numeric vector", { example_fun <- function(arg) { assert_numeric_vector(arg) } @@ -732,9 +740,21 @@ test_that("assert_numeric_vector Test 37: error if `arg` is not a numeric vector ) }) +## Test 39: error if length is not as expected ---- +test_that("assert_numeric_vector Test 39: error if length is not as expected", { + numbers <- c(1, 2, 3) + expect_snapshot( + assert_numeric_vector( + numbers, + length = 2 + ), + error = TRUE + ) +}) + # assert_s3_class ---- -## Test 38: error if `arg` is not an object of a specific class S3 ---- -test_that("assert_s3_class Test 38: error if `arg` is not an object of a specific class S3", { +## Test 40: error if `arg` is not an object of a specific class S3 ---- +test_that("assert_s3_class Test 40: error if `arg` is not an object of a specific class S3", { example_fun <- function(arg) { assert_s3_class(arg, "factor") } @@ -749,8 +769,8 @@ test_that("assert_s3_class Test 38: error if `arg` is not an object of a specifi ) }) -## Test 39: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_s3_class Test 39: no error if `arg` is NULL and optional is TRUE", { +## Test 41: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_s3_class Test 41: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_s3_class(arg, cls = "factor", optional = TRUE) } @@ -760,8 +780,8 @@ test_that("assert_s3_class Test 39: no error if `arg` is NULL and optional is TR ) }) -## Test 40: error if `arg` is NULL and optional is FALSE ---- -test_that("assert_s3_class Test 40: error if `arg` is NULL and optional is FALSE", { +## Test 42: error if `arg` is NULL and optional is FALSE ---- +test_that("assert_s3_class Test 42: error if `arg` is NULL and optional is FALSE", { example_fun <- function(arg) { assert_s3_class(arg, cls = "factor", optional = FALSE) } @@ -773,8 +793,8 @@ test_that("assert_s3_class Test 40: error if `arg` is NULL and optional is FALSE expect_snapshot(example_fun(NULL), error = TRUE) }) -## Test 41: no error if `arg` is an object of a specific class S3 ---- -test_that("assert_s3_class Test 41: no error if `arg` is an object of a specific class S3", { +## Test 43: no error if `arg` is an object of a specific class S3 ---- +test_that("assert_s3_class Test 43: no error if `arg` is an object of a specific class S3", { example_fun <- function(arg) { assert_s3_class(arg, "factor") } @@ -783,8 +803,8 @@ test_that("assert_s3_class Test 41: no error if `arg` is an object of a specific }) # assert_list_of ---- -## Test 42: error if `arg` is not a list of specific class S3 objects ---- -test_that("assert_list_of Test 42: error if `arg` is not a list of specific class S3 objects", { +## Test 44: error if `arg` is not a list of specific class S3 objects ---- +test_that("assert_list_of Test 44: error if `arg` is not a list of specific class S3 objects", { example_fun <- function(arg) { assert_list_of(arg, "factor") } @@ -799,8 +819,8 @@ test_that("assert_list_of Test 42: error if `arg` is not a list of specific clas ) }) -## Test 43: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_list_of Test 43: no error if `arg` is NULL and optional is TRUE", { +## Test 45: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_list_of Test 45: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_list_of(arg, cls = "factor", optional = TRUE) } @@ -810,8 +830,8 @@ test_that("assert_list_of Test 43: no error if `arg` is NULL and optional is TRU ) }) -## Test 44: error if `arg` is NULL and optional is FALSE ---- -test_that("assert_list_of Test 44: error if `arg` is NULL and optional is FALSE", { +## Test 46: error if `arg` is NULL and optional is FALSE ---- +test_that("assert_list_of Test 46: error if `arg` is NULL and optional is FALSE", { example_fun <- function(arg) { assert_list_of(arg, cls = "factor", optional = FALSE) } @@ -823,8 +843,8 @@ test_that("assert_list_of Test 44: error if `arg` is NULL and optional is FALSE" expect_snapshot(example_fun(NULL), error = TRUE) }) -## Test 45: no error if `arg` is a list of specific class S3 objects ---- -test_that("assert_list_of Test 45: no error if `arg` is a list of specific class S3 objects", { +## Test 47: no error if `arg` is a list of specific class S3 objects ---- +test_that("assert_list_of Test 47: no error if `arg` is a list of specific class S3 objects", { example_fun <- function(arg) { assert_list_of(arg, "factor") } @@ -836,8 +856,8 @@ test_that("assert_list_of Test 45: no error if `arg` is a list of specific class ) }) -## Test 46: error if `arg` is not a named list (no elements named) ---- -test_that("assert_list_of Test 46: error if `arg` is not a named list (no elements named)", { +## Test 48: error if `arg` is not a named list (no elements named) ---- +test_that("assert_list_of Test 48: error if `arg` is not a named list (no elements named)", { expect_error( { mylist <- list(1, 2, 3) @@ -854,8 +874,8 @@ test_that("assert_list_of Test 46: error if `arg` is not a named list (no elemen ) }) -## Test 47: error if `arg` is not a named list (some elements named) ---- -test_that("assert_list_of Test 47: error if `arg` is not a named list (some elements named)", { +## Test 49: error if `arg` is not a named list (some elements named) ---- +test_that("assert_list_of Test 49: error if `arg` is not a named list (some elements named)", { expect_error( { mylist <- list(1, 2, 3, d = 4) @@ -872,21 +892,21 @@ test_that("assert_list_of Test 47: error if `arg` is not a named list (some elem ) }) -## Test 48: no error if `arg` is a named list ---- -test_that("assert_list_of Test 48: no error if `arg` is a named list", { +## Test 50: no error if `arg` is a named list ---- +test_that("assert_list_of Test 50: no error if `arg` is a named list", { expect_invisible( assert_list_of(mylist <- list(a = 1, b = 2, c = 3), cls = "numeric", named = TRUE) ) }) # assert_named ---- -## Test 49: no error if arg is NULL and optional = TRUE ---- -test_that("assert_named Test 49: no error if arg is NULL and optional = TRUE", { +## Test 51: no error if arg is NULL and optional = TRUE ---- +test_that("assert_named Test 51: no error if arg is NULL and optional = TRUE", { expect_invisible(assert_named(arg <- NULL, optional = TRUE)) }) -## Test 50: error if no elements are named ---- -test_that("assert_named Test 50: error if no elements are named", { +## Test 52: error if no elements are named ---- +test_that("assert_named Test 52: error if no elements are named", { expect_error( { arg <- c(1, 2) @@ -905,8 +925,8 @@ test_that("assert_named Test 50: error if no elements are named", { }) # assert_function ---- -## Test 51: error if `arg` is not a function ---- -test_that("assert_function Test 51: error if `arg` is not a function", { +## Test 53: error if `arg` is not a function ---- +test_that("assert_function Test 53: error if `arg` is not a function", { example_fun <- function(arg) { assert_function(arg) } @@ -919,8 +939,8 @@ test_that("assert_function Test 51: error if `arg` is not a function", { expect_error(example_fun(), class = "assert_function") }) -## Test 52: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_function Test 52: no error if `arg` is NULL and optional is TRUE", { +## Test 54: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_function Test 54: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_function(arg, optional = TRUE) } @@ -930,8 +950,8 @@ test_that("assert_function Test 52: no error if `arg` is NULL and optional is TR ) }) -## Test 53: no error if `arg` is a function with all parameters defined ---- -test_that("assert_function Test 53: no error if `arg` is a function with all parameters defined", { +## Test 55: no error if `arg` is a function with all parameters defined ---- +test_that("assert_function Test 55: no error if `arg` is a function with all parameters defined", { example_fun <- function(arg) { assert_function(arg, params = c("x")) } @@ -939,8 +959,8 @@ test_that("assert_function Test 53: no error if `arg` is a function with all par expect_invisible(example_fun(mean)) }) -## Test 54: error if `params` is missing with no default ---- -test_that("assert_function Test 54: error if `params` is missing with no default", { +## Test 56: error if `params` is missing with no default ---- +test_that("assert_function Test 56: error if `params` is missing with no default", { example_fun <- function(arg) { assert_function(arg, params = c("x")) } @@ -968,8 +988,8 @@ test_that("assert_function Test 54: error if `params` is missing with no defau ) }) -## Test 55: If dot-dot-dot is a argument ---- -test_that("assert_function Test 55: If dot-dot-dot is a argument", { +## Test 57: If dot-dot-dot is a argument ---- +test_that("assert_function Test 57: If dot-dot-dot is a argument", { example_fun <- function(derivation, args = NULL) { assert_function(derivation, names(args)) } @@ -977,8 +997,8 @@ test_that("assert_function Test 55: If dot-dot-dot is a argument", { }) # assert_unit ---- -## Test 56: no error if the parameter is provided in the expected unit ---- -test_that("assert_unit Test 56: no error if the parameter is provided in the expected unit", { +## Test 58: no error if the parameter is provided in the expected unit ---- +test_that("assert_unit Test 58: no error if the parameter is provided in the expected unit", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -990,8 +1010,8 @@ test_that("assert_unit Test 56: no error if the parameter is provided in the exp ) }) -## Test 57: no error for multiple expected units ---- -test_that("assert_unit Test 57: no error for multiple expected units", { +## Test 59: no error for multiple expected units ---- +test_that("assert_unit Test 59: no error for multiple expected units", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1008,8 +1028,8 @@ test_that("assert_unit Test 57: no error for multiple expected units", { ) }) -## Test 58: no error if all units NA ---- -test_that("assert_unit Test 58: no error if all units NA", { +## Test 60: no error if all units NA ---- +test_that("assert_unit Test 60: no error if all units NA", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "RATIO", 80.1, NA_character_, "WEIGHT", 80.1, @@ -1026,8 +1046,8 @@ test_that("assert_unit Test 58: no error if all units NA", { ) }) -## Test 59: error if multiple units in the input dataset ---- -test_that("assert_unit Test 59: error if multiple units in the input dataset", { +## Test 61: error if multiple units in the input dataset ---- +test_that("assert_unit Test 61: error if multiple units in the input dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1044,8 +1064,8 @@ test_that("assert_unit Test 59: error if multiple units in the input dataset", { ) }) -## Test 60: error if unexpected unit in the input dataset ---- -test_that("assert_unit Test 60: error if unexpected unit in the input dataset", { +## Test 62: error if unexpected unit in the input dataset ---- +test_that("assert_unit Test 62: error if unexpected unit in the input dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1062,8 +1082,8 @@ test_that("assert_unit Test 60: error if unexpected unit in the input dataset", ) }) -## Test 61: error if get_unit_expr invalid ---- -test_that("assert_unit Test 61: error if get_unit_expr invalid", { +## Test 63: error if get_unit_expr invalid ---- +test_that("assert_unit Test 63: error if get_unit_expr invalid", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1081,8 +1101,8 @@ test_that("assert_unit Test 61: error if get_unit_expr invalid", { }) # assert_param_does_not_exist ---- -## Test 62: error if parameter exists in the input dataset ---- -test_that("assert_param_does_not_exist Test 62: error if parameter exists in the input dataset", { +## Test 64: error if parameter exists in the input dataset ---- +test_that("assert_param_does_not_exist Test 64: error if parameter exists in the input dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1100,8 +1120,8 @@ test_that("assert_param_does_not_exist Test 62: error if parameter exists in the ) }) -## Test 63: no error if the parameter exists in the dataset ---- -test_that("assert_param_does_not_exist Test 63: no error if the parameter exists in the dataset", { +## Test 65: no error if the parameter exists in the dataset ---- +test_that("assert_param_does_not_exist Test 65: no error if the parameter exists in the dataset", { advs <- dplyr::tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, @@ -1114,8 +1134,8 @@ test_that("assert_param_does_not_exist Test 63: no error if the parameter exists }) # assert_varval_list ---- -## Test 64: error if `arg` is not a list of var-value expressions ---- -test_that("assert_varval_list Test 64: error if `arg` is not a list of var-value expressions", { +## Test 66: error if `arg` is not a list of expressions ---- +test_that("assert_varval_list Test 66: error if `arg` is not a list of expressions", { example_fun <- function(arg) { assert_varval_list(arg, accept_var = FALSE) } @@ -1130,8 +1150,8 @@ test_that("assert_varval_list Test 64: error if `arg` is not a list of var-value ) }) -## Test 65: error if `arg` is not a list of var-value expressions ---- -test_that("assert_varval_list Test 65: error if `arg` is not a list of var-value expressions", { +## Test 67: error if not all elements are variables ---- +test_that("assert_varval_list Test 67: error if not all elements are variables", { example_fun <- function(arg) { assert_varval_list(arg, accept_var = TRUE) } @@ -1146,8 +1166,8 @@ test_that("assert_varval_list Test 65: error if `arg` is not a list of var-value ) }) -## Test 66: error if `required_elements` are missing from `arg` ---- -test_that("assert_varval_list Test 66: error if `required_elements` are missing from `arg`", { +## Test 68: error if `required_elements` are missing from `arg` ---- +test_that("assert_varval_list Test 68: error if `required_elements` are missing from `arg`", { example_fun <- function(arg) { assert_varval_list(arg, required_elements = "DTHDOM") } @@ -1162,8 +1182,8 @@ test_that("assert_varval_list Test 66: error if `required_elements` are missing ) }) -## Test 67: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_varval_list Test 67: no error if `arg` is NULL and optional is TRUE", { +## Test 69: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_varval_list Test 69: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_varval_list(arg, optional = TRUE) } @@ -1173,8 +1193,8 @@ test_that("assert_varval_list Test 67: no error if `arg` is NULL and optional is ) }) -## Test 68: error if `accept_expr` is TRUE and value is invalid ---- -test_that("assert_varval_list Test 68: error if `accept_expr` is TRUE and value is invalid", { +## Test 70: error if `accept_expr` is TRUE and value is invalid ---- +test_that("assert_varval_list Test 70: error if `accept_expr` is TRUE and value is invalid", { example_fun <- function(arg) { assert_varval_list(arg, accept_expr = TRUE) } @@ -1189,8 +1209,8 @@ test_that("assert_varval_list Test 68: error if `accept_expr` is TRUE and value ) }) -## Test 69: error if `accept_expr` is FALSE and value is invalid ---- -test_that("assert_varval_list Test 69: error if `accept_expr` is FALSE and value is invalid", { +## Test 71: error if `accept_expr` is FALSE and value is invalid ---- +test_that("assert_varval_list Test 71: error if `accept_expr` is FALSE and value is invalid", { example_fun <- function(arg) { assert_varval_list(arg, accept_expr = FALSE) } @@ -1205,8 +1225,8 @@ test_that("assert_varval_list Test 69: error if `accept_expr` is FALSE and value ) }) -## Test 70: no error if an argument is a variable-value list ---- -test_that("assert_varval_list Test 70: no error if an argument is a variable-value list", { +## Test 72: no error if an argument is a variable-value list ---- +test_that("assert_varval_list Test 72: no error if an argument is a variable-value list", { example_fun <- function(arg) { assert_varval_list(arg) } @@ -1217,24 +1237,24 @@ test_that("assert_varval_list Test 70: no error if an argument is a variable-val }) # assert_expr_list ---- -## Test 71: error if `arg` is not a list of expressions ---- -test_that("assert_expr_list Test 71: error if `arg` is not a list of expressions", { +## Test 73: error if `arg` is not a list of expressions ---- +test_that("assert_expr_list Test 73: error if `arg` is not a list of expressions", { expect_error( assert_expr_list(arg <- c("USUBJID", "PARAMCD", "VISIT")), class = "assert_expr_list" ) }) -## Test 72: error if `arg` is not a named list of expressions ---- -test_that("assert_expr_list Test 72: error if `arg` is not a named list of expressions", { +## Test 74: error if `arg` is not a named list of expressions ---- +test_that("assert_expr_list Test 74: error if `arg` is not a named list of expressions", { expect_error( assert_expr_list(arg <- exprs(USUBJID, PARAMCD, NULL), named = TRUE), regexp = "argument must be named" ) }) -## Test 73: error if `required_elements` are missing from `arg` ---- -test_that("assert_expr_list Test 73: error if `required_elements` are missing from `arg`", { +## Test 75: error if `required_elements` are missing from `arg` ---- +test_that("assert_expr_list Test 75: error if `required_elements` are missing from `arg`", { expect_error( assert_expr_list( arg <- exprs(DTHSEQ = AESEQ), @@ -1244,23 +1264,23 @@ test_that("assert_expr_list Test 73: error if `required_elements` are missing fr ) }) -## Test 74: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_expr_list Test 74: no error if `arg` is NULL and optional is TRUE", { +## Test 76: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_expr_list Test 76: no error if `arg` is NULL and optional is TRUE", { expect_invisible( assert_expr_list(NULL, optional = TRUE) ) }) -## Test 75: error if element is invalid ---- -test_that("assert_expr_list Test 75: error if element is invalid", { +## Test 77: error if element is invalid ---- +test_that("assert_expr_list Test 77: error if element is invalid", { expect_error( assert_expr_list(arg <- exprs(DTHSEQ = !!mean)), class = "assert_expr_list" ) }) -## Test 76: no error if argument is valid ---- -test_that("assert_expr_list Test 76: no error if argument is valid", { +## Test 78: no error if argument is valid ---- +test_that("assert_expr_list Test 78: no error if argument is valid", { expect_invisible( assert_expr_list(arg <- exprs( DTHDOM = "AE", @@ -1271,8 +1291,8 @@ test_that("assert_expr_list Test 76: no error if argument is valid", { }) # assert_list_element ---- -## Test 77: no error if the elements fulfill a certain condition ---- -test_that("assert_list_element Test 77: no error if the elements fulfill a certain condition", { +## Test 79: no error if the elements fulfill a certain condition ---- +test_that("assert_list_element Test 79: no error if the elements fulfill a certain condition", { expect_invisible( assert_list_element( list( @@ -1286,8 +1306,8 @@ test_that("assert_list_element Test 77: no error if the elements fulfill a certa ) }) -## Test 78: error if the elements do not fulfill the condition ---- -test_that("assert_list_element Test 78: error if the elements do not fulfill the condition", { +## Test 80: error if the elements do not fulfill the condition ---- +test_that("assert_list_element Test 80: error if the elements do not fulfill the condition", { expect_snapshot( assert_list_element( list( @@ -1323,8 +1343,8 @@ test_that("assert_list_element Test 78: error if the elements do not fulfill the }) # assert_one_to_one ---- -## Test 79: error if there is a one to many mapping ---- -test_that("assert_one_to_one Test 79: error if there is a one to many mapping", { +## Test 81: error if there is a one to many mapping ---- +test_that("assert_one_to_one Test 81: error if there is a one to many mapping", { dm <- dplyr::tribble( ~DOMAIN, ~USUBJID, "DM", "01-701-1015", @@ -1346,8 +1366,8 @@ test_that("assert_one_to_one Test 79: error if there is a one to many mapping", admiraldev_environment$one_to_many <- NULL }) -## Test 80: error if there is a many to one mapping ---- -test_that("assert_one_to_one Test 80: error if there is a many to one mapping", { +## Test 82: error if there is a many to one mapping ---- +test_that("assert_one_to_one Test 82: error if there is a many to one mapping", { dm <- dplyr::tribble( ~DOMAIN, ~USUBJID, "DM", "01-701-1015", @@ -1363,8 +1383,8 @@ test_that("assert_one_to_one Test 80: error if there is a many to one mapping", admiraldev_environment$many_to_one <- NULL }) -## Test 81: dataset is returned invisible if one-to-one ---- -test_that("assert_one_to_one Test 81: dataset is returned invisible if one-to-one", { +## Test 83: dataset is returned invisible if one-to-one ---- +test_that("assert_one_to_one Test 83: dataset is returned invisible if one-to-one", { df <- dplyr::tribble( ~SPECIES, ~SPECIESN, "DOG", 1L, @@ -1380,8 +1400,8 @@ test_that("assert_one_to_one Test 81: dataset is returned invisible if one-to-on }) # assert_date_var ---- -## Test 82: error if variable is not a date or datetime variable ---- -test_that("assert_date_var Test 82: error if variable is not a date or datetime variable", { +## Test 84: error if variable is not a date or datetime variable ---- +test_that("assert_date_var Test 84: error if variable is not a date or datetime variable", { example_fun <- function(dataset, var) { var <- assert_symbol(enexpr(var)) assert_date_var(dataset = dataset, var = !!var) @@ -1409,18 +1429,18 @@ test_that("assert_date_var Test 82: error if variable is not a date or datetime }) # assert_date_vector ---- -## Test 83: returns error if input vector is not a date formatted ---- -test_that("assert_date_vector Test 83: returns error if input vector is not a date formatted", { +## Test 85: returns error if input vector is not a date formatted ---- +test_that("assert_date_vector Test 85: returns error if input vector is not a date formatted", { expect_error(assert_date_vector("2018-08-23")) }) -## Test 84: returns invisible if input is date formatted ---- -test_that("assert_date_vector Test 84: returns invisible if input is date formatted", { +## Test 86: returns invisible if input is date formatted ---- +test_that("assert_date_vector Test 86: returns invisible if input is date formatted", { expect_invisible(assert_date_vector(as.Date("2022-10-25"))) }) -## Test 85: no error if `arg` is NULL and optional is TRUE ---- -test_that("assert_date_vector Test 85: no error if `arg` is NULL and optional is TRUE", { +## Test 87: no error if `arg` is NULL and optional is TRUE ---- +test_that("assert_date_vector Test 87: no error if `arg` is NULL and optional is TRUE", { example_fun <- function(arg) { assert_date_vector(arg, optional = TRUE) } @@ -1430,8 +1450,8 @@ test_that("assert_date_vector Test 85: no error if `arg` is NULL and optional is ) }) -## Test 86: error if `arg` is NULL and optional is FALSE ---- -test_that("assert_date_vector Test 86: error if `arg` is NULL and optional is FALSE", { +## Test 88: error if `arg` is NULL and optional is FALSE ---- +test_that("assert_date_vector Test 88: error if `arg` is NULL and optional is FALSE", { example_fun <- function(arg) { assert_date_vector(arg, optional = FALSE) } @@ -1448,8 +1468,8 @@ test_that("assert_date_vector Test 86: error if `arg` is NULL and optional is FA # assert_atomic_vector ---- -## Test 87: error if input is not atomic vector ---- -test_that("assert_atomic_vector Test 87: error if input is not atomic vector", { +## Test 89: error if input is not atomic vector ---- +test_that("assert_atomic_vector Test 89: error if input is not atomic vector", { x <- list("a", "a", "b", "c", "d", "d", 1, 1, 4) expect_error(assert_atomic_vector(x), class = "assert_atomic_vector") expect_snapshot( @@ -1459,15 +1479,15 @@ test_that("assert_atomic_vector Test 87: error if input is not atomic vector", { }) # assert_same_type ---- -## Test 88: no error if same type ---- -test_that("assert_same_type Test 88: no error if same type", { +## Test 90: no error if same type ---- +test_that("assert_same_type Test 90: no error if same type", { true_value <- "Y" false_value <- "N" expect_invisible(assert_same_type(true_value, false_value)) }) -## Test 89: error if different type ---- -test_that("assert_same_type Test 89: error if different type", { +## Test 91: error if different type ---- +test_that("assert_same_type Test 91: error if different type", { true_value <- "Y" false_value <- "N" missing_value <- 0 @@ -1483,8 +1503,8 @@ test_that("assert_same_type Test 89: error if different type", { ) }) -## Test 90: works as intended ---- -test_that("assert_same_type Test 90: works as intended", { +## Test 92: works as intended ---- +test_that("assert_same_type Test 92: works as intended", { expect_equal( valid_time_units(), c("years", "months", "days", "hours", "minutes", "seconds")