# ---------- as_mapper ---------- test_that("as_mapper returns functions unchanged", { f <- function(x) x + 1 expect_identical(as_mapper(f), f) }) test_that("as_mapper converts one-sided formulas to functions", { f <- as_mapper(~ .x + 1) expect_equal(f(5), 6) g <- as_mapper(~ .x + .y) expect_equal(g(2, 3), 5) h <- as_mapper(~ .x + .y + .z) expect_equal(h(1, 2, 3), 6) }) test_that("as_mapper converts atomic vectors to extractor functions", { f <- as_mapper("a") expect_true(is.function(f)) expect_equal(f(list(a = 42)), 42) g <- as_mapper(2L) expect_true(is.function(g)) expect_equal(g(list("x", "y", "z")), "y") }) # ---------- map ---------- test_that("map returns a list applying function to each element", { expect_equal(map(1:3, function(x) x * 2), list(2, 4, 6)) }) test_that("map supports formula shorthand", { expect_equal(map(1:3, ~ .x * 2), list(2, 4, 6)) }) test_that("map passes extra arguments", { expect_equal(map(list(1:3, 4:6), sum, na.rm = TRUE), list(6L, 15L)) }) # ---------- map_chr ---------- test_that("map_chr returns character vector", { result <- map_chr(letters[1:3], toupper) expect_equal(unname(result), c("A", "B", "C")) }) test_that("map_chr works with formula", { result <- map_chr(1:3, ~ paste0("x", .x)) expect_identical(result, c("x1", "x2", "x3")) }) # ---------- map_dbl ---------- test_that("map_dbl returns numeric vector", { result <- map_dbl(1:3, ~ .x^2) expect_equal(result, c(1, 4, 9)) }) # ---------- map_int ---------- test_that("map_int returns integer vector", { result <- map_int(list(1:3, 4:6), length) expect_identical(result, c(3L, 3L)) }) # ---------- map_lgl ---------- test_that("map_lgl returns logical vector", { result <- map_lgl(1:3, ~ .x > 1) expect_identical(result, c(FALSE, TRUE, TRUE)) }) # ---------- map_dfr ---------- test_that("map_dfr row-binds data.frame results", { result <- map_dfr(1:2, ~ data.frame(x = .x, y = .x * 10)) expect_equal(result, data.frame(x = 1:2, y = c(10, 20))) }) # ---------- map_dfc ---------- test_that("map_dfc column-binds data.frame results", { result <- map_dfc(list(a = 1:2, b = 3:4), ~ data.frame(val = .x)) expect_equal(ncol(result), 2) expect_equal(nrow(result), 2) }) # ---------- map2 ---------- test_that("map2 iterates over two inputs in parallel", { expect_equal(map2(1:3, 4:6, ~ .x + .y), list(5, 7, 9)) }) test_that("map2 passes extra arguments", { expect_equal( map2(list(c(1, NA)), list(c(2, 3)), function(x, y, na.rm) sum(x, y, na.rm = na.rm), na.rm = TRUE), list(6) ) }) # ---------- map2_chr ---------- test_that("map2_chr returns character vector", { result <- map2_chr(letters[1:2], 1:2, ~ paste0(.x, .y)) expect_equal(unname(result), c("a1", "b2")) }) # ---------- map2_dbl ---------- test_that("map2_dbl returns numeric vector", { result <- map2_dbl(1:3, 4:6, `+`) expect_equal(result, c(5, 7, 9)) }) # ---------- map2_int ---------- test_that("map2_int returns integer vector", { result <- map2_int(1:3, 4:6, ~ as.integer(.x + .y)) expect_identical(result, c(5L, 7L, 9L)) }) # ---------- map2_lgl ---------- test_that("map2_lgl returns logical vector", { result <- map2_lgl(1:3, 2:4, ~ .x < .y) expect_identical(result, c(TRUE, TRUE, TRUE)) }) # ---------- map2_dfr ---------- test_that("map2_dfr row-binds data.frame results", { result <- map2_dfr(1:2, 3:4, ~ data.frame(a = .x, b = .y)) expect_equal(result, data.frame(a = 1:2, b = 3:4)) }) # ---------- map2_dfc ---------- test_that("map2_dfc column-binds data.frame results", { result <- map2_dfc(list(1:2), list(3:4), ~ data.frame(x = .x, y = .y)) expect_equal(ncol(result), 2) }) # ---------- pmap ---------- test_that("pmap iterates over multiple lists in parallel", { result <- pmap(list(1:3, 4:6, 7:9), function(a, b, c) a + b + c) expect_equal(result, list(12, 15, 18)) }) test_that("pmap works with named list matching function args", { result <- pmap(list(a = 1:2, b = 3:4), function(a, b) a * b) expect_equal(result, list(3, 8)) }) # ---------- pmap_chr ---------- test_that("pmap_chr returns character vector", { result <- pmap_chr(list(a = 1:2, b = 3:4), function(a, b) paste0(a, "-", b)) expect_equal(unname(result), c("1-3", "2-4")) }) # ---------- pmap_dbl ---------- test_that("pmap_dbl returns numeric vector", { result <- pmap_dbl(list(a = 1:3, b = 4:6), function(a, b) a * b) expect_equal(result, c(4, 10, 18)) }) # ---------- pmap_int ---------- test_that("pmap_int returns integer vector", { result <- pmap_int(list(a = 1:3, b = 4:6), function(a, b) as.integer(a + b)) expect_identical(unname(result), c(5L, 7L, 9L)) }) # ---------- pmap_lgl ---------- test_that("pmap_lgl returns logical vector", { result <- pmap_lgl(list(a = 1:3, b = 2:4), function(a, b) a < b) expect_identical(unname(result), c(TRUE, TRUE, TRUE)) }) # ---------- pmap_dfr ---------- test_that("pmap_dfr row-binds data.frame results", { result <- pmap_dfr(list(a = 1:2, b = 3:4), function(a, b) data.frame(sum = a + b)) expect_equal(result, data.frame(sum = c(4, 6))) }) # ---------- pmap_dfc ---------- test_that("pmap_dfc column-binds data.frame results", { result <- pmap_dfc(list(a = 1:2, b = 3:4), function(a, b) data.frame(s = a + b)) expect_equal(nrow(result), 1) expect_equal(ncol(result), 2) }) # ---------- walk ---------- test_that("walk applies side effects and returns input invisibly", { out <- character() result <- walk(letters[1:3], function(x) out[length(out) + 1] <<- x) expect_identical(result, letters[1:3]) }) test_that("walk supports formula", { acc <- integer() walk(1:3, ~ { acc[length(acc) + 1] <<- .x }) expect_identical(acc, 1:3) }) # ---------- walk2 ---------- test_that("walk2 iterates over two inputs for side effects", { acc <- character() result <- walk2(letters[1:2], 1:2, function(l, n) acc[length(acc) + 1] <<- paste0(l, n)) expect_identical(result, letters[1:2]) expect_identical(acc, c("a1", "b2")) }) # ---------- pwalk ---------- test_that("pwalk iterates over multiple lists for side effects", { acc <- numeric() input <- list(1:3, 4:6) result <- pwalk(input, function(a, b) acc[length(acc) + 1] <<- a + b) expect_identical(result, input) expect_equal(acc, c(5, 7, 9)) }) # ---------- imap ---------- test_that("imap passes index for unnamed input", { result <- imap(10:12, ~ .x + .y) expect_equal(result, list(11, 13, 15)) }) test_that("imap passes name for named input", { result <- imap(c(a = 1, b = 2), ~ paste(.y, "=", .x)) expect_equal(result, list(a = "a = 1", b = "b = 2")) }) # ---------- imap_chr ---------- test_that("imap_chr returns character vector", { result <- imap_chr(c(a = 1, b = 2), ~ paste(.y, .x)) expect_equal(unname(result), c("a 1", "b 2")) }) test_that("imap_chr uses integer index when unnamed", { result <- imap_chr(10:12, ~ paste0(.x, ":", .y)) expect_identical(result, c("10:1", "11:2", "12:3")) }) # ---------- imap_dbl ---------- test_that("imap_dbl returns numeric vector", { result <- imap_dbl(10:12, ~ .x + .y) expect_equal(result, c(11, 13, 15)) }) # ---------- imap_dfr ---------- test_that("imap_dfr row-binds data.frames", { result <- imap_dfr(c(a = 10, b = 20), ~ data.frame(name = .y, val = .x)) expect_equal(nrow(result), 2) expect_identical(result$name, c("a", "b")) }) # ---------- pluck ---------- test_that("pluck navigates nested lists", { x <- list(a = list(b = list(c = 42))) expect_equal(pluck(x, "a", "b", "c"), 42) }) test_that("pluck returns .default on missing name", { x <- list(a = 1) expect_identical(pluck(x, "z", .default = NA), NA) }) test_that("pluck returns .default on NULL intermediate", { x <- list(a = NULL) expect_equal(pluck(x, "a", "b", .default = -1), -1) }) test_that("pluck supports integer positional access", { expect_equal(pluck(list(10, 20, 30), 2), 20) }) test_that("pluck returns .default for out-of-range index", { expect_equal(pluck(list(10, 20), 5, .default = -1), -1) }) test_that("pluck supports function accessor", { x <- list(a = list(b = 16)) expect_equal(pluck(x, "a", "b", sqrt), 4) }) test_that("pluck returns .x when no accessors given", { expect_equal(pluck(42), 42) }) test_that("pluck returns .default when result is NULL", { x <- list(a = NULL) expect_equal(pluck(x, "a", .default = "missing"), "missing") }) test_that("pluck errors on invalid accessor type", { expect_error(pluck(list(1), TRUE), "Accessor must be") }) test_that("pluck with negative index returns .default", { expect_equal(pluck(list(1, 2, 3), -1, .default = "nope"), "nope") })