test_that("Simple binding", { template <- dplyr::tibble( "a" = character(0), "b" = numeric(0), "c" = integer(0), "d" = logical(0), "e" = list(), "f" = lubridate::POSIXct() ) test_data_1 <- dplyr::tibble( "a" = 1, "b" = "2.5", "c" = 2.5, "d" = 1, "e" = NA, "f" = 0 ) test_data_2 <- dplyr::tibble( "a" = "", "b" = "", "g" = "", "h" = "" ) test_data_3 <- dplyr::tibble( "b" = 1, "c" = 2, "d" = 3 ) # Assure ourselves that the input DFs won't join. expect_error(dplyr::bind_rows(test_data_1, test_data_2), class = "vctrs_error_incompatible_type") # Then test some good cases. expect_identical( bind_as_struct(template, test_data_1, test_data_2, test_data_3), dplyr::tibble( "a" = c("1", "", NA_character_), "b" = c(2.5, NA_real_, 1), "c" = c(2L, NA_integer_, 2L), "d" = c(TRUE, NA, TRUE), "e" = list(NA, NULL, NULL), "f" = c(as.POSIXct(0, tz = "UTC"), NA, NA), "g" = c(NA_character_, "", NA_character_), "h" = c(NA_character_, "", NA_character_) ) ) expect_identical( bind_as_struct(template, test_data_2, test_data_3), dplyr::tibble( "a" = c("", NA_character_), "b" = c(NA_real_, 1), "g" = c("", NA_character_), "h" = c("", NA_character_), "c" = c(NA_integer_, 2L), "d" = c(NA, TRUE) ) ) expect_identical( bind_as_struct(template, test_data_2, test_data_3, strict = TRUE), dplyr::tibble( "a" = c("", NA_character_), "b" = c(NA_real_, 1), "c" = c(NA_integer_, 2L), "d" = c(NA, TRUE), "e" = list(NULL, NULL), "f" = as.POSIXct(c(NA, NA), tz = "UTC") ) ) }) test_that("Bad Inputs", { expect_error(bind_as_struct(NULL, iris)) }) test_that("Fuzz: .list and ... always produce identical results", { set.seed(23) template <- list(a = integer(0), b = numeric(0), c = character(0)) generators <- list( function(n) sample.int(100L, n, replace = TRUE), function(n) runif(n), function(n) replicate(n, paste(sample(letters, 4, replace = TRUE), collapse = "")) ) for (i in seq_len(50)) { n_frames <- sample(2:5, 1) frames <- lapply(seq_len(n_frames), function(j) { n_rows <- sample.int(10, 1) f <- lapply(names(template), function(f) generators[[sample.int(3, 1)]](n_rows)) names(f) <- names(template) f }) expect_identical( suppressWarnings(do.call(bind_as_struct, c(list(template), frames))), suppressWarnings(bind_as_struct(template, .list = frames)) ) } }) test_that(".list Parameter", { template <- list("a" = character(0), "b" = integer(0)) frames = list(list("a" = 1, "b" = 2.5), list("a" = "x", "b" = 3L)) expect_identical( bind_as_struct(template, .list = frames), bind_as_struct(template, frames[[1]], frames[[2]]) ) }) test_that("Mutually-Exclusive Bind Inputs", { template <- list("a" = character(0)) expect_error( bind_as_struct(template, list("a" = 1), .list = list(list("a" = 2))), "only one of" ) })