diff --git a/NAMESPACE b/NAMESPACE index 038cf0d..c907b0e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,8 @@ export(assert_all_files_exist) export(assert_all_files_have_extension) export(assert_all_greater_than) export(assert_all_greater_than_or_equal_to) +export(assert_all_less_than) +export(assert_all_less_than_or_equal_to) export(assert_character) export(assert_character_vector) export(assert_character_vector_or_glue) @@ -34,6 +36,8 @@ export(assert_length_greater_than) export(assert_length_greater_than_or_equal_to) export(assert_length_less_than) export(assert_length_less_than_or_equal_to) +export(assert_less_than) +export(assert_less_than_or_equal_to) export(assert_list) export(assert_logical) export(assert_logical_vector) diff --git a/R/assert_compare.R b/R/assert_compare.R index f22b03d..536a134 100644 --- a/R/assert_compare.R +++ b/R/assert_compare.R @@ -173,3 +173,118 @@ assert_equal <- assert_create(is_equal, default_error_msg = "{.strong {arg_name} # #' }) # #' @export #assert_type_identical <- assert_create(is_same_type, "{.strong {arg_name}} ({.strong {typeof(arg_value)}}) must be the same {aname(x)} type as {aname(y)} ({typeof(y)})") + +#' Assert input is less than a specified maximum value +#' +#' Assert all elements in a numeric vector/matrix are below some maximum value. +#' +#' @include assert_create.R +#' @include assert_type.R +#' @include is_functions.R +#' @include is_comparisons.R +#' @param x An object to check +#' @param maximum The maximum value to compare against (number) +#' @param msg A character string containing the error message to display if `x` is not less than the specified maximum value (string) +#' @inheritParams common_roxygen_params +#' +#' @return invisible(TRUE) if `x` is less than the specified maximum value, otherwise aborts with the error message specified by `msg` +#' +#' @examples +#' try({ +#' assert_all_less_than(1, 2) # Passes +#' assert_all_less_than(c(1,2,3), 4) # Passes +#' assert_all_less_than(c(1,2,3), 2) # Throws default error +#' assert_all_less_than(c(1,2,3), 2, msg = "custom error message") # Throws custom error +#' }) +#' +#' @concept assert_comparison +#' @export +assert_all_less_than <- assert_create_chain( + assert_numeric, + assert_create( + is_less_than, + default_error_msg = "{.strong {arg_name}} must {ifelse(length(arg_value) > 1, 'all ', '')}be {.strong less than} `{.strong {maximum}}`." + ) +) + +#' Assert input is less than some maximum value +#' +#' Assert a number is less than a specified maximum value. +#' To check all numbers in a vector / matrix are below a maximum value, see [assert_all_less_than()] +#' +#' @include assert_create.R +#' @include assert_type.R +#' @include is_functions.R +#' @include is_comparisons.R +#' @param x An object to check +#' @param maximum The maximum value to compare against (number) +#' @param msg A character string containing the error message to display if `x` is not less than the specified maximum value (string) +#' @inheritParams common_roxygen_params +#' +#' @return invisible(TRUE) if `x` is less than the specified maximum value, otherwise aborts with the error message specified by `msg` +#' +#' @examples +#' try({ +#' assert_less_than(1, 2) # Passes +#' assert_less_than(1, 2) # Passes +#' assert_less_than(c(1,2,3), 4) # Throws error (Must be a number) +#' assert_less_than('A', 1) # Throws error (Must be a number) +#' assert_less_than(3, 2, msg = "custom error message") # Throws custom error +#' }) +#' +#' @concept assert_comparison +#' @export +assert_less_than <- assert_create_chain( + assert_number, + assert_all_less_than +) + +#' Assert input is less than or equal to a specified maximum value +#' +#' Assert all elements in a numeric vector/matrix are below or equal to some maximum value. +#' +#' @param x An object to check +#' @param maximum The maximum value to compare against +#' @param msg A character string containing the error message to display if `x` is not less than or equal to the specified maximum value (string) +#' @inheritParams common_roxygen_params +#' +#' @return invisible(TRUE) if `x` is less than or equal to the specified maximum value, otherwise aborts with the error message specified by `msg` +#' +#' @examples +#' try({ +#' assert_less_than_or_equal_to(1, 2) # Passes +#' assert_less_than_or_equal_to(c(1, 2, 3), 3) # Passes +#' assert_less_than_or_equal_to(3, 2) # Throws error +#' }) +#' @export +assert_all_less_than_or_equal_to <- assert_create_chain( + assert_numeric, + assert_create( + is_less_than_or_equal_to, + default_error_msg = "{.strong {arg_name}} must {ifelse(length(arg_value) > 1, 'all ', '')}be {.strong less than or equal to} `{.strong {maximum}}`." + ) +) + +#' Assert input is less than or equal to a specified maximum value +#' +#' Assert a number is less than or equal to a specified maximum value. +#' For vectorized version see [assert_all_less_than_or_equal_to()] +#' +#' @param x An object to check +#' @param maximum The maximum value to compare against +#' @param msg A character string containing the error message to display if `x` is not less than or equal to the specified maximum value (string) +#' @inheritParams common_roxygen_params +#' +#' @return invisible(TRUE) if `x` is less than or equal to the specified maximum value, otherwise aborts with the error message specified by `msg` +#' +#' @examples +#' try({ +#' assert_less_than_or_equal_to(1, 2) # Passes +#' assert_less_than_or_equal_to(c(1, 2, 3), 3) # Throws error +#' assert_less_than_or_equal_to(3, 2) # Throws error +#' }) +#' @export +assert_less_than_or_equal_to <- assert_create_chain( + assert_number, + assert_all_less_than_or_equal_to +) diff --git a/R/is_comparisons.R b/R/is_comparisons.R index 5b474e6..bf218ab 100644 --- a/R/is_comparisons.R +++ b/R/is_comparisons.R @@ -80,6 +80,40 @@ is_greater_than_or_equal_to <- function(x, minimum){ compare(x = x, minimum = minimum, all_must_satisfy = TRUE, comparison_inclusive = TRUE) } +#' Check if a numeric vector is less than a specified maximum value +#' +#' This function checks if a numeric vector is less than a specified maximum value. It can also optionally check if all elements of the vector must be less than the maximum value or if only one element is sufficient +#' +#' @param x a numeric vector to check +#' @param maximum The maximum value to compare against +#' +#' @return A logical value indicating whether all elements of the numeric vector x are less than the specified maximum value +#' @examples +#' if(interactive()){ +#' is_less_than(c(1,2,3), 4) # TRUE +#' is_less_than(c(1,2,3), 2) # FALSE +#' is_less_than(c(1,2,4), 3) # FALSE +#' } +is_less_than <- function(x, maximum){ + compare(x = x, maximum = maximum, all_must_satisfy = TRUE, comparison_inclusive = FALSE) +} + +#' Check if a numeric vector is less than or equal to a specified maximum value +#' +#' This function checks if a numeric vector is less than or equal to a specified maximum value. It can also optionally check if all elements of the vector must be less than or equal to the maximum value or if only one element is sufficient +#' +#' @param x a numeric vector to check +#' @param maximum The maximum value to compare against +#' @return A logical value indicating whether all elements of the numeric vector x are less than or equal to the specified maximum value +#' @examples +#' if(interactive()){ +#' is_less_than_or_equal_to(c(1,2,3), 4) # TRUE +#' is_less_than_or_equal_to(c(1,2,3), 3) # TRUE +#' is_less_than_or_equal_to(c(1,2,4), 3) # FALSE +#' } +is_less_than_or_equal_to <- function(x, maximum){ + compare(x = x, maximum = maximum, all_must_satisfy = TRUE, comparison_inclusive = TRUE) +} #' Check if two objects are identical #' diff --git a/man/assert_all_less_than.Rd b/man/assert_all_less_than.Rd new file mode 100644 index 0000000..0334faf --- /dev/null +++ b/man/assert_all_less_than.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/assert_compare.R +\name{assert_all_less_than} +\alias{assert_all_less_than} +\title{Assert input is less than a specified maximum value} +\usage{ +assert_all_less_than( + x, + maximum, + msg = NULL, + call = rlang::caller_env(), + arg_name = NULL +) +} +\arguments{ +\item{x}{An object to check} + +\item{maximum}{The maximum value to compare against (number)} + +\item{msg}{A character string containing the error message to display if \code{x} is not less than the specified maximum value (string)} + +\item{call}{Only relevant when pooling assertions into multi-assertion helper functions. See \link[cli]{cli_abort} for details.} + +\item{arg_name}{Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name).} +} +\value{ +invisible(TRUE) if \code{x} is less than the specified maximum value, otherwise aborts with the error message specified by \code{msg} +} +\description{ +Assert all elements in a numeric vector/matrix are below some maximum value. +} +\examples{ +try({ +assert_all_less_than(1, 2) # Passes +assert_all_less_than(c(1,2,3), 4) # Passes +assert_all_less_than(c(1,2,3), 2) # Throws default error +assert_all_less_than(c(1,2,3), 2, msg = "custom error message") # Throws custom error +}) + +} +\concept{assert_comparison} diff --git a/man/assert_all_less_than_or_equal_to.Rd b/man/assert_all_less_than_or_equal_to.Rd new file mode 100644 index 0000000..b67c4fa --- /dev/null +++ b/man/assert_all_less_than_or_equal_to.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/assert_compare.R +\name{assert_all_less_than_or_equal_to} +\alias{assert_all_less_than_or_equal_to} +\title{Assert input is less than or equal to a specified maximum value} +\usage{ +assert_all_less_than_or_equal_to( + x, + maximum, + msg = NULL, + call = rlang::caller_env(), + arg_name = NULL +) +} +\arguments{ +\item{x}{An object to check} + +\item{maximum}{The maximum value to compare against} + +\item{msg}{A character string containing the error message to display if \code{x} is not less than or equal to the specified maximum value (string)} + +\item{call}{Only relevant when pooling assertions into multi-assertion helper functions. See \link[cli]{cli_abort} for details.} + +\item{arg_name}{Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name).} +} +\value{ +invisible(TRUE) if \code{x} is less than or equal to the specified maximum value, otherwise aborts with the error message specified by \code{msg} +} +\description{ +Assert all elements in a numeric vector/matrix are below or equal to some maximum value. +} +\examples{ +try({ +assert_less_than_or_equal_to(1, 2) # Passes +assert_less_than_or_equal_to(c(1, 2, 3), 3) # Passes +assert_less_than_or_equal_to(3, 2) # Throws error +}) +} diff --git a/man/assert_less_than.Rd b/man/assert_less_than.Rd new file mode 100644 index 0000000..178b2c2 --- /dev/null +++ b/man/assert_less_than.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/assert_compare.R +\name{assert_less_than} +\alias{assert_less_than} +\title{Assert input is less than some maximum value} +\usage{ +assert_less_than( + x, + maximum, + msg = NULL, + call = rlang::caller_env(), + arg_name = NULL +) +} +\arguments{ +\item{x}{An object to check} + +\item{maximum}{The maximum value to compare against (number)} + +\item{msg}{A character string containing the error message to display if \code{x} is not less than the specified maximum value (string)} + +\item{call}{Only relevant when pooling assertions into multi-assertion helper functions. See \link[cli]{cli_abort} for details.} + +\item{arg_name}{Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name).} +} +\value{ +invisible(TRUE) if \code{x} is less than the specified maximum value, otherwise aborts with the error message specified by \code{msg} +} +\description{ +Assert a number is less than a specified maximum value. +To check all numbers in a vector / matrix are below a maximum value, see \code{\link[=assert_all_less_than]{assert_all_less_than()}} +} +\examples{ +try({ +assert_less_than(1, 2) # Passes +assert_less_than(1, 2) # Passes +assert_less_than(c(1,2,3), 4) # Throws error (Must be a number) +assert_less_than('A', 1) # Throws error (Must be a number) +assert_less_than(3, 2, msg = "custom error message") # Throws custom error +}) + +} +\concept{assert_comparison} diff --git a/man/assert_less_than_or_equal_to.Rd b/man/assert_less_than_or_equal_to.Rd new file mode 100644 index 0000000..ddf8575 --- /dev/null +++ b/man/assert_less_than_or_equal_to.Rd @@ -0,0 +1,39 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/assert_compare.R +\name{assert_less_than_or_equal_to} +\alias{assert_less_than_or_equal_to} +\title{Assert input is less than or equal to a specified maximum value} +\usage{ +assert_less_than_or_equal_to( + x, + maximum, + msg = NULL, + call = rlang::caller_env(), + arg_name = NULL +) +} +\arguments{ +\item{x}{An object to check} + +\item{maximum}{The maximum value to compare against} + +\item{msg}{A character string containing the error message to display if \code{x} is not less than or equal to the specified maximum value (string)} + +\item{call}{Only relevant when pooling assertions into multi-assertion helper functions. See \link[cli]{cli_abort} for details.} + +\item{arg_name}{Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name).} +} +\value{ +invisible(TRUE) if \code{x} is less than or equal to the specified maximum value, otherwise aborts with the error message specified by \code{msg} +} +\description{ +Assert a number is less than or equal to a specified maximum value. +For vectorized version see \code{\link[=assert_all_less_than_or_equal_to]{assert_all_less_than_or_equal_to()}} +} +\examples{ +try({ +assert_less_than_or_equal_to(1, 2) # Passes +assert_less_than_or_equal_to(c(1, 2, 3), 3) # Throws error +assert_less_than_or_equal_to(3, 2) # Throws error +}) +} diff --git a/man/is_less_than.Rd b/man/is_less_than.Rd new file mode 100644 index 0000000..4c2267a --- /dev/null +++ b/man/is_less_than.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/is_comparisons.R +\name{is_less_than} +\alias{is_less_than} +\title{Check if a numeric vector is less than a specified maximum value} +\usage{ +is_less_than(x, maximum) +} +\arguments{ +\item{x}{a numeric vector to check} + +\item{maximum}{The maximum value to compare against} +} +\value{ +A logical value indicating whether all elements of the numeric vector x are less than the specified maximum value +} +\description{ +This function checks if a numeric vector is less than a specified maximum value. It can also optionally check if all elements of the vector must be less than the maximum value or if only one element is sufficient +} +\examples{ +if(interactive()){ +is_less_than(c(1,2,3), 4) # TRUE +is_less_than(c(1,2,3), 2) # FALSE +is_less_than(c(1,2,4), 3) # FALSE +} +} diff --git a/man/is_less_than_or_equal_to.Rd b/man/is_less_than_or_equal_to.Rd new file mode 100644 index 0000000..4cc8201 --- /dev/null +++ b/man/is_less_than_or_equal_to.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/is_comparisons.R +\name{is_less_than_or_equal_to} +\alias{is_less_than_or_equal_to} +\title{Check if a numeric vector is less than or equal to a specified maximum value} +\usage{ +is_less_than_or_equal_to(x, maximum) +} +\arguments{ +\item{x}{a numeric vector to check} + +\item{maximum}{The maximum value to compare against} +} +\value{ +A logical value indicating whether all elements of the numeric vector x are less than or equal to the specified maximum value +} +\description{ +This function checks if a numeric vector is less than or equal to a specified maximum value. It can also optionally check if all elements of the vector must be less than or equal to the maximum value or if only one element is sufficient +} +\examples{ +if(interactive()){ +is_less_than_or_equal_to(c(1,2,3), 4) # TRUE +is_less_than_or_equal_to(c(1,2,3), 3) # TRUE +is_less_than_or_equal_to(c(1,2,4), 3) # FALSE +} +} diff --git a/tests/testthat/_snaps/assert_less_than.md b/tests/testthat/_snaps/assert_less_than.md new file mode 100644 index 0000000..2059c3e --- /dev/null +++ b/tests/testthat/_snaps/assert_less_than.md @@ -0,0 +1,224 @@ +# assert_less_than() works [plain] + + Code + assert_less_than(3, 2) + Condition + Error: + ! 3 must be less than `2`. + +--- + + Code + assert_less_than(4, 2) + Condition + Error: + ! 4 must be less than `2`. + +--- + + Code + assert_less_than("abc", 2) + Condition + Error: + ! '"abc"' is not a number! (class is character, not numeric) + +--- + + Code + assert_less_than(list(1, 2, 3), 2) + Condition + Error: + ! 'list(1, 2, 3)' is not a number! (class is list, not numeric) + +--- + + Code + assert_less_than(c(3, 4, 5), 2) + Condition + Error: + ! 'c(3, 4, 5)' is not a number! (length is 3, not 1) + +--- + + Code + assert_less_than(factor(4), 2) + Condition + Error: + ! 'factor(4)' is not a number! (class is factor, not numeric) + +--- + + Code + assert_less_than(TRUE, 2) + Condition + Error: + ! 'TRUE' is not a number! (class is logical, not numeric) + +--- + + Code + assert_less_than(NULL, 2) + Condition + Error: + ! 'NULL' is not a number! (class is NULL, not numeric) + +# assert_all_less_than() works [plain] + + Code + assert_all_less_than(c(2, 3, 4), 3) + Condition + Error: + ! c(2, 3, 4) must all be less than `3`. + +--- + + Code + assert_all_less_than(2, 2) + Condition + Error: + ! 2 must be less than `2`. + +--- + + Code + assert_all_less_than("abc", 2) + Condition + Error: + ! '"abc"' must be numeric, not a character + +--- + + Code + assert_all_less_than(list(1, 2, 3), 2) + Condition + Error: + ! 'list(1, 2, 3)' must be numeric, not a list + +--- + + Code + assert_all_less_than(factor(c(1, 2, 3)), 2) + Condition + Error: + ! 'factor(c(1, 2, 3))' must be numeric, not a factor + +--- + + Code + assert_all_less_than(TRUE, 2) + Condition + Error: + ! 'TRUE' must be numeric, not a logical + +--- + + Code + assert_all_less_than(NULL, 2) + Condition + Error: + ! 'NULL' must be numeric, not a NULL + +# assert_all_less_than_or_equal_to() works [plain] + + Code + assert_all_less_than_or_equal_to(c(2, 3, 4), 3) + Condition + Error: + ! c(2, 3, 4) must all be less than or equal to `3`. + +--- + + Code + assert_all_less_than_or_equal_to("abc", 2) + Condition + Error: + ! '"abc"' must be numeric, not a character + +--- + + Code + assert_all_less_than_or_equal_to(list(1, 2, 3), 2) + Condition + Error: + ! 'list(1, 2, 3)' must be numeric, not a list + +--- + + Code + assert_all_less_than_or_equal_to(factor(c(1, 2, 3)), 2) + Condition + Error: + ! 'factor(c(1, 2, 3))' must be numeric, not a factor + +--- + + Code + assert_all_less_than_or_equal_to(TRUE, 2) + Condition + Error: + ! 'TRUE' must be numeric, not a logical + +--- + + Code + assert_all_less_than_or_equal_to(NULL, 2) + Condition + Error: + ! 'NULL' must be numeric, not a NULL + +# assert_less_than_or_equal_to() works [plain] + + Code + assert_less_than_or_equal_to(3, 2) + Condition + Error: + ! 3 must be less than or equal to `2`. + +--- + + Code + assert_less_than_or_equal_to("abc", 2) + Condition + Error: + ! '"abc"' is not a number! (class is character, not numeric) + +--- + + Code + assert_less_than_or_equal_to(list(1, 2, 3), 2) + Condition + Error: + ! 'list(1, 2, 3)' is not a number! (class is list, not numeric) + +--- + + Code + assert_less_than_or_equal_to(c(3, 2, 3), 2) + Condition + Error: + ! 'c(3, 2, 3)' is not a number! (length is 3, not 1) + +--- + + Code + assert_less_than_or_equal_to(factor(1), 2) + Condition + Error: + ! 'factor(1)' is not a number! (class is factor, not numeric) + +--- + + Code + assert_less_than_or_equal_to(TRUE, 2) + Condition + Error: + ! 'TRUE' is not a number! (class is logical, not numeric) + +--- + + Code + assert_less_than_or_equal_to(NULL, 2) + Condition + Error: + ! 'NULL' is not a number! (class is NULL, not numeric) + diff --git a/tests/testthat/test-assert_less_than.R b/tests/testthat/test-assert_less_than.R new file mode 100644 index 0000000..9e46db7 --- /dev/null +++ b/tests/testthat/test-assert_less_than.R @@ -0,0 +1,111 @@ +cli::test_that_cli("assert_less_than() works", config = "plain", { + + # Passes + expect_true(assert_less_than(2, 3)) + expect_true(assert_less_than(2, 4)) + + # Throws default errors when false + expect_snapshot(assert_less_than(3, 2), error = TRUE) + expect_snapshot(assert_less_than(4, 2), error = TRUE) + + # Throws custom error + expect_error(assert_less_than(3, 2, msg = "custom error message"), "custom error message") + + # Error message uses variable name of passed arguments + x <- 3 + expect_error(assert_less_than(x, 2), "x", fixed = TRUE) + + # Passes with number inputs + expect_true(assert_less_than(1, 2)) + expect_true(assert_less_than(1.5, 2)) + + # Aborts with non-number input + expect_snapshot(assert_less_than('abc', 2), error = TRUE) + expect_snapshot(assert_less_than(list(1, 2, 3), 2), error = TRUE) + expect_snapshot(assert_less_than(c(3, 4, 5), 2), error = TRUE) + expect_snapshot(assert_less_than(factor(4), 2), error = TRUE) + expect_snapshot(assert_less_than(TRUE, 2), error = TRUE) + expect_snapshot(assert_less_than(NULL, 2), error = TRUE) +}) + +cli::test_that_cli("assert_all_less_than() works", config = "plain", { + + # Passes with number inputs + expect_true(assert_all_less_than(2, 3)) + expect_true(assert_all_less_than(2, 4)) + expect_true(assert_all_less_than(1, 2)) + expect_true(assert_all_less_than(1.5, 2)) + + # Passes with numeric inputs + expect_true(assert_all_less_than(c(1, 2, 3), 4)) + + # Throws default errors when false + expect_snapshot(assert_all_less_than(c(2,3,4), 3), error = TRUE) + expect_snapshot(assert_all_less_than(2, 2), error = TRUE) + + # Throws custom error + expect_error(assert_all_less_than(3, 2, msg = "custom error message"), "custom error message") + + # Error message uses variable name of passed arguments + x <- 3 + expect_error(assert_all_less_than(x, 2), "x", fixed = TRUE) + + # Aborts with non-numeric input + expect_snapshot(assert_all_less_than('abc', 2), error = TRUE) + expect_snapshot(assert_all_less_than(list(1, 2, 3), 2), error = TRUE) + expect_snapshot(assert_all_less_than(factor(c(1, 2, 3)), 2), error = TRUE) + expect_snapshot(assert_all_less_than(TRUE, 2), error = TRUE) + expect_snapshot(assert_all_less_than(NULL, 2), error = TRUE) +}) + +cli::test_that_cli("assert_all_less_than_or_equal_to() works", config = "plain", { + + # Passes + expect_true(assert_all_less_than_or_equal_to(2, 3)) + expect_true(assert_all_less_than_or_equal_to(c(2, 3, 3), 3)) + expect_true(assert_all_less_than_or_equal_to(c(1,2,3), 4)) + + # Throws default errors when false + expect_snapshot(assert_all_less_than_or_equal_to(c(2,3,4), 3), error = TRUE) + + # Throws custom error + expect_error(assert_all_less_than_or_equal_to(3, 2, msg = "custom error message"), "custom error message") + + # Error message uses variable name of passed arguments + x <- 3 + expect_error(assert_all_less_than_or_equal_to(x, 2), "x", fixed = TRUE) + + # Aborts with non-numeric input + expect_snapshot(assert_all_less_than_or_equal_to('abc', 2), error = TRUE) + expect_snapshot(assert_all_less_than_or_equal_to(list(1, 2, 3), 2), error = TRUE) + expect_snapshot(assert_all_less_than_or_equal_to(factor(c(1, 2, 3)), 2), error = TRUE) + expect_snapshot(assert_all_less_than_or_equal_to(TRUE, 2), error = TRUE) + expect_snapshot(assert_all_less_than_or_equal_to(NULL, 2), error = TRUE) +}) + +cli::test_that_cli("assert_less_than_or_equal_to() works", config = "plain", { + + # Passes + expect_true(assert_less_than_or_equal_to(2, 3)) + expect_true(assert_less_than_or_equal_to(2, 2)) + expect_true(assert_less_than_or_equal_to(1, 2)) + expect_true(assert_less_than_or_equal_to(1.5, 2)) + + # Throws default errors when false + expect_snapshot(assert_less_than_or_equal_to(3, 2), error = TRUE) + + # Throws custom error + expect_error(assert_less_than_or_equal_to(3, 2, msg = "custom error message"), "custom error message") + + # Error message uses variable name of passed arguments + x <- 3 + expect_error(assert_less_than_or_equal_to(x, 2), "x", fixed = TRUE) + + # Aborts with non-number input + expect_snapshot(assert_less_than_or_equal_to('abc', 2), error = TRUE) + expect_snapshot(assert_less_than_or_equal_to(list(1, 2, 3), 2), error = TRUE) + expect_snapshot(assert_less_than_or_equal_to(c(3, 2, 3), 2), error = TRUE) + expect_snapshot(assert_less_than_or_equal_to(factor(1), 2), error = TRUE) + expect_snapshot(assert_less_than_or_equal_to(TRUE, 2), error = TRUE) + expect_snapshot(assert_less_than_or_equal_to(NULL, 2), error = TRUE) +})