# Tests for standalone functional helpers (as_mapper, map, map2, pmap, walk, imap, pluck) # These are internal functions accessed via ::: # ============================================================ # as_mapper # ============================================================ test_that("as_mapper passes functions through unchanged", { fn <- function(x) x^2 f <- tidyOhdsiSolutions:::as_mapper(fn) expect_identical(f, fn) expect_equal(f(4), 16) }) test_that("as_mapper converts one-argument formula with .x", { f <- tidyOhdsiSolutions:::as_mapper(~ .x + 10) expect_equal(f(5), 15) expect_equal(f(-3), 7) }) test_that("as_mapper converts two-argument formula with .x and .y", { f <- tidyOhdsiSolutions:::as_mapper(~ .x * .y) expect_equal(f(3, 4), 12) }) test_that("as_mapper supports ..1, ..2 positional arguments", { f <- tidyOhdsiSolutions:::as_mapper(~ ..1 + ..2) expect_equal(f(10, 20), 30) }) # ============================================================ # map # ============================================================ test_that("map applies function and returns a list", { result <- tidyOhdsiSolutions:::map(1:3, ~ .x * 2) expect_type(result, "list") expect_equal(result, list(2, 4, 6)) }) test_that("map works with named input and preserves names", { result <- tidyOhdsiSolutions:::map(c(a = 1, b = 2), ~ .x + 1) expect_named(result, c("a", "b")) }) test_that("map_chr returns character vector", { result <- tidyOhdsiSolutions:::map_chr(c("hello", "world"), toupper) expect_type(result, "character") expect_equal(unname(result), c("HELLO", "WORLD")) }) test_that("map_dbl returns double vector", { result <- tidyOhdsiSolutions:::map_dbl(1:3, ~ .x * 1.5) expect_type(result, "double") expect_equal(result, c(1.5, 3.0, 4.5)) }) test_that("map_int returns integer vector", { result <- tidyOhdsiSolutions:::map_int(1:3, ~ .x + 1L) expect_type(result, "integer") expect_equal(result, 2:4) }) test_that("map_lgl returns logical vector", { result <- tidyOhdsiSolutions:::map_lgl(c(0, 1, 0), as.logical) expect_type(result, "logical") expect_equal(result, c(FALSE, TRUE, FALSE)) }) test_that("map_dfr row-binds results into a data frame", { result <- tidyOhdsiSolutions:::map_dfr( list(list(x = 1), list(x = 2)), ~ data.frame(x = .x$x) ) expect_s3_class(result, "data.frame") expect_equal(nrow(result), 2L) }) test_that("map_dfc col-binds results into a data frame", { result <- tidyOhdsiSolutions:::map_dfc( list(a = 1:2, b = 3:4), ~ as.data.frame(.x) ) expect_s3_class(result, "data.frame") expect_equal(ncol(result), 2L) }) test_that("map passes extra ... arguments to .f", { result <- tidyOhdsiSolutions:::map(1:3, `+`, 10) expect_equal(result, list(11, 12, 13)) }) # ============================================================ # map2 # ============================================================ test_that("map2 applies function to parallel element pairs", { result <- tidyOhdsiSolutions:::map2(1:3, 4:6, ~ .x + .y) expect_equal(result, list(5, 7, 9)) }) test_that("map2_chr returns character vector", { result <- tidyOhdsiSolutions:::map2_chr(c("a", "b"), c("1", "2"), paste0) expect_equal(unname(result), c("a1", "b2")) }) test_that("map2_dbl returns double vector", { result <- tidyOhdsiSolutions:::map2_dbl(c(1, 2), c(3, 4), ~ .x * .y) expect_equal(result, c(3, 8)) }) test_that("map2_int returns integer vector", { result <- tidyOhdsiSolutions:::map2_int(1:2, 3:4, `+`) expect_type(result, "integer") expect_equal(result, c(4L, 6L)) }) test_that("map2_lgl returns logical vector", { result <- tidyOhdsiSolutions:::map2_lgl(c(1, 0), c(0, 0), ~ .x > .y) expect_type(result, "logical") expect_equal(result, c(TRUE, FALSE)) }) test_that("map2_dfr row-binds results", { result <- tidyOhdsiSolutions:::map2_dfr( list(1, 2), list(3, 4), ~ data.frame(sum = .x + .y) ) expect_s3_class(result, "data.frame") expect_equal(nrow(result), 2L) }) # ============================================================ # pmap # ============================================================ test_that("pmap applies function across multiple parallel lists", { result <- tidyOhdsiSolutions:::pmap(list(a = 1:3, b = 4:6), function(a, b) a + b) expect_equal(result, list(5, 7, 9)) }) test_that("pmap_chr returns character vector", { result <- tidyOhdsiSolutions:::pmap_chr( list(c("x", "y"), c("1", "2")), paste0 ) expect_equal(result, c("x1", "y2")) }) test_that("pmap_dbl returns double vector", { result <- tidyOhdsiSolutions:::pmap_dbl(list(c(1.5, 2.5), c(0.5, 0.5)), `+`) expect_equal(result, c(2.0, 3.0)) }) test_that("pmap_int returns integer vector", { result <- tidyOhdsiSolutions:::pmap_int(list(c(1L, 2L), c(3L, 4L)), `+`) expect_type(result, "integer") }) test_that("pmap_lgl returns logical vector", { result <- tidyOhdsiSolutions:::pmap_lgl(list(c(TRUE, FALSE), c(FALSE, FALSE)), `&`) expect_type(result, "logical") expect_equal(result, c(FALSE, FALSE)) }) test_that("pmap_dfr row-binds results", { result <- tidyOhdsiSolutions:::pmap_dfr( list(x = c(1, 2), y = c(3, 4)), function(x, y) data.frame(total = x + y) ) expect_s3_class(result, "data.frame") expect_equal(nrow(result), 2L) }) # ============================================================ # walk # ============================================================ test_that("walk calls .f for side effects and returns .x invisibly", { seen <- c() result <- tidyOhdsiSolutions:::walk(1:3, function(x) { seen <<- c(seen, x) }) expect_equal(seen, 1:3) expect_equal(result, 1:3) }) test_that("walk2 calls .f for side effects and returns .x invisibly", { labels <- c() result <- tidyOhdsiSolutions:::walk2(c("a", "b"), 1:2, function(x, y) { labels <<- c(labels, paste(x, y)) }) expect_equal(labels, c("a 1", "b 2")) expect_equal(result, c("a", "b")) }) test_that("pwalk calls .f for side effects and returns .l invisibly", { totals <- c() l <- list(x = 1:2, y = 3:4) result <- tidyOhdsiSolutions:::pwalk(l, function(x, y) { totals <<- c(totals, x + y) }) expect_equal(totals, c(4, 6)) expect_identical(result, l) }) # ============================================================ # imap # ============================================================ test_that("imap provides element name as second argument", { x <- list(a = 1, b = 2) result <- tidyOhdsiSolutions:::imap(x, ~ paste(.y, "=", .x)) expect_equal(result, list(a = "a = 1", b = "b = 2")) }) test_that("imap provides integer index when input is unnamed", { result <- tidyOhdsiSolutions:::imap(c(10, 20, 30), ~ .y) expect_equal(result, list(1, 2, 3)) }) test_that("imap_chr returns character vector", { result <- tidyOhdsiSolutions:::imap_chr(c(a = "x", b = "y"), ~ paste(.y, .x)) expect_equal(unname(result), c("a x", "b y")) }) test_that("imap_dbl returns numeric vector", { result <- tidyOhdsiSolutions:::imap_dbl(c(10, 20, 30), ~ .x + .y) expect_equal(result, c(11, 22, 33)) }) test_that("imap_dfr row-binds results", { result <- tidyOhdsiSolutions:::imap_dfr(c(a = 1, b = 2), function(v, nm) { data.frame(name = nm, value = v) }) expect_s3_class(result, "data.frame") expect_equal(nrow(result), 2L) }) # ============================================================ # pluck # ============================================================ test_that("pluck retrieves a top-level element by name", { x <- list(a = 42, b = "hello") expect_equal(tidyOhdsiSolutions:::pluck(x, "a"), 42) }) test_that("pluck retrieves nested elements by name chain", { x <- list(a = list(b = list(c = 99))) expect_equal(tidyOhdsiSolutions:::pluck(x, "a", "b", "c"), 99) }) test_that("pluck retrieves elements by integer index", { x <- list(10, 20, 30) expect_equal(tidyOhdsiSolutions:::pluck(x, 2), 20) }) test_that("pluck returns NULL on missing name", { x <- list(a = 1) expect_null(tidyOhdsiSolutions:::pluck(x, "z")) }) test_that("pluck returns .default on missing name", { x <- list(a = 1) expect_equal(tidyOhdsiSolutions:::pluck(x, "z", .default = -1), -1) }) test_that("pluck returns .default on out-of-bounds index", { x <- list(1, 2) expect_null(tidyOhdsiSolutions:::pluck(x, 5)) expect_equal(tidyOhdsiSolutions:::pluck(x, 5, .default = 0), 0) }) test_that("pluck returns .x itself when no accessors given", { x <- list(a = 1, b = 2) expect_identical(tidyOhdsiSolutions:::pluck(x), x) }) test_that("pluck returns .default when intermediate node is NULL", { x <- list(a = NULL) expect_equal(tidyOhdsiSolutions:::pluck(x, "a", "b", .default = "gone"), "gone") }) test_that("pluck supports function accessor", { x <- list(1, 2, 3) result <- tidyOhdsiSolutions:::pluck(x, function(v) v[[2]]) expect_equal(result, 2) }) test_that("pluck errors on invalid accessor type", { expect_error(tidyOhdsiSolutions:::pluck(list(1), TRUE)) })