Skip to content

Commit

Permalink
Use doctest: add doctests for utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hughjonesd committed Dec 13, 2022
1 parent a870ede commit cec1949
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 4 deletions.
10 changes: 10 additions & 0 deletions R/utils.R
Expand Up @@ -13,9 +13,12 @@
#' oo <- options(onetime.dir = tempdir(check = TRUE)) #' oo <- options(onetime.dir = tempdir(check = TRUE))
#' id <- sample(10000L, 1) #' id <- sample(10000L, 1)
#' #'
#' @expect message("will")
#' onetime_message("will be shown", id = id) #' onetime_message("will be shown", id = id)
#' @expect silent()
#' onetime_message("won't be shown", id = id) #' onetime_message("won't be shown", id = id)
#' onetime_reset(id = id) #' onetime_reset(id = id)
#' @expect message("will")
#' onetime_message("will be shown", id = id) #' onetime_message("will be shown", id = id)
#' #'
#' onetime_reset(id = id) #' onetime_reset(id = id)
Expand Down Expand Up @@ -58,7 +61,9 @@ onetime_reset <- function (
#' oo <- options(onetime.dir = tempdir(check = TRUE)) #' oo <- options(onetime.dir = tempdir(check = TRUE))
#' id <- sample(10000L, 1) #' id <- sample(10000L, 1)
#' #'
#' @expect true()
#' onetime_mark_as_done(id = id) #' onetime_mark_as_done(id = id)
#' @expect silent()
#' onetime_message("Won't be shown", id = id) #' onetime_message("Won't be shown", id = id)
#' #'
#' onetime_reset(id = id) #' onetime_reset(id = id)
Expand Down Expand Up @@ -86,8 +91,10 @@ onetime_mark_as_done <- function (
#' oo <- options(onetime.dir = tempdir(check = TRUE)) #' oo <- options(onetime.dir = tempdir(check = TRUE))
#' id <- sample(10000L, 1) #' id <- sample(10000L, 1)
#' #'
#' @expect false()
#' onetime_been_done(id = id) #' onetime_been_done(id = id)
#' onetime_message("Creating an ID", id = id) #' onetime_message("Creating an ID", id = id)
#' @expect true()
#' onetime_been_done(id = id) #' onetime_been_done(id = id)
#' #'
#' onetime_reset(id = id) #' onetime_reset(id = id)
Expand Down Expand Up @@ -129,8 +136,11 @@ onetime_been_done <- function (
#' @export #' @export
#' #'
#' @doctest #' @doctest
#'
#' @expect match("my-folder$")
#' onetime_dir("my-folder") #' onetime_dir("my-folder")
#' #'
#' @omit
#' oo <- options(onetime.dir = tempdir(check = TRUE)) #' oo <- options(onetime.dir = tempdir(check = TRUE))
#' onetime_dir("my-folder") #' onetime_dir("my-folder")
#' options(oo) #' options(oo)
Expand Down
1 change: 0 additions & 1 deletion man/onetime_been_done.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/onetime_dir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/onetime_mark_as_done.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/onetime_reset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

test_that("Doctest: onetime_been_done", {
# Created from @doctest for `onetime_been_done`
# Source file: R/utils.R
# Source line: 90
oo <- options(onetime.dir = tempdir(check = TRUE))
id <- sample(10000L, 1)
expect_false(onetime_been_done(id = id))
onetime_message("Creating an ID", id = id)
expect_true(onetime_been_done(id = id))
onetime_reset(id = id)
options(oo)
})

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

test_that("Doctest: onetime_dir", {
# Created from @doctest for `onetime_dir`
# Source file: R/utils.R
# Source line: 138
expect_match(onetime_dir("my-folder"), "my-folder$")
})

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

test_that("Doctest: onetime_mark_as_done", {
# Created from @doctest for `onetime_mark_as_done`
# Source file: R/utils.R
# Source line: 60
oo <- options(onetime.dir = tempdir(check = TRUE))
id <- sample(10000L, 1)
expect_true(onetime_mark_as_done(id = id))
expect_silent(onetime_message("Won't be shown", id = id))
onetime_reset(id = id)
options(oo)
})

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

test_that("Doctest: onetime_reset", {
# Created from @doctest for `onetime_reset`
# Source file: R/utils.R
# Source line: 12
oo <- options(onetime.dir = tempdir(check = TRUE))
id <- sample(10000L, 1)
expect_message(onetime_message("will be shown", id = id), "will")
expect_silent(onetime_message("won't be shown", id = id))
onetime_reset(id = id)
expect_message(onetime_message("will be shown", id = id), "will")
onetime_reset(id = id)
options(oo)
})

0 comments on commit cec1949

Please sign in to comment.