|
| 1 | +# Tests for as_gglist() and the ggforce paginated-facet expansion it performs. |
| 2 | + |
| 3 | +make_paginated_plot <- function() { |
| 4 | + # 3 cyl levels, ncol=1, nrow=1 -> 3 pages |
| 5 | + ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) + |
| 6 | + ggplot2::geom_point() + |
| 7 | + ggforce::facet_wrap_paginate(~ cyl, ncol = 1, nrow = 1) |
| 8 | +} |
| 9 | + |
| 10 | +make_plain_plot <- function() { |
| 11 | + ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) + |
| 12 | + ggplot2::geom_point() |
| 13 | +} |
| 14 | + |
| 15 | +# is_paginated #### |
| 16 | + |
| 17 | +test_that("is_paginated identifies paginated and non-paginated plots", { |
| 18 | + expect_false(is_paginated(make_plain_plot())) |
| 19 | + expect_false(is_paginated("not a plot")) |
| 20 | + expect_false(is_paginated(NULL)) |
| 21 | + skip_if_not_installed("ggforce") |
| 22 | + expect_true(is_paginated(make_paginated_plot())) |
| 23 | +}) |
| 24 | + |
| 25 | +# n_pages_for_plot #### |
| 26 | + |
| 27 | +test_that("n_pages_for_plot returns 1 for non-paginated plots", { |
| 28 | + expect_identical(n_pages_for_plot(make_plain_plot()), 1L) |
| 29 | +}) |
| 30 | + |
| 31 | +test_that("n_pages_for_plot returns the actual page count for paginated plots", { |
| 32 | + skip_if_not_installed("ggforce") |
| 33 | + expect_identical(n_pages_for_plot(make_paginated_plot()), 3L) |
| 34 | +}) |
| 35 | + |
| 36 | +# gg_to_pages #### |
| 37 | + |
| 38 | +test_that("gg_to_pages returns list(plot) for non-paginated plots", { |
| 39 | + p <- make_plain_plot() |
| 40 | + result <- gg_to_pages(p) |
| 41 | + expect_length(result, 1) |
| 42 | + expect_identical(result[[1]], p) |
| 43 | +}) |
| 44 | + |
| 45 | +test_that("gg_to_pages expands paginated plot to one element per page", { |
| 46 | + skip_if_not_installed("ggforce") |
| 47 | + p <- make_paginated_plot() |
| 48 | + result <- gg_to_pages(p) |
| 49 | + expect_length(result, 3) |
| 50 | + pages_used <- vapply(result, function(pl) pl$facet$params$page, integer(1)) |
| 51 | + expect_identical(pages_used, 1:3) |
| 52 | +}) |
| 53 | + |
| 54 | +# as_gglist #### |
| 55 | + |
| 56 | +test_that("as_gglist.default errors", { |
| 57 | + expect_error(as_gglist(1L), regexp = "No `as_gglist\\(\\)` method for class") |
| 58 | +}) |
| 59 | + |
| 60 | +test_that("as_gglist.NULL wraps NULL in a length-1 gglist", { |
| 61 | + result <- as_gglist(NULL) |
| 62 | + expect_s3_class(result, "gglist") |
| 63 | + expect_length(result, 1) |
| 64 | + expect_null(result[[1]]) |
| 65 | +}) |
| 66 | + |
| 67 | +test_that("as_gglist.labels wraps a labels object", { |
| 68 | + result <- as_gglist(ggplot2::labs(x = "foo")) |
| 69 | + expect_s3_class(result, "gglist") |
| 70 | + expect_length(result, 1) |
| 71 | +}) |
| 72 | + |
| 73 | +test_that("as_gglist.gg wraps a non-paginated plot as a length-1 gglist", { |
| 74 | + result <- as_gglist(make_plain_plot()) |
| 75 | + expect_s3_class(result, "gglist") |
| 76 | + expect_length(result, 1) |
| 77 | +}) |
| 78 | + |
| 79 | +test_that("as_gglist.gg expands a paginated plot to one element per page", { |
| 80 | + skip_if_not_installed("ggforce") |
| 81 | + result <- as_gglist(make_paginated_plot()) |
| 82 | + expect_s3_class(result, "gglist") |
| 83 | + expect_length(result, 3) |
| 84 | +}) |
| 85 | + |
| 86 | +test_that("as_gglist.list works with a list of plain plots", { |
| 87 | + result <- as_gglist(list(make_plain_plot(), make_plain_plot())) |
| 88 | + expect_s3_class(result, "gglist") |
| 89 | + expect_length(result, 2) |
| 90 | +}) |
| 91 | + |
| 92 | +test_that("as_gglist.list expands paginated elements inline", { |
| 93 | + skip_if_not_installed("ggforce") |
| 94 | + result <- as_gglist(list(make_plain_plot(), make_paginated_plot())) |
| 95 | + expect_s3_class(result, "gglist") |
| 96 | + expect_length(result, 4) |
| 97 | +}) |
| 98 | + |
| 99 | +test_that("as_gglist.gglist is identity (idempotent for non-paginated)", { |
| 100 | + g <- new_gglist(list(make_plain_plot(), make_plain_plot())) |
| 101 | + expect_identical(as_gglist(g), g) |
| 102 | +}) |
| 103 | + |
| 104 | +test_that("as_gglist.gglist is identity even when elements are paginated", { |
| 105 | + skip_if_not_installed("ggforce") |
| 106 | + g <- new_gglist(list(make_plain_plot(), make_paginated_plot())) |
| 107 | + expect_identical(as_gglist(g), g) |
| 108 | + expect_identical(as_gglist(as_gglist(g)), g) |
| 109 | +}) |
| 110 | + |
| 111 | +# Render integration through as_gglist #### |
| 112 | + |
| 113 | +test_that("ggsave() saves every page when the gglist is first passed through as_gglist", { |
| 114 | + skip_if_not_installed("ggforce") |
| 115 | + withr::with_tempdir({ |
| 116 | + flat <- as_gglist(make_paginated_plot()) |
| 117 | + ggsave(filename = c("p1.png", "p2.png", "p3.png"), plot = flat, width = 4, height = 3) |
| 118 | + expect_setequal(list.files(pattern = "\\.png$"), c("p1.png", "p2.png", "p3.png")) |
| 119 | + }) |
| 120 | +}) |
0 commit comments