Skip to content

Commit

Permalink
xml2 and officer are easier to load in in setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Melkiades committed Mar 14, 2024
1 parent d74707d commit f81e2b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
8 changes: 5 additions & 3 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Collection of functions and data pre-processing to help with testing
library(officer)
library(xml2)

# xml related functions --------------------------------------------------------
get_docx_xml <- function(x) {
Expand All @@ -10,7 +12,7 @@ get_docx_xml <- function(x) {
x <- docx_file
}
redoc <- read_docx(x)
xml2::xml_child(docx_body_xml(redoc))
xml_child(docx_body_xml(redoc))
}

get_pptx_xml <- function(x) {
Expand All @@ -25,7 +27,7 @@ get_pptx_xml <- function(x) {

redoc <- read_pptx(x)
slide <- redoc$slide$get_slide(redoc$cursor)
xml2::xml_child(slide$get())
xml_child(slide$get())
}

get_html_xml <- function(x) {
Expand All @@ -35,7 +37,7 @@ get_html_xml <- function(x) {
x <- html_file
}
doc <- read_html(x)
xml2::xml_child(doc, "body")
xml_child(doc, "body")
}
get_pdf_text <- function(x, extract_fun) {
stopifnot(grepl("\\.pdf$", x))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-borders.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test_that("pdf complex borders", {
# skip_if_not_installed("rmarkdown") # in imports surely installed during tests
# skip_if_not(pandoc_available()) # I guess this is in pandoc_version()
# skip_if_not(pandoc_version() > numeric_version("2.7.3"))
rmarkdown::render(rmd_file,
render(rmd_file,
output_format = rmarkdown::pdf_document(latex_engine = "xelatex"),
output_file = pdf_file,
envir = new.env(),
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-captions-rmd.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ html_file <- gsub("\\.Rmd$", ".html", rmd_file)
docx_file <- gsub("\\.Rmd$", ".docx", rmd_file)
pdf_file <- gsub("\\.Rmd$", ".pdf", rmd_file)

testthat::test_that("with html_document", {
test_that("with html_document", {
skip_if_not_local_testing()
unlink(html_file, force = TRUE)
render(rmd_file,
Expand Down Expand Up @@ -110,10 +110,10 @@ test_that("with word_document", {
expect_length(bookmarks, 0)
})

testthat::test_that("with word_document2", {
test_that("with word_document2", {
skip_if_not_local_testing(min_pandoc_version = "2.7.3")
skip_if(pandoc_version() == numeric_version("2.9.2.1"))
testthat::skip_if_not_installed("bookdown")
skip_if_not_installed("bookdown")

unlink(docx_file, force = TRUE)
render(rmd_file,
Expand Down Expand Up @@ -149,7 +149,7 @@ testthat::test_that("with word_document2", {



testthat::test_that("word with officer", {
test_that("word with officer", {
unlink(docx_file, force = TRUE)
ft <- flextable(head(cars))
ft <- theme_vanilla(ft)
Expand Down Expand Up @@ -193,7 +193,7 @@ test_that("with pdf_document2", {
doc <- get_pdf_text(pdf_file, extract_fun = pdftools::pdf_text)
expect_true(any(grepl("Cross-reference is there: 2", doc, fixed = TRUE)))
} else {
testthat::expect_false(sucess) # only necessary to avoid a note
expect_false(sucess) # only necessary to avoid a note
}
})

Expand Down

0 comments on commit f81e2b8

Please sign in to comment.