-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
assert_count_true()
to verify that an expected number of values…
… are `TRUE`
- Loading branch information
1 parent
3769ab0
commit 7a8e651
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#' Verify that a vector of values has the expected number of `TRUE` values | ||
#' | ||
#' @param x A logical vecotor without `NA` values | ||
#' @param n The expected number of `TRUE` values | ||
#' @returns `x` if `sum(x) == n` or an informative error message otherwise | ||
#' @examples | ||
#' data.frame(A = 1:5) %>% | ||
#' dplyr::mutate( | ||
#' big_values = assert_count_true(A > 2, n = 3) | ||
#' ) | ||
#' @export | ||
assert_count_true <- function(x, n = 1) { | ||
stopifnot(is.logical(x)) | ||
if (any(is.na(x))) { | ||
stop(deparse(substitute(x)), " has NA values") | ||
} | ||
if (sum(x) != n) { | ||
stop_message <- | ||
sprintf( | ||
"`%s` expected %g `TRUE` %s but %g %s found.", | ||
deparse(substitute(x)), | ||
n, | ||
ngettext(n, "value", "values"), | ||
sum(x), | ||
ngettext(sum(x), "was", "were") | ||
) | ||
stop(stop_message) | ||
} | ||
x | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
test_that("assert_count_true", { | ||
expect_equal( | ||
assert_count_true(TRUE, 1), | ||
TRUE | ||
) | ||
expect_equal( | ||
assert_count_true(rep(TRUE, 3), 3), | ||
rep(TRUE, 3) | ||
) | ||
my_vector <- c(rep(TRUE, 3), FALSE) | ||
expect_equal( | ||
assert_count_true(my_vector, 3), | ||
my_vector | ||
) | ||
expect_error( | ||
assert_count_true(NA), | ||
regexp = "NA has NA values" | ||
) | ||
# more informative errors | ||
my_vector <- c(NA, TRUE) | ||
expect_error( | ||
assert_count_true(my_vector), | ||
regexp = "my_vector has NA values" | ||
) | ||
my_vector <- c(FALSE, TRUE) | ||
expect_error( | ||
assert_count_true(my_vector, n = 2), | ||
regexp = "`my_vector` expected 2 `TRUE` values but 1 was found." | ||
) | ||
# Check grammar of error message | ||
my_vector <- c(TRUE, TRUE) | ||
expect_error( | ||
assert_count_true(my_vector, n = 1), | ||
regexp = "`my_vector` expected 1 `TRUE` value but 2 were found." | ||
) | ||
}) |