Skip to content

Commit

Permalink
fixing typos; tests on R CMD check
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkf committed Apr 14, 2024
1 parent 0f2ca2e commit e7ddbfe
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 102 deletions.
6 changes: 3 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# testex (development)

> **Pre-`v1.0.0` Lifecycle Policy**
> **Life-cycle Policy Prior to `v1.0.0`**
>
> Be aware that this package produces code that enters into your package's
> R documentation files. Until `testex` reaches `1.0.0`, there are no
Expand All @@ -24,12 +24,12 @@
testex(style = "testthat", srcref = "fn.R:10:11", { code })
```

This syntax is intended to be more resiliant to changes to keep your
This syntax is intended to be more resilient to changes to keep your
tests from relying to heavily on an unchanging `testex` function interface.

## New Features

* Adds config (`Config/testex/options`) field `"version"`, which is
* Adds configuration (`Config/testex/options`) field `"version"`, which is
automatically updated when a newer version of `testex` is first used.

This field is checked to decide whether the currently loaded version of
Expand Down
2 changes: 0 additions & 2 deletions R/opts.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ read_testex_options <- function(path, warn = TRUE, update = FALSE) {
width = 80L,
indent = 2L
)

return(read_testex_options(path, warn = warn, update = FALSE))
}
}

Expand Down
2 changes: 1 addition & 1 deletion R/testex.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#' `.Last.value`.
#' @param example_srcref An option `srcref_key` string used to indicate where
#' the relevant example code originated from.
#' @param test_srcref An option `srcref_key` string used to indicate where the
#' @param srcref An option `srcref_key` string used to indicate where the
#' relevant test code originated from.
#' @param envir An environment in which tests should be evaluated. By default
#' the parent environment where tests are evaluated.
Expand Down
40 changes: 14 additions & 26 deletions R/testthat.R
Original file line number Diff line number Diff line change
@@ -1,45 +1,30 @@
#' Support for `testthat` example expectations
#' Support for `testthat` Expectations
#'
#' Various functions that are used to produce a more native `testthat`
#' experience, automatically converting [testex] tests into `testthat` code and
#' executing tests such that they produce informative messages on failure.
#'
#' [testex] operates on the previous value produced in example code. This is
#' unlike `testthat` expectations, which expect a value to be provided as a
#' first argument.
#'
#' To accommodate a more native `testthat` interface, [testex] provides a few
#' convenience functions to make [testex] expectations run more idiomatically
#' in the style of `testthat`.
#'
#' @param ... Expectations to evaluate with `testthat`
#' @param value A symbol or quote to use to refer to the subject of `testthat`
#' tests.
#' `testthat` support is managed through a "style" provided to [`testex`].
#' When using the `testthat` style (automatically when using the `@testthat`
#' tag), expectations are processed such that they always refer to the previous
#' example. Special care is taken to manage propagation of this value through
#' your test code, regardless of how `testthat` is executed.
#'
#' @examplesIf requireNamespace("testthat", quietly = TRUE)
#' # example code
#' 1 + 2
#'
#' # within `testthat_block`, test code refers to previous result with `.`
#' testthat_block({ \dontshow{
#' # within `testex` block, test code refers to previous result with `.`
#' testex(style = "testthat", srcref = "abc.R:1:3", { \dontshow{
#' . <- 3 # needed because roxygen2 @examplesIf mutates .Last.value
#' }
#' test_that("addition holds up", {
#' expect_equal(., 3)
#' })
#'
#' # `with_srcref` to spoof the source of the code that caused the failure
#' test_that("test failure is spoofed to report error at abc.R:1:0", {
#' with_srcref("abc.R:1:3", expect_equal(., 3))
#' })
#' })
#'
#' @name testex-testthat
NULL



#' @describeIn testex-testthat
#' Raise `testthat` Expectations With A Known Source Reference
#'
#' Retroactively assigns a source file and location to a expectation. This
#' allows `testthat` to report an origin for any code that raised an example
Expand Down Expand Up @@ -71,7 +56,10 @@ with_srcref <- function(src, expr, envir = parent.frame()) {



#' @describeIn testex-testthat
#' Expect no Error
#'
#' @note This is a stop-gap implementation, and will only be used for legacy
#' versions of `testthat` before this was properly supported.
#'
#' A `testthat` expectation that the provided code can be evaluated without
#' producing an error. This is the most basic expectation one should expect of
Expand Down Expand Up @@ -144,7 +132,7 @@ expect_no_error_call <- function() {
#' `roxygen2` prior to testing. When not `FALSE`, tests written in `roxygen2`
#' tags will be used to update R documentation files prior to testing to use
#' the most up-to-date example tests. May be `TRUE`, or a `list` of arguments
#' passed to [roxygen2::roxygenize]. By default, only enabled when running
#' passed to [`roxygen2::roxygenize`]. By default, only enabled when running
#' outside of `R CMD check` and while taking `roxygen2` as a dependency.
#' @param ... Additional argument unused
#' @param reporter A `testthat` reporter to use. Defaults to the active
Expand Down
28 changes: 28 additions & 0 deletions man/fallback_expect_no_error.Rd

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

2 changes: 1 addition & 1 deletion man/test_examples_as_testthat.Rd

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

70 changes: 8 additions & 62 deletions man/testex-testthat.Rd

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

6 changes: 3 additions & 3 deletions man/testex.Rd

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

27 changes: 27 additions & 0 deletions man/with_srcref.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
as_r_cmd_check <- function(expr, pkg = "pkg") {
withr::with_envvar(
list("_R_CHECK_PACKAGE_NAME_" = pkg),
expr
)
}

as_not_r_cmd_check <- function(expr) {
withr::with_envvar(
list("_R_CHECK_PACKAGE_NAME_" = NA),
expr
)
}
16 changes: 12 additions & 4 deletions tests/testthat/test-options.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ Config/testex/options: list(a = 1)
withr::defer(unlink(test_dir, recursive = TRUE))

writeLines(trimws(desc), desc_path)
expect_warning(testex_options(desc_path), "version")
expect_silent(orig <- testex_options(""))

as_not_r_cmd_check({
expect_warning(testex_options(desc_path), "version")
expect_silent(orig <- testex_options(""))
})

expect_length(orig, 2)
expect_identical(orig$a, 1)
expect_identical(orig$check, FALSE)
Expand All @@ -34,8 +38,12 @@ Config/testex/options: list(a = 1, b = 2)

# expect invalidation of cached value and new values stored
writeLines(trimws(desc), desc_path)
expect_warning(testex_options(desc_path))
expect_silent(updated <- testex_options(""))

as_not_r_cmd_check({
expect_warning(testex_options(desc_path))
expect_silent(updated <- testex_options(""))
})

expect_true(.testex_options$.fingerprint$mtime != orig_mtime)
expect_length(updated, 3)
expect_identical(updated$a, 1)
Expand Down

0 comments on commit e7ddbfe

Please sign in to comment.