Skip to content

Commit 981caef

Browse files
committed
Add tests for plot_theme
1 parent d6a5066 commit 981caef

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
@@ -379,14 +379,14 @@ theme <- function(line,
379379

380380

381381
# Combine plot defaults with current theme to get complete theme for a plot
382-
plot_theme <- function(x) {
382+
plot_theme <- function(x, default_theme=theme_get()) {
383383
complete <- attr(x$theme, "complete")
384384
if (is.null(complete)) {
385-
theme_get()
385+
default_theme
386386
} else if (complete) {
387387
x$theme
388388
} else {
389-
defaults(x$theme, theme_get())
389+
defaults(x$theme, default_theme)
390390
}
391391
}
392392

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)