Skip to content

Commit e800651

Browse files
authored
feat: Add transform= parameter to $expect_download() and $expect_values() (#403)
1 parent 0679813 commit e800651

File tree

11 files changed

+87
-3
lines changed

11 files changed

+87
-3
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
* `{shinytest2}` now imports `{cli}` and no longer imports `{crayon}` (@olivroy, #399).
1616

17+
* Added [`{testthat}`'s snapshot file `transform=` parameter support](https://github.com/r-lib/testthat/pull/1530) to `AppDriver$expect_download()` and `AppDriver$expect_values()`. This allows for text transformations of the snapshot files before they are compared. (#403)
18+
19+
1720
# shinytest2 0.3.2
1821

1922
## Bug / Improvements

R/app-driver-expect-download.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ app_expect_download <- function(
9090
output,
9191
...,
9292
compare = NULL,
93-
name = NULL
93+
name = NULL,
94+
transform = NULL
9495
) {
9596
ckm8_assert_app_driver(self, private)
9697
rlang::check_dots_empty()
@@ -111,6 +112,7 @@ app_expect_download <- function(
111112
self,
112113
private,
113114
snapshot_info$download_path,
115+
transform = transform,
114116
compare = compare
115117
)
116118

R/app-driver-expect-values.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ app_expect_values <- function(
176176
output = missing_arg(),
177177
export = missing_arg(),
178178
name = NULL,
179-
screenshot_args = missing_arg()
179+
screenshot_args = missing_arg(),
180+
transform = NULL
180181
) {
181182
ckm8_assert_app_driver(self, private)
182183
rlang::check_dots_empty()
@@ -271,6 +272,7 @@ app_expect_values <- function(
271272
self,
272273
private,
273274
json_path,
275+
transform = transform,
274276
compare = testthat::compare_file_text
275277
)
276278

R/app-driver.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,11 @@ check_cran_deprecated <- function(
147147
#' Defaults to the resolved `timeout` value during the `AppDriver` initialization.
148148
#' @param timeout_ Amount of time to wait before giving up (milliseconds).
149149
#' Defaults to the resolved `timeout` value during the `AppDriver` initialization.
150+
#' @param transform Optionally, a function to scrub sensitive or stochastic text
151+
#' from the output. Should take a character vector of lines as input and
152+
#' return a modified character vector as output.
150153
#' @param cran Deprecated. With `AppDriver` never testing on CRAN,
151-
#' this parameter no longer has any effect.
154+
#' this parameter no longer has any effect.
152155
#' @param wait_ Wait until all reactive updates have completed?
153156
#' @param hash_images If `TRUE`, images will be hashed before being returned.
154157
#' Otherwise, all images will return their full data64 encoded value.
@@ -571,6 +574,7 @@ AppDriver <- R6Class(
571574
export = missing_arg(),
572575
screenshot_args = missing_arg(),
573576
name = NULL,
577+
transform = transform,
574578
cran = deprecated()
575579
) {
576580
check_cran_deprecated(cran)
@@ -584,6 +588,7 @@ AppDriver <- R6Class(
584588
export = export,
585589
screenshot_args = screenshot_args,
586590
name = name,
591+
transform = transform
587592
)
588593
},
589594
#' @description
@@ -750,6 +755,7 @@ AppDriver <- R6Class(
750755
...,
751756
compare = NULL,
752757
name = NULL,
758+
transform = NULL,
753759
cran = deprecated()
754760
) {
755761
check_cran_deprecated(cran)
@@ -760,6 +766,7 @@ AppDriver <- R6Class(
760766
output = output,
761767
...,
762768
compare = compare,
769+
transform = transform,
763770
name = name
764771
)
765772
},

R/expect-snapshot.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ app__expect_snapshot_file <- function(
2121
file,
2222
variant,
2323
name = fs::path_file(file),
24+
transform = NULL,
2425
compare = testthat::compare_file_binary
2526
) {
2627
ckm8_assert_app_driver(self, private)
@@ -43,6 +44,7 @@ app__expect_snapshot_file <- function(
4344
name = name_safe,
4445
cran = FALSE,
4546
compare = compare,
47+
transform = transform,
4648
variant = self$get_variant()
4749
),
4850
expectation_failure = function(cnd) {

man/AppDriver.Rd

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# transform can be passed to expect_snapshot_file()
2+
3+
"download-button.txt"
4+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Download content!
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"input": {
3+
4+
},
5+
"output": {
6+
"txt": "(app loaded)"
7+
},
8+
"export": {
9+
10+
}
11+
}
16.4 KB
Loading

0 commit comments

Comments
 (0)