Skip to content

Commit d7be1c6

Browse files
committed
add test
1 parent e1ee2ca commit d7be1c6

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# position_connection validates connections
2+
3+
Code
4+
p$setup_params(NULL)
5+
Condition
6+
Error in `position_connect()`:
7+
! `connection` must be one of "hv", "vh", "mid", or "linear", not "foobar".
8+
9+
---
10+
11+
Code
12+
p$setup_params(NULL)
13+
Condition
14+
Error in `position_connect()`:
15+
! `connection` must be a numeric <matrix> with 2 columns, not an integer matrix with 1 columns.
16+
17+
---
18+
19+
Code
20+
p$setup_params(NULL)
21+
Condition
22+
Error in `position_connect()`:
23+
! `connection` cannot contain missing or other non-finite values.
24+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
test_that("position_connection closes off ends", {
3+
data <- data.frame(x = c(1, 2, 3), y = c(1, 2, 0), group = -1L)
4+
5+
params <- list(flipped_aes = FALSE, connection = validate_connection("hv"))
6+
test <- PositionConnect$compute_panel(data, params, list())
7+
8+
n <- nrow(test)
9+
expect_equal(test$x[c(1, n)], data$x[c(1, 3)])
10+
expect_equal(test$y[c(1, n)], data$y[c(1, 3)])
11+
12+
params <- list(flipped_aes = FALSE, connection = validate_connection("vh"))
13+
test <- PositionConnect$compute_panel(data, params, list())
14+
15+
n <- nrow(test)
16+
expect_equal(test$x[c(1, n)], data$x[c(1, 3)])
17+
expect_equal(test$y[c(1, n)], data$y[c(1, 3)])
18+
19+
params <- list(flipped_aes = FALSE, connection = validate_connection("mid"))
20+
test <- PositionConnect$compute_panel(data, params, list())
21+
22+
n <- nrow(test)
23+
expect_equal(test$x[c(1, n)], data$x[c(1, 3)])
24+
expect_equal(test$y[c(1, n)], data$y[c(1, 3)])
25+
26+
})
27+
28+
test_that("position_connection works with 1-row connection", {
29+
data <- data.frame(x = c(1, 2, 3), y = c(1, 2, 0), group = -1L)
30+
31+
params <- list(flipped_aes = FALSE, connection = cbind(0.5, 0.5))
32+
test <- PositionConnect$compute_panel(data, params, list())
33+
34+
expect_equal(test$x, c(1, 1.5, 2.5, 3))
35+
expect_equal(test$y, c(1, 1.5, 1.0, 0))
36+
})
37+
38+
test_that("position_connection works with ribbons regardless of orientation", {
39+
40+
data <- data.frame(x = 1:4, ymin = c(1, 2, 0, 1), ymax = c(3, 4, 3, 4))
41+
expected <- data.frame(
42+
x = c(1, 2, 2, 3, 3, 4, 4),
43+
ymin = c(1, 1, 2, 2, 0, 0, 1),
44+
ymax = c(3, 3, 4, 4, 3, 3, 4)
45+
)
46+
47+
p <- ggplot(data, aes(x, ymin = ymin, ymax = ymax)) +
48+
geom_ribbon(position = position_connect(connection = "hv"))
49+
test <- layer_data(p)
50+
expect_equal(test[c("x", "ymin", "ymax")], expected)
51+
52+
p <- ggplot(data, aes(y = x, xmin = ymin, xmax = ymax)) +
53+
geom_ribbon(position = position_connect(connection = "vh"))
54+
test <- layer_data(p)
55+
expect_equal(test[c("y", "xmin", "xmax")], flip_data(expected, TRUE))
56+
57+
})
58+
59+
test_that("position_connection validates connections", {
60+
61+
# Good: one of the keywords
62+
p <- position_connect(connection = "linear")
63+
params <- p$setup_params(NULL)
64+
expect_vector(params$connection, size = 2L, ptype = matrix(NA_real_, 0, 2))
65+
66+
# Good: manual matrix
67+
p <- position_connect(connection = cbind(c(0, 1), c(0, 1)))
68+
params <- p$setup_params(NULL)
69+
expect_vector(params$connection, size = 2L, ptype = matrix(NA_real_, 0, 2))
70+
71+
# Allowed: 0-row matrix, becomes NULL
72+
p <- position_connect(connection = matrix(NA_real_, nrow = 0, ncol = 2))
73+
params <- p$setup_params(NULL)
74+
expect_null(params$connection)
75+
76+
# Forbidden: non-keywords
77+
p <- position_connect(connection = "foobar")
78+
expect_snapshot(p$setup_params(NULL), error = TRUE)
79+
80+
# Forbidden: malformed matrices
81+
p <- position_connect(connection = matrix(1:3, ncol = 1))
82+
expect_snapshot(p$setup_params(NULL), error = TRUE)
83+
84+
# Forbidden: NAs
85+
p <- position_connect(connection = matrix(c(1:3, NA), 2, 2))
86+
expect_snapshot(p$setup_params(NULL), error = TRUE)
87+
})

0 commit comments

Comments
 (0)