Skip to content

Commit cfd4962

Browse files
committed
add unit tests, simplify code; closes tidyverse#3314
1 parent cee085f commit cfd4962

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

R/annotation.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
4848
lengths <- vapply(aesthetics, length, integer(1))
4949
n <- unique(setdiff(lengths, 1L))
5050
if (length(n) == 0L) n <- 1L # if all lengths are equal to 1L then above line fails, this fixes that
51-
unequal <- length(n) > 1L
52-
if (unequal) {
51+
52+
if (length(n) > 1L) {
5353
bad <- lengths != 1L
5454
details <- paste(names(aesthetics)[bad], " (", lengths[bad], ")",
5555
sep = "", collapse = ", ")

tests/testthat/test-performance.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
context("Performance related alternatives")
22

3+
# ********************
4+
# modify_list()
5+
36
testlist <- list(
47
a = 5.5,
58
b = "x",
@@ -32,3 +35,35 @@ test_that("modify_list erases null elements", {
3235
expect_null(res$c)
3336
expect_named(res, c('a', 'b', 'd'))
3437
})
38+
39+
40+
# ********************
41+
# new_data_frame()
42+
43+
test_that("new_data_frame handles zero-length inputs", {
44+
# zero-length input creates zero-length data frame
45+
d <- new_data_frame(list(x = numeric(0), y = numeric(0)))
46+
expect_equal(nrow(d), 0L)
47+
48+
# constants are ignored in the context of zero-length input
49+
d <- new_data_frame(list(x = numeric(0), y = numeric(0), z = 1))
50+
expect_equal(nrow(d), 0L)
51+
52+
# vectors of length > 1 don't mix with zero-length input
53+
expect_error(
54+
new_data_frame(list(x = numeric(0), y = numeric(0), z = 1, a = c(1, 2))),
55+
"Elements must equal the number of rows or 1"
56+
)
57+
58+
# explicit recycling doesn't work with zero-length input
59+
expect_error(
60+
new_data_frame(list(x = numeric(0), z = 1), n = 5),
61+
"Elements must equal the number of rows or 1"
62+
)
63+
# but it works without
64+
d <- new_data_frame(list(x = 1, y = "a"), n = 5)
65+
expect_equal(nrow(d), 5L)
66+
expect_identical(d$x, rep(1, 5L))
67+
expect_identical(d$y, rep("a", 5L))
68+
69+
})

0 commit comments

Comments
 (0)