Skip to content

Commit

Permalink
tests: renew test-df_printer
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgohel committed Mar 21, 2024
1 parent 708f302 commit f008367
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
35 changes: 35 additions & 0 deletions tests/testthat/rmd/use-printer.Rmd
Original file line number Diff line number Diff line change
@@ -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
```
31 changes: 29 additions & 2 deletions tests/testthat/test-df_printer.R
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit f008367

Please sign in to comment.