66 lines
1.7 KiB
R
66 lines
1.7 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)))
|
|
})
|
|
|
|
|
|
context("Test Utilities - Pretty Count")
|
|
|
|
|
|
|