Skip to content

Commit

Permalink
Update several testthat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rich-iannone committed Feb 12, 2024
1 parent 4e67e8f commit a5012cc
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 73 deletions.
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/footer.md

Large diffs are not rendered by default.

64 changes: 32 additions & 32 deletions tests/testthat/_snaps/group_column_label.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/summary_rows.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/tab_footnote.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,14 @@
Code
.
Output
[1] "<table class=\"gt_table\" data-quarto-disable-processing=\"false\" data-quarto-bootstrap=\"false\">\n <thead>\n <tr class=\"gt_col_headings\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_left\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"char\">char</th>\n </tr>\n </thead>\n <tbody class=\"gt_table_body\">\n <tr><td headers=\"char\" class=\"gt_row gt_left\"><div class='gt_from_md'><p>apricot<span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span></p></div></td></tr>\n </tbody>\n \n <tfoot class=\"gt_footnotes\">\n <tr>\n <td class=\"gt_footnote\" colspan=\"1\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span> note</td>\n </tr>\n </tfoot>\n</table>"
[1] "<table class=\"gt_table\" data-quarto-disable-processing=\"false\" data-quarto-bootstrap=\"false\">\n <thead>\n <tr class=\"gt_col_headings\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_left\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"char\">char</th>\n </tr>\n </thead>\n <tbody class=\"gt_table_body\">\n <tr><td headers=\"char\" class=\"gt_row gt_left\"><span class='gt_from_md'>apricot</span><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span></td></tr>\n </tbody>\n \n <tfoot class=\"gt_footnotes\">\n <tr>\n <td class=\"gt_footnote\" colspan=\"1\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span> note</td>\n </tr>\n </tfoot>\n</table>"

---

Code
.
Output
[1] "<table class=\"gt_table\" data-quarto-disable-processing=\"false\" data-quarto-bootstrap=\"false\">\n <thead>\n <tr class=\"gt_col_headings\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_left\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"char\">char</th>\n </tr>\n </thead>\n <tbody class=\"gt_table_body\">\n <tr><td headers=\"char\" class=\"gt_row gt_left\"><div class='gt_from_md'><p><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span> apricot</p>\n</div></td></tr>\n </tbody>\n \n <tfoot class=\"gt_footnotes\">\n <tr>\n <td class=\"gt_footnote\" colspan=\"1\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span> note</td>\n </tr>\n </tfoot>\n</table>"
[1] "<table class=\"gt_table\" data-quarto-disable-processing=\"false\" data-quarto-bootstrap=\"false\">\n <thead>\n <tr class=\"gt_col_headings\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_left\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"char\">char</th>\n </tr>\n </thead>\n <tbody class=\"gt_table_body\">\n <tr><td headers=\"char\" class=\"gt_row gt_left\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span> <span class='gt_from_md'>apricot</span></td></tr>\n </tbody>\n \n <tfoot class=\"gt_footnotes\">\n <tr>\n <td class=\"gt_footnote\" colspan=\"1\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span> note</td>\n </tr>\n </tfoot>\n</table>"

