From dd65ab8144fbdbe6e2fb58b9b2c1a68a9adcc387 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Fri, 4 Feb 2022 16:28:09 +0100 Subject: [PATCH] closes #29 error if objects are entered as arguments --- DESCRIPTION | 1 + NEWS.md | 6 ++++++ R/error-checks.R | 26 +++++++++--------------- tests/testthat/test-validation-helpers.R | 14 +++++++++++++ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d84b7d2..d2b6ee1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,6 +23,7 @@ BugReports: Depends: R (>= 3.6) Imports: + purrr, R6 Suggests: knitr, diff --git a/NEWS.md b/NEWS.md index 7673aa5..b6dd3b6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # ospsuite.utils 1.0.0.9000 +MAJOR CHANGES + +* `isIncluded()` now only accepts base types as valid inputs. + MINOR CHANGES * Improvements to documentation. @@ -8,6 +12,8 @@ MINOR CHANGES * `getEnumKey()` is added as an alias for `enumGetKey()` function. +* Package gains `{purrr}` as a new dependency. + # ospsuite.utils 1.0.0 * Initial release. diff --git a/R/error-checks.R b/R/error-checks.R index d0b2386..b033b66 100644 --- a/R/error-checks.R +++ b/R/error-checks.R @@ -71,14 +71,20 @@ isOfType <- function(object, type, nullAllowed = FALSE) { #' isIncluded(list("x", 1), list("a", "b", "x")) # FALSE #' @export isIncluded <- function(values, parentValues) { + if (is.vector(values)) { + objCount <- length(purrr::keep(values, is.object)) + } else { + objCount <- length(purrr::keep(c(values), is.object)) + } + + if (objCount > 0L) { + stop("Only vectors of base object types are allowed.", call. = FALSE) + } + if (is.null(values) || length(values) == 0) { return(FALSE) } - # make sure they are vectors - values <- .toVector(values) - parentValues <- .toVector(parentValues) - as.logical(min(values %in% parentValues)) } @@ -159,18 +165,6 @@ hasUniqueValues <- function(values, na.rm = TRUE) { return(!any(duplicated(values))) } - -# utilities --------------------------------------------- - -#' @keywords internal -.toVector <- function(x) { - if (!is.vector(x)) { - x <- c(x) - } - - return(x) -} - #' @keywords internal .typeNamesFrom <- function(type) { type <- c(type) diff --git a/tests/testthat/test-validation-helpers.R b/tests/testthat/test-validation-helpers.R index 1e873f3..7685455 100644 --- a/tests/testthat/test-validation-helpers.R +++ b/tests/testthat/test-validation-helpers.R @@ -95,3 +95,17 @@ test_that("enum validation works as expected", { expect_null(validateEnumValue(1, Symbol)) expect_error(validateEnumValue(4, Symbol)) }) + + +test_that("isInclude doesn't accept objects as arguments", { + Person <- R6::R6Class("Person", list( + name = NULL, + initialize = function(name) self$name <- name + )) + + Jack <- Person$new(name = "Jack") + Jill <- Person$new(name = "Jill") + + expect_error(isIncluded(Jack, Jill)) + expect_error(isIncluded(c(Jack), list(Jack, Jill))) +})