From 19d45502557533fa03a645e34b9306e87a096b2b Mon Sep 17 00:00:00 2001 From: catalamarti Date: Thu, 15 Aug 2024 14:31:59 -0700 Subject: [PATCH] correct misleading error (#6043) --- 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..23bc143a45 --- /dev/null +++ b/tests/testthat/test-utilities-break.R @@ -0,0 +1,3 @@ +test_that("cut_interval throws the correct error message", { + expect_snapshot_error(cut_interval(x = 1:10, width = 10)) +})