# Footnotes work with group labels in 2-column stub arrangements

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/tab_spanner_delim.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/testthat/test-as_raw_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ test_that("The `as_raw_html()` function produces the same table every time", {
gsub("id=\"[a-z]*?\"", "", .)

gt_html_2_sha1 <- digest::sha1(gt_html_2)
expect_equal(gt_html_2_sha1, "67497ed4ef778f5108217d0ed3eee65f385aa3bf")
expect_equal(gt_html_2_sha1, "1692648dac8cca18e6a7f63c6f058e74885ea776")

# Expect that font family values with multiple words (i.e., have a space
# character) added with `tab_style()` preserve single-quote characters
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-extract_cells.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ test_that("Extraction of table cells works well", {
fmt_markdown(columns = a) %>%
extract_cells(columns = a, output = "html"),
c(
"<div class='gt_from_md'><p><em>hey</em></p>\n</div>",
"<div class='gt_from_md'><p><strong>there</strong></p>\n</div>"
"<span class='gt_from_md'><em>hey</em></span>",
"<span class='gt_from_md'><strong>there</strong></span>"
)
)
expect_equal(
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-fmt_markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,39 @@ There's a quick reference [here](https://commonmark.org/help/).
(tab %>%
render_formats_test(context = "html"))[["column_1"]][[1]],
paste0(
"<div class='gt_from_md'><p>This is Markdown <em>text</em>",
"<span class='gt_from_md'>This is Markdown <em>text</em>",
".</p>\n<p>Markdown’s syntax is comprised entirely of\npunctuation ",
"characters, which punctuation\ncharacters have been carefully chosen ",
"so as\nto look like what they mean… assuming\nyou’ve ever ",
"used email.</p>\n</div>"
"used email.</span>"
)
)

expect_equal(
(tab %>%
render_formats_test(context = "html"))[["column_1"]][[2]],
paste0(
"<div class='gt_from_md'><p>Info on Markdown syntax can be ",
"<span class='gt_from_md'>Info on Markdown syntax can be ",
"found\n<a href=\"https://daringfireball.net/projects/markdown/\">",
"here</a>.</p>\n</div>"
"here</a>.</span>"
)
)

expect_equal(
(tab %>%
render_formats_test(context = "html"))[["column_2"]][[1]],
paste0(
"<div class='gt_from_md'><p>The <strong>gt</strong> package has ",
"<span class='gt_from_md'>The <strong>gt</strong> package has ",
"these datasets:</p>\n<ul>\n<li><code>countrypops</code></li>\n<li>",
"<code>sza</code></li>\n<li><code>gtcars</code></li>\n<li><code>sp500",
"</code></li>\n<li><code>pizzaplace</code></li>\n<li><code>exibble",
"</code></li>\n</ul>\n</div>"
"</code></li>\n</ul>\n</span>"
)
)

expect_equal(
(tab %>%
render_formats_test(context = "html"))[["column_2"]][[2]],
"<div class='gt_from_md'><p>There’s a quick reference <a href=\"https://commonmark.org/help/\">here</a>.</p>\n</div>"
"<span class='gt_from_md'>There’s a quick reference <a href=\"https://commonmark.org/help/\">here</a>.</span>"
)
})
20 changes: 10 additions & 10 deletions tests/testthat/test-stub.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test_that("The stub can be formatted with `fmt_*()` functions", {
# Expect certain stub text elements
tbl_html_1 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(paste0(c("one", "two", "three", "4", "5ive", "six"), "\n"))
expect_equal(c("one", "two", "three", "4", "5ive", "six"))

# Create a gt table with a stub that has row labels, and, row groups
# that run wide
Expand All @@ -56,7 +56,7 @@ test_that("The stub can be formatted with `fmt_*()` functions", {
# Expect certain stub text elements (same as in `gt_tbl_1`)
tbl_html_2 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(paste0(c("one", "two", "three", "4", "5ive", "six"), "\n"))
expect_equal(c("one", "two", "three", "4", "5ive", "six"))

# Create a gt table with a stub that has row labels, row groups that
# run wide, and one footnote
Expand All @@ -75,7 +75,7 @@ test_that("The stub can be formatted with `fmt_*()` functions", {
# be present
tbl_html_3 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(c("one\n", "two1", "three\n", "4\n", "5ive\n", "six\n"))
expect_equal(c("one", "two1", "three", "4", "5ive", "six"))

# Expect the footnote text to be present
tbl_html_3 %>%
Expand Down Expand Up @@ -108,7 +108,7 @@ test_that("The stub can be formatted with `fmt_*()` functions", {
# Expect certain stub text elements and the footnote glyphs to be present
tbl_html_4 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(c("one\n", "three1", "5ive\n", "two\n", "42", "six\n"))
expect_equal(c("one", "three1", "5ive", "two", "42", "six"))

# Expect values in the `vals` column to be in the correct order
tbl_html_4 %>%
Expand Down Expand Up @@ -143,7 +143,7 @@ test_that("The stub can be formatted with `fmt_*()` functions", {
# Expect certain stub text elements and the footnote glyphs to be present
tbl_html_5 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(c("one\n", "three1", "5ive\n", "two\n", "42", "six\n"))
expect_equal(c("one", "three1", "5ive", "two", "42", "six"))

# Expect values in the `vals` column to be in the correct order
tbl_html_5 %>%
Expand Down Expand Up @@ -231,7 +231,7 @@ test_that("The stub can be formatted with `fmt_*()` functions and `stub()", {
# Expect certain stub text elements
tbl_html_1 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(paste0(c("one", "two", "three", "4", "5ive", "six"), "\n"))
expect_equal(c("one", "two", "three", "4", "5ive", "six"))

# Create a gt table with a stub that has row labels, and, row groups
# that run wide
Expand All @@ -248,7 +248,7 @@ test_that("The stub can be formatted with `fmt_*()` functions and `stub()", {
# Expect certain stub text elements (same as in `gt_tbl_1`)
tbl_html_2 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(paste0(c("one", "two", "three", "4", "5ive", "six"), "\n"))
expect_equal(c("one", "two", "three", "4", "5ive", "six"))

# Create a gt table with a stub that has row labels, row groups that
# run wide, and one footnote
Expand All @@ -267,7 +267,7 @@ test_that("The stub can be formatted with `fmt_*()` functions and `stub()", {
# be present
tbl_html_3 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(c("one\n", "two1", "three\n", "4\n", "5ive\n", "six\n"))
expect_equal(c("one", "two1", "three", "4", "5ive", "six"))

# Expect the footnote text to be present
tbl_html_3 %>%
Expand Down Expand Up @@ -300,7 +300,7 @@ test_that("The stub can be formatted with `fmt_*()` functions and `stub()", {
# Expect certain stub text elements and the footnote glyphs to be present
tbl_html_4 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(c("one\n", "three1", "5ive\n", "two\n", "42", "six\n"))
expect_equal(c("one", "three1", "5ive", "two", "42", "six"))

# Expect values in the `vals` column to be in the correct order
tbl_html_4 %>%
Expand Down Expand Up @@ -335,7 +335,7 @@ test_that("The stub can be formatted with `fmt_*()` functions and `stub()", {
# Expect certain stub text elements and the footnote glyphs to be present
tbl_html_5 %>%
selection_text(selection = "[class='gt_row gt_left gt_stub']") %>%
expect_equal(c("one\n", "three1", "5ive\n", "two\n", "42", "six\n"))
expect_equal(c("one", "three1", "5ive", "two", "42", "six"))

# Expect values in the `vals` column to be in the correct order
tbl_html_5 %>%
Expand Down
19 changes: 14 additions & 5 deletions tests/testthat/test-substitution.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,13 @@ test_that("The `sub_missing()` function works correctly", {
sub_missing(columns = "num_1", missing_text = md("**a** *b*")) %>%
render_formats_test(context = "html"))[["num_1"]],
c(
"<strong>a</strong> <em>b</em>", "74", "<strong>a</strong> <em>b</em>",
"93", "<strong>a</strong> <em>b</em>", "76", "<strong>a</strong> <em>b</em>"
"<span class='gt_from_md'><strong>a</strong> <em>b</em></span>",
"74",
"<span class='gt_from_md'><strong>a</strong> <em>b</em></span>",
"93",
"<span class='gt_from_md'><strong>a</strong> <em>b</em></span>",
"76",
"<span class='gt_from_md'><strong>a</strong> <em>b</em></span>"
)
)

Expand Down Expand Up @@ -318,7 +323,11 @@ test_that("The `sub_zero()` function works correctly", {
(tab %>%
sub_zero(columns = "num_1", zero_text = md("*nil*")) %>%
render_formats_test(context = "html"))[["num_1"]],
c("NA", "74.0000", "NA", "<em>nil</em>", "NA", "0.0001", "NA")
c(
"NA", "74.0000", "NA",
"<span class='gt_from_md'><em>nil</em></span>",
"NA", "0.0001", "NA"
)
)

expect_equal(
Expand Down Expand Up @@ -461,8 +470,8 @@ test_that("The `sub_small_vals()` function works correctly", {
sub_small_vals(columns = "num_1", sign = "-") %>%
render_formats_test(context = "html"))[["num_1"]],
c(
"&lt;<em>abs</em>(-0.01)", "74.00", "NA", "0.00",
"5,000,000,000,000.00", "0.00", "84.34"
"<span class='gt_from_md'>&lt;<em>abs</em>(-0.01)</span>",
"74.00", "NA", "0.00", "5,000,000,000,000.00", "0.00", "84.34"
)
)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-tab_caption.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test_that("A caption can be added/removed with `tab_caption()`/`rm_caption()`",
# Expect that the caption is rendered correctly
expect_equal(
as.character(create_caption_component_h(data = tbl_2)),
"<!--/html_preserve--><caption class='gt_caption'>A <em>caption</em>.</caption><!--html_preserve-->"
"<!--/html_preserve--><caption class='gt_caption'><span class='gt_from_md'>A <em>caption</em>.</span></caption><!--html_preserve-->"
)

# Create a table, add a caption in `gt()`, overwrite with `tab_caption()`
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-tab_footnote.R
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ test_that("Footnotes are correctly placed with text produced by `fmt_markdown()`
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_row gt_left']") %>%
expect_equal(paste0("1", "\U000A0", "apricot\n"))
expect_equal(paste0("1", "\U000A0", "apricot"))

exibble[1, 2] %>%
gt() %>%
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-table_parts.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ test_that("Row groups can be successfully generated with `tab_row_group()", {
# row group labels
expect_match(
tbl_html,
regexp = "<strong><em>void</em></strong><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span>",
regexp = "<span class='gt_from_md'><strong><em>void</em></strong></span><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;\"><sup>1</sup></span>",
fixed = TRUE
)
expect_match(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-util_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ test_that("the `process_text()` function works correctly", {
# be returned from `process_text` as character-based
# text that's been transformed to HTML
process_text(text = md_text) %>%
expect_equal("this is <em>text</em> interpreted as <strong>markdown</strong>")
expect_equal("<span class='gt_from_md'>this is <em>text</em> interpreted as <strong>markdown</strong></span>")

md_text %>% expect_s3_class("from_markdown")
process_text(text = md_text) %>% expect_type("character")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-utils_render_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test_that("captioning processes text correctly", {
expect_null(create_caption_component_h(exibble %>% gt()))

expect_caption_eq("**hello & goodbye**", "**hello &amp; goodbye**")
expect_caption_eq(md("**hello & goodbye**"), "<strong>hello &amp; goodbye</strong>")
expect_caption_eq(md("**hello & goodbye**"), "<span class='gt_from_md'><strong>hello &amp; goodbye</strong></span>")
expect_caption_eq(I("**hello & goodbye**"), "**hello & goodbye**")
expect_caption_eq(htmltools::strong("hello & goodbye"), "<strong>hello &amp; goodbye</strong>")
expect_caption_eq(htmltools::HTML("<strong>hello &amp; goodbye</strong>"), "<strong>hello &amp; goodbye</strong>")
Expand Down

0 comments on commit a5012cc

Please sign in to comment.