|
| 1 | +test_that("stat_connect closes off ends", { |
| 2 | + |
| 3 | + data <- data.frame(x = 1:3, y = c(1, 2, 0)) |
| 4 | + |
| 5 | + ld <- get_layer_data( |
| 6 | + ggplot(data, aes(x, y)) + |
| 7 | + stat_connect(connection = "mid") |
| 8 | + ) |
| 9 | + |
| 10 | + i <- c(1L, nrow(ld)) |
| 11 | + j <- c(1L, nrow(data)) |
| 12 | + expect_equal(ld$x[i], data$x[j]) |
| 13 | + expect_equal(ld$y[i], data$y[j]) |
| 14 | + |
| 15 | +}) |
| 16 | + |
| 17 | +test_that("stat_connect works with 1-row connections", { |
| 18 | + data <- data.frame(x = 1:3, y = c(1, 2, 0)) |
| 19 | + |
| 20 | + ld <- get_layer_data( |
| 21 | + ggplot(data, aes(x, y)) + |
| 22 | + stat_connect(connection = cbind(0.5, 0.5)) |
| 23 | + ) |
| 24 | + |
| 25 | + expect_equal(ld$x, c(1, 1.5, 2.5, 3)) |
| 26 | + expect_equal(ld$y, c(1, 1.5, 1.0, 0)) |
| 27 | +}) |
| 28 | + |
| 29 | +test_that("stat_connect works with ribbons in both orientations", { |
| 30 | + |
| 31 | + data <- data.frame(x = 1:4, ymin = c(1, 2, 0, 1), ymax = c(3, 4, 3, 4)) |
| 32 | + expected <- data.frame( |
| 33 | + x = c(1, 2, 2, 3, 3, 4, 4), |
| 34 | + ymin = c(1, 1, 2, 2, 0, 0, 1), |
| 35 | + ymax = c(3, 3, 4, 4, 3, 3, 4) |
| 36 | + ) |
| 37 | + |
| 38 | + ld <- layer_data( |
| 39 | + ggplot(data, aes(x, ymin = ymin, ymax = ymax)) + |
| 40 | + geom_ribbon(stat = "connect", connection = "hv") |
| 41 | + ) |
| 42 | + |
| 43 | + expect_equal(ld[c("x", "ymin", "ymax")], expected) |
| 44 | + |
| 45 | + ld <- layer_data( |
| 46 | + ggplot(data, aes(y = x, xmin = ymin, xmax = ymax)) + |
| 47 | + geom_ribbon(stat = "connect", connection = "hv", orientation = "y") |
| 48 | + ) |
| 49 | + |
| 50 | + expect_equal(ld[c("y", "xmin", "xmax")], flip_data(expected, TRUE)) |
| 51 | +}) |
| 52 | + |
| 53 | +test_that("stat_connect rejects invalid connections", { |
| 54 | + |
| 55 | + test_setup <- function(...) { |
| 56 | + StatConnect$setup_params(NULL, list(...)) |
| 57 | + } |
| 58 | + |
| 59 | + # Accept keyword parameter |
| 60 | + p <- test_setup(connection = "linear") |
| 61 | + expect_vector(p$connection, size = 2L, ptype = matrix(NA_real_, 0, 2)) |
| 62 | + |
| 63 | + # Accept xy coord matrix |
| 64 | + p <- test_setup(connection = cbind(c(0, 1), c(0, 1))) |
| 65 | + expect_vector(p$connection, size = 2L, ptype = matrix(NA_real_, 0, 2)) |
| 66 | + |
| 67 | + |
| 68 | + p <- test_setup(connection = matrix(NA_real_, 0, 2)) |
| 69 | + expect_null(p$connection) |
| 70 | + |
| 71 | + expect_snapshot( |
| 72 | + test_setup(connection = "foobar"), |
| 73 | + error = TRUE |
| 74 | + ) |
| 75 | + |
| 76 | + expect_snapshot( |
| 77 | + test_setup(connection = matrix(1:3, ncol = 1)), |
| 78 | + error = TRUE |
| 79 | + ) |
| 80 | + |
| 81 | + expect_snapshot( |
| 82 | + test_setup(connection = matrix(c(1:3, NA), ncol = 2)), |
| 83 | + error = TRUE |
| 84 | + ) |
| 85 | +}) |
0 commit comments