Skip to content

Commit 97e42bb

Browse files
committed
Adding news item and tests. Closes tidyverse#2540.
1 parent c40abc0 commit 97e42bb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
* `Geom` now gains a `setup_params()` method in line with the other ggproto
1515
classes (@thomasp85, #3509)
1616

17+
* Themes can now modify the theme element tree, via the
18+
`element_tree` argument. This allows extension packages to add functionality that
19+
alters the element tree (@clauswilke, #2540).
20+
1721
* `element_text()` now issues a warning when vectorized arguments are provided, as in
1822
`colour = c("red", "green", "blue")`. Such use is discouraged and not officially supported
1923
(@clauswilke, #3492).

tests/testthat/test-theme.r

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,31 @@ test_that("theme(validate=FALSE) means do not validate_element", {
196196
expect_equal(red.before$theme$animint.width, 500)
197197
})
198198

199+
test_that("theme validation happens at build stage", {
200+
# adding a non-valid theme element to a theme is no problem
201+
expect_silent(theme_gray() + theme(text = 0))
202+
203+
# the error occurs when we try to render the plot
204+
p <- ggplot() + theme(text = 0)
205+
expect_error(print(p), "must be an `element_text`")
206+
207+
# without validation, the error occurs when the element is accessed
208+
p <- ggplot() + theme(text = 0, validate = FALSE)
209+
expect_error(print(p), "text should have class element_text")
210+
})
211+
212+
test_that("element tree can be modified", {
213+
# we cannot add a new theme element without modifying the element tree
214+
p <- ggplot() + theme(blablabla = element_text())
215+
expect_error(print(p), "Theme element `blablabla` is not defined in the element hierarchy")
216+
217+
# things work once we add a new element to the element tree
218+
q <- p + theme(
219+
element_tree = list(blablabla = el_def("element_text", "text"))
220+
)
221+
expect_silent(print(q))
222+
})
223+
199224
test_that("all elements in complete themes have inherit.blank=TRUE", {
200225
inherit_blanks <- function(theme) {
201226
all(vapply(theme, function(el) {

0 commit comments

Comments
 (0)