Skip to content

Commit 4870485

Browse files
committed
tests: reflect new block element with title
1 parent 35c39d9 commit 4870485

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

tests/testthat/helper-Reporter.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,27 @@ test_card2.ReportCard <- local({ # nolint: object_name.
2929
}
3030
})
3131

32-
test_card1 <- function() {
32+
test_card1 <- function(title = NULL) {
3333
withr::with_environment(emptyenv(), plot <- ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) +
3434
ggplot2::geom_histogram(binwidth = 0.2))
35-
report_document("## Header 2 text", "A paragraph of default text", plot)
35+
new_card <- report_document("## Header 2 text", "A paragraph of default text", plot)
36+
if (!is.null(title)) metadata(new_card, "title") <- title
37+
new_card
3638
}
3739

3840
test_card2 <- local({
39-
fun <- function() {
41+
fun <- function(title = NULL) {
4042
lyt <- rtables::analyze(rtables::split_rows_by(rtables::basic_table(), "Day"), "Ozone", afun = mean)
4143
table_res2 <- rtables::build_table(lyt, within(airquality, Day <- factor(Day))) # nolint: object_name.
42-
report_document("## Header 2 text", "A paragraph of default text", table_res2, iris)
44+
new_card <- report_document("## Header 2 text", "A paragraph of default text", table_res2, iris)
45+
if (!is.null(title)) metadata(new_card, "title") <- title
46+
new_card
4347
}
44-
cache <- NULL
45-
function() {
46-
if (is.null(cache)) cache <<- fun()
47-
cache
48+
cache <- list()
49+
function(title = NULL) {
50+
title_ix <- title %||% "no_title"
51+
if (is.null(cache[[title_ix]])) cache[[title_ix]] <<- fun(title)
52+
cache[[title_ix]]
4853
}
4954
})
5055

tests/testthat/test-Reporter.R

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,27 @@ testthat::test_that("get_cards returns the same cards which was added to reporte
5858
})
5959

6060
testthat::test_that("get_blocks returns the same blocks which was added to reporter, sep = NULL", {
61-
reporter <- test_reporter(card1 <- test_card1(), card2 <- test_card2())
62-
testthat::expect_identical(reporter$get_blocks(sep = NULL), append(unclass(card1), unclass(card2)))
61+
reporter <- test_reporter(card1 <- test_card1("A title"), card2 <- test_card2("Another title"))
62+
testthat::expect_identical(
63+
reporter$get_blocks(sep = NULL),
64+
append(
65+
c(sprintf("# %s", metadata(card1, "title")), card1),
66+
c(sprintf("# %s", metadata(card2, "title")), card2)
67+
)
68+
)
6369
})
6470

6571
testthat::test_that("get_blocks by default adds NewpageBlock$new() between cards", {
66-
reporter <- test_reporter(card1 <- test_card1(), card2 <- test_card2())
72+
card1 <- test_card1("A title")
73+
card2 <- test_card2("Another title")
74+
reporter <- test_reporter(card1, card2)
75+
76+
reporter_1 <- Reporter$new()$append_cards(card1)
77+
reporter_2 <- Reporter$new()$append_cards(card2)
78+
6779
reporter_blocks <- reporter$get_blocks()
68-
reporter_blocks2 <- append(reporter_blocks[1:3], "\\newpage")
69-
reporter_blocks2 <- append(reporter_blocks2, reporter_blocks[5:8])
80+
reporter_blocks2 <- append(reporter_1$get_blocks(), "\\newpage")
81+
reporter_blocks2 <- append(reporter_blocks2, reporter_2$get_blocks())
7082
testthat::expect_equal(reporter$get_blocks(), reporter_blocks2)
7183
})
7284

0 commit comments

Comments
 (0)