Skip to content

Commit

Permalink
add tests for markdown print
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Feb 13, 2024
1 parent 755f7ca commit 07e7df2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/testthat/_snaps/data_tabulate.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,76 @@
Total | 1 | 2 | 0 | 3

# data_tabulate, cross tables, markdown

Code
print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "cell"))
Output
[1] "|efc$c172code | male| female| NA | Total|"
[2] "|:------------|----------:|----------:|:--------|-----:|"
[3] "|1 | 5 (5.0%)| 2 (2.0%)|1 (1.0%) | 8|"
[4] "|2 | 31 (31.0%)| 33 (33.0%)|2 (2.0%) | 66|"
[5] "|3 | 4 (4.0%)| 11 (11.0%)|1 (1.0%) | 16|"
[6] "|<NA> | 5 (5.0%)| 4 (4.0%)|1 (1.0%) | 10|"
[7] "| | | | | |"
[8] "|Total | 45| 50| 5 | 100|"
attr(,"format")
[1] "pipe"
attr(,"class")
[1] "knitr_kable" "character"

---

Code
print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "cell",
include_na = FALSE))
Output
[1] "|efc$c172code | male| female| Total|"
[2] "|:------------|----------:|----------:|-----:|"
[3] "|1 | 5 (5.8%)| 2 (2.3%)| 7|"
[4] "|2 | 31 (36.0%)| 33 (38.4%)| 64|"
[5] "|3 | 4 (4.7%)| 11 (12.8%)| 15|"
[6] "| | | | |"
[7] "|Total | 40| 46| 86|"
attr(,"format")
[1] "pipe"
attr(,"class")
[1] "knitr_kable" "character"

---

Code
print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "cell",
weights = efc$weights))
Output
[1] "|efc$c172code | male| female| NA | Total|"
[2] "|:------------|----------:|----------:|:--------|-----:|"
[3] "|1 | 5 (4.8%)| 3 (2.9%)|2 (1.9%) | 10|"
[4] "|2 | 32 (30.5%)| 32 (30.5%)|3 (2.9%) | 67|"
[5] "|3 | 3 (2.9%)| 11 (10.5%)|1 (1.0%) | 15|"
[6] "|<NA> | 8 (7.6%)| 5 (4.8%)|1 (1.0%) | 14|"
[7] "| | | | | |"
[8] "|Total | 48| 51| 7 | 105|"
attr(,"format")
[1] "pipe"
attr(,"class")
[1] "knitr_kable" "character"

---

Code
print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "cell",
include_na = FALSE, weights = efc$weights))
Output
[1] "|efc$c172code | male| female| Total|"
[2] "|:------------|----------:|----------:|-----:|"
[3] "|1 | 5 (5.8%)| 3 (3.5%)| 8|"
[4] "|2 | 32 (37.2%)| 32 (37.2%)| 64|"
[5] "|3 | 3 (3.5%)| 11 (12.8%)| 14|"
[6] "| | | | |"
[7] "|Total | 40| 46| 86|"
attr(,"format")
[1] "pipe"
attr(,"class")
[1] "knitr_kable" "character"

15 changes: 15 additions & 0 deletions tests/testthat/test-data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,18 @@ test_that("data_tabulate, cross tables, errors", {
expect_error(data_tabulate(efc, "c172code", weights = "weigths"), regex = "not found")
expect_error(data_tabulate(efc, "c172code", weights = c("e16sex", "e42dep")), regex = "length 1")
})


# markdown -------------------------

test_that("data_tabulate, cross tables, markdown", {
data(efc, package = "datawizard")
set.seed(123)
efc$weights <- abs(rnorm(n = nrow(efc), mean = 1, sd = 0.5))
efc$e16sex[sample.int(nrow(efc), 5)] <- NA

expect_snapshot(print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "cell")))
expect_snapshot(print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "cell", include_na = FALSE)))
expect_snapshot(print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "cell", weights = efc$weights)))
expect_snapshot(print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "cell", include_na = FALSE, weights = efc$weights))) # nolint
})

0 comments on commit 07e7df2

Please sign in to comment.