Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronwolen authored and eddelbuettel committed Dec 16, 2018
1 parent 9068760 commit 6901f23
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ Imports: rmarkdown, knitr
VignetteBuilder: knitr
License: GPL-3
RoxygenNote: 6.0.1
Suggests:
testthat
6 changes: 6 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library(testthat)
library(linl)
library(rmarkdown)
library(yaml)

test_check("linl")
22 changes: 22 additions & 0 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# variables ---------------------------------------------------------------
frontmatter <- yaml::read_yaml("resources/frontmatter.yaml")
body <- readLines("resources/body.txt")
quiet <- !interactive()


# helpers -----------------------------------------------------------------

# emit logicals as true/false since yes/no is ignored by rmarkdown
to_lgl <- function(x) structure(ifelse(x, 'true', 'false'), class = "verbatim")

build_rmd <- function(frontmatter, body) {
stopifnot(is.list(frontmatter) && is.character(body))
outfile <- tempfile(fileext = ".Rmd")
writeLines(c(
"---",
yaml::as.yaml(frontmatter, handlers = list(logical = to_lgl)),
"---\n",
body
), outfile)
return(outfile)
}
5 changes: 5 additions & 0 deletions tests/testthat/resources/body.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas elementum neque eget dolor [egestas fringilla](http://example.com):

> Nullam eget dapibus quam, sit amet sagittis magna.

Vestibulum id sodales lectus, sed scelerisque quam.
13 changes: 13 additions & 0 deletions tests/testthat/resources/frontmatter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
author:
- Aaron Wolen
- Your Organization
opening: To whom it may concern,
closing: Sincerely,
address:
- 123 Street Rd
- Chicago, IL
return-address:
- My Home
- 456 Road St.
- New York, NY
output: linl::linl
Binary file added tests/testthat/resources/letterhead.pdf
Binary file not shown.
32 changes: 32 additions & 0 deletions tests/testthat/test-linl.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
context("Letter Options")

test_that("Base letter", {
frontmatter$plainquote <- TRUE
input <- build_rmd(frontmatter, body)
output <- render(input, output_dir = dirname(input), quiet = quiet)
expect_true(file.exists(output))
})

test_that("Styled blockquotes", {
skip_on_cran()
input <- build_rmd(frontmatter, body)
output <- render(input, output_dir = dirname(input), quiet = quiet)
expect_true(file.exists(output))
})

test_that("PDF letterhead", {
skip_on_cran()
frontmatter$letterhead <- normalizePath("resources/letterhead.pdf")
input <- build_rmd(frontmatter, body)
output <- render(input, output_dir = dirname(input), quiet = quiet)
expect_true(file.exists(output))
})

test_that("no option clash with colorlinks disabled (#19)", {
skip_on_cran()
frontmatter$letterhead <- normalizePath("resources/letterhead.pdf")
frontmatter$colorlinks <- FALSE
input <- build_rmd(frontmatter, body)
output <- render(input, output_dir = dirname(input), quiet = quiet)
expect_true(file.exists(output))
})

0 comments on commit 6901f23

Please sign in to comment.