diff --git a/tests/testthat/test-add_n.tbl_survfit.R b/tests/testthat/test-add_n.tbl_survfit.R new file mode 100644 index 000000000..890886087 --- /dev/null +++ b/tests/testthat/test-add_n.tbl_survfit.R @@ -0,0 +1,35 @@ +test_that("add_n.tbl_survfit() works", { + # add_n.tbl_survfit works + fit1 <- survival::survfit(survival::Surv(ttdeath, death) ~ 1, trial) + fit2 <- survival::survfit(survival::Surv(ttdeath, death) ~ grade, trial) + + expect_silent( + res1 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) |> + add_n() + ) + expect_equal( + as.data.frame(res1, col_label = FALSE)$N, + c("200", "200", NA, NA, NA) + ) + + + # add_n.tbl_survfit does not accept additional arguments + expect_error( + res2 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) |> + add_n(statistic = "{N_nonmiss} / {N_obs}"), + regexp = "`...` must be empty" + ) + + # mess with the call object, trigger `safe_survfit_eval` function + trial2 <- NA + fit1$call$data <- trial2 + + expect_error( + res3 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) |> + add_n(), + regexp = "error may be a due to the construction of the original" + ) +}) diff --git a/tests/testthat/test-add_nevent.tbl_survfit.R b/tests/testthat/test-add_nevent.tbl_survfit.R new file mode 100644 index 000000000..78d5aa5a8 --- /dev/null +++ b/tests/testthat/test-add_nevent.tbl_survfit.R @@ -0,0 +1,48 @@ +test_that("add_n.tbl_survfit() works", { + tbl <- trial |> + tbl_survfit( + include = trt, + y = "Surv(ttdeath, death)", + times = 12 + ) + + # total N events added to table is accurate + expect_silent( + res <- tbl |> add_nevent() + ) + + expect_equal( + as.data.frame(res, col_label = FALSE)$nevent, + c("112", NA, NA) + ) + + # stacked fits work + fit1 <- survival::survfit(survival::Surv(ttdeath, death) ~ 1, trial) + fit2 <- survival::survfit(survival::Surv(ttdeath, death) ~ trt, trial) + + expect_silent( + res1 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) |> + add_nevent() + ) + expect_equal( + as.data.frame(res1, col_label = FALSE)$nevent, + c("112", "112", NA, NA) + ) + + + # add_nevent.tbl_survfit does not accept additional arguments (yet) + expect_error( + res2 <- tbl |> add_nevent(location = "level"), + regexp = "`...` must be empty" + ) + + # mess with the tbl_survfit object to trigger error + res3 <- list(fit1, fit2) |> + tbl_survfit(times = c(12, 24)) + class(res3$inputs$x[[1]]) <- NULL + expect_error( + res3 |> add_nevent(), + regexp = "objects must have class" + ) +}) diff --git a/tests/testthat/test-add_p.tbl_survfit.R b/tests/testthat/test-add_p.tbl_survfit.R new file mode 100644 index 000000000..d2f76aec9 --- /dev/null +++ b/tests/testthat/test-add_p.tbl_survfit.R @@ -0,0 +1,14 @@ +test_that("add_p.tbl_survfit() works", { + tbl <- trial |> + tbl_survfit( + include = trt, + y = "Surv(ttdeath, death)", + times = 12 + ) + + # total N added to table is accurate + expect_error( + res <- tbl |> add_p(), + NA + ) +})