library(testthat)
# Load already included functions
pkgload::load_all(export_all = FALSE)

My function

#' Add page
#'
#' Module template for golem
#'
#' @return Used for side effect
#' @inheritParams golem::module_template
#' @export
#'
#' @examples
new_page <- function(name, path, export, ...) {
  tmplt <- readLines(
    system.file(
      "golem/mod_home.R",
      package = "brochure"
    )
  )
  tmplt <- gsub(
    "home",
    name,
    tmplt
  )
  tmplt <- gsub(
    "/",
    sprintf("/%s", name),
    tmplt
  )
  write(tmplt, path)
}
if (requireNamespace("golem") & interactive()) {
  golem::add_module(name = "home", module_template = brochure::new_page)
}
test_that("golem_hook works", {
  x <- tempfile(fileext = ".R")
  new_page("pouet", x)

  tmplt <- readLines(
    x
  )
  expect_true(
    tmplt[1] == "#' pouet UI Function"
  )
  tmplt <- paste(tmplt, collapse = " ")
  expect_true(
    grepl("pouet", tmplt)
  )
  expect_true(
    grepl("page", tmplt)
  )

  skip_if_not_installed("golem")
  old <- setwd(tempdir())
  on.exit(setwd(old))
  unlink("testgolembrochure", TRUE, TRUE)
  golem::create_golem("testgolembrochure", project_hook = brochure::golem_hook)
  setwd("testgolembrochure")
  golem::add_module(name = "pouet", module_template = brochure::new_page)
  expect_true(
    file.exists("R/mod_pouet.R")
  )
})
# Run but keep eval=FALSE to avoid infinite loop
# Execute in the console directly
fusen::inflate(flat_file = "dev/flat_add_page.Rmd", vignette_name = NA, document = FALSE)


ColinFay/brochure documentation built on March 30, 2022, 5:02 a.m.