Skip to content

Commit ea873b2

Browse files
committed
Add tests for plot_theme
1 parent fca59ae commit ea873b2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

R/theme.r

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,14 @@ theme <- function(line,
415415

416416

417417
# Combine plot defaults with current theme to get complete theme for a plot
418-
plot_theme <- function(x) {
418+
plot_theme <- function(x, default_theme=theme_get()) {
419419
complete <- attr(x$theme, "complete")
420420
if (is.null(complete)) {
421-
theme_get()
421+
default_theme
422422
} else if (complete) {
423423
x$theme
424424
} else {
425-
defaults(x$theme, theme_get())
425+
defaults(x$theme, default_theme)
426426
}
427427
}
428428

tests/testthat/test-theme.r

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ test_that("Complete and non-complete themes interact correctly with ggplot objec
163163
expect_false(attr(p$plot$theme, "complete"))
164164
expect_equal(p$plot$theme$text$colour, "red")
165165
expect_equal(p$plot$theme$text$face, "italic")
166+
167+
# The final calculated plot theme should not blend a complete theme
168+
# with the default theme
169+
default_theme <- theme_gray() + theme(axis.text.x = element_text(colour = "red"))
170+
171+
ptheme <- plot_theme(qplot(1:3, 1:3) + theme_void(), default_theme)
172+
expect_null(ptheme$axis.text.x)
173+
174+
ptheme <- plot_theme(qplot(1:3, 1:3) + theme_gray(), default_theme)
175+
expect_true(is.null(ptheme$axis.text.x$colour) || ptheme$axis.text.x$colour != "red")
176+
177+
ptheme <- plot_theme(qplot(1:3, 1:3) + theme(axis.text.y = element_text(colour = "blue")), default_theme)
178+
expect_equal(ptheme$axis.text.x$colour, "red")
179+
expect_equal(ptheme$axis.text.y$colour, "blue")
166180
})
167181

168182
test_that("theme(validate=FALSE) means do not validate_element", {

0 commit comments

Comments
 (0)