From 551c94cf7f0f059153c707bc695fd9df2e2309c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Mon, 25 Nov 2019 22:24:35 +0000 Subject: [PATCH] Add @examplesIf tag --- DESCRIPTION | 2 +- NAMESPACE | 2 ++ R/rd-examples.R | 29 +++++++++++++++++++++++++++ tests/testthat/test-rd-examples.R | 15 ++++++++++++++ tests/testthat/test-rd-examplesIf.txt | 10 +++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-rd-examplesIf.txt diff --git a/DESCRIPTION b/DESCRIPTION index 285d56bb0..cd4ceaee5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -59,5 +59,5 @@ VignetteBuilder: knitr Encoding: UTF-8 Roxygen: list(markdown = TRUE, load = "installed") -RoxygenNote: 7.0.0.9000 +RoxygenNote: 7.0.1.9000 Language: en-GB diff --git a/NAMESPACE b/NAMESPACE index 93da52999..81bbd1d19 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -110,6 +110,7 @@ S3method(roxy_tag_parse,roxy_tag_evalNamespace) S3method(roxy_tag_parse,roxy_tag_evalRd) S3method(roxy_tag_parse,roxy_tag_example) S3method(roxy_tag_parse,roxy_tag_examples) +S3method(roxy_tag_parse,roxy_tag_examplesIf) S3method(roxy_tag_parse,roxy_tag_export) S3method(roxy_tag_parse,roxy_tag_exportClass) S3method(roxy_tag_parse,roxy_tag_exportMethod) @@ -165,6 +166,7 @@ S3method(roxy_tag_rd,roxy_tag_encoding) S3method(roxy_tag_rd,roxy_tag_evalRd) S3method(roxy_tag_rd,roxy_tag_example) S3method(roxy_tag_rd,roxy_tag_examples) +S3method(roxy_tag_rd,roxy_tag_examplesIf) S3method(roxy_tag_rd,roxy_tag_family) S3method(roxy_tag_rd,roxy_tag_field) S3method(roxy_tag_rd,roxy_tag_format) diff --git a/R/rd-examples.R b/R/rd-examples.R index 0e3d955c4..b1e1c926d 100644 --- a/R/rd-examples.R +++ b/R/rd-examples.R @@ -3,6 +3,31 @@ roxy_tag_parse.roxy_tag_examples <- function(x) { tag_examples(x) } #' @export +roxy_tag_parse.roxy_tag_examplesIf <- function(x) { + lines <- unlist(strsplit(x$raw, "\r?\n")) + + condition <- lines[1] + tryCatch( + suppressWarnings(parse(text = condition)), + error = function(err) { + roxy_tag_warning(x, "failed to parse condition of @examplesIf") + } + ) + + dontshow <- paste0( + "\\dontshow{if (", + condition, + ") (if (getRversion() >= \"3.4\") withAutoprint else force)(\\{ # examplesIf}" + ) + + x$raw <- paste( + c(dontshow, lines[-1], "\\dontshow{\\}) # examplesIf}"), + collapse = "\n" + ) + + x <- tag_examples(x) +} +#' @export roxy_tag_parse.roxy_tag_example <- function(x) { x <- tag_value(x) @@ -20,6 +45,10 @@ roxy_tag_rd.roxy_tag_examples <- function(x, base_path, env) { rd_section("examples", x$val) } #' @export +roxy_tag_rd.roxy_tag_examplesIf <- function(x, base_path, env) { + rd_section("examples", x$val) +} +#' @export roxy_tag_rd.roxy_tag_example <- function(x, base_path, env) { path <- file.path(base_path, x$val) if (!file.exists(path)) { diff --git a/tests/testthat/test-rd-examples.R b/tests/testthat/test-rd-examples.R index be3809e2b..80dfa6b04 100644 --- a/tests/testthat/test-rd-examples.R +++ b/tests/testthat/test-rd-examples.R @@ -77,6 +77,21 @@ test_that("warns if path doesn't exist", { ) }) +test_that("@examplesIf", { + out <- roc_proc_text(rd_roclet(), " + #' @name a + #' @title a + #' @examplesIf foo::bar() + #' maybe-run-this-code + #' @examplesIf foobar() + #' and-this + NULL")[[1]] + + verify_output(test_path("test-rd-examplesIf.txt"), { + out$get_section("examples") + }) +}) + test_that("% in @examples escaped before matching braces test (#213)", { out <- roc_proc_text(rd_roclet(), " #' @name a diff --git a/tests/testthat/test-rd-examplesIf.txt b/tests/testthat/test-rd-examplesIf.txt new file mode 100644 index 000000000..ba7ad9c75 --- /dev/null +++ b/tests/testthat/test-rd-examplesIf.txt @@ -0,0 +1,10 @@ +> out$get_section("examples") +\examples{ +\dontshow{if (foo::bar()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +maybe-run-this-code +\dontshow{\}) # examplesIf} +\dontshow{if (foobar()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +and-this +\dontshow{\}) # examplesIf} +} +