# ── coalesceX ───────────────────────────────────────────────────────────────── test_that("coalesceX returns first non-NA in vector", { expect_equal(coalesceX(c(NA, NA, NA, 5, 3)), 5) }) test_that("coalesceX works with multiple vectors", { expect_equal(coalesceX(c(NA, 2), c(1, NA)), c(1, 2)) }) test_that("coalesceX works with data.frame", { d <- data.frame(a = c(NA, 2), b = c(1, NA)) expect_equal(coalesceX(d), c(1, 2)) }) test_that("coalesceX method is.finite skips Inf", { expect_equal(coalesceX(c(Inf, NA, 3), c(1, 2, 4), method = "is.finite"), c(1, 2, 3)) }) test_that("coalesceX handles characters", { expect_equal(coalesceX(c(NA, "b"), c("a", NA)), c("a", "b")) }) test_that("coalesceX method is.null skips NULL elements", { res <- coalesceX(NULL, 5, 3) expect_equal(res, 5) }) test_that("coalesceX works with matrix input", { m <- matrix(c(NA, 2, 1, NA), nrow = 2) res <- coalesceX(m) expect_equal(res, c(1, 2)) }) test_that("coalesceX works with list input", { res <- coalesceX(list(c(NA, 2), c(1, NA))) expect_equal(res, c(1, 2)) }) test_that("coalesceX method is.finite skips NaN", { expect_equal( coalesceX(c(NaN, NA, 3), c(1, 2, 4), method = "is.finite"), c(1, 2, 3) ) }) test_that("coalesceX method is.null operates on whole objects", { obj <- data.frame(x = 1:2) expect_identical( coalesceX(NULL, NULL, obj, method = "is.null"), obj ) expect_identical( coalesceX(list(NULL, 7), method = "is.null"), 7 ) expect_null(coalesceX(NULL, NULL, method = "is.null")) }) test_that("coalesceX supports flatten = FALSE", { expect_identical( coalesceX(c(NA, 2), 1, flatten = FALSE), c(1, 2) ) }) test_that("coalesceX does not recycle non-scalar inputs", { expect_error( coalesceX(c(NA, 2), c(1, NA, 3, 4)) ) }) test_that("coalesceX validates method", { expect_error(coalesceX(1, method = "unknown"), "arg") })