Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Collate:
'is_functions.R'
'utils.R'
'assert_create.R'
'has.R'
'assert_type.R'
'assert_compare.R'
'assert_dataframe.R'
Expand All @@ -45,7 +46,6 @@ Collate:
'assert_null.R'
'assert_numerical.R'
'assert_packages.R'
'has.R'
'assert_regex.R'
'assert_set.R'
'coverage_testing.R'
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

* Improved factor support for `assert_includes()` and `assert_excludes()`

* Improved missing-value errors for numeric comparison assertions (e.g., `assert_greater_than()` with `NaN`)

* Added `assert_connection()`

* Added `assert_set_equal()`
Expand Down
7 changes: 7 additions & 0 deletions R/assert_compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#'
#' @include assert_create.R
#' @include assert_type.R
#' @include has.R
#' @include is_functions.R
#' @include is_comparisons.R
#' @param x An object to check
Expand All @@ -26,6 +27,7 @@
#' @export
assert_all_greater_than <- assert_create_chain(
assert_numeric,
assert_no_missing,
assert_create(
is_greater_than,
default_error_msg = "{.strong {arg_name}} must {ifelse(length(arg_value) > 1, 'all ', '')}be {.strong greater than} `{.strong {minimum}}`."
Expand Down Expand Up @@ -84,6 +86,7 @@ assert_greater_than <- assert_create_chain(
#' @export
assert_all_greater_than_or_equal_to <- assert_create_chain(
assert_numeric,
assert_no_missing,
assert_create(
is_greater_than_or_equal_to,
default_error_msg = "{.strong {arg_name}} must {ifelse(length(arg_value) > 1, 'all ', '')}be {.strong greater than or equal to} `{.strong {minimum}}`."
Expand Down Expand Up @@ -201,6 +204,7 @@ assert_equal <- assert_create(is_equal, default_error_msg = "{.strong {arg_name}
#' @export
assert_all_less_than <- assert_create_chain(
assert_numeric,
assert_no_missing,
assert_create(
is_less_than,
default_error_msg = "{.strong {arg_name}} must {ifelse(length(arg_value) > 1, 'all ', '')}be {.strong less than} `{.strong {maximum}}`."
Expand Down Expand Up @@ -259,6 +263,7 @@ assert_less_than <- assert_create_chain(
#' @export
assert_all_less_than_or_equal_to <- assert_create_chain(
assert_numeric,
assert_no_missing,
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}}`."
Expand Down Expand Up @@ -315,6 +320,7 @@ assert_less_than_or_equal_to <- assert_create_chain(
#' @export
assert_all_between <- assert_create_chain(
assert_numeric,
assert_no_missing,
assert_create(
is_between,
default_error_msg = "{.strong {arg_name}} must {ifelse(length(arg_value) > 1, 'all ', '')}be {.strong between} {.strong {minimum}} and {.strong {maximum}} {ifelse(inclusive, '(inclusive)', '(exclusive)')}."
Expand Down Expand Up @@ -346,6 +352,7 @@ assert_all_between <- assert_create_chain(
#' @export
assert_between <- assert_create_chain(
assert_number,
assert_no_missing,
assert_create(
is_between,
default_error_msg = "{.strong {arg_name}} must be {.strong between} {.strong {minimum}} and {.strong {maximum}} {ifelse(inclusive, '(inclusive)', '(exclusive)')}."
Expand Down
1 change: 1 addition & 0 deletions R/has.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ has_class <- function(x, class){
#' This function asserts that the input vector has no missing values (`NA`) and aborts
#' with an error message if it does.
#'
#' @include assert_create.R
#' @param x A vector.
#' @param msg A character string containing the error message to display if `x` has missing values.
#' @inheritParams common_roxygen_params
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/assert_compare.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
Code
assert_all_greater_than_or_equal_to(c(4, NA), 3)
Condition
Error in `compare()`:
Error:
! 'c(4, NA)' must have no missing values! Found 1

# assert_greater_than_or_equal_to() works [plain]
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-assert_compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ cli::test_that_cli("assert_between() works", config = "plain", {
expect_error(assert_between(NULL, 1, 4), "is not a number", fixed = TRUE)
})

cli::test_that_cli("numeric comparison assertions reject NaN with a missing-values error", config = "plain", {
expect_error(assert_greater_than(NaN, 2), "must have no missing values", fixed = FALSE)
expect_error(assert_all_greater_than(c(2, NaN), 1), "must have no missing values", fixed = FALSE)

expect_error(assert_greater_than_or_equal_to(NaN, 2), "must have no missing values", fixed = FALSE)
expect_error(assert_all_greater_than_or_equal_to(c(2, NaN), 2), "must have no missing values", fixed = FALSE)

expect_error(assert_less_than(NaN, 2), "must have no missing values", fixed = FALSE)
expect_error(assert_all_less_than(c(1, NaN), 2), "must have no missing values", fixed = FALSE)

expect_error(assert_less_than_or_equal_to(NaN, 2), "must have no missing values", fixed = FALSE)
expect_error(assert_all_less_than_or_equal_to(c(1, NaN), 2), "must have no missing values", fixed = FALSE)

expect_error(assert_between(NaN, 1, 3), "must have no missing values", fixed = FALSE)
expect_error(assert_all_between(c(1, NaN), 0, 2), "must have no missing values", fixed = FALSE)
})

cli::test_that_cli("assert_identical() works", config = "plain", {

# Passes
Expand Down