-
Notifications
You must be signed in to change notification settings - Fork 340
Implement expect_all_equal() and friends
#2259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ae4c8aa
f73352d
cef8ccb
c4ee1d6
1516212
0feafcb
b1fdcd6
b930131
2aed10c
8b6bf16
565eb14
c5719e6
4e2e0d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #' Do you expect every value in a vector to have this value? | ||
| #' | ||
| #' These expectations are similar to `expect_true(all(x == "x"))`, | ||
| #' `expect_true(all(x))` and `expect_true(all(!x))` but give more informative | ||
| #' failure messages if the expectations are not met. | ||
| #' | ||
| #' @inheritParams expect_equal | ||
| #' @export | ||
| #' @examples | ||
| #' x1 <- c(1, 1, 1, 1, 1, 1) | ||
| #' expect_all_equal(x1, 1) | ||
| #' | ||
| #' x2 <- c(1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2) | ||
| #' show_failure(expect_all_equal(x2, 1)) | ||
| #' | ||
| #' # expect_all_true() and expect_all_false() are helpers for common cases | ||
| #' set.seed(1016) | ||
| #' show_failure(expect_all_true(rpois(100, 10) < 20)) | ||
hadley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #' show_failure(expect_all_false(rpois(100, 10) > 20)) | ||
| expect_all_equal <- function(object, expected) { | ||
| act <- quasi_label(enquo(object)) | ||
| exp <- quasi_label(enquo(expected)) | ||
|
|
||
| expect_all_equal_(act, exp) | ||
| invisible(act$val) | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname expect_all_equal | ||
| expect_all_true <- function(object) { | ||
| act <- quasi_label(enquo(object)) | ||
| exp <- labelled_value(TRUE, "TRUE") | ||
|
|
||
| expect_all_equal_(act, exp) | ||
| invisible(act$val) | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname expect_all_equal | ||
| expect_all_false <- function(object) { | ||
| act <- quasi_label(enquo(object)) | ||
| exp <- labelled_value(FALSE, "FALSE") | ||
|
|
||
| expect_all_equal_(act, exp) | ||
| invisible(act$val) | ||
| } | ||
|
|
||
|
|
||
| expect_all_equal_ <- function(act, exp, trace_env = caller_env()) { | ||
| check_vector(act$val, error_call = trace_env, error_arg = "object") | ||
| if (length(act$val) == 0) { | ||
| cli::cli_abort("{.arg object} must not be empty.", call = trace_env) | ||
| } | ||
|
|
||
| check_vector(exp$val, error_call = trace_env, error_arg = "expected") | ||
| if (length(exp$val) != 1) { | ||
| cli::cli_abort("{.arg expected} must be length 1.", call = trace_env) | ||
| } | ||
|
|
||
| exp$val <- rep(exp$val, length(act$val)) | ||
| names(exp$val) <- names(act$val) | ||
| expect_waldo_equal_( | ||
| "Expected every element of %s to equal %s.", | ||
| act, | ||
| exp, | ||
| tolerance = testthat_tolerance(), | ||
| trace_env = trace_env | ||
| ) | ||
hadley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||
| # validates its inputs | ||||||
|
|
||||||
| Code | ||||||
| expect_all_equal(mean, 1) | ||||||
| Condition | ||||||
| Error in `expect_all_equal()`: | ||||||
| ! `object` must be a vector, not a function. | ||||||
| Code | ||||||
| expect_all_equal(logical(), 1) | ||||||
| Condition | ||||||
| Error in `expect_all_equal()`: | ||||||
| ! `object` must not be empty. | ||||||
| Code | ||||||
| expect_all_equal(1:10, mean) | ||||||
| Condition | ||||||
| Error in `expect_all_equal()`: | ||||||
| ! `expected` must be a vector, not a function. | ||||||
| Code | ||||||
| expect_all_equal(1:10, 1:2) | ||||||
| Condition | ||||||
| Error in `expect_all_equal()`: | ||||||
| ! `expected` must be length 1. | ||||||
|
|
||||||
| # can compare atomic vectors | ||||||
|
|
||||||
| Code | ||||||
| expect_all_equal(x, TRUE) | ||||||
| Condition | ||||||
| Error: | ||||||
| ! Expected every element of `x` to equal TRUE. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW that would be inconsistent with |
||||||
| Differences: | ||||||
| `actual[2:8]`: TRUE TRUE TRUE FALSE TRUE TRUE TRUE | ||||||
| `expected[2:8]`: TRUE TRUE TRUE TRUE TRUE TRUE TRUE | ||||||
|
|
||||||
| # can compare named lists | ||||||
|
|
||||||
| Code | ||||||
| expect_all_equal(x, list(1)) | ||||||
| Condition | ||||||
| Error: | ||||||
| ! Expected every element of `x` to equal `list(1)`. | ||||||
| Differences: | ||||||
| `actual$c`: 2.0 | ||||||
| `expected$c`: 1.0 | ||||||
|
|
||||||
| # truncates very long differences | ||||||
|
|
||||||
| Code | ||||||
| expect_all_equal(x, FALSE) | ||||||
| Condition | ||||||
| Error: | ||||||
| ! Expected every element of `x` to equal FALSE. | ||||||
| Differences: | ||||||
| `actual`: TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE | ||||||
| `expected`: FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| test_that("validates its inputs", { | ||
| expect_snapshot(error = TRUE, { | ||
| expect_all_equal(mean, 1) | ||
| expect_all_equal(logical(), 1) | ||
| expect_all_equal(1:10, mean) | ||
| expect_all_equal(1:10, 1:2) | ||
| }) | ||
| }) | ||
|
|
||
| test_that("can compare atomic vectors", { | ||
| x <- rep(TRUE, 10) | ||
| expect_success(expect_all_equal(x, TRUE)) | ||
|
|
||
| x[5] <- FALSE | ||
| expect_snapshot_failure(expect_all_equal(x, TRUE)) | ||
| }) | ||
|
|
||
| test_that("can compare named lists", { | ||
| x <- list(a = 1, b = 1, c = 2, d = 1, e = 1) | ||
| expect_snapshot_failure(expect_all_equal(x, list(1))) | ||
| }) | ||
|
|
||
| test_that("has tolerance enabled", { | ||
| expect_success(expect_all_equal(1, 1L)) | ||
| }) | ||
|
|
||
| test_that("truncates very long differences", { | ||
| x <- rep(TRUE, 10) | ||
| expect_snapshot_failure(expect_all_equal(x, FALSE)) | ||
| }) | ||
|
|
||
| test_that("has TRUE and FALSE helpers", { | ||
| x1 <- rep(TRUE, 10) | ||
| x2 <- rep(FALSE, 10) | ||
| expect_success(expect_all_true(x1)) | ||
| expect_success(expect_all_false(x2)) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just thinking out loud
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constant makes me think unchanging (over time)