diff --git a/R/shiny2screenshot.R b/R/shiny2screenshot.R index ab9430d..1e76442 100644 --- a/R/shiny2screenshot.R +++ b/R/shiny2screenshot.R @@ -201,6 +201,9 @@ roxy_tag_parse.roxy_tag_crowInsertSnaps <- function(x) { roxy_tag_rd.roxy_tag_crowInsertSnaps <- function(x, base_path, env) { args <- as.list(x[["val"]]) if (length(args) >= 2) args[[3]] <- as.logical(args[[3]]) + possible_names <- c("test_file", "name", "auto_numbered", "variant", "fps") + # this is fine because the order of arguments for tags is always the same + names(args) <- possible_names[seq_along(args)] roxygen2::rd_section( type = "crowInsertSnaps", value = rlang::exec(snaps2rd, !!!args) @@ -212,7 +215,7 @@ format.rd_section_crowInsertSnaps <- function(x, ...) { paste0( "\\section{Screenshots from Tests}{\n", "\\if{html}", - x$value, + paste(x$value, collapse = "\n"), "\\if{latex}{Screenshots cannot be shown in this output format.}", "}\n" ) @@ -285,15 +288,31 @@ snaps2md <- function(...) { #' For a custom roxygen2 tag with equivalent funcionality, #' see [crowInsertSnaps()]. #' @export -snaps2rd <- function(...) { - path <- snaps2fig(...) - paste0( - "{\\figure{", - path, - "}{options: width='100\\%' alt=", - snap_alt_text(), - "}}", - collapse = "" +snaps2rd <- function(test_file = character(), + name = NULL, + auto_numbered = TRUE, + variant = shinytest2::platform_variant(), + fps = 5) { + path <- snaps2fig( + test_file = test_file, + name = name, + auto_numbered = auto_numbered, + variant = variant, + fps = fps + ) + glue::glue_collapse( + c( + glue::glue("{{name: \\code{{{name}}}, variant: \\code{{{variant}}}"), + paste0( + "\\figure{", + path, + "}{options: width='100\\%' alt=", + snap_alt_text(), + "}}", + collapse = "" + ) + ), + sep = " " ) } diff --git a/inst/examples/snaps2fig/multiple.R b/inst/examples/snaps2fig/multiple.R new file mode 100644 index 0000000..63c9eeb --- /dev/null +++ b/inst/examples/snaps2fig/multiple.R @@ -0,0 +1,12 @@ +#' An example documentation with inserted snaps from multiple tags +#' @crowInsertSnaps +#' helpers +#' bins +#' FALSE +#' linux +#' @crowInsertSnaps +#' helpers +#' bins +#' FALSE +#' mac +bins_app <- function() examples_app() diff --git a/tests/testthat/_snaps/shiny2screenshot.md b/tests/testthat/_snaps/shiny2screenshot.md index 6f21076..58841e0 100644 --- a/tests/testthat/_snaps/shiny2screenshot.md +++ b/tests/testthat/_snaps/shiny2screenshot.md @@ -40,10 +40,10 @@ # roxy_tag_crowInsertSnaps: can be parsed Code - roxygen2::parse_text(example)[[1]]$tags + roxygen2::parse_text(single)[[1]]$tags Output [[1]] - [: 1] @title 'An example documentation with inserted snaps' {parsed} + [: 1] @title 'An example documentation with inserted snaps fr...' {parsed} [[2]] [: 2] @crowInsertSnaps '...' {parsed} @@ -58,13 +58,23 @@ [: 7] @backref '' {parsed} -# roxy_tag_crowInsertSnaps: can be formatted +# roxy_tag_crowInsertSnaps: can be formatted with single tag + + Code + topic$get_section("crowInsertSnaps") + Output + \section{Screenshots from Tests}{ + \if{html}{name: \code{bins}, variant: \code{linux} \figure{crow_screenshots/helpers/bins.gif}{options: width='100\%' alt=Screenshot from App}}\if{latex}{Screenshots cannot be shown in this output format.}} + + +# roxy_tag_crowInsertSnaps: can be formatted with multiple tags joined Code topic$get_section("crowInsertSnaps") Output \section{Screenshots from Tests}{ - \if{html}{\figure{crow_screenshots/helpers/bins.gif}{options: width='100\%' alt=Screenshot from App}}\if{latex}{Screenshots cannot be shown in this output format.}} + \if{html}{name: \code{bins}, variant: \code{linux} \figure{crow_screenshots/helpers/bins.gif}{options: width='100\%' alt=Screenshot from App}} + {name: \code{bins}, variant: \code{mac} \figure{crow_screenshots/helpers/bins.gif}{options: width='100\%' alt=Screenshot from App}}\if{latex}{Screenshots cannot be shown in this output format.}} # snaps2fig and friends work: writes out markdown syntax diff --git a/tests/testthat/test-shiny2screenshot.R b/tests/testthat/test-shiny2screenshot.R index 9dd3cde..1d9b872 100644 --- a/tests/testthat/test-shiny2screenshot.R +++ b/tests/testthat/test-shiny2screenshot.R @@ -69,7 +69,7 @@ test_that("screenshots fail according to `strict` setting", { }) describe("roxy_tag_crowInsertSnaps", { - example <- brio::read_file( + single <- brio::read_file( fs::path_package( package = "crow", "examples", "snaps2fig", "single", ext = "R" @@ -77,12 +77,25 @@ describe("roxy_tag_crowInsertSnaps", { ) it( "can be parsed", - expect_snapshot(roxygen2::parse_text(example)[[1]]$tags) + expect_snapshot(roxygen2::parse_text(single)[[1]]$tags) ) it( - "can be formatted", + "can be formatted with single tag", { - topic <- roxygen2::roc_proc_text(roxygen2::rd_roclet(), example)[[1]] + topic <- roxygen2::roc_proc_text(roxygen2::rd_roclet(), single)[[1]] + expect_snapshot(topic$get_section("crowInsertSnaps")) + } + ) + multiple <- brio::read_file( + fs::path_package( + package = "crow", + "examples", "snaps2fig", "multiple", ext = "R" + ) + ) + it( + "can be formatted with multiple tags joined", + { + topic <- roxygen2::roc_proc_text(roxygen2::rd_roclet(), multiple)[[1]] expect_snapshot(topic$get_section("crowInsertSnaps")) } )