Skip to content

Commit

Permalink
v0.1.0 release version
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkf committed Apr 4, 2024
1 parent f7b9406 commit ee5e7de
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
38 changes: 20 additions & 18 deletions R/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
#' tests.
#' @inheritParams testex
#'
#' @examples
#' library(testthat)
#'
#' @examplesIf requireNamespace("testthat", quietly = TRUE)
#' # example code
#' 1 + 2
#'
#' # within `testthat_block`, test code refers to previous result with `.`
#' testthat_block({
#' testthat_block({ \dontshow{
#' . <- 3 # needed because roxygen2 @examplesIf mutates .Last.value
#' }
#' test_that("addition holds up", {
#' expect_equal(., 3)
#' })
Expand Down Expand Up @@ -52,9 +52,9 @@ NULL
#' @return The result of evaluating provided expressions
#'
#' @export
testthat_block <- function(..., value = get_example_value(), obj = NULL,
example = NULL, tests = NULL, envir = parent.frame()) {

testthat_block <- function(
..., value = get_example_value(), obj = NULL,
example = NULL, tests = NULL, envir = parent.frame()) {
if (!missing(value)) value <- substitute(value)

exprs <- substitute(...())
Expand Down Expand Up @@ -168,23 +168,22 @@ fallback_expect_no_error <- function(object, ...) {
#' @return The result of [`testthat::source_file()`], after iterating over
#' generated test files.
#'
#' @examples
#' @examplesIf requireNamespace("testthat", quietly = TRUE)
#' \donttest{
#' library(pkg.example) # from /inst/pkg.example
#' # library(pkg.example)
#' path <- system.file("pkg.example", package = "testex")
#' test_examples_as_testthat(path = path)
#' }
#'
#' @export
test_examples_as_testthat <- function(
package, path, ..., test_dir = tempfile("testex"), clean = TRUE,
overwrite = TRUE, reporter = testthat::get_reporter()
) {
package, path, ..., test_dir = tempfile("testex"), clean = TRUE,
overwrite = TRUE, reporter = testthat::get_reporter()) {
requireNamespace("testthat")

testthat_envvar_val <- Sys.getenv("TESTTHAT")
Sys.setenv(TESTTHAT = "true")
on.exit(Sys.setenv(TESTTHAT = testthat_envvar_val))
on.exit(Sys.setenv(TESTTHAT = testthat_envvar_val), add = TRUE)

if (missing(path)) {
path <- find_package_root(testthat::test_path())
Expand All @@ -195,10 +194,10 @@ test_examples_as_testthat <- function(

if (!test_dir_exists) {
dir.create(test_dir)
if (clean) on.exit(unlink(test_dir))
if (clean) on.exit(unlink(test_dir), add = TRUE)
}

if (test_dir_exists && !overwrite) {
if (test_dir_exists && !overwrite) {
test_files <- list.files(test_dir, full.names = TRUE)
test_files(test_files, chdir = FALSE, "examples [run from testex]")
return()
Expand All @@ -216,7 +215,7 @@ test_examples_as_testthat <- function(
exprs[is_ex] <- lapply(
exprs[is_ex],
wrap_expect_no_error,
value = quote(..Last.value) # can't use base::.Last.value in testthat env
value = quote(..Last.value) # can't use base::.Last.value in testthat env
)

# write out test code to file in test dir
Expand Down Expand Up @@ -285,6 +284,9 @@ wrap_expect_no_error <- function(expr, value) {
#'
#' @keywords internal
get_example_value <- function() {
if (testthat::is_testing()) quote(..Last.value)
else quote(.Last.value)
if (testthat::is_testing()) {
quote(..Last.value)
} else {
quote(.Last.value)
}
}
5 changes: 3 additions & 2 deletions man/test_examples_as_testthat.Rd

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

9 changes: 5 additions & 4 deletions man/testex-testthat.Rd

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

10 changes: 7 additions & 3 deletions tests/spelling.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if(requireNamespace('spelling', quietly = TRUE))
spelling::spell_check_test(vignettes = TRUE, error = FALSE,
skip_on_cran = TRUE)
if (requireNamespace("spelling", quietly = TRUE)) {
spelling::spell_check_test(
vignettes = TRUE,
error = FALSE,
skip_on_cran = TRUE
)
}

0 comments on commit ee5e7de

Please sign in to comment.