Skip to content

Commit

Permalink
Use doctest: add doctests for onetime_do() and onetime_only()
Browse files Browse the repository at this point in the history
  • Loading branch information
hughjonesd committed Dec 13, 2022
1 parent cec1949 commit d3be66f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
9 changes: 7 additions & 2 deletions R/onetime.R
Expand Up @@ -41,6 +41,7 @@
#' id <- sample(10000L, 1L) #' id <- sample(10000L, 1L)
#' #'
#' for (n in 1:3) { #' for (n in 1:3) {
#' @expect output(regexp = if (n == 1L) "once" else NA)
#' onetime_do(print("printed once"), id = id) #' onetime_do(print("printed once"), id = id)
#' } #' }
#' #'
Expand All @@ -49,7 +50,9 @@
#' expiry <- as.difftime(1, units = "secs") #' expiry <- as.difftime(1, units = "secs")
#' onetime_do(print("Expires quickly, right?"), id = id2, expiry = expiry) #' onetime_do(print("Expires quickly, right?"), id = id2, expiry = expiry)
#' Sys.sleep(2) #' Sys.sleep(2)
#' @expect silent()
#' onetime_do(print("This won't be shown..."), id = id2) #' onetime_do(print("This won't be shown..."), id = id2)
#' @expect output("but this will")
#' onetime_do(print("... but this will"), id = id2, expiry = expiry) #' onetime_do(print("... but this will"), id = id2, expiry = expiry)
#' #'
#' #'
Expand Down Expand Up @@ -94,16 +97,18 @@ onetime_do <- function(
#' id <- sample(10000L, 1) #' id <- sample(10000L, 1)
#' #'
#' sample_once <- onetime_only(sample, id = id) #' sample_once <- onetime_only(sample, id = id)
#' @expect length(10)
#' sample_once(1:10) #' sample_once(1:10)
#' @expect null()
#' sample_once(1:10) #' sample_once(1:10)
#' #'
#' onetime_reset(id) #' onetime_reset(id)
#' options(oo) #' options(oo)
onetime_only <- function ( onetime_only <- function (
.f, .f,
id = deprecate_calling_package(), id = deprecate_calling_package(),
path = default_lockfile_dir(), path = default_lockfile_dir(),
default = NULL, default = NULL,
without_permission = "warn" without_permission = "warn"
) { ) {
force(id) force(id)
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-doctest-onetime_do.R
@@ -0,0 +1,25 @@
# Generated by doctest: do not edit by hand
# Please edit file in R/onetime.R

test_that("Doctest: onetime_do", {
# Created from @doctest for `onetime_do`
# Source file: R/onetime.R
# Source line: 39
oo <- options(onetime.dir = tempdir(check = TRUE))
id <- sample(10000L, 1L)
for (n in 1:3) {
expect_output(onetime_do(print("printed once"), id = id), regexp = if (n ==
1L) "once" else NA)
}
id2 <- sample(10000L, 1L)
expiry <- as.difftime(1, units = "secs")
onetime_do(print("Expires quickly, right?"), id = id2, expiry = expiry)
Sys.sleep(2)
expect_silent(onetime_do(print("This won't be shown..."), id = id2))
expect_output(onetime_do(print("... but this will"), id = id2, expiry = expiry),
"but this will")
onetime_reset(id = id)
onetime_reset(id = id2)
options(oo)
})

16 changes: 16 additions & 0 deletions tests/testthat/test-doctest-onetime_only.R
@@ -0,0 +1,16 @@
# Generated by doctest: do not edit by hand
# Please edit file in R/onetime.R

test_that("Doctest: onetime_only", {
# Created from @doctest for `onetime_only`
# Source file: R/onetime.R
# Source line: 95
oo <- options(onetime.dir = tempdir(check = TRUE))
id <- sample(10000L, 1)
sample_once <- onetime_only(sample, id = id)
expect_length(sample_once(1:10), 10)
expect_null(sample_once(1:10))
onetime_reset(id)
options(oo)
})

0 comments on commit d3be66f

Please sign in to comment.