From c5830b0928aa3ed915e70494a73a1ee9766f015e Mon Sep 17 00:00:00 2001 From: simonpcouch Date: Tue, 4 Jun 2024 20:29:59 -0500 Subject: [PATCH] basic type checking the rlang type check standalone is probably overkill for this project, but some simple type checks should still be helpful. --- R/syrup.R | 9 +++++++++ tests/testthat/_snaps/syrup.md | 24 ++++++++++++++++++++++++ tests/testthat/test-syrup.R | 8 ++++++++ 3 files changed, 41 insertions(+) diff --git a/R/syrup.R b/R/syrup.R index 9d2e5ce..4b0d212 100644 --- a/R/syrup.R +++ b/R/syrup.R @@ -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() diff --git a/tests/testthat/_snaps/syrup.md b/tests/testthat/_snaps/syrup.md index 4b6d049..470460e 100644 --- a/tests/testthat/_snaps/syrup.md +++ b/tests/testthat/_snaps/syrup.md @@ -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. diff --git a/tests/testthat/test-syrup.R b/tests/testthat/test-syrup.R index 33d1e58..eaf4003 100644 --- a/tests/testthat/test-syrup.R +++ b/tests/testthat/test-syrup.R @@ -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)) })