Skip to content

Commit

Permalink
Initial commit using testthat(3e) snapshot feature to unit test the T…
Browse files Browse the repository at this point in the history
…plyr helper function nest_rowlabels() in pilot3utils.
  • Loading branch information
Robert Devine committed Feb 20, 2024
1 parent df6246b commit fdf1446
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Suggests:
fs,
ggplot2,
haven,
testthat,
testthat (>= 3.0.0),
knitr,
pharmaRTF,
r2rtf,
Expand All @@ -37,3 +37,4 @@ Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Config/testthat/edition: 3
24 changes: 24 additions & 0 deletions tests/testthat/_snaps/Tplyr_helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# nest_rowlabels works

Code
nest_rowlabels(.data)
Output
# A tibble: 12 x 7
row_label var1_placebo var1_x~1 var1_~2 ord_l~3 ord_l~4 ord_l~5
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 Baseline "" "" "" 1 0 0
2 n "79" "74" "81" 1 1 1
3 Mean (SD) "24.1 (12.19)" "21.3 (~ "24.4 ~ 1 1 2
4 Median (Range) "21.0 ( 5;61)" "18.0 (~ "21.0 ~ 1 1 3
5 Week 24 "" "" "" 2 0 0
6 n "79" "74" "81" 2 1 1
7 Mean (SD) "26.7 (13.79)" "22.8 (~ "26.4 ~ 2 1 2
8 Median (Range) "24.0 ( 5;62)" "20.0 (~ "25.0 ~ 2 1 3
9 Change from Baseline "" "" "" 3 0 0
10 n "79" "74" "81" 3 1 1
11 Mean (SD) " 2.5 ( 5.80)" " 1.5 (~ " 2.0 ~ 3 1 2
12 Median (Range) " 2.0 (-11;16)" " 1.0 (~ " 2.0 ~ 3 1 3
# ... with abbreviated variable names 1: var1_xanomeline_high_dose,
# 2: var1_xanomeline_low_dose, 3: ord_layer_index, 4: ord_layer_1,
# 5: ord_layer_2

37 changes: 37 additions & 0 deletions tests/testthat/test-Tplyr_helpers.r
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,40 @@ test_that("Pilot3 can use the testthat 3e", {
local_edition(3)
expect_true(TRUE)
})

test_that("nest_rowlabels works", {
.data <- tibble::tribble(
~row_label1, ~row_label2, ~var1_placebo, ~var1_xanomeline_high_dose, ~var1_xanomeline_low_dose, ~ord_layer_index, ~ord_layer_1, ~ord_layer_2,
"Baseline", "n", "79", "74", "81", 1, 1, 1,
"Baseline", "Mean (SD)", "24.1 (12.19)", "21.3 (11.74)", "24.4 (12.92)", 1, 1, 2,
"Baseline", "Median (Range)", "21.0 ( 5;61)", "18.0 ( 3;57)", "21.0 ( 5;57)", 1, 1, 3,
"Week 24", "n", "79", "74", "81", 2, 1, 1,
"Week 24", "Mean (SD)", "26.7 (13.79)", "22.8 (12.48)", "26.4 (13.18)", 2, 1, 2,
"Week 24", "Median (Range)", "24.0 ( 5;62)", "20.0 ( 3;62)", "25.0 ( 6;62)", 2, 1, 3,
"Change from Baseline", "n", "79", "74", "81", 3, 1, 1,
"Change from Baseline", "Mean (SD)", " 2.5 ( 5.80)", " 1.5 ( 4.26)", " 2.0 ( 5.55)", 3, 1, 2,
"Change from Baseline", "Median (Range)", " 2.0 (-11;16)", " 1.0 ( -7;13)", " 2.0 (-11;17)", 3, 1, 3
)
.nest_rowlabels <- tibble::tribble(
~row_label, ~var1_placebo, ~var1_xanomeline_high_dose, ~var1_xanomeline_low_dose, ~ord_layer_index, ~ord_layer_1, ~ord_layer_2,
"Baseline", "", "", "", 1, 0, 0,
"n", "79", "74", "81", 1, 1, 1,
"Mean (SD)", "24.1 (12.19)", "21.3 (11.74)", "24.4 (12.92)", 1, 1, 2,
"Median (Range)", "21.0 ( 5;61)", "18.0 ( 3;57)", "21.0 ( 5;57)", 1, 1, 3,
"Week 24", "", "", "", 2, 0, 0,
"n", "79", "74", "81", 2, 1, 1,
"Mean (SD)", "26.7 (13.79)", "22.8 (12.48)", "26.4 (13.18)", 2, 1, 2,
"Median (Range)", "24.0 ( 5;62)", "20.0 ( 3;62)", "25.0 ( 6;62)", 2, 1, 3,
"Change from Baseline", "", "", "", 3, 0, 0,
"n", "79", "74", "81", 3, 1, 1,
"Mean (SD)", " 2.5 ( 5.80)", " 1.5 ( 4.26)", " 2.0 ( 5.55)", 3, 1, 2,
"Median (Range)", " 2.0 (-11;16)", " 1.0 ( -7;13)", " 2.0 (-11;17)", 3, 1, 3
)
expect_equal(nrow(.data), 9)
expect_equal(nrow(.nest_rowlabels), 12)
expect_equal(nrow(nest_rowlabels(.data)), 12)
expect_s3_class(nest_rowlabels(.data), "tbl_df")
expect_snapshot(nest_rowlabels(.data), dplyr::pull(.nest_rowlabels))
expect_length(nrow(.data), 1)
expect_length(nrow(.nest_rowlabels), 1)
})

0 comments on commit fdf1446

Please sign in to comment.