|
| 1 | +# ============================================================================= |
| 2 | +# Unit tests for rtf_span_row |
| 3 | +# ============================================================================= |
| 4 | + |
| 5 | +# --- rtf_span_row() function tests --- |
| 6 | + |
| 7 | +test_that("rtf_span_row sets attribute with logical vector", { |
| 8 | + tbl <- iris[1:5, ] |> rtf_body() |
| 9 | + result <- rtf_span_row(tbl, span_row = c(TRUE, FALSE, FALSE, TRUE, FALSE)) |
| 10 | + expect_equal(attr(result, "rtf_span_row"), c(TRUE, FALSE, FALSE, TRUE, FALSE)) |
| 11 | +}) |
| 12 | + |
| 13 | +test_that("rtf_span_row sets attribute with integer indices", { |
| 14 | + tbl <- iris[1:5, ] |> rtf_body() |
| 15 | + result <- rtf_span_row(tbl, span_row = c(1L, 4L)) |
| 16 | + expect_equal(attr(result, "rtf_span_row"), c(TRUE, FALSE, FALSE, TRUE, FALSE)) |
| 17 | +}) |
| 18 | + |
| 19 | +test_that("rtf_span_row errors on wrong length", { |
| 20 | + tbl <- iris[1:5, ] |> rtf_body() |
| 21 | + expect_error(rtf_span_row(tbl, span_row = c(TRUE, FALSE))) |
| 22 | +}) |
| 23 | + |
| 24 | +test_that("rtf_span_row errors when called before rtf_body", { |
| 25 | + expect_error(rtf_span_row(iris[1:5, ], span_row = c(TRUE, FALSE, FALSE, TRUE, FALSE))) |
| 26 | +}) |
| 27 | + |
| 28 | +test_that("rtf_span_row errors on out-of-range indices", { |
| 29 | + tbl <- iris[1:5, ] |> rtf_body() |
| 30 | + expect_error(rtf_span_row(tbl, span_row = c(0L, 6L))) |
| 31 | +}) |
| 32 | + |
| 33 | +test_that("rtf_span_row errors on non-logical non-integer input", { |
| 34 | + |
| 35 | + tbl <- iris[1:5, ] |> rtf_body() |
| 36 | + expect_error(rtf_span_row(tbl, span_row = "row1")) |
| 37 | +}) |
| 38 | + |
| 39 | + |
| 40 | +# --- rtf_table_content() with span --- |
| 41 | + |
| 42 | +test_that("rtf_table_content emits clmgf and clmrg for span rows", { |
| 43 | + tbl <- iris[1:3, ] |> rtf_body() |> rtf_span_row(span_row = c(TRUE, FALSE, FALSE)) |
| 44 | + result <- rtf_table_content(tbl, use_border_bottom = TRUE) |
| 45 | + |
| 46 | + # result is a matrix; columns correspond to rows in the table |
| 47 | + # Row 1 (column 1 of result) should have \\clmgf and \\clmrg |
| 48 | + col1 <- paste(result[, 1], collapse = "\n") |
| 49 | + expect_true(grepl("\\\\clmgf", col1)) |
| 50 | + expect_true(grepl("\\\\clmrg", col1)) |
| 51 | + |
| 52 | + # Row 2 (column 2) should NOT have merge codes |
| 53 | + |
| 54 | + col2 <- paste(result[, 2], collapse = "\n") |
| 55 | + expect_false(grepl("\\\\clmgf", col2)) |
| 56 | + expect_false(grepl("\\\\clmrg", col2)) |
| 57 | +}) |
| 58 | + |
| 59 | +test_that("rtf_table_content empties continuation cells for span rows", { |
| 60 | + tbl <- iris[1:3, ] |> rtf_body() |> rtf_span_row(span_row = c(TRUE, FALSE, FALSE)) |
| 61 | + result <- rtf_table_content(tbl, use_border_bottom = TRUE) |
| 62 | + |
| 63 | + # For span row (column 1 of result matrix), continuation cells should be \\pard\\cell |
| 64 | + # The cell content rows start after row_begin + n_col border rows |
| 65 | + n_col <- ncol(iris) |
| 66 | + # Content for columns 2..n_col should be \\pard\\cell |
| 67 | + content_rows <- result[(1 + n_col + 2):(1 + n_col + n_col), 1] |
| 68 | + expect_true(all(content_rows == "\\pard\\cell")) |
| 69 | +}) |
| 70 | + |
| 71 | +test_that("rtf_table_content first cell retains content for span rows", { |
| 72 | + tbl <- iris[1:3, ] |> rtf_body() |> rtf_span_row(span_row = c(TRUE, FALSE, FALSE)) |
| 73 | + result <- rtf_table_content(tbl, use_border_bottom = TRUE) |
| 74 | + |
| 75 | + # First cell content (row after borders) should NOT be just \\pard\\cell |
| 76 | + n_col <- ncol(iris) |
| 77 | + first_cell_content <- result[1 + n_col + 1, 1] |
| 78 | + expect_false(first_cell_content == "\\pard\\cell") |
| 79 | + expect_true(grepl("5.1", first_cell_content)) |
| 80 | +}) |
| 81 | + |
| 82 | + |
| 83 | +# --- as_rtf_table() with span + group_by --- |
| 84 | + |
| 85 | +test_that("as_rtf_table preserves span_row through group_by", { |
| 86 | + tbl <- iris[1:4, 4:5] |> |
| 87 | + rtf_body(group_by = "Species") |> |
| 88 | + rtf_span_row(span_row = c(TRUE, FALSE, FALSE, FALSE)) |
| 89 | + |
| 90 | + result <- as_rtf_table(tbl) |
| 91 | + expect_true(grepl("\\\\clmgf", result[1])) |
| 92 | +}) |
| 93 | + |
| 94 | + |
| 95 | +# --- rtf_subset() with span --- |
| 96 | + |
| 97 | +test_that("rtf_subset subsets rtf_span_row attribute", { |
| 98 | + tbl <- iris[1:5, ] |> |
| 99 | + rtf_body() |> |
| 100 | + rtf_span_row(span_row = c(TRUE, FALSE, TRUE, FALSE, TRUE)) |
| 101 | + |
| 102 | + sub <- rtf_subset(tbl, row = 2:4, col = 1:3) |
| 103 | + expect_equal(attr(sub, "rtf_span_row"), c(FALSE, TRUE, FALSE)) |
| 104 | +}) |
| 105 | + |
| 106 | + |
| 107 | +# --- End-to-end: rtf_encode with span --- |
| 108 | + |
| 109 | +test_that("rtf_encode produces valid RTF with span rows", { |
| 110 | + tbl <- iris[1:3, ] |> |
| 111 | + rtf_body() |> |
| 112 | + rtf_span_row(span_row = c(TRUE, FALSE, FALSE)) |> |
| 113 | + rtf_encode() |
| 114 | + |
| 115 | + rtf_text <- paste(unlist(tbl), collapse = "\n") |
| 116 | + expect_true(grepl("\\\\clmgf", rtf_text)) |
| 117 | + expect_true(grepl("\\\\clmrg", rtf_text)) |
| 118 | +}) |
| 119 | + |
| 120 | +test_that("rtf_encode without span_row produces no merge codes", { |
| 121 | + tbl <- iris[1:3, ] |> |
| 122 | + rtf_body() |> |
| 123 | + rtf_encode() |
| 124 | + |
| 125 | + rtf_text <- paste(unlist(tbl), collapse = "\n") |
| 126 | + expect_false(grepl("\\\\clmgf", rtf_text)) |
| 127 | + expect_false(grepl("\\\\clmrg", rtf_text)) |
| 128 | +}) |
| 129 | + |
| 130 | + |
| 131 | +# --- Edge cases --- |
| 132 | + |
| 133 | +test_that("single-column table with span_row does not error", { |
| 134 | + tbl <- data.frame(x = 1:3) |> |
| 135 | + rtf_body() |> |
| 136 | + rtf_span_row(span_row = c(TRUE, FALSE, FALSE)) |
| 137 | + |
| 138 | + result <- rtf_table_content(tbl, use_border_bottom = TRUE) |
| 139 | + # Should not contain merge codes (only 1 column, merge is no-op) |
| 140 | + col1 <- paste(result[, 1], collapse = "\n") |
| 141 | + expect_false(grepl("\\\\clmgf", col1)) |
| 142 | +}) |
| 143 | + |
| 144 | +test_that("all rows as span rows works", { |
| 145 | + tbl <- iris[1:3, ] |> |
| 146 | + rtf_body() |> |
| 147 | + rtf_span_row(span_row = c(TRUE, TRUE, TRUE)) |> |
| 148 | + rtf_encode() |
| 149 | + |
| 150 | + rtf_text <- paste(unlist(tbl), collapse = "\n") |
| 151 | + expect_true(grepl("\\\\clmgf", rtf_text)) |
| 152 | +}) |
| 153 | + |
| 154 | +test_that("span_row on first and last rows works with border_first/last", { |
| 155 | + tbl <- iris[1:5, ] |> |
| 156 | + rtf_body(border_first = "single", border_last = "single") |> |
| 157 | + rtf_span_row(span_row = c(TRUE, FALSE, FALSE, FALSE, TRUE)) |> |
| 158 | + rtf_encode() |
| 159 | + |
| 160 | + rtf_text <- paste(unlist(tbl), collapse = "\n") |
| 161 | + expect_true(grepl("\\\\clmgf", rtf_text)) |
| 162 | +}) |
0 commit comments