Skip to content

Commit fa141b8

Browse files
committed
Add layer name tests for get_layer_data(), get_layer_grob()
1 parent 0287675 commit fa141b8

File tree

1 file changed

+79
-16
lines changed

1 file changed

+79
-16
lines changed

tests/testthat/test-layer.R

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ test_that("layer() checks its input", {
55
expect_snapshot_error(layer(geom = "point", position = "identity"))
66
expect_snapshot_error(layer(geom = "point", stat = "identity"))
77

8-
expect_snapshot_error(layer("point", "identity", mapping = 1:4, position = "identity"))
9-
expect_snapshot_error(layer("point", "identity", mapping = ggplot(), position = "identity"))
8+
expect_snapshot_error(layer(
9+
"point",
10+
"identity",
11+
mapping = 1:4,
12+
position = "identity"
13+
))
14+
expect_snapshot_error(layer(
15+
"point",
16+
"identity",
17+
mapping = ggplot(),
18+
position = "identity"
19+
))
1020

1121
expect_snapshot_error(validate_subclass("test", "geom"))
1222
expect_snapshot_error(validate_subclass(environment(), "geom"))
@@ -29,7 +39,8 @@ test_that("unknown aesthetics create warning", {
2939
})
3040

3141
test_that("empty aesthetics create warning", {
32-
p <- ggplot(mtcars) + geom_point(aes(disp, mpg), fill = NULL, shape = character())
42+
p <- ggplot(mtcars) +
43+
geom_point(aes(disp, mpg), fill = NULL, shape = character())
3344
expect_snapshot_warning(ggplot_build(p))
3445
})
3546

@@ -80,7 +91,13 @@ test_that("function aesthetics are wrapped with after_stat()", {
8091
test_that("computed stats are in appropriate layer", {
8192
df <- data_frame(x = 1:10)
8293
expect_snapshot_error(
83-
ggplot_build(ggplot(df, aes(colour = after_stat(density), fill = after_stat(density))) + geom_point())
94+
ggplot_build(
95+
ggplot(
96+
df,
97+
aes(colour = after_stat(density), fill = after_stat(density))
98+
) +
99+
geom_point()
100+
)
84101
)
85102
})
86103

@@ -96,9 +113,15 @@ test_that("layers are stateless except for the computed params", {
96113
p <- ggplot(df) +
97114
geom_col(aes(x = x, y = y), width = 0.8, fill = "red")
98115
col_layer <- as.list(p@layers[[1]])
99-
stateless_names <- setdiff(names(col_layer), c("computed_geom_params", "computed_stat_params", "computed_mapping"))
116+
stateless_names <- setdiff(
117+
names(col_layer),
118+
c("computed_geom_params", "computed_stat_params", "computed_mapping")
119+
)
100120
invisible(ggplotGrob(p))
101-
expect_identical(as.list(p@layers[[1]])[stateless_names], col_layer[stateless_names])
121+
expect_identical(
122+
as.list(p@layers[[1]])[stateless_names],
123+
col_layer[stateless_names]
124+
)
102125
})
103126

104127
test_that("inherit.aes works", {
@@ -109,11 +132,14 @@ test_that("inherit.aes works", {
109132
geom_col(aes(x = x, y = y), inherit.aes = FALSE)
110133
invisible(ggplotGrob(p1))
111134
invisible(ggplotGrob(p2))
112-
expect_identical(p1@layers[[1]]$computed_mapping, p2@layers[[1]]$computed_mapping)
135+
expect_identical(
136+
p1@layers[[1]]$computed_mapping,
137+
p2@layers[[1]]$computed_mapping
138+
)
113139
})
114140

115141
test_that("retransform works on computed aesthetics in `map_statistic`", {
116-
df <- data.frame(x = rep(c(1,2), c(9, 25)))
142+
df <- data.frame(x = rep(c(1, 2), c(9, 25)))
117143
p <- ggplot(df, aes(x)) + geom_bar() + scale_y_sqrt()
118144
expect_equal(get_layer_data(p)$y, c(3, 5))
119145

@@ -147,7 +173,6 @@ test_that("layer warns for constant aesthetics", {
147173
})
148174

149175
test_that("layer names can be resolved", {
150-
151176
p <- ggplot() + geom_point() + geom_point()
152177
expect_named(p@layers, c("geom_point", "geom_point...2"))
153178

@@ -159,7 +184,6 @@ test_that("layer names can be resolved", {
159184
})
160185

161186
test_that("validate_subclass can resolve classes via constructors", {
162-
163187
env <- new_environment(list(
164188
geom_foobar = geom_point,
165189
stat_foobar = stat_boxplot,
@@ -169,9 +193,14 @@ test_that("validate_subclass can resolve classes via constructors", {
169193

170194
expect_s3_class(validate_subclass("foobar", "Geom", env = env), "GeomPoint")
171195
expect_s3_class(validate_subclass("foobar", "Stat", env = env), "StatBoxplot")
172-
expect_s3_class(validate_subclass("foobar", "Position", env = env), "PositionNudge")
173-
expect_s3_class(validate_subclass("foobar", "Guide", env = env), "GuideAxisTheta")
174-
196+
expect_s3_class(
197+
validate_subclass("foobar", "Position", env = env),
198+
"PositionNudge"
199+
)
200+
expect_s3_class(
201+
validate_subclass("foobar", "Guide", env = env),
202+
"GuideAxisTheta"
203+
)
175204
})
176205

177206
test_that("attributes on layer data are preserved", {
@@ -182,7 +211,9 @@ test_that("attributes on layer data are preserved", {
182211
# * It has an `after_stat()` so it enters the map_statistic method
183212
old <- stat_summary(
184213
aes(fill = after_stat(y)),
185-
fun = mean, geom = "col", position = "dodge"
214+
fun = mean,
215+
geom = "col",
216+
position = "dodge"
186217
)
187218
# We modify the compute aesthetics method to append a test attribute
188219
new <- ggproto(NULL, old, compute_aesthetics = function(self, data, plot) {
@@ -192,7 +223,9 @@ test_that("attributes on layer data are preserved", {
192223
})
193224
# At the end of plot building, we want to retrieve that metric
194225
ld <- layer_data(
195-
ggplot(mpg, aes(drv, hwy, colour = factor(year))) + new + facet_grid(~year) +
226+
ggplot(mpg, aes(drv, hwy, colour = factor(year))) +
227+
new +
228+
facet_grid(~year) +
196229
scale_y_sqrt()
197230
)
198231
expect_equal(attr(ld, "test"), "preserve me")
@@ -226,6 +259,36 @@ test_that("layer_data returns a data.frame", {
226259
expect_snapshot_error(l$layer_data(mtcars))
227260
})
228261

262+
test_that("get_layer_data works with layer names", {
263+
p <- ggplot() + geom_point(name = "foo") + geom_point(name = "bar")
264+
265+
# name has higher precedence than index
266+
expect_identical(
267+
get_layer_data(p, i = 1L, name = "bar"),
268+
get_layer_data(p, i = 2L)
269+
)
270+
271+
# name falls back to index
272+
expect_snapshot_error(
273+
get_layer_data(p, i = 1L, name = "none")
274+
)
275+
})
276+
277+
test_that("get_layer_grob works with layer names", {
278+
p <- ggplot() + geom_point(name = "foo") + geom_point(name = "bar")
279+
280+
# name has higher precedence than index
281+
expect_identical(
282+
get_layer_grob(p, i = 1L, name = "bar"),
283+
get_layer_grob(p, i = 2L)
284+
)
285+
286+
# name falls back to index
287+
expect_snapshot_error(
288+
get_layer_grob(p, i = 1L, name = "none")
289+
)
290+
})
291+
229292
test_that("data.frames and matrix aesthetics survive the build stage", {
230293
df <- data_frame0(
231294
x = 1:2,
@@ -240,5 +303,5 @@ test_that("data.frames and matrix aesthetics survive the build stage", {
240303
scale_shape_identity()
241304
)
242305
expect_vector(p$colour, matrix(NA_integer_, nrow = 0, ncol = 2), size = 2)
243-
expect_vector(p$shape, data_frame0(a = integer(), b = character()), size = 2)
306+
expect_vector(p$shape, data_frame0(a = integer(), b = character()), size = 2)
244307
})

0 commit comments

Comments
 (0)