Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor snapshot tests to use helper functions #1844

Merged
merged 4 commits into from
Aug 19, 2024

Conversation

olivroy
Copy link
Collaborator

@olivroy olivroy commented Aug 19, 2024

Summary

Changes in the snapshots is currently very painful to do due to an issue in testthat.

It seems that there are issues with updating snapshots when pipes %>% are present.

This refactors the tests to use new helpers

  • expect_snapshot_html()
  • expect_snapshot_latex()
  • expect_snapshot_word()
  • expect_snapshot_rtf()

I used the following script to replace programatically the easiest ones.

test_files <- fs::dir_ls("tests/testthat", glob = "*.R")
test_files <- fs::path_filter(test_files, regexp = "helper-", invert = TRUE)

obj <- test_files %>% 
  purrr::set_names() %>% 
  purrr::map(\(x) readLines(x, warn = FALSE, encoding = "UTF-8"))

html_snap_old_regex <- " %>% render_as_html() %>% expect_snapshot()"
html_snap_new_regex <- "expect_snapshot_html(\\1)"

latex_snap_old_regex <- " %>% as_latex() %>% as.character() %>% expect_snapshot()"
latex_snap_new_regex <- "expect_snapshot_latex(\\1)"

rtf_snap_old_regex <- " %>% as_rtf() %>% expect_snapshot()"
rtf_snap_new_regex <- "expect_snapshot_rtf(\\1)"

final_obj <- obj %>% 
  purrr::map(\(x) replace_pipe_in_snap(x, html_snap_old_regex, html_snap_new_regex)) %>% 
  purrr::map(\(x) replace_pipe_in_snap(x, latex_snap_old_regex, latex_snap_new_regex)) %>%
  purrr::map(\(x) replace_pipe_in_snap(x, rtf_snap_old_regex, rtf_snap_new_regex)) %>% 
  purrr::imap(\(x, name) writeLines(x, con = name))

replace_pipe_in_snap <- function(lines, old_snap_regex, new_snap_regex) {
  has_to_replace <- grepl(old_snap_regex, lines, fixed = TRUE)
  
  lines_to_change <- lines[has_to_replace]
  
  lines_to_change <- gsub(old_snap_regex, "", lines_to_change, fixed = TRUE)
  
  lines_new <- gsub("(\\w+)", new_snap_regex, lines_to_change)
  
  lines[has_to_replace] <- lines_new
  
  lines
}

Related GitHub Issues and PRs

Context, while working on #1842, it was difficult to iterate quickly as the snapshots were impossible to update via the usual way. I tried looking into #1837, but it proved challenging due to snapshot difficulties again.

Workaround r-lib/testthat#1646 and facilitate iteration. (Some people said that pipes were problematic)

Checklist

gt() %>%
tab_header(
title = "TABLE TITLE",
subtitle = "table subtitle"
) %>%
as_word(caption_location = "bottom") %>%
expect_snapshot()
expect_snapshot_word(gt_tbl_1, caption_location = "bottom")

## basic table with title embedded on the top of table
exibble_min %>%
gt() %>%
tab_header(
title = "TABLE TITLE",
subtitle = "table subtitle"
) %>%
as_word(caption_location = "embed") %>%
expect_snapshot()
expect_snapshot_word(gt_tbl_1, caption_location = "embed")

## basic table with split enabled
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was the same with different settings, hence the test simplification

Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rich-iannone rich-iannone merged commit 02fef72 into rstudio:master Aug 19, 2024
12 checks passed
@olivroy olivroy deleted the cols-width-merge branch August 19, 2024 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants