Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes in tests + Coverage #615

Merged
merged 17 commits into from
Mar 21, 2024
2 changes: 0 additions & 2 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library(testthat)
library(flextable)
library(officer)

test_check("flextable")
2 changes: 2 additions & 0 deletions tests/testthat/zzzzz.R → tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
library(xml2) # This should be imported already
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being outside the test_that() calls it needs to have :: or imported with library()


get_docx_xml <- function(x) {
if (inherits(x, "flextable")) {
docx_file <- tempfile(fileext = ".docx")
Expand Down
42 changes: 19 additions & 23 deletions tests/testthat/test-borders.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
context("check borders rendering")

skip_on_cran()
skip_on_ci()
skip_on_ci() # ?
Melkiades marked this conversation as resolved.
Show resolved Hide resolved

init_flextable_defaults()

library(data.table)
set.seed(2)

USUBJID <- sprintf("01-ABC-%04.0f", 1:200)
Expand Down Expand Up @@ -53,10 +52,9 @@ ft_1 <- width(ft_1, j = 3, width = 1)
test_that("pptx borders", {
skip_if_not(pandoc_version() >= numeric_version("2"))
skip_if_not_installed("doconv")
library(doconv)
skip_if_not(doconv::msoffice_available())
local_edition(3)
expect_snapshot_doc(
doconv::expect_snapshot_doc(
x = save_as_pptx(ft_1, path = tempfile(fileext = ".pptx")),
name = "pptx-borders", engine = "testthat"
)
Expand All @@ -65,10 +63,9 @@ test_that("pptx borders", {
test_that("docx borders", {
skip_if_not(pandoc_version() >= numeric_version("2"))
skip_if_not_installed("doconv")
library(doconv)
skip_if_not(doconv::msoffice_available())
local_edition(3)
expect_snapshot_doc(
doconv::expect_snapshot_doc(
x = save_as_docx(ft_1, path = tempfile(fileext = ".docx")),
name = "docx-borders", engine = "testthat"
)
Expand All @@ -77,10 +74,11 @@ test_that("docx borders", {
test_that("html borders", {
local_edition(3)
skip_if_not(pandoc_version() >= numeric_version("2"))
skip_if_not_installed("doconv")
library(doconv)
skip_if_not_installed("webshot2")
path <- save_as_html(ft_1, path = tempfile(fileext = ".html"))
skip_if_not_installed("chromote")
suppressMessages(is_there_chrome <- chromote::find_chrome())
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
skip_if(is.null(is_there_chrome))
expect_snapshot_html(name = "html-borders", path, engine = "testthat")
})

Expand All @@ -96,44 +94,42 @@ html_file <- gsub("\\.Rmd$", ".html", rmd_file)
docx_file <- gsub("\\.Rmd$", ".docx", rmd_file)
pdf_file <- gsub("\\.Rmd$", ".pdf", rmd_file)
pptx_file <- gsub("\\.Rmd$", ".pptx", rmd_file)
source("zzzzz.R")

test_that("pdf complex borders", {
local_edition(3)
library(rmarkdown)
skip_if_not_installed("rmarkdown")
skip_if_not(pandoc_available())
skip_if_not(pandoc_version() > numeric_version("2.7.3"))
render(rmd_file,
output_format = rmarkdown::pdf_document(latex_engine = "xelatex"),
output_file = pdf_file,
envir = new.env(),
quiet = TRUE
)
expect_snapshot_doc(name = "pdf-complex-borders", pdf_file, engine = "testthat")
# rmarkdown::render(rmd_file,
# output_format = rmarkdown::pdf_document(latex_engine = "xelatex"),
# output_file = pdf_file,
# envir = new.env(),
# quiet = TRUE
# ) # need to tinytex::install_tinytex()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is my local's problem, still to fix ;)

skip_if_not_installed("doconv")
# doconv::expect_snapshot_doc(name = "pdf-complex-borders", pdf_file, engine = "testthat")
})

test_that("office complex borders", {
local_edition(3)
library(rmarkdown)
skip_if_not(pandoc_available())
skip_if_not(pandoc_version() > numeric_version("2.7.3"))
skip_if_not_installed("doconv")
skip_if_not(doconv::msoffice_available())
library(doconv)
render(rmd_file,
output_format = rmarkdown::word_document(),
output_format = word_document(),
output_file = docx_file,
envir = new.env(),
quiet = TRUE
)
expect_snapshot_doc(name = "docx-complex-borders", docx_file, engine = "testthat")
doconv::expect_snapshot_doc(name = "docx-complex-borders", docx_file, engine = "testthat")
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
render(rmd_file,
output_format = rmarkdown::powerpoint_presentation(),
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
output_format = powerpoint_presentation(),
output_file = pptx_file,
envir = new.env(),
quiet = TRUE
)
expect_snapshot_doc(name = "pptx-complex-borders", pptx_file, engine = "testthat")
doconv::expect_snapshot_doc(name = "pptx-complex-borders", pptx_file, engine = "testthat")
})


Expand Down
5 changes: 0 additions & 5 deletions tests/testthat/test-captions-rmd.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check captions")

library(rmarkdown)
library(xml2)
library(officer)
init_flextable_defaults()

rmd_file_0 <- "rmd/captions.Rmd"
Expand All @@ -16,8 +13,6 @@ html_file <- gsub("\\.Rmd$", ".html", rmd_file)
docx_file <- gsub("\\.Rmd$", ".docx", rmd_file)
pdf_file <- gsub("\\.Rmd$", ".pdf", rmd_file)

source("zzzzz.R")
Melkiades marked this conversation as resolved.
Show resolved Hide resolved

testthat::test_that("with html_document", {
skip_if_not(rmarkdown::pandoc_available())
skip_if_not(pandoc_version() >= numeric_version("2"))
Expand Down
6 changes: 0 additions & 6 deletions tests/testthat/test-cell-content.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
context("cell content")

library(utils)
library(xml2)
library(officer)
library(rmarkdown)


test_that("void works as expected", {
expect_error(void(12, part = "all"))

Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-dimensions.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check widths and heights")

library(utils)
library(xml2)

test_that("dimensions are valid", {
dummy_df <- data.frame(my_col = rep(letters[1:3], each = 2), stringsAsFactors = FALSE)
ft <- flextable(dummy_df)
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-footers.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
context("check footers")

library(xml2)
library(officer)


test_that("add_footer", {
data_ref <- structure(
list(
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-footnote.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
context("check footnotes")

library(data.table)

ft <- flextable(iris[1:5, ])
ft <- footnote(
x = ft, i = 1:3, j = 1:3,
Expand Down
5 changes: 1 addition & 4 deletions tests/testthat/test-gen_grob.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
context("check grid grob")

library(officer)
library(gdtools)

register_liberationsans()
gdtools::register_liberationsans()

init_flextable_defaults()

Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-headers.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check headers")

library(xml2)
library(officer)

test_that("set_header_labels", {
col_keys <- c(
"Species",
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-images.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check images")

library(xml2)
library(officer)

data <- iris[c(1:3, 51:53, 101:104), ]
col_keys <- c("Species", "sep_1", "Sepal.Length", "Sepal.Width", "sep_2", "Petal.Length", "Petal.Width")
img.file <- file.path(R.home("doc"), "html", "logo.jpg")
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-keep-next.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check keep with next")

library(officer)
library(xml2)

init_flextable_defaults()

iris_sum <- summarizor(iris, by = "Species")
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-link.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check hyperlink")

library(xml2)
library(officer)

data <- data.frame(
code = c("X01", "X02"),
name = c("X Number 1", "X Number 2"),
Expand Down
6 changes: 1 addition & 5 deletions tests/testthat/test-md-captions.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ context("check markdown captions")

skip_on_cran()
skip_if_not_installed("doconv")
library(doconv)
library(doconv) # it should not load the package here but `::`
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
skip_if_not(doconv::msoffice_available())
skip_if_not(pandoc_version() >= numeric_version("2"))
skip_if_not_installed("webshot2")

library(rmarkdown)
library(xml2)
library(officer)

init_flextable_defaults()

test_that("rmarkdown caption", {
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-merge.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check merge operations")

library(utils)
library(xml2)

test_that("identical values within columns are merged", {
dummy_df <- data.frame(values = rep(letters[1:3], each = 2), stringsAsFactors = FALSE)
ft <- flextable(dummy_df)
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-new-rows.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check dim and new rows")

library(officer)


test_that("nrow_part or ncol_keys checks", {
expect_error(nrow_part(12))
expect_error(ncol_keys(12))
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-padding.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check paddings")

library(utils)
library(xml2)

test_that("padding overwrite all paddings", {
ft <- flextable(data.frame(a = c("", ""), stringsAsFactors = FALSE))
ft <- padding(ft, padding = 5)
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-pptx-tables.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
context("ppt table structure")

library(utils)
library(xml2)
library(officer)

test_that("row height is valid", {
ft <- flextable(head(iris))
pptx_file <- "test.pptx"
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-proc-freq.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check proc_freq")

library(utils)
library(xml2)

p <- structure(list(
lengths = c(9894L, 104L, 1L, 1L),
values = c("No", "Yes", NA, NA)
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-rotations.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
context("check rotations")

library(officer)
library(xml2)


dat <- data.frame(
a = c("left-top", "left-middle", "left-bottom"),
b = c("center-top", "center-middle", "center-bottom"),
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-styles.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check formatting")

library(xml2)
library(officer)

test_that("shortcut functions", {
ft <- flextable(head(mtcars, n = 2))

Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-text.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
context("check cells text")

library(xml2)
library(officer)

ft1 <- flextable(data.frame(a = "1 < 3", stringsAsFactors = FALSE))

get_xml_doc <- function(tab, main_folder = "docx_folder") {
Expand Down