Skip to content

Commit

Permalink
as_markdown uses dots, with_* refactor
Browse files Browse the repository at this point in the history
- `as_markdown` now accepts ... parameter instead of an x argument
- Replace `quartools:::check_extension_arg` with `rlang::arg_match0`
- majority of tests utilise snapshots instead of equivalence testing
  • Loading branch information
ElianHugh committed Nov 19, 2023
1 parent 0fed7c8 commit 51e779f
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 50 deletions.
11 changes: 0 additions & 11 deletions R/errors.R

This file was deleted.

6 changes: 3 additions & 3 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#' This is a wrapper around [base::paste0] which sets the class of the resulting
#' character vector to "knit_asis". This means that the [knitr::knitr] engine will render
#' the character vector as markdown content, rather than as an R value.
#' @param x object to convert to character vector
#' @param ... dots to convert to character vector
#' @inheritParams base::paste0
#' @examples
#' as_markdown(c("Hello world!"))
#' @return character vector of length 1
#' @export
as_markdown <- function(x, collapse = "") {
as_markdown <- function(..., collapse = "") {
structure(
paste0(x, collapse = collapse),
paste0(..., collapse = collapse),
class = c("knit_asis", "quarto_block")
)
}
Expand Down
21 changes: 13 additions & 8 deletions R/with.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
handle_extensions <- function(extension, valid_extensions) {
if (!is.null(extension)) {
rlang::arg_match0(extension, valid_extensions)
extension_string <- paste0("-", extension)
return(extension_string)
}
return("")
}

#' Temporarily modify page layout
#' @description
#' Create a div block that modifies the current quarto layout column temporarily.
Expand All @@ -13,36 +22,32 @@
#' @return character vector of length 1
#' @rdname page-layout
with_body_column <- function(..., attr = NULL, outset = FALSE, extension = NULL) {
check_extension_arg(extension, c("left", "right"))
extension_string <- handle_extensions(extension, c("left", "right"))
outset_string <- ifelse(isTRUE(outset), "-outset", "")
extension_string <- ifelse(!is.null(extension), paste0("-", extension), "")
cls <- sprintf(".column-body%s%s", outset_string, extension_string)
div(..., attr = c(cls, attr))
}

#' @export
#' @rdname page-layout
with_page_column <- function(..., attr = NULL, extension = NULL) {
check_extension_arg(extension, c("left", "right"))
extension_string <- ifelse(!is.null(extension), paste0("-", extension), "")
extension_string <- handle_extensions(extension, c("left", "right"))
cls <- sprintf(".column-page%s", extension_string)
div(..., attr = c(cls, attr))
}

#' @export
#' @rdname page-layout
with_screen_inset_column <- function(..., attr = NULL, extension = NULL) {
check_extension_arg(extension, c("left", "right", "shaded"))
extension_string <- ifelse(!is.null(extension), paste0("-", extension), "")
extension_string <- handle_extensions(extension, c("left", "right", "shaded"))
cls <- sprintf(".column-screen-inset%s", extension_string)
div(..., attr = c(cls, attr))
}

#' @export
#' @rdname page-layout
with_screen_column <- function(..., attr = NULL, extension = NULL) {
check_extension_arg(extension, c("left", "right"))
extension_string <- ifelse(!is.null(extension), paste0("-", extension), "")
extension_string <- handle_extensions(extension, c("left", "right"))
cls <- sprintf(".column-screen%s", extension_string)
div(..., attr = c(cls, attr))
}
Expand Down
7 changes: 4 additions & 3 deletions man/as_markdown.Rd

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

42 changes: 42 additions & 0 deletions tests/testthat/_snaps/elements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# element output is accurate

Code
div("Hello world!")
Output
:::{}
Hello world!
:::

---

Code
div("This is a tip!", attr = ".callout-tip")
Output
:::{.callout-tip}
This is a tip!
:::

---

Code
span()
Output
[]{}

---

Code
span("Hello world!", attr = ".bold")
Output
[Hello world!]{.bold}

30 changes: 30 additions & 0 deletions tests/testthat/_snaps/misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# mdapply works

Code
mdapply(input, function(x) {
div(x$msg, attr = sprintf(".callout-%s", x$type))
})
Output
:::{.callout-tip}
a tip
:::
:::{.callout-warning}
a warning
:::
:::{.callout-message}
a message
:::

126 changes: 126 additions & 0 deletions tests/testthat/_snaps/with.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# with_*() output is as expected

Code
input[[i]]
Output
:::{.column-body .bar}
foo
:::

---

Code
input[[i]]
Output
:::{.column-page .bar}
foo
:::

---

Code
input[[i]]
Output
:::{.column-screen-inset .bar}
foo
:::

---

Code
input[[i]]
Output
:::{.column-screen .bar}
foo
:::

---

Code
input[[i]]
Output
:::{.column-margin .bar}
foo
:::

---

Code
extensions_input[[i]]
Output
:::{.column-body-left .bar}
foo
:::

---

Code
extensions_input[[i]]
Output
:::{.column-page-right .bar}
foo
:::

---

Code
extensions_input[[i]]
Output
:::{.column-screen-inset-shaded .bar}
foo
:::

---

Code
extensions_input[[i]]
Output
:::{.column-screen-left .bar}
foo
:::

14 changes: 5 additions & 9 deletions tests/testthat/test_elements.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
test_that("div() output is accurate", {
expect_true(div() == "\n\n:::{}\n\n\n\n:::\n")
expect_true(div("Hello world!") == "\n\n:::{}\n\nHello world!\n\n:::\n")
expect_true(div("This is a tip!", attr = ".callout-tip") == "\n\n:::{.callout-tip}\n\nThis is a tip!\n\n:::\n")
})

test_that("span output is accurate", {
expect_true(span() == "[]{}")
expect_true(span("Hello world!", attr = ".bold") == "[Hello world!]{.bold}")
test_that("element output is accurate", {
expect_snapshot(div("Hello world!"))
expect_snapshot(div("This is a tip!", attr = ".callout-tip"))
expect_snapshot(span())
expect_snapshot(span("Hello world!", attr = ".bold"))
})

test_that("unnamed dots throw errors", {
Expand Down
15 changes: 7 additions & 8 deletions tests/testthat/test_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ test_that("mdapply works", {
list(type = "warning", msg = "a warning"),
list(type = "message", msg = "a message")
)
res <- mdapply(
input,
function(x) {
div(x$msg, attr = sprintf(".callout-%s", x$type))
}
)
expect_true(
res == "\n\n:::{.callout-tip}\n\na tip\n\n:::\n\n\n:::{.callout-warning}\n\na warning\n\n:::\n\n\n:::{.callout-message}\n\na message\n\n:::\n" # nolint: line_length_linter.
expect_snapshot(
mdapply(
input,
function(x) {
div(x$msg, attr = sprintf(".callout-%s", x$type))
}
)
)
})
22 changes: 14 additions & 8 deletions tests/testthat/test_with.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ test_that("with_*() output is as expected", {
with_screen_column("foo", attr = ".bar"),
with_margin_column("foo", attr = ".bar")
)
expectations <- list(
"\n\n:::{.column-body .bar}\n\nfoo\n\n:::\n",
"\n\n:::{.column-page .bar}\n\nfoo\n\n:::\n",
"\n\n:::{.column-screen-inset .bar}\n\nfoo\n\n:::\n",
"\n\n:::{.column-screen .bar}\n\nfoo\n\n:::\n",
"\n\n:::{.column-margin .bar}\n\nfoo\n\n:::\n"
)

lapply(seq_along(input), function(i) {
expect_true(input[[i]] == expectations[[i]])
expect_snapshot(input[[i]])
})

extensions_input <- list(
with_body_column("foo", attr = ".bar", extension = "left"),
with_page_column("foo", attr = ".bar", extension = "right"),
with_screen_inset_column("foo", attr = ".bar", extension = "shaded"),
with_screen_column("foo", attr = ".bar", extension = "left")
)

lapply(seq_along(extensions_input), function(i) {
expect_snapshot(extensions_input[[i]])
})


})

test_that("with_*() disallows invalid extensions", {
Expand Down

0 comments on commit 51e779f

Please sign in to comment.