tests/testthat/helpers.R

local_temp_rmd_file <- function(..., .env = parent.frame()) {
  path <- withr::local_tempfile(.local_envir = .env, fileext = ".Rmd")
  xfun::write_utf8(c(...), path)
  path
}

local_temp_draft <- function(.env = parent.frame()) {
  path <- withr::local_tempfile(.local_envir = .env, fileext = ".Rmd")
  # TODO: Use `rmarkdown::draft()` when rmarkdown 2.12 is out.  
  pkg_file <- getFromNamespace("pkg_file", "rmarkdown")
  template_path <- pkg_file("rmarkdown", "templates", "revealjs_presentation", 
                            package = "revealjs")
  rmarkdown::draft(path, template_path, edit = FALSE)
}

.render_and_read <- function(input, xml = TRUE, ...) {
  skip_if_not_pandoc()
  output_file <- withr::local_tempfile(fileext = ".html")
  res <- rmarkdown::render(input, output_file = output_file, quiet = TRUE, ...)
  if (xml) {
    xml2::read_html(res)
  } else {
    xfun::read_utf8(res)
  }
}

# Use to test pandoc availability or version lower than
skip_if_not_pandoc <- function(ver = NULL) {
  if (!pandoc_available(ver)) {
    msg <- if (is.null(ver)) {
      "Pandoc is not available"
    } else {
      sprintf("Version of Pandoc is lower than %s.", ver)
    }
    skip(msg)
  }
}

# Use to test version greater than
skip_if_pandoc <- function(ver = NULL) {
  if (pandoc_available(ver)) {
    msg <- if (is.null(ver)) {
      "Pandoc is available"
    } else {
      sprintf("Version of Pandoc is greater than %s.", ver)
    }
    skip(msg)
  }
}
rstudio/revealjs documentation built on March 25, 2023, 9:58 p.m.