From 429997868e359cb4b49dfcc71db03a21af2070f2 Mon Sep 17 00:00:00 2001 From: catalamarti Date: Thu, 15 Aug 2024 13:37:11 -0700 Subject: [PATCH 1/2] correct misleading error fixes #4460 --- R/utilities-break.R | 3 +++ tests/testthat/_snaps/utilities-break.md | 4 ++++ tests/testthat/test-utilities-break.R | 3 +++ 3 files changed, 10 insertions(+) create mode 100644 tests/testthat/_snaps/utilities-break.md create mode 100644 tests/testthat/test-utilities-break.R diff --git a/R/utilities-break.R b/R/utilities-break.R index 1bcce62ec3..0ed711ad7a 100644 --- a/R/utilities-break.R +++ b/R/utilities-break.R @@ -24,6 +24,9 @@ #' table(cut_width(runif(1000), 0.1, center = 0)) #' table(cut_width(runif(1000), 0.1, labels = FALSE)) cut_interval <- function(x, n = NULL, length = NULL, ...) { + if ((!is.null(n) && !is.null(length)) || (is.null(n) && is.null(length))) { + cli::cli_abort("Specify exactly one of {.var n} and {.var length}.") + } cut(x, breaks(x, "width", n, length), include.lowest = TRUE, ...) } diff --git a/tests/testthat/_snaps/utilities-break.md b/tests/testthat/_snaps/utilities-break.md new file mode 100644 index 0000000000..c8115c4c48 --- /dev/null +++ b/tests/testthat/_snaps/utilities-break.md @@ -0,0 +1,4 @@ +# cut_interval gives the correct + + Specify exactly one of `n` and `length`. + diff --git a/tests/testthat/test-utilities-break.R b/tests/testthat/test-utilities-break.R new file mode 100644 index 0000000000..a862b3e46c --- /dev/null +++ b/tests/testthat/test-utilities-break.R @@ -0,0 +1,3 @@ +test_that("cut_interval gives the correct", { + expect_snapshot_error(cut_interval(x = 1:10, width = 10)) +}) From f32d5cbcbd3b14ad8658ea5f3ed542d2bb3d59fc Mon Sep 17 00:00:00 2001 From: catalamarti Date: Thu, 15 Aug 2024 14:11:02 -0700 Subject: [PATCH 2/2] Update tests/testthat/test-utilities-break.R Co-authored-by: Thomas Lin Pedersen --- tests/testthat/test-utilities-break.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-utilities-break.R b/tests/testthat/test-utilities-break.R index a862b3e46c..23bc143a45 100644 --- a/tests/testthat/test-utilities-break.R +++ b/tests/testthat/test-utilities-break.R @@ -1,3 +1,3 @@ -test_that("cut_interval gives the correct", { +test_that("cut_interval throws the correct error message", { expect_snapshot_error(cut_interval(x = 1:10, width = 10)) })