From f008367c8142f183880d48b263ee33b006853d17 Mon Sep 17 00:00:00 2001 From: David Gohel Date: Fri, 22 Mar 2024 00:27:31 +0100 Subject: [PATCH] tests: renew test-df_printer --- DESCRIPTION | 2 +- tests/testthat/rmd/use-printer.Rmd | 35 ++++++++++++++++++++++++++++++ tests/testthat/test-df_printer.R | 31 ++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tests/testthat/rmd/use-printer.Rmd diff --git a/DESCRIPTION b/DESCRIPTION index 760522c7..5e51b010 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: flextable Type: Package Title: Functions for Tabular Reporting -Version: 0.9.6.001 +Version: 0.9.6.002 Authors@R: c( person("David", "Gohel", role = c("aut", "cre"), email = "david.gohel@ardata.fr"), diff --git a/tests/testthat/rmd/use-printer.Rmd b/tests/testthat/rmd/use-printer.Rmd new file mode 100644 index 00000000..3731b231 --- /dev/null +++ b/tests/testthat/rmd/use-printer.Rmd @@ -0,0 +1,35 @@ +--- +title: "use printers" +--- + +```{r setup, include=FALSE} +library(knitr) +opts_chunk$set(echo = FALSE) +library(flextable) +use_model_printer() +use_df_printer() +``` + +# a model + +```{r} +clotting <- data.frame( + u = c(5,10,15,20,30,40,60,80,100), + lot1 = c(118,58,42,35,27,25,21,19,18), + lot2 = c(69,35,26,21,18,16,13,12,12)) +model <- glm(lot1 ~ log(u), data = clotting, family = Gamma) +model +``` + +# no stars + +```{r} +options(show.signif.stars = FALSE) +model +``` + +## example 2 + +```{r} +airquality +``` diff --git a/tests/testthat/test-df_printer.R b/tests/testthat/test-df_printer.R index b25feac6..b5a242a0 100644 --- a/tests/testthat/test-df_printer.R +++ b/tests/testthat/test-df_printer.R @@ -1,6 +1,33 @@ context("df_printer and utilities") test_that("use_model_printer and use_df_printer works", { - expect_silent(use_model_printer()) - expect_silent(use_df_printer()) + rmd_file <- tempfile(fileext = ".Rmd") + file.copy("rmd/use-printer.Rmd", rmd_file) + outfile <- tempfile(fileext = ".html") + rmarkdown::render(rmd_file, + output_file = outfile, output_format = "html_document", + envir = new.env(), quiet = TRUE + ) + + doc <- read_html(outfile) + table_nodes <- xml_find_all(doc, "body//*/table") + testthat::expect_length(table_nodes, n = 3) + + # check last table that should be a summary table + table_node_3 <- table_nodes[[3]] + expect_length(xml_children(xml_child(table_node_3, search = "/thead")), 2) + expect_length(xml_children(xml_child(table_node_3, search = "/tbody")), 10) + + tfoot_content <- xml_child(table_node_3, search = "/tfoot") + expect_length(xml_children(tfoot_content), 1) + expect_equal(xml_text(tfoot_content), "n: 153") + + # check first table and last column should have stars + first_tr <- xml_find_first(doc, "//table/tbody/tr") + expect_length(xml_children(first_tr), 6) + expect_equal(xml_text(xml_child(first_tr, 6)), "***") + + # check second table has 5 columns + first_tr <- xml_find_first(doc, "//*[@id='no-stars']//table/tbody/tr") + expect_length(xml_children(first_tr), 5) })