Skip to content

Commit

Permalink
basic type checking
Browse files Browse the repository at this point in the history
the rlang type check standalone is probably overkill for this project, but some simple type checks should still be helpful.
  • Loading branch information
simonpcouch committed Jun 5, 2024
1 parent 2f9ddc1 commit c5830b0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions R/syrup.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
#' @export
syrup <- function(expr, interval = .5, peak = FALSE, env = caller_env()) {
expr <- substitute(expr)
if (!is_double(interval, n = 1, finite = TRUE)) {
abort("`interval` must be a single, finite numeric.")
}
if (!is_bool(peak)) {
abort("`peak` must be `TRUE` or `FALSE`.")
}
if (!is_environment(env)) {
abort("`env` must be an environment.")
}

# create a new temporary R session `sesh`
sesh <- callr::r_session$new()
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/_snaps/syrup.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# syrup does basic type checks

Code
syrup(1, interval = "boop")
Condition
Error in `syrup()`:
! `interval` must be a single, finite numeric.

---

Code
syrup(1, peak = "no")
Condition
Error in `syrup()`:
! `peak` must be `TRUE` or `FALSE`.

---

Code
syrup(1, env = "schmenv")
Condition
Error in `syrup()`:
! `env` must be an environment.

# syrup warns with only one ID

! `expr` evaluated fully before syrup could take a snapshot of memory usage.
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-syrup.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ test_that("syrup(interval) works", {
expect_true(length(unique(res_01$id)) > length(unique(res_1$id)))
})

test_that("syrup does basic type checks", {
# the rlang type check standalone is probably overkill for this project,
# but some simple type checks should still be helpful.
expect_snapshot(error = TRUE, syrup(1, interval = "boop"))
expect_snapshot(error = TRUE, syrup(1, peak = "no"))
expect_snapshot(error = TRUE, syrup(1, env = "schmenv"))
})

test_that("syrup warns with only one ID", {
expect_snapshot_warning(syrup(1))
})

0 comments on commit c5830b0

Please sign in to comment.