Skip to content

Commit

Permalink
Merge pull request #124 from selkamand/114-assert_one_of
Browse files Browse the repository at this point in the history
feat: added assert_one_of
  • Loading branch information
selkamand authored Nov 12, 2024
2 parents 87ad3f7 + fdedfc0 commit ffc924e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export(assert_null)
export(assert_number)
export(assert_numeric)
export(assert_numeric_vector)
export(assert_one_of)
export(assert_reactive)
export(assert_scalar)
export(assert_string)
Expand Down
30 changes: 29 additions & 1 deletion R/assert_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' This function checks that `x` is a subset of `y`
#'
#' @param x A vector to check
#' @param y the acceptible values that x can take
#' @param y the acceptable values that x can take
#' @inheritParams common_roxygen_params
#'
#' @return Returns invisible(TRUE) if `x` is a subset of `y`, otherwise throws an error
Expand All @@ -19,3 +19,31 @@ assert_subset <- assert_create(
x = "'{.strong {arg_name}}' {cli::qty(length(setopts_exlusive_to_first(arg_value, y)))} contain{?s} {?an/} invalid value{?s}: {.strong {setopts_exlusive_to_first(arg_value, y)}}. Valid values include: {unique(y)}"
)
)


#' Check if a scalar value is one of the acceptable values
#'
#' Assert `x` is one of the values of `y`.
#'
#' @param x A scalar value to check
#' @param y A vector of acceptable values that `x` can take
#' @inheritParams common_roxygen_params
#'
#' @return Returns invisible(TRUE) if `x` is a scalar and is one of the values in `y`, otherwise throws an error
#' @examples
#' assert_one_of(3, 1:5) # Passes because 3 is in 1:5
#' assert_one_of("A", c("A", "B", "C")) # Passes because "A" is in the vector
#'
#' try({
#' assert_one_of("D", c("A", "B", "C")) # Throws error because "D" is not in the vector
#' })
#' @export
assert_one_of <- assert_create_chain(
assert_scalar,
assert_create(
func = is_subset,
default_error_msg = c(
x = "'{.strong {arg_name}}' must be one of {.or {cli::cli_vec(y)}}, not {arg_value}."
)
)
)
33 changes: 33 additions & 0 deletions man/assert_one_of.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/assert_subset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions tests/testthat/_snaps/assert_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,43 @@
Error:
x 'c("A")' contain an invalid value: A. Valid values include: 1, 2, and 3

# assert_one_of() works [plain]

Code
assert_one_of(4, c(1, 2, 3))
Condition
Error:
x '4' must be one of 1, 2, or 3, not 4.

---

Code
assert_one_of("D", c("A", "B", "C"))
Condition
Error:
x '"D"' must be one of A, B, or C, not D.

---

Code
assert_one_of(3.5, c(1, 2.5, 3))
Condition
Error:
x '3.5' must be one of 1, 2.5, or 3, not 3.5.

---

Code
assert_one_of(c(1, 2), c(1, 2, 3))
Condition
Error:
! 'c(1, 2)' must be a scalar, not a numeric

---

Code
assert_one_of(list(1, 2), c(1, 2, 3))
Condition
Error:
! 'list(1, 2)' must be a scalar, not a list

26 changes: 26 additions & 0 deletions tests/testthat/test-assert_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,29 @@ cli::test_that_cli("assert_subset() works", config = "plain", {
# Custom error messages work
expect_error(assert_subset(c(2, 3), 1, msg = "Custom error message"), "Custom error message")
})


cli::test_that_cli("assert_one_of() works", config = "plain", {
# Works for scalar values that are part of the required set
expect_true(assert_one_of(1, c(1, 2, 3))) # 1 is in the set
expect_true(assert_one_of("A", c("A", "B", "C"))) # "A" is in the set
expect_true(assert_one_of(2.5, c(1.0, 2.5, 3.0))) # 2.5 is in the set

# Aborts for scalar values that are not part of the required set
expect_snapshot(assert_one_of(4, c(1, 2, 3)), error = TRUE) # 4 is not in the set
expect_snapshot(assert_one_of("D", c("A", "B", "C")), error = TRUE) # "D" is not in the set
expect_snapshot(assert_one_of(3.5, c(1.0, 2.5, 3.0)), error = TRUE) # 3.5 is not in the set

# Aborts for non-scalar values (vectors, lists, etc.)
expect_snapshot(assert_one_of(c(1, 2), c(1, 2, 3)), error = TRUE) # c(1, 2) is not a scalar
expect_snapshot(assert_one_of(list(1, 2), c(1, 2, 3)), error = TRUE) # list(1, 2) is not a scalar

# Error messages use variable name of passed arguments
x <- 4
expect_error(assert_one_of(x, c(1, 2, 3)), "'x' must be one of 1, 2, or 3, not 4", fixed = TRUE)
x <- "D"
expect_error(assert_one_of(x, c("A", "B", "C")), "'x' must be one of A, B, or C, not D", fixed = TRUE)

# Custom error messages work
expect_error(assert_one_of(2, c(1, 3), msg = "Custom error message"), "Custom error message")
})

0 comments on commit ffc924e

Please sign in to comment.