From a713ef8ecde583133c3d0c521563d32d025a2cb8 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 25 Jun 2024 21:28:28 -0400 Subject: [PATCH 1/5] Minor test refactoring --- tests/testthat/_snaps/cols_merge.md | 2 +- tests/testthat/test-cols_merge.R | 24 ++++++------- tests/testthat/test-fmt_currency.R | 30 +++++++--------- tests/testthat/test-gt_object.R | 48 ++++++++++++-------------- tests/testthat/test-opt_functions.R | 5 --- tests/testthat/test-summary_rows.R | 53 +++++++++++++---------------- tests/testthat/test-tab_footnote.R | 18 +++++----- 7 files changed, 80 insertions(+), 100 deletions(-) diff --git a/tests/testthat/_snaps/cols_merge.md b/tests/testthat/_snaps/cols_merge.md index edc0f4e5c9..9f7391b09c 100644 --- a/tests/testthat/_snaps/cols_merge.md +++ b/tests/testthat/_snaps/cols_merge.md @@ -47,7 +47,7 @@ Output [1] "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
value
34.5+1.8
−2.1
29.2
36.3
31.6+NA
−1.8
28.5
30.9
NA
NA
Inf
30.0 ± 0.0
32.0+0.0
−0.1
34.0
NaN
" -# cols_merge_range() works correctly +# cols_merge_range() produces the correct output Code . diff --git a/tests/testthat/test-cols_merge.R b/tests/testthat/test-cols_merge.R index c0c92932c1..1b3556386a 100644 --- a/tests/testthat/test-cols_merge.R +++ b/tests/testthat/test-cols_merge.R @@ -202,7 +202,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { cols_merge(columns = c(a, b, c), pattern = "{1}{2}<<{3}>>") expect_equal( - (tbl_gt_1 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_1, "html")[["a"]], c("111", "2NA2", "33", "4NA") ) @@ -212,7 +212,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { sub_missing(missing_text = "X") expect_equal( - (tbl_gt_2 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_2, "html")[["a"]], c("111", "2X2", "33X", "4XX") ) @@ -221,7 +221,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { cols_merge(columns = c(a, b, c), pattern = "{1}<<{2}<<{3}>>>>") expect_equal( - (tbl_gt_3 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_3, "html")[["a"]], c("111", "2", "33", "4") ) @@ -230,7 +230,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { cols_merge(columns = c(a, b, c), pattern = "{1}<<{2}-{3}>>") expect_equal( - (tbl_gt_4 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_4, "html")[["a"]], c("11-1", "2", "3", "4") ) @@ -239,7 +239,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { cols_merge(columns = c(a, b, c), pattern = "<<{1}-{2}-{3}>>") expect_equal( - (tbl_gt_5 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_5, "html")[["a"]], c("1-1-1", "", "", "") ) @@ -248,7 +248,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { cols_merge(columns = c(a, b, c), pattern = "<<{1}<<{2}<<{3}>>>>>>") expect_equal( - (tbl_gt_6 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_6, "html")[["a"]], c("111", "2", "33", "4") ) @@ -257,7 +257,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { cols_merge(columns = c(a, b, c), pattern = "<<<<<>>>>>") expect_equal( - (tbl_gt_7 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_7, "html")[["a"]], rep("X", 4) ) @@ -266,7 +266,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { cols_merge(columns = c(a, b, d), pattern = "{1}{2}<<{3}>>") expect_equal( - (tbl_gt_9 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_9, "html")[["a"]], c("111", "2NA2", "33", "4NA") ) @@ -276,7 +276,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { cols_merge(columns = c(a, b, d), pattern = "{1}{2}<<{3}>>") expect_equal( - (tbl_gt_10 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_10, "html")[["a"]], c("111", "2X2", "33X", "4XX") ) @@ -285,7 +285,7 @@ test_that("The secondary pattern language works well in `cols_merge()`", { cols_merge(columns = c(a, b, e), pattern = "{1}{2}<<{3}>>") expect_equal( - (tbl_gt_11 %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_gt_11, "html")[["a"]], c("11TRUE", "2NAFALSE", "33", "4NA") ) @@ -873,7 +873,7 @@ test_that("cols_merge_n_pct() works correctly", { expect_equal("") expect_equal( - (tbl_html %>% render_formats_test("html"))[["a"]], + render_formats_test(tbl_html, "html")[["a"]], c( "1 (7.1%)", "5 (35.7%)", "0", "2 (14.3%)", "NA", "6 (42.9%)", "5", "NA", "0", "NA" @@ -881,7 +881,7 @@ test_that("cols_merge_n_pct() works correctly", { ) expect_equal( - (tbl_html %>% render_formats_test("html"))[["b"]], + render_formats_test(tbl_html, "html")[["b"]], c( "7.1%", "35.7%", "0.0%", "14.3%", "NA", "42.9%", "NA", "100,000.0%", "NA", diff --git a/tests/testthat/test-fmt_currency.R b/tests/testthat/test-fmt_currency.R index 577e668203..c8eee32a79 100644 --- a/tests/testthat/test-fmt_currency.R +++ b/tests/testthat/test-fmt_currency.R @@ -534,18 +534,10 @@ test_that("The currency() helper works correctly", { # Expect that the object produced by `currency()` is a # list with `gt_currency` class - expect_type( - currency(html = "₿", latex = "BTC", default = "BTC"), - "list" - ) - - expect_s3_class( - currency(html = "₿", latex = "BTC", default = "BTC"), - "gt_currency" - ) - - # Expect as many components as there are named arguments - expect_length(currency(html = "₿", latex = "BTC", default = "BTC"), 3) + cur <- currency(html = "₿", latex = "BTC", default = "BTC") + expect_type(cur, "list") + expect_s3_class(cur, "gt_currency") + expect_length(cur, 3) expect_length(currency(html = "₿", default = "BTC"), 2) expect_length(currency(default = "BTC"), 1) @@ -553,12 +545,14 @@ test_that("The currency() helper works correctly", { # to the `default` context single_default_currency <- currency("BTC") - single_default_currency %>% expect_type("list") - single_default_currency %>% expect_s3_class("gt_currency") - single_default_currency %>% expect_length(1) - single_default_currency %>% expect_named("default") - single_default_currency[[1]] %>% expect_equal("BTC") + expect_type(single_default_currency, "list") + expect_s3_class(single_default_currency, "gt_currency") + expect_length(single_default_currency, 1) + expect_named(single_default_currency, "default") + expect_equal(single_default_currency[[1]], "BTC") +}) +test_that("currency() errors" , { # Expect an error if nothing is provided expect_error(currency()) @@ -574,7 +568,9 @@ test_that("The currency() helper works correctly", { # Expect an error if there are duplicate names expect_error(currency(html = "₿", default = "BTC", default = "BT")) +}) +test_that("fmt_currency() works with the currency() helper", { # Create an input data frame four columns: two # character-based and two that are numeric data_tbl <- diff --git a/tests/testthat/test-gt_object.R b/tests/testthat/test-gt_object.R index c4fd5b5d4a..72239fb2b2 100644 --- a/tests/testthat/test-gt_object.R +++ b/tests/testthat/test-gt_object.R @@ -130,9 +130,8 @@ test_that("A gt table can be made from a table with no rows", { expect_tab(tab = tab, df = data_e) # Expect that the `stub_df` data frame is empty - dt_stub_df_get(data = tab) %>% - nrow() %>% - expect_equal(0) + n_rows <- nrow(dt_stub_df_get(data = tab)) + expect_equal(n_rows, 0) # Create a `gt_tbl` object with `gt()` and a # grouped version of the `data_e` dataset @@ -539,33 +538,30 @@ test_that("rowname_col in gt() will be overridden by `rownames_to_stub = TRUE`", build_data(context = "html") # Expect that no groups are available in the gt object - built_tbl$`_row_groups` %>% expect_equal(character(0L)) - + expect_equal(built_tbl$`_row_groups`, character(0L)) # Expect no rows in `_groups_rows` - built_tbl$`_groups_rows` %>% - nrow() %>% - expect_equal(0) + expect_equal(nrow(built_tbl$`_groups_rows`), 0L) - built_tbl$`_boxhead` %>% .[, 1:2] %>% - expect_equal( - dplyr::tibble( - var = c("__GT_ROWNAME_PRIVATE__", colnames(mtcars)), - type = c("stub", rep("default", 11)) - ) + expect_equal( + built_tbl$`_boxhead`[, 1:2], + dplyr::tibble( + var = c("__GT_ROWNAME_PRIVATE__", colnames(mtcars)), + type = c("stub", rep("default", 11)) ) + ) - built_tbl$`_stub_df` %>% - expect_equal( - dplyr::tibble( - rownum_i = 1:10, - row_id = rownames(mtcars)[1:10], - group_id = NA_character_, - group_label = list(NULL), - indent = NA_character_, - built_group_label = "" - ), - ignore_attr = TRUE - ) + expect_equal( + built_tbl$`_stub_df`, + dplyr::tibble( + rownum_i = 1:10, + row_id = rownames(mtcars)[1:10], + group_id = NA_character_, + group_label = list(NULL), + indent = NA_character_, + built_group_label = "" + ), + ignore_attr = TRUE + ) # Render the HTML table and read the HTML with the `xml2::read_html()` fn html_tbl <- diff --git a/tests/testthat/test-opt_functions.R b/tests/testthat/test-opt_functions.R index ddf22bd690..b34465aa8e 100644 --- a/tests/testthat/test-opt_functions.R +++ b/tests/testthat/test-opt_functions.R @@ -1,8 +1,3 @@ -# Helper function to compare a contiguous set of HTML fragments with raw html -html_fragment_within <- function(raw_html, ...) { - grepl(paste0("\\Q", c(...), "\\E", "[\\n\\s]*?", collapse = ""), raw_html, perl = TRUE) -} - test_that("opt_footnote_marks() sets the correct options", { set_marks <- c("*", "+", "~", "<", ">") diff --git a/tests/testthat/test-summary_rows.R b/tests/testthat/test-summary_rows.R index 9d7d0a80c4..fc0eb705b5 100644 --- a/tests/testthat/test-summary_rows.R +++ b/tests/testthat/test-summary_rows.R @@ -39,43 +39,36 @@ test_that("summary_rows() can make group-wise summaries", { # Expect that the internal `summary` list object has a length # of `1` since there was only one call of `summary_rows()` - length(summary) %>% expect_equal(1) + expect_length(summary, 1) # For the single list component in `summary`, expect specific # names within it - summary[[1]] %>% - expect_named( - c( - "groups", "columns", "fns", "fmt", "side", - "missing_text", "formatter", "formatter_options" - ) + expect_named( + summary[[1]], + c( + "groups", "columns", "fns", "fmt", "side", + "missing_text", "formatter", "formatter_options" ) + ) # Expect the `groups` provided in `summary[[1]]$groups` - summary[[1]]$groups %>% expect_equal("W02") + expect_equal(summary[[1]]$groups, "W02") # Expect the `columns` provided in `summary[[1]]$columns` - summary[[1]]$columns %>% expect_equal(c("open", "high", "low", "close")) - - # Expect that `summary[[1]]$fns` is a `list` object - summary[[1]]$fns %>% expect_type("list") + expect_equal(summary[[1]]$columns , c("open", "high", "low", "close")) + expect_type(summary[[1]]$fns, "list") # Expect that the components of `summary[[1]]$fns` are lists - summary[[1]]$fns$average %>% expect_type("list") - summary[[1]]$fns$total %>% expect_type("list") - summary[[1]]$fns$`std dev` %>% expect_type("list") - - # Expect that `summary[[1]]$missing_text` has a specific value - summary[[1]]$missing_text %>% expect_equal("---") - - # Expect that `summary[[1]]$formatter` is NULL - expect_null(summary[[1]]$formatter) - - # Expect that `summary[[1]]$formatter_options` is a list + expect_type(summary[[1]]$fns$average, "list") + expect_type(summary[[1]]$fns$total, "list") + expect_type(summary[[1]]$fns$`std dev`, "list") + + # Expect that + expect_equal(summary[[1]]$missing_text, "---") + expect_equal(summary[[1]]$formatter, NULL) + # expect a zero-length list expect_type(summary[[1]]$formatter_options, "list") - - # Expect that `summary[[1]]$formatter_options` is of length 0 - summary[[1]]$formatter_options %>% expect_length(0) + expect_length(summary[[1]]$formatter_options, 0) # Create a table with summary rows for the `W02` group; # the 3 summary rows for this group represent the mean, sum, @@ -1761,8 +1754,8 @@ test_that("Extracting a summary from a gt table is possible", { # Expect that each component of the list inherits # from `tbl_df` - expect_s3_class(gt_tbl_summary_groupwise$summary_df_data_list[[1]], "tbl_df") - expect_s3_class(gt_tbl_summary_groupwise$summary_df_data_list[[2]], "tbl_df") + expect_s3_class(gt_tbl_summary_groupwise$summary_df_data_list[[1]], "data.frame") + expect_s3_class(gt_tbl_summary_groupwise$summary_df_data_list[[2]], "data.frame") # Expect specific column names for each of the # tibbles in `gt_tbl_summary_groupwise` @@ -2294,7 +2287,7 @@ test_that("normalize_summary_fns() works with a variety of inputs", { out <- normalize_summary_fns(fns = fns) - expect_equal(length(out), 1) + expect_length(out, 1) expect_type(out, "list") # Get id @@ -2510,7 +2503,7 @@ test_that("normalize_fmt_fns() works with a variety of inputs", { out <- normalize_fmt_fns(fmt = fmt) - expect_equal(length(out), 1) + expect_length(out, 1) expect_type(out, "list") # Get formula diff --git a/tests/testthat/test-tab_footnote.R b/tests/testthat/test-tab_footnote.R index 612eee5c3e..9121c17cac 100644 --- a/tests/testthat/test-tab_footnote.R +++ b/tests/testthat/test-tab_footnote.R @@ -782,29 +782,29 @@ test_that("The `list_of_summaries` table is structured correctly", { gtcars_built_summary_df_display <- dt_summary_df_display_get(data = gtcars_built) # Expect that the list of summaries has length `2` - expect_equal(length(gtcars_built_summary_df), 2) + expect_length(gtcars_built_summary_df, 2) # Expect specific names in the `list_of_summaries` list - expect_equal( - names(gtcars_built_summary_df), + expect_named( + gtcars_built_summary_df, c("summary_df_data_list", "summary_df_display_list") ) # Expect three tibbles in the `summary_df_data_list` component - expect_equal(length(gtcars_built_summary_df_data$summary_df_data_list), 3) + expect_length(gtcars_built_summary_df_data$summary_df_data_list, 3) # Expect three tibbles in the `summary_df_display_list` component - expect_equal(length(gtcars_built_summary_df_display$summary_df_display_list), 3) + expect_length(gtcars_built_summary_df_display$summary_df_display_list, 3) # Expect specific names for the subcomponents of the # `summary_df_data_list` and `summary_df_data_list` # parent components - expect_equal( - names(gtcars_built_summary_df_data$summary_df_data_list), + expect_named( + gtcars_built_summary_df_data$summary_df_data_list, c("BMW", "Audi", "::GRAND_SUMMARY") ) - expect_equal( - names(gtcars_built_summary_df_display$summary_df_display_list), + expect_named( + gtcars_built_summary_df_display$summary_df_display_list, c("::GRAND_SUMMARY", "Audi", "BMW") ) From eea466b40e466f058f73bb1460ff8e243934a03a Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 25 Jun 2024 21:29:38 -0400 Subject: [PATCH 2/5] Refactor locale test inside helper function --- tests/testthat/helper.R | 13 +++ tests/testthat/test-cols_merge.R | 195 ++++++++++++------------------- 2 files changed, 87 insertions(+), 121 deletions(-) diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index aa0db56c56..4a18c2787a 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -28,3 +28,16 @@ html_fragment_within <- function(raw_html, ...) { # Create a shortened version of `mtcars` mtcars_short <- datasets::mtcars[1:5, ] + +expect_merge_locale_sep <- function(locale = NULL, global_locale = NULL, sep = NULL, expected_sep) { + tbl <- data.frame( + col_1 = 1, + col_2 = 2, + col_3 = 3, + col_4 = 4 + ) + gt_tbl <- gt(tbl, locale = global_locale) + merged_gt <- cols_merge_range(gt_tbl, col_begin = "col_1", col_end = "col_2", locale = locale, sep = sep) + actual_merge_sep <- dt_col_merge_get(merged_gt)[[1]]$sep + expect_equal(actual_merge_sep, expected_sep, label = paste0("locale = ", locale)) +} diff --git a/tests/testthat/test-cols_merge.R b/tests/testthat/test-cols_merge.R index 1b3556386a..5c79ec085d 100644 --- a/tests/testthat/test-cols_merge.R +++ b/tests/testthat/test-cols_merge.R @@ -540,9 +540,6 @@ test_that("cols_merge_uncert() works nicely with different error bounds", { test_that("cols_merge_range() works correctly", { - # Check that specific suggested packages are available - check_suggests() - # Create a `tbl_html` object with `gt()`; merge two columns # with `cols_merge_range()` tbl_html <- @@ -553,18 +550,15 @@ test_that("cols_merge_range() works correctly", { col_end = "col_2" ) - # Expect that merging statements are stored in `col_merge`\ - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("col_1", "col_2")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_range") + merged_range <- dt_col_merge_get(tbl_html) + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_range[[1]]$pattern, "{1}{sep}{2}") + expect_equal(merged_range[[1]]$vars, c("col_1", "col_2")) + expect_equal(merged_range[[1]]$type, "merge_range") + expect_equal(merged_range[[1]]$sep, "\U2013") +}) - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep %>% - expect_equal("\U2013") +test_that("cols_merge_range works 2", { # Create a `tbl_html` object with `gt()`; merge two columns # with `cols_merge_range()` @@ -576,95 +570,59 @@ test_that("cols_merge_range() works correctly", { col_end = col_2 ) + merged_range <- dt_col_merge_get(data = tbl_html) # Expect that merging statements are stored in `col_merge`\ - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("col_1", "col_2")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_range") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep %>% - expect_equal("\U2013") + expect_equal(merged_range[[1]]$pattern, "{1}{sep}{2}") + expect_equal(merged_range[[1]]$vars, c("col_1", "col_2")) + expect_equal(merged_range[[1]]$type, "merge_range") + expect_equal(merged_range[[1]]$sep, "\U2013") +}) +test_that("cols_merge_range() works with 2 statements", { # Create a `tbl_html` object with `gt()`; merge two columns, twice, # with `cols_merge_range()` tbl_html <- tbl %>% gt() %>% - cols_merge_range( - col_begin = col_1, - col_end = col_2 - ) %>% - cols_merge_range( - col_begin = col_3, - col_end = col_4 - ) + cols_merge_range(col_begin = col_1, col_end = col_2) %>% + cols_merge_range(col_begin = col_3, col_end = col_4) - # Expect that merging statements are stored in `col_merge`\ - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("col_1", "col_2")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_range") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep %>% - expect_equal("\U2013") - - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") + merged_range <- dt_col_merge_get(data = tbl_html) + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_range[[1]]$pattern, "{1}{sep}{2}") + expect_equal(merged_range[[1]]$vars, c("col_1", "col_2")) + expect_equal(merged_range[[1]]$type, "merge_range") + expect_equal(merged_range[[1]]$sep, "\U2013") - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$vars %>% - expect_equal(c("col_3", "col_4")) + expect_equal(merged_range[[2]]$pattern, "{1}{sep}{2}") + expect_equal(merged_range[[2]]$vars, c("col_3", "col_4")) + expect_equal(merged_range[[2]]$type, "merge_range") + expect_equal(merged_range[[2]]$sep, "\U2013") - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$type %>% - expect_equal("merge_range") +}) - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$sep %>% - expect_equal("\U2013") - - # Expect a variety of different range separators depending on - # the `locale` value provided to `cols_merge_range()` - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "en") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("\U2013") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "es") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("-") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "nl") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("-") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "zh") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("-") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "ja") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("\UFF5E") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "ko") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("~") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "to") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("\U2014") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "bg") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal(" \U2013 ") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "bs") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal(" \U2013 ") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "pt") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("\U2013") - gt(tbl) %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "pt-PT") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal(" - ") +test_that("cols_merge_range() respects locale for separators", { + expect_merge_locale_sep(locale = "en", expected_sep = "\U2013") + expect_merge_locale_sep(locale = "es", expected_sep = "-") + expect_merge_locale_sep(locale = "nl", expected_sep = "-") + expect_merge_locale_sep(locale = "zh", expected_sep = "-") + expect_merge_locale_sep(locale = "ja", expected_sep = "\UFF5E") + expect_merge_locale_sep(locale = "ko", expected_sep = "~") + expect_merge_locale_sep(locale = "to", expected_sep = "\U2014") + expect_merge_locale_sep(locale = "bg", expected_sep = " \U2013 ") + expect_merge_locale_sep(locale = "bs", expected_sep = " \U2013 ") + expect_merge_locale_sep(locale = "pt", expected_sep = "\U2013") + expect_merge_locale_sep(locale = "pt-PT", expected_sep = " - ") # Expect that different `locale` priorities and `sep` overrides work correctly - gt(tbl, locale = "pt-PT") %>% cols_merge_range(col_begin = "col_1", col_end = "col_2") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal(" - ") - gt(tbl, locale = "ja") %>% cols_merge_range(col_begin = "col_1", col_end = "col_2") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("\UFF5E") - gt(tbl, locale = "ja") %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "ja") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("\UFF5E") - gt(tbl, locale = "ja") %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", locale = "en") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("\U2013") - gt(tbl, locale = "ja") %>% cols_merge_range(col_begin = "col_1", col_end = "col_2", sep = "q", locale = "ko") %>% - dt_col_merge_get() %>% .[[1]] %>% .$sep %>% expect_equal("q") + expect_merge_locale_sep(global_locale = "pt-PT", expected_sep = " - ") + expect_merge_locale_sep(global_locale = "ja", expected_sep = "\UFF5E") + expect_merge_locale_sep(global_locale = "ja", locale = "ja", expected_sep = "\UFF5E") + expect_merge_locale_sep(global_locale = "ja", locale = "en", expected_sep = "\U2013") + expect_merge_locale_sep(global_locale = "ja", locale = "ko", sep = "q", expected_sep = "q") +}) +test_that("cols_merge_range() works", { # Create a `tbl_html` object with `gt()`; merge two # columns with `cols_merge_range()` but use the `I()` # function to keep the `--` separator text as is @@ -677,23 +635,17 @@ test_that("cols_merge_range() works correctly", { sep = I("--") ) - # Expect that merging statements are stored in `col_merge`\ - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("col_1", "col_2")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_range") + merged_res <- dt_col_merge_get(data = tbl_html)[[1]] - # Get the separator object - sep <- dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1}{sep}{2}") + expect_equal(merged_res$vars, c("col_1", "col_2")) + expect_equal(merged_res$type, "merge_range") # Expect that `sep` has the `AsIs` class - expect_s3_class(sep, "AsIs") - expect_equal(as.character(sep), "--") - expect_equal(sep, I("--")) + expect_s3_class(merged_res$sep, "AsIs") + expect_equal(as.character(merged_res$sep), "--") + expect_equal(merged_res$sep, I("--")) # Create a `tbl_html` object with `gt()`; merge two # columns with `cols_merge_range()` but use the `I()` @@ -707,24 +659,21 @@ test_that("cols_merge_range() works correctly", { sep = I("---") ) - # Expect that merging statements are stored in `col_merge`\ - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("col_1", "col_2")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_range") + # Expect that merging statements are stored in `col_merge` + merged_res <- dt_col_merge_get(data = tbl_html)[[1]] - # Get the separator object - sep <- dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1}{sep}{2}") + expect_equal(merged_res$vars, c("col_1", "col_2")) + expect_equal(merged_res$type, "merge_range") # Expect that `sep` has the `AsIs` class - expect_s3_class(sep, "AsIs") - expect_equal(as.character(sep), "---") - expect_equal(sep, I("---")) + expect_s3_class(merged_res$sep, "AsIs") + expect_equal(as.character(merged_res$sep), "---") + expect_equal(merged_res$sep, I("---")) +}) +test_that("cols_merge_range() works well", { # Create two gt table objects; the first will be based # on `tbl` while the second will use a different column name # in `tbl` (`sep`) that collides with a pattern element name @@ -747,8 +696,8 @@ test_that("cols_merge_range() works correctly", { # Expect that the HTML produced from the two tables is the same expect_identical( - tbl_html_1 %>% as_raw_html() %>% gsub("id=\"[a-z]*?\"", "", .), - tbl_html_2 %>% as_raw_html() %>% gsub("id=\"[a-z]*?\"", "", .) + gsub("id=\"[a-z]*?\"", "", as_raw_html(tbl_html_1)), + gsub("id=\"[a-z]*?\"", "", as_raw_html(tbl_html_2)) ) # Create another variant that renames `col_2` as `1`, which @@ -765,10 +714,14 @@ test_that("cols_merge_range() works correctly", { # Expect that the HTML produced from `tbl_html_2` and # `tbl_html_3` is the same expect_identical( - tbl_html_2 %>% as_raw_html() %>% gsub("id=\"[a-z]*?\"", "", .), - tbl_html_3 %>% as_raw_html() %>% gsub("id=\"[a-z]*?\"", "", .) + gsub("id=\"[a-z]*?\"", "", as_raw_html(tbl_html_2)), + gsub("id=\"[a-z]*?\"", "", as_raw_html(tbl_html_3)) ) +}) +test_that("cols_merge_range() produces the correct output", { + + check_suggests() # # Expect that the column set as the row group can participate # in column merging through `col_merge_range()` From 26859d1c75cefd914c8c960407665d1f04ebd624 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 25 Jun 2024 21:38:53 -0400 Subject: [PATCH 3/5] Other test refactoring --- tests/testthat/test-opt_functions.R | 22 ++++++------- tests/testthat/test-util_functions.R | 47 ++++++++++++++-------------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/tests/testthat/test-opt_functions.R b/tests/testthat/test-opt_functions.R index b34465aa8e..ace17296e5 100644 --- a/tests/testthat/test-opt_functions.R +++ b/tests/testthat/test-opt_functions.R @@ -355,14 +355,14 @@ test_that("opt_table_font() sets the correct options", { # Expect that equally valid inputs include wrapping in a list or `c()` expect_equal( - tbl %>% opt_table_font(font = google_font(name = "Dancing Script")) %>% compile_scss(), - tbl %>% opt_table_font(font = list(google_font(name = "Dancing Script"))) %>% compile_scss(), + opt_table_font(tbl, font = google_font(name = "Dancing Script")) %>% compile_scss(), + opt_table_font(tbl, font = list(google_font(name = "Dancing Script"))) %>% compile_scss(), ignore_attr = TRUE ) expect_equal( - tbl %>% opt_table_font(font = google_font(name = "Dancing Script")) %>% compile_scss(), - tbl %>% opt_table_font(font = c(google_font(name = "Dancing Script"))) %>% compile_scss(), + opt_table_font(tbl, font = google_font(name = "Dancing Script")) %>% compile_scss(), + opt_table_font(tbl, font = c(google_font(name = "Dancing Script"))) %>% compile_scss(), ignore_attr = TRUE ) @@ -425,16 +425,16 @@ test_that("opt_table_font() sets the correct options", { # Expect that weights given as numbers or strings create the # same outputs (e.g., 500 and "500") expect_equal( - tbl %>% opt_table_font(font = google_font(name = "Dancing Script"), weight = 500) %>% compile_scss(), - tbl %>% opt_table_font(font = google_font(name = "Dancing Script"), weight = "500") %>% compile_scss(), + opt_table_font(tbl, font = google_font(name = "Dancing Script"), weight = 500) %>% compile_scss(), + opt_table_font(tbl, font = google_font(name = "Dancing Script"), weight = "500") %>% compile_scss(), ignore_attr = TRUE ) # Expect an error if input to `font` is not a character vector # or a list (but no errors otherwise) - expect_error(tbl %>% opt_table_font(font = c(TRUE, FALSE))) - expect_error(tbl %>% opt_table_font(font = 1:3)) - expect_no_error(tbl %>% opt_table_font(font = c("Courier", "Comic Sans MS"))) - expect_no_error(tbl %>% opt_table_font(font = list("Courier", "Comic Sans MS"))) - expect_no_error(tbl %>% opt_table_font(font = LETTERS)) + expect_error(opt_table_font(tbl, font = c(TRUE, FALSE))) + expect_error(opt_table_font(tbl, font = 1:3)) + expect_no_error(opt_table_font(tbl, font = c("Courier", "Comic Sans MS"))) + expect_no_error(opt_table_font(tbl, font = list("Courier", "Comic Sans MS"))) + expect_no_error(opt_table_font(tbl, font = LETTERS)) }) diff --git a/tests/testthat/test-util_functions.R b/tests/testthat/test-util_functions.R index 8f0783c610..ad559e3ddc 100644 --- a/tests/testthat/test-util_functions.R +++ b/tests/testthat/test-util_functions.R @@ -223,31 +223,31 @@ test_that("get_currency_exponent() works correctly", { # Expect that various currency codes (3-letter) # return a currency exponent - get_currency_exponent(currency = "BIF") %>% - expect_equal(0) + bif_exp <- get_currency_exponent(currency = "BIF") + expect_equal(bif_exp, 0) - get_currency_exponent(currency = "AED") %>% - expect_equal(2) + aed_exp <- get_currency_exponent(currency = "AED") + expect_equal(aed_exp, 2) - get_currency_exponent(currency = "TND") %>% - expect_equal(3) + tnd_exp <- get_currency_exponent(currency = "TND") + expect_equal(tnd_exp, 3) - get_currency_exponent(currency = "CLF") %>% - expect_equal(4) + clf_exp <- get_currency_exponent(currency = "CLF") + # other code + clf_exp2 <- get_currency_exponent(currency = 990) + expect_equal(clf_exp, 4) + expect_equal(clf_exp2, 4) # Expect that various currency codes (3-number) # return a currency exponent - get_currency_exponent(currency = "533") %>% - expect_equal(2) + awg_exp <- get_currency_exponent(currency = "533") + expect_equal(awg_exp, 2) - get_currency_exponent(currency = "152") %>% - expect_equal(0) + clp_exp <- get_currency_exponent(currency = "152") + expect_equal(clp_exp, 0) - get_currency_exponent(currency = 990) %>% - expect_equal(4) - - get_currency_exponent(currency = 886) %>% - expect_equal(2) + yer_exp <- get_currency_exponent(currency = 886) + expect_equal(yer_exp, 2) # Expect an exponent of 0 if the currency # exponent field is NA @@ -274,10 +274,9 @@ test_that("process_text() works correctly", { # Expect that text with the class `character` will # be returned from `process_text` as is - process_text(text = simple_text) %>% - expect_equal(simple_text) - - simple_text %>% expect_type("character") + processed <- process_text(text = simple_text) + expect_equal(processed, simple_text) + expect_type(processed, "character") # Expect that text with the class `from_markdown` will # be returned from `process_text` as character-based @@ -285,7 +284,7 @@ test_that("process_text() works correctly", { process_text(text = md_text) %>% expect_equal("this is text interpreted as markdown") - md_text %>% expect_s3_class("from_markdown") + expect_s3_class(md_text, "from_markdown") process_text(text = md_text) %>% expect_type("character") # Expect that text with the class `html` will @@ -294,8 +293,8 @@ test_that("process_text() works correctly", { process_text(text = html_text) %>% expect_equal(as.character(html_text)) - html_text %>% expect_s3_class("html") - html_text %>% expect_type("character") + expect_s3_class(html_text, "html") + expect_type(html_text, "character") process_text(text = html_text) %>% expect_type("character") }) From ec0f39624d5b664869f50fad48705bd3e3093894 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 25 Jun 2024 21:49:50 -0400 Subject: [PATCH 4/5] Refactor: mutate_all removed. --- R/dt_summary.R | 36 ++++++++++-------------------------- man/extract_summary.Rd | 40 ++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 50 deletions(-) diff --git a/R/dt_summary.R b/R/dt_summary.R index 0850192f1b..63d01d886c 100644 --- a/R/dt_summary.R +++ b/R/dt_summary.R @@ -229,6 +229,8 @@ dt_summary_build <- function(data, context) { .data[[group_id_col_private]] ) + # TODO find a way to switch to across() + # https://github.com/tidyverse/dplyr/issues/6707 select_data_tbl <- dplyr::ungroup( dplyr::summarize_at( @@ -238,14 +240,8 @@ dt_summary_build <- function(data, context) { ) ) - select_data_tbl <- - dplyr::mutate_all( - select_data_tbl, - .funs = function(x) { - x[is.nan(x)] <- NA - x - } - ) + # Replace NaN by NA + select_data_tbl[is.na(select_data_tbl)] <- NA select_data_tbl <- dplyr::mutate( @@ -297,14 +293,9 @@ dt_summary_build <- function(data, context) { locale = resolve_locale(data = data, locale = NULL) ) - summary_dfs_display_gt[["_data"]] <- - dplyr::mutate_all( - summary_dfs_display_gt[["_data"]], - .funs = function(x) { - x[is.nan(x)] <- NA - x - } - ) + # Replace NaN with NA + summary_dfs_display_gt[["_data"]][is.na(summary_dfs_display_gt[["_data"]])] <- + NA summary_dfs_display_gt[["_stub_df"]] <- dplyr::mutate( @@ -321,7 +312,7 @@ dt_summary_build <- function(data, context) { # Determine if we are actually formatting a grand summary section; # in that case we'd want to ignore any supplied group directive group_is_grand_summary <- - length(groups) == 1 && groups == "::GRAND_SUMMARY" + identical(groups, "::GRAND_SUMMARY") if (!is.null(format_lhs) && !group_is_grand_summary) { @@ -348,7 +339,7 @@ dt_summary_build <- function(data, context) { # formatted to the group fmt_expr_lines <- deparse(rlang::f_rhs(fmt_exprs[[k]])) - fmt_expr_lines <- gsub("^\\s+", "", fmt_expr_lines) + fmt_expr_lines <- trimws(fmt_expr_lines, "left", " ") format_fn_grp <- paste(fmt_expr_lines, collapse = "") fmt_expr_names <- names(rlang::f_rhs(fmt_exprs[[k]])) @@ -416,14 +407,7 @@ dt_summary_build <- function(data, context) { summary_dfs_display[["::group_id::"]] <- summary_dfs_data[["::group_id::"]] summary_dfs_display[["::row_id::"]] <- summary_dfs_data[["::row_id::"]] - summary_dfs_display <- - dplyr::mutate_all( - summary_dfs_display, - .funs = function(x) { - x[x == "NA"] <- NA - x - } - ) + summary_dfs_display[summary_dfs_display == "NA"] <- NA summary_dfs_display$`::rowname::` <- NA_character_ diff --git a/man/extract_summary.Rd b/man/extract_summary.Rd index 0b5ddce1f3..90cb3631aa 100644 --- a/man/extract_summary.Rd +++ b/man/extract_summary.Rd @@ -55,36 +55,28 @@ rows as a list object. summary_extracted #> $summary_df_data_list #> $summary_df_data_list$W02 -#> # A tibble: 3 x 9 -#> group_id row_id rowname date open high low close week -#> -#> 1 W02 min min NA 2006. 2030. 1992. 2003. NA -#> 2 W02 max max NA 2063. 2064. 2038. 2062. NA -#> 3 W02 avg avg NA 2035. 2049. 2017. 2031. NA +#> group_id row_id rowname date open high low close week +#> 1 W02 min min NA 2005.55 2029.610 1992.440 2002.610 NA +#> 2 W02 max max NA 2063.45 2064.430 2038.330 2062.140 NA +#> 3 W02 avg avg NA 2035.24 2048.562 2016.854 2031.208 NA #> #> $summary_df_data_list$W03 -#> # A tibble: 3 x 9 -#> group_id row_id rowname date open high low close week -#> -#> 1 W03 min min NA 1992. 2018. 1988. 1993. NA -#> 2 W03 max max NA 2046. 2057. 2023. 2028. NA -#> 3 W03 avg avg NA 2020. 2033. 2000. 2015. NA +#> group_id row_id rowname date open high low close week +#> 1 W03 min min NA 1992.250 2018.400 1988.120 1992.67 NA +#> 2 W03 max max NA 2046.130 2056.930 2022.580 2028.26 NA +#> 3 W03 avg avg NA 2020.422 2033.288 1999.772 2014.93 NA #> #> $summary_df_data_list$W04 -#> # A tibble: 3 x 9 -#> group_id row_id rowname date open high low close week -#> -#> 1 W04 min min NA 2020. 2029. 2004. 2023. NA -#> 2 W04 max max NA 2063. 2065. 2051. 2063. NA -#> 3 W04 avg avg NA 2035. 2049. 2023. 2042. NA +#> group_id row_id rowname date open high low close week +#> 1 W04 min min NA 2020.190 2028.940 2004.490 2022.55 NA +#> 2 W04 max max NA 2062.980 2064.620 2050.540 2063.15 NA +#> 3 W04 avg avg NA 2034.557 2048.707 2023.362 2042.41 NA #> #> $summary_df_data_list$W05 -#> # A tibble: 3 x 9 -#> group_id row_id rowname date open high low close week -#> -#> 1 W05 min min NA 2002. 2023. 1989. 1995. NA -#> 2 W05 max max NA 2050. 2058. 2041. 2057. NA -#> 3 W05 avg avg NA 2030. 2039. 2009. 2021. NA +#> group_id row_id rowname date open high low close week +#> 1 W05 min min NA 2002.450 2023.320 1989.180 1994.990 NA +#> 2 W05 max max NA 2050.420 2057.620 2040.970 2057.090 NA +#> 3 W05 avg avg NA 2030.484 2039.186 2008.986 2021.008 NA }\if{html}{\out{}} Use the summary list to make a new \strong{gt} table. The key thing is to use From f29fbfe1472d37dee3f1c452ddadf3c32cd2be05 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 25 Jun 2024 22:15:06 -0400 Subject: [PATCH 5/5] Finalize test refactoring in `cols_merge()` to be standardized. --- tests/testthat/_snaps/cols_merge.md | 2 +- tests/testthat/test-cols_merge.R | 209 ++++++++++++---------------- 2 files changed, 89 insertions(+), 122 deletions(-) diff --git a/tests/testthat/_snaps/cols_merge.md b/tests/testthat/_snaps/cols_merge.md index 9f7391b09c..dcb5a15d68 100644 --- a/tests/testthat/_snaps/cols_merge.md +++ b/tests/testthat/_snaps/cols_merge.md @@ -26,7 +26,7 @@ Output [1] "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n \n \n \n
mpgcyldisphpdratqsecvsamgearcarb
21.061601103.9016.460144
21.061601103.90 (2.875)17.020144
22.84108933.8518.611141
21.462581103.08 (3.215)19.441031
18.783601753.1517.020032
" -# cols_merge_uncert() works correctly +# cols_merge_uncert() works with row groups Code . diff --git a/tests/testthat/test-cols_merge.R b/tests/testthat/test-cols_merge.R index 5c79ec085d..50454bcb1e 100644 --- a/tests/testthat/test-cols_merge.R +++ b/tests/testthat/test-cols_merge.R @@ -48,15 +48,12 @@ test_that("cols_merge() works correctly", { pattern = "{1} ({2})" ) - # Expect that merging statements are stored in `col_merge` - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1} ({2})") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("drat", "wt")) + merged_res <- dt_col_merge_get(data = tbl_html)[[1]] - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge") + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1} ({2})") + expect_equal(merged_res$vars, c("drat", "wt")) + expect_equal(merged_res$type, "merge") # Create a `tbl_html` object with `gt()`; merge two columns # with a `pattern` and use `c()` @@ -69,15 +66,12 @@ test_that("cols_merge() works correctly", { pattern = "{1} ({2})" ) - # Expect that merging statements are stored in `col_merge` - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1} ({2})") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("drat", "wt")) + merged_res <- dt_col_merge_get(data = tbl_html)[[1]] - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge") + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1} ({2})") + expect_equal(merged_res$vars, c("drat", "wt")) + expect_equal(merged_res$type, "merge") # Create a `tbl_html` object with `gt()`; merge two columns, twice, # with two different `pattern`s; use `c()` @@ -96,23 +90,19 @@ test_that("cols_merge() works correctly", { ) # Expect that merging statements are stored in `col_merge` - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1} ({2})") + merged_res1 <- dt_col_merge_get(data = tbl_html)[[1]] - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("drat", "wt")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge") - - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$pattern %>% - expect_equal("{1}-{2}") + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res1$pattern, "{1} ({2})") + expect_equal(merged_res1$vars, c("drat", "wt")) + expect_equal(merged_res1$type, "merge") - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$vars %>% - expect_equal(c("gear", "carb")) + merged_res2 <- dt_col_merge_get(data = tbl_html)[[2]] - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$type %>% - expect_equal("merge") + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res2$pattern, "{1}-{2}") + expect_equal(merged_res2$vars, c("gear", "carb")) + expect_equal(merged_res2$type, "merge") # Expect a warning if additional, out of scope columns, are # included in `hide_columns` @@ -325,21 +315,16 @@ test_that("cols_merge_uncert() works correctly", { col_uncert = "col_2" ) - # Expect that merging statements are stored in `col_merge` - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("col_1", "col_2")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_uncert") + merged_res <- dt_col_merge_get(data = tbl_html)[[1]] - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep %>% - expect_equal(" +/- ") + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1}{sep}{2}") + expect_equal(merged_res$vars, c("col_1", "col_2")) + expect_equal(merged_res$type, "merge_uncert") + expect_equal(merged_res$sep, " +/- ") # Create a `tbl_html` object with `gt()`; merge two columns - # with `cols_merge_uncert()` and use `c()` + # with `cols_merge_uncert()` and tidyselect tbl_html <- tbl %>% gt() %>% @@ -349,17 +334,13 @@ test_that("cols_merge_uncert() works correctly", { ) # Expect that merging statements are stored in `col_merge` - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("col_1", "col_2")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_uncert") + merged_res <- dt_col_merge_get(data = tbl_html)[[1]] - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep %>% - expect_equal(" +/- ") + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1}{sep}{2}") + expect_equal(merged_res$vars, c("col_1", "col_2")) + expect_equal(merged_res$type, "merge_uncert") + expect_equal(merged_res$sep, " +/- ") # Create a `tbl_html` object with `gt()`; merge two columns, twice, # with `cols_merge_uncert()` @@ -376,29 +357,22 @@ test_that("cols_merge_uncert() works correctly", { ) # Expect that merging statements are stored in `col_merge` - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("col_1", "col_2")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_uncert") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep %>% - expect_equal(" +/- ") - - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") + merged_res <- dt_col_merge_get(data = tbl_html)[[1]] - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$vars %>% - expect_equal(c("col_3", "col_4")) + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1}{sep}{2}") + expect_equal(merged_res$vars, c("col_1", "col_2")) + expect_equal(merged_res$type, "merge_uncert") + expect_equal(merged_res$sep, " +/- ") - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$type %>% - expect_equal("merge_uncert") + # Expect that merging statements are stored in `col_merge` + merged_res <- dt_col_merge_get(data = tbl_html)[[2]] - dt_col_merge_get(data = tbl_html) %>% .[[2]] %>% .$sep %>% - expect_equal(" +/- ") + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1}{sep}{2}") + expect_equal(merged_res$vars, c("col_3", "col_4")) + expect_equal(merged_res$type, "merge_uncert") + expect_equal(merged_res$sep, " +/- ") # Create a `tbl_html` object with `gt()`; merge two # columns with `cols_merge_uncert()` but use the `I()` @@ -412,28 +386,24 @@ test_that("cols_merge_uncert() works correctly", { sep = I(" +/- ") ) - # Expect that merging statements are stored in `col_merge`\ - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("col_1", "col_2")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_uncert") + # Expect that merging statements are stored in `col_merge` + merged_res <- dt_col_merge_get(data = tbl_html)[[1]] - # Get the separator object - sep <- dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1}{sep}{2}") + expect_equal(merged_res$vars, c("col_1", "col_2")) + expect_equal(merged_res$type, "merge_uncert") # Expect that `sep` has the `AsIs` class - expect_s3_class(sep, "AsIs") - expect_equal(as.character(sep), " +/- ") - expect_equal(sep, I(" +/- ")) + expect_s3_class(merged_res$sep, "AsIs") + expect_equal(as.character(merged_res$sep), " +/- ") + expect_equal(merged_res$sep, I(" +/- ")) - # +}) + +test_that("cols_merge_uncert() works with row groups", { # Expect that the column set as the row group can participate # in column merging through `cols_merge_uncert()` - # tbl <- dplyr::tibble( @@ -550,12 +520,12 @@ test_that("cols_merge_range() works correctly", { col_end = "col_2" ) - merged_range <- dt_col_merge_get(tbl_html) + merged_range <- dt_col_merge_get(tbl_html)[[1]] # Expect that merging statements are stored in `col_merge` - expect_equal(merged_range[[1]]$pattern, "{1}{sep}{2}") - expect_equal(merged_range[[1]]$vars, c("col_1", "col_2")) - expect_equal(merged_range[[1]]$type, "merge_range") - expect_equal(merged_range[[1]]$sep, "\U2013") + expect_equal(merged_range$pattern, "{1}{sep}{2}") + expect_equal(merged_range$vars, c("col_1", "col_2")) + expect_equal(merged_range$type, "merge_range") + expect_equal(merged_range$sep, "\U2013") }) test_that("cols_merge_range works 2", { @@ -570,12 +540,12 @@ test_that("cols_merge_range works 2", { col_end = col_2 ) - merged_range <- dt_col_merge_get(data = tbl_html) - # Expect that merging statements are stored in `col_merge`\ - expect_equal(merged_range[[1]]$pattern, "{1}{sep}{2}") - expect_equal(merged_range[[1]]$vars, c("col_1", "col_2")) - expect_equal(merged_range[[1]]$type, "merge_range") - expect_equal(merged_range[[1]]$sep, "\U2013") + merged_range <- dt_col_merge_get(data = tbl_html)[[1]] + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_range$pattern, "{1}{sep}{2}") + expect_equal(merged_range$vars, c("col_1", "col_2")) + expect_equal(merged_range$type, "merge_range") + expect_equal(merged_range$sep, "\U2013") }) test_that("cols_merge_range() works with 2 statements", { @@ -587,21 +557,22 @@ test_that("cols_merge_range() works with 2 statements", { cols_merge_range(col_begin = col_1, col_end = col_2) %>% cols_merge_range(col_begin = col_3, col_end = col_4) - merged_range <- dt_col_merge_get(data = tbl_html) + merged_range1 <- dt_col_merge_get(data = tbl_html)[[1]] # Expect that merging statements are stored in `col_merge` - expect_equal(merged_range[[1]]$pattern, "{1}{sep}{2}") - expect_equal(merged_range[[1]]$vars, c("col_1", "col_2")) - expect_equal(merged_range[[1]]$type, "merge_range") - expect_equal(merged_range[[1]]$sep, "\U2013") - - expect_equal(merged_range[[2]]$pattern, "{1}{sep}{2}") - expect_equal(merged_range[[2]]$vars, c("col_3", "col_4")) - expect_equal(merged_range[[2]]$type, "merge_range") - expect_equal(merged_range[[2]]$sep, "\U2013") - + expect_equal(merged_range1$pattern, "{1}{sep}{2}") + expect_equal(merged_range1$vars, c("col_1", "col_2")) + expect_equal(merged_range1$type, "merge_range") + expect_equal(merged_range1$sep, "\U2013") + + merged_range2 <- dt_col_merge_get(data = tbl_html)[[2]] + expect_equal(merged_range2$pattern, "{1}{sep}{2}") + expect_equal(merged_range2$vars, c("col_3", "col_4")) + expect_equal(merged_range2$type, "merge_range") + expect_equal(merged_range2$sep, "\U2013") }) test_that("cols_merge_range() respects locale for separators", { + # only locale set in cols_merge expect_merge_locale_sep(locale = "en", expected_sep = "\U2013") expect_merge_locale_sep(locale = "es", expected_sep = "-") expect_merge_locale_sep(locale = "nl", expected_sep = "-") @@ -812,18 +783,14 @@ test_that("cols_merge_n_pct() works correctly", { cols_merge_n_pct(col_n = a, col_pct = b) %>% fmt_percent(columns = b, decimals = 1) - # # Expect that merging statements are stored in `col_merge` - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$pattern %>% - expect_equal("{1}{sep}{2}") - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$vars %>% - expect_equal(c("a", "b")) - - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$type %>% - expect_equal("merge_n_pct") + # Expect that merging statements are stored in `col_merge` + merged_res <- dt_col_merge_get(data = tbl_html)[[1]] - dt_col_merge_get(data = tbl_html) %>% .[[1]] %>% .$sep %>% - expect_equal("") + # Expect that merging statements are stored in `col_merge` + expect_equal(merged_res$pattern, "{1}{sep}{2}") + expect_equal(merged_res$vars, c("a", "b")) + expect_equal(merged_res$type, "merge_n_pct") + expect_equal(merged_res$sep, "") expect_equal( render_formats_test(tbl_html, "html")[["a"]],