-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
drop {assertthat} dependency (#243)
Showing
12 changed files
with
149 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ Description: | |
Depends: | ||
R (>= 3.3.0) | ||
Imports: | ||
assertthat (>= 0.2.0), | ||
data.table, | ||
futile.logger, | ||
httr, | ||
|
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 |
---|---|---|
@@ -1,21 +1,64 @@ | ||
|
||
# [title] assert_that wrapper | ||
# [name] assert | ||
# [description] When making an assertion you might call: | ||
# | ||
# \code{assertthat::assert_that(assertthat::is.date(x))} | ||
# | ||
# or something like that. This is an alias to \code{\link[assertthat]{assert_that}} to be used | ||
# for two benefits: \enumerate{ | ||
# \item{This uses \code{\link{log_fatal}} instead of \code{\link{stop}} on failure} | ||
# \item{Much less clutter in the source code} | ||
# } | ||
#' @importFrom assertthat see_if | ||
.assert <- function(..., msg = NULL) { | ||
res <- assertthat::see_if(..., env = parent.frame(), msg = msg) | ||
if (res) { | ||
# [title] assert something and raise an exception if it isn't true | ||
# [name] .assert | ||
# [description] If the condition passed to .assert() does not evaluate to TRUE, | ||
# issues a FATAL-level log message and then raises an R exception, | ||
# both with the content of `msg`. | ||
.assert <- function(expr, msg) { | ||
res <- eval(expr, env = parent.frame()) | ||
if (isTRUE(res)) { | ||
return(invisible(TRUE)) | ||
} else { | ||
log_fatal(attr(res, "msg")) | ||
} | ||
log_fatal(msg) | ||
} | ||
|
||
# [title] check if an object is a count | ||
# [name] .is_count | ||
# [description] Returns TRUE if `x` is a single positive integer | ||
# and FALSE otherwise. | ||
.is_count <- function(x) { | ||
return( | ||
length(x) == 1 && | ||
is.numeric(x) && | ||
!is.na(x) && | ||
x > 0 && | ||
trunc(x) == x | ||
) | ||
} | ||
|
||
# [title] check if an object is a scalar logical | ||
# [name] .is_flag | ||
# [description] Returns TRUE if `x` is `TRUE` or `FALSE` | ||
# and `FALSE` otherwise. | ||
.is_flag <- function(x) { | ||
return( | ||
is.logical(x) && | ||
length(x) == 1L && | ||
!is.na(x) | ||
) | ||
} | ||
|
||
# [title] check if an object is a string | ||
# [name] .is_string | ||
# [description] Returns TRUE if `x` is a non-empty string | ||
# and FALSE otherwise. | ||
.is_string <- function(x) { | ||
return( | ||
is.character(x) && | ||
length(x) == 1L && | ||
!is.na(x) && | ||
x != "" | ||
) | ||
} | ||
|
||
# [title] check if an object is a writeable filepath that exists | ||
# [name] .is_writeable | ||
# [description] Returns TRUE if `x` is a filepath that already exists | ||
# and is writeable, and FALSE otherwise. | ||
.is_writeable <- function(x) { | ||
return( | ||
.is_string(x) && | ||
file.exists(x) && | ||
file.access(x, mode = 2L)[[1L]] == 0L | ||
) | ||
} |
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,67 @@ | ||
test_that(".is_count() works", { | ||
expect_true(.is_count(1L)) | ||
expect_true(.is_count(8L)) | ||
expect_false(.is_count(-2L)) | ||
expect_false(.is_count(0)) | ||
expect_false(.is_count(15.2)) | ||
expect_false(.is_count(NA)) | ||
expect_false(.is_count(NA_character_)) | ||
expect_false(.is_count(NA_integer_)) | ||
expect_false(.is_count(NA_real_)) | ||
expect_false(.is_count(c(1L, 2L))) | ||
expect_false(.is_count("a-number")) | ||
expect_false(.is_count(NULL)) | ||
expect_false(.is_count(TRUE)) | ||
}) | ||
|
||
test_that(".is_flag() works", { | ||
expect_true(.is_flag(TRUE)) | ||
expect_true(.is_flag(FALSE)) | ||
expect_false(.is_flag(-1)) | ||
expect_false(.is_flag(-1L)) | ||
expect_false(.is_flag(0)) | ||
expect_false(.is_flag(0L)) | ||
expect_false(.is_flag(1)) | ||
expect_false(.is_flag(1L)) | ||
expect_false(.is_flag(15.2)) | ||
expect_false(.is_flag(NA)) | ||
expect_false(.is_flag(NA_character_)) | ||
expect_false(.is_flag(NA_integer_)) | ||
expect_false(.is_flag(NA_real_)) | ||
expect_false(.is_flag(c(1L, 2L))) | ||
expect_false(.is_flag("a-number")) | ||
expect_false(.is_flag(NULL)) | ||
}) | ||
|
||
test_that(".is_string() works", { | ||
expect_true(.is_string("abc")) | ||
expect_true(.is_string(" ")) | ||
expect_false(.is_string("")) | ||
expect_false(.is_string(-2L)) | ||
expect_false(.is_string(0)) | ||
expect_false(.is_string(15.2)) | ||
expect_false(.is_string(NA)) | ||
expect_false(.is_string(NA_character_)) | ||
expect_false(.is_string(NA_integer_)) | ||
expect_false(.is_string(NA_real_)) | ||
expect_false(.is_string(c(1L, 2L))) | ||
expect_false(.is_string(NULL)) | ||
expect_false(.is_string(TRUE)) | ||
}) | ||
|
||
test_that(".is_writeable() works", { | ||
expect_true(.is_writeable(getwd())) | ||
expect_false(.is_writeable(file.path(tempdir(), "some-nonsense"))) | ||
expect_false(.is_writeable("")) | ||
expect_false(.is_writeable(-2L)) | ||
expect_false(.is_writeable(0)) | ||
expect_false(.is_writeable(15.2)) | ||
expect_false(.is_writeable(NA)) | ||
expect_false(.is_writeable(NA_character_)) | ||
expect_false(.is_writeable(NA_integer_)) | ||
expect_false(.is_writeable(NA_real_)) | ||
expect_false(.is_writeable(c(1L, 2L))) | ||
expect_false(.is_writeable("a-number")) | ||
expect_false(.is_writeable(NULL)) | ||
expect_false(.is_writeable(TRUE)) | ||
}) |
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