1context("Performance related alternatives")
2
3# modify_list() -----------------------------------------------------------
4
5testlist <- list(
6  a = 5.5,
7  b = "x",
8  c = 1:10
9)
10testappend <- list(
11  b = "y",
12  c = NULL,
13  d = FALSE
14)
15
16test_that("modifyList is masked", {
17  expect_error(modifyList(testlist, testappend))
18})
19
20test_that("modify_list retains unreferenced elements", {
21  res <- modify_list(testlist, testappend)
22  expect_equal(testlist$a, res$a)
23})
24test_that("modify_list overwrites existing values", {
25  res <- modify_list(testlist, testappend)
26  expect_equal(res$b, testappend$b)
27})
28test_that("modify_list adds new values", {
29  res <- modify_list(testlist, testappend)
30  expect_equal(res$d, testappend$d)
31})
32test_that("modify_list erases null elements", {
33  res <- modify_list(testlist, testappend)
34  expect_null(res$c)
35  expect_named(res, c('a', 'b', 'd'))
36})
37
38
39# new_data_frame() --------------------------------------------------------
40
41test_that("new_data_frame handles zero-length inputs", {
42  # zero-length input creates zero-length data frame
43  d <- new_data_frame(list(x = numeric(0), y = numeric(0)))
44  expect_equal(nrow(d), 0L)
45
46  # constants are ignored in the context of zero-length input
47  d <- new_data_frame(list(x = numeric(0), y = numeric(0), z = 1))
48  expect_equal(nrow(d), 0L)
49
50  # vectors of length > 1 don't mix with zero-length input
51  expect_error(
52    new_data_frame(list(x = numeric(0), y = numeric(0), z = 1, a = c(1, 2))),
53    "Elements must equal the number of rows or 1"
54  )
55
56  # explicit recycling doesn't work with zero-length input
57  expect_error(
58    new_data_frame(list(x = numeric(0), z = 1), n = 5),
59    "Elements must equal the number of rows or 1"
60  )
61  # but it works without
62  d <- new_data_frame(list(x = 1, y = "a"), n = 5)
63  expect_equal(nrow(d), 5L)
64  expect_identical(d$x, rep(1, 5L))
65  expect_identical(d$y, rep("a", 5L))
66
67})
68