Skip to content

Commit

Permalink
adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Sep 2, 2024
1 parent a3c339e commit b5a8b17
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/testthat/_snaps/add_stat.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,38 @@
Error in `add_stat()`:
! The `x` argument must be class <tbl_summary/tbl_svysummary/tbl_continuous>, not a data frame.

# add_stat() with curly braces in errors/warnings

Code
curly_warning <- (function(x, ...) {
warning("{curly} warning")
10
})
as.data.frame(add_stat(tbl, fns = ~curly_warning))
Message
There was a warning for variable "age"
! {curly} warning
There was a warning for variable "marker"
! {curly} warning
Output
**Characteristic** **Drug A** \nN = 98 **Drug B** \nN = 102 add_stat_1
1 Age 46 (37, 60) 48 (39, 56) 10.0
2 Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21) 10.0

---

Code
curly_error <- (function(x, ...) {
stop("{curly} error")
})
as.data.frame(add_stat(tbl, fns = ~curly_error))
Message
There was a error for variable "age"
x {curly} error
There was a error for variable "marker"
x {curly} error
Output
**Characteristic** **Drug A** \nN = 98 **Drug B** \nN = 102 add_stat_1
1 Age 46 (37, 60) 48 (39, 56) <NA>
2 Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21) <NA>

18 changes: 18 additions & 0 deletions tests/testthat/test-add_stat.R
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,22 @@ test_that("add_stat() with tbl_svysummary()", {
)
})

test_that("add_stat() with curly braces in errors/warnings", {
expect_snapshot({
curly_warning <- \(x, ...) {
warning("{curly} warning"); 10
}
tbl |>
add_stat(fns = ~ curly_warning) |>
as.data.frame()
})

expect_snapshot({
curly_error <- \(x, ...) {
stop("{curly} error")
}
tbl |>
add_stat(fns = ~ curly_error) |>
as.data.frame()
})
})

0 comments on commit b5a8b17

Please sign in to comment.