-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
257 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#' Coerce an argument to a length-1 logical vector | ||
#' | ||
#' This function wraps [to_lgl()], adding a quick check to confirm that the | ||
#' input contains a single value. | ||
#' | ||
#' @inheritParams to_lgl | ||
#' | ||
#' @return A logical vector equivalent to `x`. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' to_lgl_scalar("TRUE") | ||
#' try(to_lgl_scalar(c(TRUE, FALSE))) | ||
to_lgl_scalar <- function(x, | ||
allow_null = TRUE, | ||
x_arg = rlang::caller_arg(x), | ||
call = rlang::caller_env(), | ||
x_class = object_type(x)) { | ||
force(x_arg) | ||
x <- to_lgl( | ||
x, | ||
allow_null = allow_null, | ||
x_arg = x_arg, | ||
call = call, | ||
x_class = x_class | ||
) | ||
.check_scalar( | ||
x, | ||
allow_null = allow_null, | ||
x_arg = x_arg, | ||
call = call, | ||
x_class = x_class | ||
) | ||
return(x) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# to_lgl_scalar() provides informative error messages | ||
|
||
Code | ||
to_lgl_scalar(given) | ||
Condition | ||
Error: | ||
! `given` must be a single <logical>. | ||
x `given` has 3 values. | ||
|
||
--- | ||
|
||
Code | ||
wrapper(given) | ||
Condition | ||
Error in `wrapper()`: | ||
! `wrapper_val` must be a single <logical>. | ||
x `wrapper_val` has 3 values. | ||
|
||
--- | ||
|
||
Code | ||
to_lgl_scalar(given) | ||
Condition | ||
Error: | ||
! `given` <character> must be coercible to <logical> | ||
x Can't convert some values due to incompatible values. | ||
* Locations: 1 | ||
|
||
--- | ||
|
||
Code | ||
wrapper(given) | ||
Condition | ||
Error in `wrapper()`: | ||
! `wrapper_val` <character> must be coercible to <logical> | ||
x Can't convert some values due to incompatible values. | ||
* Locations: 1 | ||
|
||
--- | ||
|
||
Code | ||
to_lgl_scalar(given, allow_null = FALSE) | ||
Condition | ||
Error: | ||
! `given` must not be <NULL>. | ||
|
||
--- | ||
|
||
Code | ||
wrapper(given, allow_null = FALSE) | ||
Condition | ||
Error in `wrapper()`: | ||
! `wrapper_val` must not be <NULL>. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
test_that("to_lgl_scalar() allows length-1 lgls through", { | ||
given <- TRUE | ||
expect_true(to_lgl_scalar(given)) | ||
given <- FALSE | ||
expect_false(to_lgl_scalar(given)) | ||
given <- NULL | ||
expect_null(to_lgl_scalar(given)) | ||
}) | ||
|
||
test_that("to_lgl_scalar() provides informative error messages", { | ||
given <- c(TRUE, FALSE, TRUE) | ||
expect_snapshot( | ||
to_lgl_scalar(given), | ||
error = TRUE | ||
) | ||
|
||
wrapper <- function(wrapper_val, ...) { | ||
return(to_lgl_scalar(wrapper_val, ...)) | ||
} | ||
expect_snapshot( | ||
wrapper(given), | ||
error = TRUE | ||
) | ||
|
||
given <- "a" | ||
expect_snapshot( | ||
to_lgl_scalar(given), | ||
error = TRUE | ||
) | ||
expect_snapshot( | ||
wrapper(given), | ||
error = TRUE | ||
) | ||
|
||
given <- NULL | ||
expect_snapshot( | ||
to_lgl_scalar(given, allow_null = FALSE), | ||
error = TRUE | ||
) | ||
expect_snapshot( | ||
wrapper(given, allow_null = FALSE), | ||
error = TRUE | ||
) | ||
}) |