Skip to content

Commit 11c0ac5

Browse files
explicit roundtrip verification
1 parent 7efc836 commit 11c0ac5

File tree

4 files changed

+66
-58
lines changed

4 files changed

+66
-58
lines changed

R/transform-files.R

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,11 @@ parse_transform_serialize <- function(text, transformers) {
104104
)
105105
serialized_transformed_text <-
106106
serialize_parse_data_flattened(flattened_pd, start_line = start_line)
107-
verify_roundtrip(text, serialized_transformed_text, transformers)
108-
serialized_transformed_text
109107

108+
if (can_verify_roundtrip(transformers)) {
109+
verify_roundtrip(text, serialized_transformed_text)
110+
}
111+
serialized_transformed_text
110112
}
111113

112114
#' Apply transformers to a parse table
@@ -157,40 +159,44 @@ apply_transformers <- function(pd_nested, transformers) {
157159
}
158160

159161

160-
#' Verify the styling if possible
162+
163+
#' Check whether a roundtip verification can be carried out
164+
#'
165+
#' If scope was set to "line_breaks" or lower (compare [tidyverse_style()]),
166+
#' we can compare the expression before and after styling and return an error if
167+
#' it is not the same.
168+
#' @param transformers The list of transformer functions used for styling.
169+
#' Needed for reverse engineering the scope.
170+
can_verify_roundtrip <- function(transformers) {
171+
is.null(transformers$token)
172+
}
173+
174+
#' Verify the styling
161175
#'
162176
#' If scope was set to "line_breaks" or lower (compare [tidyverse_style()]),
163177
#' we can compare the expression before and after styling and return an error if
164178
#' it is not the same. Note that this method ignores comments and no
165179
#' verification can be conducted if scope > "line_breaks".
166180
#' @param old_text The initial expression in its character representation.
167181
#' @param new_text The styled expression in its character representation.
168-
#' @param transformers The list of transformer functions used for styling.
169-
#' Needed for reverse engineering the scope.
170182
#' @examples
171-
#' styler:::verify_roundtrip("a+1", "a + 1", tidyverse_style(scope = "line_breaks"))
172-
#' styler:::verify_roundtrip(
173-
#' "a+1",
174-
#' "a + 1 # comments are dropped",
175-
#' tidyverse_style(scope = "line_breaks")
176-
#' )
183+
#' styler:::verify_roundtrip("a+1", "a + 1")
184+
#' styler:::verify_roundtrip("a+1", "a + 1 # comments are dropped")
177185
#' \dontrun{
178-
#' styler:::verify_roundtrip("a+1", "b - 3", tidyverse_style(scope = "line_breaks"))
186+
#' styler:::verify_roundtrip("a+1", "b - 3")
179187
#' }
180-
verify_roundtrip <- function(old_text, new_text, transformers) {
181-
if (is.null(transformers$token)) {
182-
expressions_are_identical <- identical(
183-
parse(text = old_text, keep.source = FALSE),
184-
parse(text = new_text, keep.source = FALSE)
188+
verify_roundtrip <- function(old_text, new_text) {
189+
expressions_are_identical <- identical(
190+
parse(text = old_text, keep.source = FALSE),
191+
parse(text = new_text, keep.source = FALSE)
192+
)
193+
if (!expressions_are_identical) {
194+
msg <- paste(
195+
"The expression evaluated before the styling is not the same as the",
196+
"expression after styling. This should not happen. Please file a",
197+
"bug report on GitHub (https://github.com/r-lib/styler/issues)",
198+
"using a reprex."
185199
)
186-
if (!expressions_are_identical) {
187-
msg <- paste(
188-
"The expression evaluated before the styling is not the same as the",
189-
"expression after styling. This should not happen. Please file a",
190-
"bug report on GitHub (https://github.com/r-lib/styler/issues)",
191-
"using a reprex."
192-
)
193-
stop(msg, call. = FALSE)
194-
}
200+
stop(msg, call. = FALSE)
195201
}
196202
}

man/can_verify_roundtrip.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/verify_roundtrip.Rd

Lines changed: 5 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-roundtrip.R

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
context("roundtrip works")
22

3-
test_that("correct styling does not give an error for scope < tokens", {
4-
expect_error(verify_roundtrip(
5-
"1+1", "1 + 1", tidyverse_style(scope = "spaces")
6-
), NA)
7-
expect_error(verify_roundtrip(
8-
"1+1", "1 + 1", tidyverse_style(scope = "line_breaks")
9-
), NA)
10-
expect_error(verify_roundtrip(
11-
"1+1", "10+10", tidyverse_style(scope = "tokens")
12-
), NA)
3+
4+
test_that("can_verify_roundtrip works", {
5+
expect_true(can_verify_roundtrip(tidyverse_style(scope = "line_breaks")))
6+
expect_true(can_verify_roundtrip(tidyverse_style(scope = "spaces")))
7+
expect_true(can_verify_roundtrip(tidyverse_style(scope = "indention")))
8+
expect_false(can_verify_roundtrip(tidyverse_style(scope = "tokens")))
9+
})
10+
11+
test_that("correct styling does not give an error", {
12+
expect_error(verify_roundtrip("1+1", "1 + 1"), NA)
1313
})
1414

15-
test_that("incorrect styling does give an error", {
16-
expect_error(verify_roundtrip(
17-
"1-1", "1 + 1", tidyverse_style(scope = "spaces")
18-
), "bug")
19-
expect_error(verify_roundtrip(
20-
"1-1", "1 + 1", tidyverse_style(scope = "line_breaks")
21-
), "bug")
22-
expect_error(verify_roundtrip(
23-
"1-1", "1 + 1", tidyverse_style(scope = "tokens")
24-
), NA)
15+
test_that("corrupt styling does give an error", {
16+
expect_error(verify_roundtrip("1-1", "1 + 1"), "bug")
2517
})

0 commit comments

Comments
 (0)