Skip to content

Commit 51a0fee

Browse files
more thorough transformer dropping testing
1 parent e2f3931 commit 51a0fee

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

R/style-guides.R

+2
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
357357
#' circumstances the transformer does not have an impact on styling and can
358358
#' therefore be safely removed without affecting the styling outcome.
359359
#'
360+
#' You can use the un-exported function [test_transformers_dropping()] for some
361+
#' checks.
360362
#' @examples
361363
#' dropping <- specify_transformer_dropping(
362364
#' spaces = c(remove_space_after_excl = "'!'")

R/testing.R

+23
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,26 @@ fresh_testthat_cache <- function() {
341341
cache_more_specs_default <- function() {
342342
cache_more_specs(include_roxygen_examples = TRUE, base_indention = 0)
343343
}
344+
345+
#' Check if the transformers_dropping in [create_style_guide()] is consistent
346+
#' with the transformers specified.
347+
#' @param transformers The output of [create_style_guide()] we want to test.
348+
#' @keywords internal
349+
test_transformers_dropping <- function(transformers) {
350+
scopes <- intersect(
351+
names(transformers$transformers_drop),
352+
names(transformers)
353+
)
354+
355+
purrr::walk2(transformers$transformers_drop, transformers[scopes], function(x, y) {
356+
# all x must be in y. select the x that are not in y
357+
diff <- setdiff(names(x), names(y))
358+
if (length(diff) > 0) {
359+
rlang::abort(paste(
360+
"transformer_dropping specifies exclusion rules for transformers that ",
361+
"are not in the style guilde. Please add the rule to the style guide ",
362+
"or remove the dropping rules:", paste(diff, collapse = ", ")
363+
))
364+
}
365+
})
366+
}

man/test_transformers_dropping.Rd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-serialize_tests.R

+23
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,26 @@ test_that("properly detects match", {
2626
"identical"
2727
)
2828
})
29+
30+
test_that('detects non-matching style guides', {
31+
sg <- create_style_guide(
32+
space = list(
33+
a1 = function(...) NULL,
34+
b1 = function(... ) 1
35+
),
36+
transformers_drop = specify_transformer_dropping(
37+
spaces = c(a1 = "'+'")
38+
)
39+
)
40+
expect_silent(test_transformers_dropping(sg))
41+
42+
sg <- create_style_guide(
43+
space = list(
44+
a1 = function(...) NULL
45+
),
46+
transformers_drop = specify_transformer_dropping(
47+
spaces = c(a2 = "'+'")
48+
)
49+
)
50+
expect_error(test_transformers_dropping(sg))
51+
})

tests/testthat/test-transformers-drop.R

+12-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ remove_space_after_excl_ <- function(pd_flat) {
99

1010
t <- create_style_guide(
1111
space = lst(remove_space_after_excl_),
12-
transformers_drop = list(space = list(remove_space_after_excl_ = c("'!'"))),
12+
transformers_drop = specify_transformer_dropping(
13+
spaces = list(remove_space_after_excl_ = c("'!'"))
14+
),
1315
style_guide_name = "styler::t@https://github.com/r-lib",
1416
style_guide_version = as.character(packageVersion("styler"))
1517
)
@@ -45,28 +47,18 @@ test_that("transformers are removed if they are unused", {
4547
expect_equal(t_fun, t_manual)
4648
})
4749

50+
test_that("tidyverse transformers are correctly named", {
51+
# test that all dropping rules match an actual rule in the style guide
52+
expect_silent(
53+
test_transformers_dropping(tidyverse_style())
54+
)
55+
})
56+
4857
test_that("tidyverse transformers are correctly dropped", {
58+
# TODO maybe there is a more minimal test than this.
4959
t_style <- tidyverse_style()
60+
t_fun <- transformers_drop("x", t_style)
5061

51-
t_fun <- transformers_drop(
52-
"x", t_style
53-
)
54-
# test that all dropping rules match an actual rule in the style guide
55-
scopes <- intersect(
56-
names(t_fun$transformers_drop),
57-
names(t_fun)
58-
)
59-
purrr::map2(t_fun$transformers_drop, t_style[scopes], function(x, y) {
60-
# all x must be in y. select the x that are not in y
61-
diff <- setdiff(names(x),names(y))
62-
if (length(diff) > 0) {
63-
rlang::abort(paste(
64-
"transformer_dropping specifies exclusion rules for transformers that ",
65-
"are not in the style guilde. Please add the rule to the style guide ",
66-
"or remove the dropping rules:", paste(diff, collapse = ", "))
67-
)
68-
}
69-
})
7062
names_line_break <- c(
7163
"set_line_break_around_comma_and_or",
7264
"set_line_break_after_assignment",
@@ -97,7 +89,6 @@ test_that("tidyverse transformers are correctly dropped", {
9789
"remove_terminal_token_before_and_after"
9890
)
9991
expect_setequal(names(t_fun$token), names_tokens)
100-
10192
})
10293

10394

0 commit comments

Comments
 (0)