context("generics")
test_that("more complicated set works", {
d <- dict()
d["fun"] <- sum
d["env"] <- new.env()
d[["seq"]] <- seq(10)
expect_equal(length(d), 3)
expect_equal(d[["fun"]], sum)
expect_is(d[["env"]], "environment")
expect_equal(d["seq"], list(seq = seq(10)))
it <- items(d)
expect_length(it, 3)
expect_equal(it, d$items())
expect_equal(size(d), 3)
expect_error(size(list(a = 1)))
expect_equal(values(d), d$values())
expect_error(values(list()))
expect_equal(keys(d), d$keys())
expect_error(keys(c(a = 1)))
expect_equal(dict(x = list())["x"], list(x = list()))
expect_equal(dict(x = list())[["x"]], list())
d[[c("fu", "quo")]] <- c(function(x) {x + 1}, quote(a + b))
expect_is(d["fu"], "list")
expect_is(d[["fu"]], "function")
expect_is(d[["quo"]], "call")
vec_len_3 <- c(function(x) {x + 1}, quote(a + b), "one_too_many")
# should throw error
# because it's unclear how to map 2 names to 3 values
expect_error(d[[c("1", "2")]] <- vec_len_3)
# $<- should also throw an error
expect_error(d$forbidden <- "assignment")
# length<- should also be impossible
expect_error(length(d) <- 10L)
# setting 3 values to 1 name should work though
d[c("1")] <- vec_len_3
expect_equal(d[["1"]], vec_len_3)
expect_equal(d["1"][[2]], quote(a + b))
# print should also return invisibly
capture_output(printed <- print(d))
expect_is(printed, "dict")
})
test_that("dict nicely printed", {
d <- dict()
set.seed(1234)
d["x"] <- runif(10)
d["fun"] <- mean
d["exp"] <- expression(1 + 2 * pi)
d[["string"]] <- "this is a string"
expect_is(d[["y"]] <- quote(exp(10)), "call")
expect_is(d["nested_dict"] <- 1, "numeric")
expect_equal(length(d), 6)
expect_known_output(
print(d),
test_path("test-print.txt")
)
expect_known_output(
print(dict()),
test_path("test-print-empty.txt")
)
expect_known_output(
str(d),
test_path("test-str.txt")
)
})
test_that("listify works as expected", {
l <- list(a = 1, b = 2)
expect_equal(listify(l), l)
v <- c(a = 1, b = 2)
expect_equal(listify(v), list(v))
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.