From f0662aa9be3a176ccfd8768a311b15aab1f7a881 Mon Sep 17 00:00:00 2001 From: Duncan Kennedy Date: Tue, 1 Oct 2024 16:08:01 -0600 Subject: [PATCH 1/2] added informative error message if no matches are found when running `if_else2()` --- R/if-else2.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/if-else2.R b/R/if-else2.R index 37bf0a8..da39494 100644 --- a/R/if-else2.R +++ b/R/if-else2.R @@ -35,5 +35,8 @@ #' x3 = dplyr::if_else(str_detect2(y, "x is false"), FALSE, x) #' ) if_else2 <- function(condition, true, false) { + if (!any(condition)) { + stop("No matches found. Did not make any replacements.") + } dplyr::if_else(condition, true, false, missing = false) } From 6a933dfd4637a47f04cfccbe43f5d4fc46df35a7 Mon Sep 17 00:00:00 2001 From: Duncan Kennedy Date: Tue, 1 Oct 2024 16:09:19 -0600 Subject: [PATCH 2/2] test for `if_else2()` error message resolves infrastructure-issues#154 --- tests/testthat/test-if-else2.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/testthat/test-if-else2.R b/tests/testthat/test-if-else2.R index 8e58d45..bb995f4 100644 --- a/tests/testthat/test-if-else2.R +++ b/tests/testthat/test-if-else2.R @@ -27,3 +27,11 @@ test_that("works with lists", { list(1, 2, NULL) ) }) + +test_that("throws an informative error when no matches are found", { + x <- c(1, 2, 3, 4, 5) + expect_error( + if_else2(x < 0, 0, x), + "No matches found. Did not make any replacements." + ) +})