From 07e7df2e71bd0cf70a8ab3199971d6bd3d254896 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Feb 2024 15:08:12 +0100 Subject: [PATCH] add tests for markdown print --- tests/testthat/_snaps/data_tabulate.md | 73 ++++++++++++++++++++++++++ tests/testthat/test-data_tabulate.R | 15 ++++++ 2 files changed, 88 insertions(+) diff --git a/tests/testthat/_snaps/data_tabulate.md b/tests/testthat/_snaps/data_tabulate.md index 2564735ce..a7af67dbf 100644 --- a/tests/testthat/_snaps/data_tabulate.md +++ b/tests/testthat/_snaps/data_tabulate.md @@ -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] "| | 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] "| | 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" + diff --git a/tests/testthat/test-data_tabulate.R b/tests/testthat/test-data_tabulate.R index 9a24252b0..30b17db8d 100644 --- a/tests/testthat/test-data_tabulate.R +++ b/tests/testthat/test-data_tabulate.R @@ -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 +})