From 607c051d483ae93a3d93a4ba9b7082bd529753ab Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Wed, 3 Jan 2024 20:22:04 -0800 Subject: [PATCH] more unit tests for ARD column names (#101) **What changes are proposed in this pull request?** **Reference GitHub issue associated with pull request.** _e.g., 'closes #1'_ #96 -------------------------------------------------------------------------------- Reviewer Checklist (if item does not apply, mark is as complete) - [ ] Ensure all package dependencies are installed: `devtools::install_dev_deps()` - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` - [ ] `usethis::use_spell_check()` runs with no spelling errors in documentation When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cards (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] Increment the version number using `usethis::use_version(which = "dev")` - [ ] Run `usethis::use_spell_check()` again - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge". --- tests/testthat/test-ard_categorical.R | 40 +++++++++++++++++++++++++++ tests/testthat/test-ard_continuous.R | 34 +++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/tests/testthat/test-ard_categorical.R b/tests/testthat/test-ard_categorical.R index fcf9614d5..a93a2dc30 100644 --- a/tests/testthat/test-ard_categorical.R +++ b/tests/testthat/test-ard_categorical.R @@ -255,3 +255,43 @@ test_that("ard_categorical() messaging", { error = TRUE ) }) + +test_that("ard_continuous() and ARD column names", { + ard_colnames <- c("group1", "group1_level", "variable", "variable_level", + "context", "stat_name", "stat_label", "statistic", + "statistic_fmt_fn", "warning", "error") + + # no errors when these variables are the summary vars + expect_error({ + lapply( + ard_colnames, + function(var) { + df <- mtcars[c("am", "mpg")] + names(df) <- c("am", var) + ard_categorical( + data = df, + by = "am", + variables = all_of(var) + ) + } + )}, + NA + ) + + # no errors when these vars are the by var + expect_error({ + lapply( + ard_colnames, + function(byvar) { + df <- mtcars[c("am", "cyl")] + names(df) <- c(byvar, "cyl") + ard_continuous( + data = df, + by = all_of(byvar), + variables = "cyl" + ) + } + )}, + NA + ) +}) diff --git a/tests/testthat/test-ard_continuous.R b/tests/testthat/test-ard_continuous.R index 73e3a2739..e00f15239 100644 --- a/tests/testthat/test-ard_continuous.R +++ b/tests/testthat/test-ard_continuous.R @@ -156,6 +156,40 @@ test_that("ard_continuous(stat_labels) argument works", { dplyr::select(variable, stat_name, stat_label) expect_equal(ard1, ard2) +}) + +test_that("ard_continuous() and ARD column names", { + ard_colnames <- c("group1", "group1_level", "variable", "variable_level", + "context", "stat_name", "stat_label", "statistic", + "statistic_fmt_fn", "warning", "error") + # no errors when these variables are the summary vars + expect_error({ + df <- mtcars + names(df) <- ard_colnames + ard_continuous( + data = cbind(mtcars["am"], df), + variables = all_of(ard_colnames), + by = "am" + )}, + NA + ) + # no errors when these vars are the by var + expect_error({ + lapply( + ard_colnames, + function(byvar) { + df <- mtcars[c("am", "mpg")] + names(df) <- c(byvar, "mpg") + ard_continuous( + data = df, + by = all_of(byvar), + variables = "mpg" + ) + } + )}, + NA + ) }) +