Skip to content

Commit

Permalink
Use cli_abort() for all errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Oct 30, 2023
1 parent b1f0225 commit 2d9e6a7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions R/shim.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ package_name <- function(package, character.only = FALSE) {
}

if (!is.character(package) || length(package) != 1L) {
abort("`package` must be character vector of length 1.")
cli::cli_abort("{.arg package} must be character vector of length 1.")
}
if (is.na(package) || (package == "")) {
abort("`package` must not be NA or ''.")
cli::cli_abort("{.arg package} must not be NA or ''.")
}

package
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/_snaps/shim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# package_name throws errors with invalid names

Code
package_name_(c("x", "y"))
Condition
Error in `package_name()`:
! `package` must be character vector of length 1.
Code
package_name_(1:10)
Condition
Error in `package_name()`:
! `package` must be character vector of length 1.
Code
package_name_(NA_character_)
Condition
Error in `package_name()`:
! `package` must not be NA or ''.
Code
package_name_("")
Condition
Error in `package_name()`:
! `package` must not be NA or ''.

21 changes: 10 additions & 11 deletions tests/testthat/test-shim.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ test_that("package_name mimics library", {
})

test_that("package_name throws errors with invalid names" ,{
x <- c("x", "y")
expect_error(package_name(quo(x), TRUE), "character vector")

x <- 1:10
expect_error(package_name(quo(x), TRUE), "character vector")

x <- NA_character_
expect_error(package_name(quo(x), TRUE), "NA")

x <- ""
expect_error(package_name(quo(x), TRUE), "''")
package_name_ <- function(x) {
package_name(quo(x), character.only = TRUE)
}

expect_snapshot(error = TRUE, {
package_name_(c("x", "y"))
package_name_(1:10)
package_name_(NA_character_)
package_name_("")
})
})

0 comments on commit 2d9e6a7

Please sign in to comment.