diff --git a/NEWS.md b/NEWS.md index af643760..84085548 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # bayesplot (development version) +* Added tests for theme/aesthetic helpers (`plot_bg()`, `panel_bg()`, `facet_bg()`, `legend_move()`, `legend_text()`, `overlay_function()`) verifying return types, edge cases, and composition with plots. * Fixed `is_chain_list()` to correctly reject empty lists instead of silently returning `TRUE`. * Added unit tests for `mcmc_areas_ridges_data()`, `mcmc_parcoord_data()`, and `mcmc_trace_data()`. * Added unit tests for `ppc_error_data()` and `ppc_loo_pit_data()` covering output structure, argument handling, and edge cases. diff --git a/tests/testthat/test-convenience-functions.R b/tests/testthat/test-convenience-functions.R index a6f76499..23fa38a0 100644 --- a/tests/testthat/test-convenience-functions.R +++ b/tests/testthat/test-convenience-functions.R @@ -91,6 +91,24 @@ test_that("facet_bg returns correct theme object", { expect_identical(bg2, theme(strip.background = element_rect(fill = "blue", linetype = 2))) expect_identical(facet_bg(on = FALSE), theme(strip.background = element_blank())) }) +test_that("background helpers are incomplete theme objects", { + expect_false(attr(plot_bg(), "complete")) + expect_false(attr(panel_bg(), "complete")) + expect_false(attr(facet_bg(), "complete")) +}) +test_that("on = FALSE produces element_blank for background helpers", { + expect_s3_class(plot_bg(on = FALSE)$plot.background, "element_blank") + expect_s3_class(panel_bg(on = FALSE)$panel.background, "element_blank") + expect_s3_class(facet_bg(on = FALSE)$strip.background, "element_blank") +}) +test_that("background helpers compose with bayesplot plots", { + y <- rnorm(100) + yrep <- matrix(rnorm(500), nrow = 5) + p <- ppc_hist(y, yrep[1:3, ]) + expect_s3_class(p + plot_bg(fill = "gray90"), "ggplot") + expect_s3_class(p + panel_bg(fill = "white"), "ggplot") + expect_s3_class(p + facet_bg(fill = "gray80"), "ggplot") +}) # legend position and text ------------------------------------------------ test_that("legend_none returns correct theme object", { @@ -120,6 +138,21 @@ test_that("legend_text returns correct theme object", { theme(legend.text = element_text(color = "purple", size = 16)) ) }) +test_that("legend_move('none') behaves like legend_none", { + expect_equal( + legend_move("none")$legend.position, + legend_none()$legend.position, + ignore_attr = TRUE + ) +}) +test_that("legend helpers compose with bayesplot plots", { + y <- rnorm(100) + yrep <- matrix(rnorm(500), nrow = 5) + p <- ppc_dens_overlay(y, yrep) + expect_s3_class(p + legend_move("bottom"), "ggplot") + expect_s3_class(p + legend_move(c(0.7, 0.8)), "ggplot") + expect_s3_class(p + legend_text(size = 12, color = "blue"), "ggplot") +}) # axis and facet text -------------------------------------------------- test_that("xaxis_text returns correct theme object", { @@ -186,6 +219,30 @@ test_that("overlay_function returns the correct object", { a$constructor <- b$constructor <- NULL expect_equal(a, b, ignore_function_env = TRUE) }) +test_that("overlay_function returns a layer, not a theme", { + layer <- overlay_function(fun = dnorm) + expect_s3_class(layer, "LayerInstance") + expect_false(inherits(layer, "theme")) +}) +test_that("overlay_function composes with bayesplot plots", { + y <- rnorm(200) + yrep <- matrix(rnorm(1000), nrow = 5) + p <- ppc_dens(y, yrep) + expect_s3_class(p + overlay_function(fun = dnorm), "ggplot") +}) + + +# multiple helpers stack --------------------------------------------------- +test_that("multiple theme helpers can be stacked on a single plot", { + y <- rnorm(100) + yrep <- matrix(rnorm(500), nrow = 5) + p <- ppc_dens_overlay(y, yrep) + + plot_bg(fill = "gray95") + + panel_bg(fill = "white") + + legend_move("bottom") + + legend_text(size = 10) + expect_s3_class(p, "ggplot") +}) # tagged functions -------------------------------------------------------