Skip to content

Commit f759594

Browse files
committed
test for completeness too
1 parent d0a49ac commit f759594

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

tests/testthat/_snaps/legend-draw/all-legend-keys.svg

Lines changed: 1 addition & 1 deletion
Loading

tests/testthat/test-legend-draw.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ test_that("all keys can be drawn without 'params'", {
77

88
# Render every key
99
# If we're to develop new legend keys, we can keep appending this pattern
10-
# for new keys and layout should adjust automatically
10+
# for new keys and layout should adjust automatically.
11+
# This is also an implicit test whether the key can be constructed without errors
1112
keys <- list(
1213
point = draw_key_point(GeomPoint$use_defaults(NULL), params, size),
1314
abline = draw_key_abline(GeomAbline$use_defaults(NULL), params, size),
1415
rect = draw_key_rect(GeomRect$use_defaults(NULL), params, size),
1516
polygon = draw_key_polygon(GeomPolygon$use_defaults(NULL), params, size),
1617
blank = draw_key_blank(GeomBlank$use_defaults(NULL), params, size),
1718
boxplot = draw_key_boxplot(GeomBoxplot$use_defaults(NULL), params, size),
18-
crosssbar = draw_key_crossbar(GeomCrossbar$use_defaults(NULL), params, size),
19+
crossbar = draw_key_crossbar(GeomCrossbar$use_defaults(NULL), params, size),
1920
path = draw_key_path(GeomPath$use_defaults(NULL), params, size),
2021
vpath = draw_key_vpath(GeomPath$use_defaults(NULL), params, size),
2122
dotplot = draw_key_dotplot(GeomDotplot$use_defaults(NULL), params, size),
@@ -28,8 +29,14 @@ test_that("all keys can be drawn without 'params'", {
2829
timeseries = draw_key_timeseries(GeomPath$use_defaults(NULL), params, size)
2930
)
3031

32+
# Test that we've covered all exported keys above
33+
nse <- getNamespaceExports(asNamespace("ggplot2"))
34+
nse <- grep("^draw_key", nse, value = TRUE)
35+
nse <- gsub("^draw_key_", "", nse)
36+
expect_in(nse, names(keys))
37+
38+
# Add title to every key
3139
template <- gtable(width = unit(size, "mm"), heights = unit(c(1, size), c("lines", "mm")))
32-
# Add names
3340
keys <- Map(
3441
function(key, name) {
3542
text <- textGrob(name, gp = gpar(fontsize = 8))
@@ -38,12 +45,14 @@ test_that("all keys can be drawn without 'params'", {
3845
key = keys, name = names(keys)
3946
)
4047

48+
# Set layout
4149
n <- length(keys)
4250
nrow <- ceiling(n / 5)
4351
ncol <- ceiling(n / nrow)
4452
mtx <- matrix(list(zeroGrob()), nrow = nrow, ncol = ncol)
4553
mtx[seq_along(keys)] <- keys
4654

55+
# Render as gtable
4756
gt <- gtable_matrix(
4857
name = "layout", grobs = mtx,
4958
widths = unit(rep(size, ncol(mtx)), "mm"),
@@ -54,5 +63,4 @@ test_that("all keys can be drawn without 'params'", {
5463
gt <- gtable_add_row_space(gt, unit(1, "cm"))
5564

5665
expect_doppelganger("all legend keys", gt)
57-
5866
})

0 commit comments

Comments
 (0)