Skip to content

Commit

Permalink
closes #29
Browse files Browse the repository at this point in the history
error if objects are entered as arguments
  • Loading branch information
IndrajeetPatil committed Feb 4, 2022
1 parent faa6130 commit dd65ab8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ BugReports:
Depends:
R (>= 3.6)
Imports:
purrr,
R6
Suggests:
knitr,
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
26 changes: 10 additions & 16 deletions R/error-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-validation-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})

0 comments on commit dd65ab8

Please sign in to comment.