Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve snap description #44

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions R/shiny2screenshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
)
Expand Down Expand Up @@ -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 = " "
)
}

Expand Down
12 changes: 12 additions & 0 deletions inst/examples/snaps2fig/multiple.R
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 14 additions & 4 deletions tests/testthat/_snaps/shiny2screenshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
[<text>: 1] @title 'An example documentation with inserted snaps' {parsed}
[<text>: 1] @title 'An example documentation with inserted snaps fr...' {parsed}

[[2]]
[<text>: 2] @crowInsertSnaps '...' {parsed}
Expand All @@ -58,13 +58,23 @@
[<text>: 7] @backref '<generated>' {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
Expand Down
21 changes: 17 additions & 4 deletions tests/testthat/test-shiny2screenshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,33 @@ 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"
)
)
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"))
}
)
Expand Down