Skip to content

Commit

Permalink
clean up spelling, convert to @test tag
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkf committed Mar 26, 2024
1 parent b136de6 commit a70faa3
Show file tree
Hide file tree
Showing 40 changed files with 288 additions and 286 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
\.github
codecov\.yml
LICENSE\.md
pkgdown
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ Depends:
Imports:
utils
Suggests:
testthat,
withr,
callr,
roxygen2
testthat,
withr,
callr,
roxygen2,
spelling
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Language: en-US
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(roxygen2::roxy_tag_parse,roxy_tag_expect)
S3method(roxygen2::roxy_tag_parse,roxy_tag_test)
export(fallback_expect_no_error)
export(s3_register)
export(test_examples_as_testthat)
Expand Down
7 changes: 4 additions & 3 deletions R/opts.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#'
#' As long as the `fingerprint` has not changed, the package `DESCRIPTION` will
#' be read only once to parse and retrieve configuration options. If the
#' `DESCRIPTION` file is modified or if run from a separate process, the config
#' will be refreshed based on the most recent version of the file.
#' `DESCRIPTION` file is modified or if run from a separate process, the
#' configured settings will be refreshed based on the most recent version of
#' the file.
#'
#' @param path A path in which to search for a package `DESCRIPTION`
#' @param fingerprint An object used to indicate when the cached values have
Expand Down Expand Up @@ -39,7 +40,7 @@ update_testex_desc <- function(path, fingerprint) {

#' @describeIn testex-options
#'
#' @return The test options environemnt as a list
#' @return The test options environment as a list
#'
testex_options <- function(path = package_desc()) {
if (is_r_cmd_check()) {
Expand Down
10 changes: 5 additions & 5 deletions R/register-s3.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# This source code file is licensed under the unlicense license
# This source code file is licensed under the `unlicense` license
# https://unlicense.org

#' Register a method for a suggested dependency
#'
#' Generally, the recommend way to register an S3 method is to use the
#' `S3Method()` namespace directive (often generated automatically by the
#' `@export` roxygen2 tag). However, this technique requires that the generic
#' `@export` `roxygen2` tag). However, this technique requires that the generic
#' be in an imported package, and sometimes you want to suggest a package,
#' and only provide a method when that package is loaded. `s3_register()`
#' can be called from your package's `.onLoad()` to dynamically register
Expand All @@ -24,10 +24,10 @@
#' ```
#'
#' @section Usage in other packages:
#' To avoid taking a dependency on vctrs, you copy the source of
#' To avoid taking a dependency on `vctrs`, you copy the source of
#' [`s3_register()`](https://github.com/r-lib/vctrs/blob/main/R/register-s3.R)
#' into your own package. It is licensed under the permissive
#' [unlicense](https://choosealicense.com/licenses/unlicense/) to make it
#' [`unlicense`](https://choosealicense.com/licenses/unlicense/) to make it
#' crystal clear that we're happy for you to do this. There's no need to include
#' the license or even credit us when using this function.
#'
Expand All @@ -39,7 +39,7 @@
#' in the package environment.
#'
#' Note that providing `method` can be dangerous if you use
#' devtools. When the namespace of the method is reloaded by
#' `devtools`. When the namespace of the method is reloaded by
#' `devtools::load_all()`, the function will keep inheriting from
#' the old namespace. This might cause crashes because of dangling
#' `.Call()` pointers.
Expand Down
70 changes: 33 additions & 37 deletions R/roxygen2.R
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
#' testex replacement for roxygen2 rd roclet
#' [`testex`] `roxygen2` tags
#'
#' This roclet aims to be feature compatible with \pkg{roxygen2}'s \code{"rd"}
#' roclet. In addition it supports two new \code{roxygen} tags, \code{@expect}
#' and \code{@testthat}.
#'
#' @return A new `roxygen2` `"rd"` roclet.
#' [`testex`] provides two new `roxygen2` tags, `@test` and `@testthat`.
#'
#' @section tags:
#' \code{testex} tags are all sub-tags meant to be used within an
#' \code{@examples} block. They should be considered as tags \emph{within} the
#' \code{@examples} block and used to construct blocks of testing code within
#' [testex] tags are all sub-tags meant to be used within an
#' `@examples` block. They should be considered as tags \emph{within} the
#' `@examples` block and used to construct blocks of testing code within
#' example code.
#'
#' \describe{
#' \item{\code{@expect}: }{
#' \item{`@test`: }{
#' In-line expectations to test the output of the previous command within an
#' example. If \code{.} is used within the expecation, it will be used to
#' example. If `.` is used within the test expression, it will be used to
#' refer to the output of the previous example command. Otherwise, the
#' result of the expression is expected to be identical to the previous
#' output.
#'
#' #' @examples
#' #' 1 + 2
#' #' @expect 3
#' #' @expect . == 3
#' #' @test 3
#' #' @test . == 3
#' #'
#' #' @examples
#' #' 3 + 4
#' #' @expect identical(., 7)
#' #' @test identical(., 7)
#' }
#' }
#'
#' \describe{
#' \item{\code{@testthat}: }{
#' Similar to \code{@expect}, \code{@testthat} can be used to make in-line
#' assertions using \pkg{testthat} expectations. \pkg{testthat} expectations
#' \item{`@testthat`: }{
#' Similar to `@test`, `@testthat` can be used to make in-line
#' assertions using `testthat` expectations. `testthat` expectations
#' follow a convention where the first argument is an object to compare
#' against an expected value or characteristic. Since the value will always
#' be the result of the previous example, this part of the code is
#' implicitly constructed for you.
#'
#' If you want to use the example result elsewhere in your expectation, you
#' can refer to it with a \code{.}. When used in this way, \pkg{testex} will
#' can refer to it with a `.`. When used in this way, [testex] will
#' not do any further implicit modification of your expectation.
#'
#' #' @examples
Expand All @@ -55,34 +51,34 @@
#' }
#' }
#'
#' @name testex-roclets
#' @name testex-roxygen-tags
NULL



#' @importFrom utils head tail
#' @exportS3Method roxygen2::roxy_tag_parse roxy_tag_expect
roxy_tag_parse.roxy_tag_expect <- function(x) {
#' @exportS3Method roxygen2::roxy_tag_parse roxy_tag_test
roxy_tag_parse.roxy_tag_test <- function(x) {
x$raw <- x$val <- format_tag_expect_test(x)
as_example(x)
}

#' @importFrom utils head tail
#' @exportS3Method roxygen2::roxy_tag_parse roxy_tag_expect
#' @exportS3Method roxygen2::roxy_tag_parse roxy_tag_test
roxy_tag_parse.roxy_tag_testthat <- function(x) {
x$raw <- x$val <- format_tag_testthat_test(x)
as_example(x)
}



#' Convert a Roxygen Tag to an Examples Tag
#' Convert a `roxygen2` Tag to an `@examples` Tag
#'
#' Allows for converting testing tags into additional `@examples` tags, which
#' `roxygen2` will joint together into a single examples section.
#'
#' @param tag A `roxygen2` tag, whose class should be converted into an
#' examples tag.
#' `@examples` tag.
#' @return The tag with an appropriate examples s3 class.
#'
#' @noRd
Expand All @@ -95,16 +91,16 @@ as_example <- function(tag) {



#' Format An `@expect` Tag
#' Format An `@test` Tag
#'
#' @param tag A `roxygen2` `@expect` tag.
#' @param tag A `roxygen2` `@test` tag.
#' @return A formatted string of R documentation `\testonly{}` code.
#'
#' @noRd
#' @keywords internal
format_tag_expect_test <- function(tag) { # nolint
parsed_test <- parse(text = tag$raw, n = 1, keep.source = TRUE)
test <- populate_expect_dot(parsed_test)
test <- populate_test_dot(parsed_test)
n <- first_expr_end(parsed_test)

paste0(
Expand All @@ -116,17 +112,17 @@ format_tag_expect_test <- function(tag) { # nolint
)
}

#' Populate An Implicit `@expect` Lambda Function
#' Populate An Implicit `@test` Lambda Function
#'
#' When an expect tag does not contain a `.` object, its result is considered
#' When a `@test` tag does not contain a `.` object, its result is considered
#' an an implicit test for an identical object.
#'
#' @param expr A (possibly) implicity lambda function
#' @return A new expression, calling identical if needed.
#'
#' @noRd
#' @keywords internal
populate_expect_dot <- function(expr) {
populate_test_dot <- function(expr) {
if (is.expression(expr)) expr <- expr[[1]]
if (!"." %in% all.names(expr)) {
expr <- bquote(identical(., .(expr)))
Expand All @@ -150,7 +146,7 @@ format_tag_testthat_test <- function(tag) { # nolint
n <- first_expr_end(parsed_test)
test_str <- substring(tag$raw, 1L, n)

nlines <- string_line_count(trimws(test_str, "right"))
nlines <- string_newline_count(trimws(test_str, "right"))
lines <- tag$line + c(0L, nlines)

src <- paste0(basename(tag$file), ":", lines[[1]], ":", lines[[2]])
Expand All @@ -172,9 +168,9 @@ format_tag_testthat_test <- function(tag) { # nolint

#' Populate An Implicit `@testthat` Lambda Function
#'
#' When a testthat tag does not contain a `.` object, its result is considered
#' an an implicit testthat expectation, which should be injected with a `.`
#' as a first argument.
#' When a `testthat` tag does not contain a `.` object, its result is
#' onsidered an an implicit `testthat` expectation, which should be injected
#' with a `.` as a first argument.
#'
#' @param expr A (possibly) implicity lambda function
#' @return A new expression, injecting a `.` argument if needed.
Expand All @@ -193,7 +189,7 @@ populate_testthat_dot <- function(expr) {

#' Find The Last Character of the First Expression
#'
#' @param x A parsed expression with srcref.
#' @param x A parsed expression with [`srcref`].
#' @return An integer representing the character position of the end of the
#' first call in a in a parsed expression.
#'
Expand All @@ -209,9 +205,9 @@ first_expr_end <- function(x) {



#' Escape escaped Rd \\testonly strings
#' Escape R Documentation `\\testonly` Strings
#'
#' @param x A \code{character} value
#' @param x A `character` value
#' @return An escaped string, where any `\` is converted to `\\`
#'
#' @noRd
Expand Down
18 changes: 9 additions & 9 deletions R/testex.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#'
#' @section Documenting with `testex`:
#'
#' `testex` is a simple wrapper around execution that propegates the
#' `.Last.value` returned before running, allowing you to chain expectations
#' `testex` is a simple wrapper around execution that propagates the
#' `.Last.value` returned before running, allowing you to chain tests
#' more easily.
#'
#' ## Use in `Rd` files:
Expand All @@ -30,24 +30,24 @@
#'
#' ## Use with `roxygen2`
#'
#' Within a `roxygen2` `@examples` block you can instead use the `@expect` tag
#' Within a `roxygen2` `@examples` block you can instead use the `@test` tag
#' which will generate Rd code as shown above.
#'
#' \preformatted{
#' #' @examples
#' #' f <- function(a, b) a + b
#' #' f(3, 4)
#' #' @expect is.numeric(.)
#' #' @expect identical(., 7)
#' #' @test is.numeric(.)
#' #' @test identical(., 7)
#' }
#'
#' @param ... Expressions to evaluated. \code{.} will be replaced with the
#' expression passed to \code{val}, and may be used as a shorthand for the
#' @param ... Expressions to evaluated. `.` will be replaced with the
#' expression passed to `val`, and may be used as a shorthand for the
#' last example result.
#' @param value A value to test against. By default, this will use the example's
#' \code{.Last.value}.
#' `.Last.value`.
#' @param obj An optional object name used to construct a more helpful error
#' message testthat failure message.
#' message `testthat` failure message.
#' @param example An option `srcref_key` string used to indicate where the
#' relevant example code originated from.
#' @param tests An option `srcref_key` string used to indicate where the
Expand Down
Loading

0 comments on commit a70faa3

Please sign in to comment.