Skip to content

Commit

Permalink
feat: added missing assert_less_than assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
selkamand committed Nov 9, 2024
1 parent 39bc364 commit 0439316
Show file tree
Hide file tree
Showing 11 changed files with 701 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
115 changes: 115 additions & 0 deletions R/assert_compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
34 changes: 34 additions & 0 deletions R/is_comparisons.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down
41 changes: 41 additions & 0 deletions man/assert_all_less_than.Rd

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

38 changes: 38 additions & 0 deletions man/assert_all_less_than_or_equal_to.Rd

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

43 changes: 43 additions & 0 deletions man/assert_less_than.Rd

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

39 changes: 39 additions & 0 deletions man/assert_less_than_or_equal_to.Rd

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

26 changes: 26 additions & 0 deletions man/is_less_than.Rd

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

26 changes: 26 additions & 0 deletions man/is_less_than_or_equal_to.Rd

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

Loading

0 comments on commit 0439316

Please sign in to comment.