Files
civilyticsR/tests/testthat/test_utils.R
T
jared 2f1cdcde52
R-CMD-check / R CMD check (pull_request) Failing after 4m43s
Fix issues #1, #3, #5: misc improvements
- #5: Unwrap _brand.yml for Quarto 1.9+ compatibility (remove top-level
  brand: wrapper so meta/logo/color/typography are at the top level)
- #1: Add quiet parameter to na_sum() to suppress warnings in loops/pipelines
- #3: Add beep() function for CLI beep, desktop notifications, and webhook
  alerts (supports type=beep|notify|webhook|all)
2026-06-03 19:01:30 +00:00

72 lines
1.9 KiB
R

# Test utils
context("Test Utilities - Safe Max")
test_that("safe_max excludes NAs", {
samp_a <- c(1:10, NA)
expect_equal(safe_max(samp_a), 10)
expect_equal(safe_max(samp_a), max(samp_a, na.rm = TRUE))
expect_equal(safe_max(c("cat", "box", "dog")), "dog")
expect_equal(safe_max(c("cat", "box", "dog")), max(c("cat", "box", "dog"), na.rm = TRUE))
})
test_that("safe_max handles all NAs", {
expect_equal(safe_max(rep(NA, 100)), NA)
expect_equal(safe_max(NULL), NA)
})
context("Test Utilities - Pretty Per")
test_that("pretty_per respects rounding", {
expect_identical(pretty_per(0.2), "20.0%")
expect_identical(pretty_per(0.2432522), "24.3%")
expect_identical(pretty_per(0.2432522, ndigit = 3), "24.325%")
})
test_that("pretty_per handles NAs", {
expect_equivalent(pretty_per(rep(NA, 5)), c("-", "-", "-", "-", "-"))
})
test_that("pretty_per deals with large numbers", {
expect_message(pretty_per(c(0.2, 0.5, 0.1, 110)))
})
context("Test Utilities - NA Zero")
test_that("Function works when no NAs present", {
expect_equivalent(na_zero(1:10), 1:10)
expect_equivalent(na_zero(letters), letters)
})
test_that("Function subs out NAs in numeric vectors with 0", {
expect_equivalent(na_zero(c(1:10, NA)), c(1:10, 0))
})
# Test that na_sum works
test_that("NA Sum takes sum setting NA values to 0", {
expect_equivalent(na_sum(c(1:10, NA)), sum(1:10, 0))
expect_message(na_sum(c(1:10, NA)), "Taking a sum with missing values equal to 0, be careful!")
})
test_that("na_sum fails with non-numerics", {
expect_error(na_sum(LETTERS))
expect_error(na_sum(as.factor(1:10)))
})
test_that("na_sum quiet=TRUE suppresses the message", {
expect_message(na_sum(c(1:10, NA)), "Taking a sum")
expect_silent(na_sum(c(1:10, NA), quiet = TRUE))
expect_equal(na_sum(c(1:10, NA), quiet = TRUE), 55)
})
context("Test Utilities - Pretty Count")