context("ioslides") .generate_markdown_for_test <- function() { c("# Header1\n", "## Header2\n", "### Header3\n", "\nTEXT HERE\n", "## Header2 again\n", "### Header3 again\n", "\nTEXT HERE\n" ) } mock_markdown <- function(mdtext = NULL, outputdir = NULL, ... ) { # create input file mdfile <- tempfile(pattern = "mock_XXXXX", tmpdir = outputdir, fileext = ".Rmd") cat(c("---\ntitle: Test\n---\n", mdtext), file = mdfile, sep = "\n", append = FALSE) # output file name outfile <- basename( tempfile(pattern = "mock_XXXXX", tmpdir = outputdir, fileext = ".html" ) ) # convert output <- capture.output( render(mdfile, output_dir = outputdir, output_file = outfile, ioslides_presentation(...) ) ) # read in output html_file <- readLines(file.path(outputdir, outfile)) # return structure for testing properties of invisible(structure( list( output = output, html_file = html_file ), class = "mocked") ) } test_ioslides_presentation <- function() { outputdir <- tempfile() dir.create(outputdir) on.exit(unlink(outputdir), add = TRUE) # Generate mock md file mdtext <- .generate_markdown_for_test() mock2 <- mock_markdown(mdtext = mdtext, outputdir = outputdir) # test argument passing to pandoc expect_true(any(grepl("--slide-level 2", paste(mock2$output), fixed = TRUE))) # test status of headers in resulting file # Header3 should not be a slide header html_file <- mock2$html_file header_lines <- c( any(grepl("

Header1

", html_file, fixed = TRUE)), any(grepl("

Header2

", html_file, fixed = TRUE)), any(grepl("

Header3

", html_file, fixed = TRUE)), any(grepl("

Header2 again

", html_file, fixed = TRUE)), any(grepl("

Header3 again

", html_file, fixed = TRUE)) ) expect_true(all(header_lines)) # test status of css slide class # Only header 1 have slide class with a level header_classes <- c( any(grepl("level1.*Header1", html_file)) ) expect_true(header_classes) # but not level 2 and 3 header_classes <- c( any(grepl("level2.*Header2", html_file)), any(grepl("level3.*Header3", html_file)) ) expect_false(any(header_classes)) mock3 <- mock_markdown(mdtext = mdtext, outputdir = outputdir, slide_level = 3) # Place the header 3 as title slide rout3 <- mock3$output # test argument passing to pandoc expect_true(any(grepl("--slide-level 3", paste(rout3), fixed = TRUE))) # test status of headers in resulting file # Header3 should be a slide header html_file <- mock3$html_file header_lines <- c( any(grepl("

Header1

", html_file, fixed = TRUE)), any(grepl("

Header2

", html_file, fixed = TRUE)), any(grepl("

Header3

", html_file, fixed = TRUE)), any(grepl("

Header2 again

", html_file, fixed = TRUE)), any(grepl("

Header3 again

", html_file, fixed = TRUE)) ) expect_true(all(header_lines)) # test status of css slide class # Header 1 and header 2 have slide class with a level header_classes <- c( any(grepl("level1.*Header1", html_file)), any(grepl("level2.*Header2", html_file)) ) expect_true(all(header_classes)) # but not level 3 header_classes <- c( any(grepl("level3.*Header3", html_file)) ) expect_false(header_classes) } test_that("test_ioslides_presentation", test_ioslides_presentation()) test_ioslides_presentation_css <- function() { outputdir <- tempfile() dir.create(outputdir) on.exit(unlink(outputdir), add = TRUE) # Generate mock md file for data-background mdtext <- c("# Slide One\n", "## Slide Two {data-background=#CCC}\n", "## Slide Three {data-background=img/test.png}\n", "# Slide Four {data-background=#ABCDEF}\n" ) mock <- mock_markdown(mdtext = mdtext, outputdir = outputdir, self_contained = FALSE) html <- mock$html_file slide_lines <- c(any(grepl(']*class="[^"]*\\bsegue\\b[^"]*".*

Slide One

', html, perl = TRUE)) ## separated to be order agnostic , any(grepl(']*class="[^"]*\\bnobackground\\b[^"]*".*

Slide Two

', html, perl = TRUE)) , any(grepl(']*class="[^"]*\\bfill\\b[^"]*".*

Slide Two

', html, perl = TRUE)) , any(grepl(']*style="background: #CCC;".*

Slide Two

', html, perl = TRUE)) ## separated to be order agnostic - within values of attributes also (hence [^"]*) , any(grepl(']*class="[^"]*\\bnobackground\\b[^"]*".*

Slide Two

', html, perl = TRUE)) , any(grepl(']*class="[^"]*\\bfill\\b[^"]*".*

Slide Two

', html, perl = TRUE)) , any(grepl(']*style="[^"]*background-image: url\\(img/test.png\\);[^"]*".*

Slide Three

', html)) , any(grepl(']*style="[^"]*background-size: contain;[^"]*".*

Slide Three

', html)) ## separated to be order agnostic , any(grepl(']*class="[^"]*\\bsegue\\b[^"]*".*

Slide Four

', html, perl = TRUE)) , any(grepl(']*class="[^"]*\\bnobackground\\b[^"]*".*

Slide Four

', html, perl = TRUE)) , any(grepl(']*class="[^"]*\\bfill\\b[^"]*".*

Slide Four

', html, perl = TRUE)) , any(grepl(']*class="[^"]*\\blevel1\\b[^"]*".*

Slide Four

', html, perl = TRUE)) , any(grepl(']*style="background: #ABCDEF;".*

Slide Four

', html, perl = TRUE)) ) expect_true(all(slide_lines), info = "slide lines - style attribute") # Generate mock md file for data-background plot <- file.path(getwd(), 'resources', 'tinyplot.png') mdtext <- c(paste0("## BG Slide {data-background=", plot, "}\n")) mock <- mock_markdown(mdtext = mdtext, outputdir = outputdir, self_contained = TRUE) html <- mock$html_file slide_lines <- c(any(grepl(']*style="[^"]*background-image: url\\(data:image/png;base64,[^\\)]*);[^"]*".*

BG Slide

', html)) ## still separate , any(grepl(']*style="[^"]*background-size: contain;[^"]*".*

BG Slide

', html)) ) expect_true(all(slide_lines), info = "slide lines - self contained image") } test_that("ioslides presentation is styled", test_ioslides_presentation_css()) test_ioslides_presentation_logo <- function() { outputdir <- tempfile() dir.create(outputdir) on.exit(unlink(outputdir), add = TRUE) # Generate mock md file with logo void <- file.copy("resources/empty.png", file.path(outputdir, "logo.png")) mdtext <- c("# Slide One\n") mock <- mock_markdown(mdtext = mdtext, outputdir = outputdir, self_contained = TRUE, logo = "logo.png") html <- mock$html_file slide_lines <- c( !any(grepl("logo\\.png", html)) , any(grep("favIcon: 'data:image/png;base64,[^']*'", html)) , any(grepl("background: url\\(data:image/png;base64,[^\\)]*)", html)) ) expect_true(all(slide_lines), info = "slide lines - self contained logo") # if logo is passed as base64 string, do not re-encode logobase64 <- xfun::base64_uri("resources/empty.png") mock2 <- mock_markdown(mdtext = mdtext, outputdir = outputdir, self_contained = TRUE, logo = logobase64) html2 <- mock2$html_file expect_equal(html, html2) } test_that("ioslides presentation embeds logo", test_ioslides_presentation_logo())