Skip to content

Commit e95d346

Browse files
committed
Merge branch 'main' into f-759-named-funs
2 parents 1244cae + 0592e8e commit e95d346

35 files changed

+586
-594
lines changed

R/nest.R

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -330,33 +330,35 @@ set_spaces <- function(spaces_after_prefix, force_one) {
330330
#' @return A nested parse table.
331331
#' @keywords internal
332332
nest_parse_data <- function(pd_flat) {
333-
if (all(pd_flat$parent <= 0L)) {
334-
return(pd_flat)
333+
repeat {
334+
if (all(pd_flat$parent <= 0L)) {
335+
return(pd_flat)
336+
}
337+
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0L))
338+
split_data <- split(pd_flat, pd_flat$internal)
339+
340+
child <- split_data$`FALSE`
341+
internal <- split_data$`TRUE`
342+
343+
internal$internal_child <- internal$child
344+
internal$child <- NULL
345+
346+
child$parent_ <- child$parent
347+
348+
rhs <- nest_(child, "child", setdiff(names(child), "parent_"))
349+
350+
nested <- left_join(internal, rhs, by = c("id" = "parent_"))
351+
352+
children <- nested$child
353+
for (i in seq_along(children)) {
354+
new <- combine_children(children[[i]], nested$internal_child[[i]])
355+
# Work around is.null(new)
356+
children[i] <- list(new)
357+
}
358+
nested$child <- children
359+
nested$internal_child <- NULL
360+
pd_flat <- nested
335361
}
336-
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0L))
337-
split_data <- split(pd_flat, pd_flat$internal)
338-
339-
child <- split_data$`FALSE`
340-
internal <- split_data$`TRUE`
341-
342-
internal$internal_child <- internal$child
343-
internal$child <- NULL
344-
345-
child$parent_ <- child$parent
346-
347-
rhs <- nest_(child, "child", setdiff(names(child), "parent_"))
348-
349-
nested <- left_join(internal, rhs, by = c("id" = "parent_"))
350-
351-
children <- nested$child
352-
for (i in seq_along(children)) {
353-
new <- combine_children(children[[i]], nested$internal_child[[i]])
354-
# Work around is.null(new)
355-
children[i] <- list(new)
356-
}
357-
nested$child <- children
358-
nested$internal_child <- NULL
359-
nest_parse_data(nested)
360362
}
361363

362364
#' Combine child and internal child

R/roxygen-examples-parse.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,12 @@ emulate_rd <- function(roxygen) {
143143
)
144144
roxygen <- gsub("(^#)[^']", "#' #", roxygen)
145145

146-
text <- roxygen2::roc_proc_text(
146+
processed <- roxygen2::roc_proc_text(
147147
roxygen2::rd_roclet(),
148148
paste(roxygen, collapse = "\n")
149-
)[[1L]]$get_section("examples")
149+
)
150+
151+
text <- processed[[1L]]$get_section("examples")
150152
text <- as.character(text)[-1L]
151153
text <- c(
152154
if (grepl("^#'(\\s|\t)*@examples(\\s|\t)*$", roxygen[2L])) "",

tests/testthat/test-public_api-0.R

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
test_that("styler can style package", {
2+
capture_output(expect_false({
3+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"))
4+
any(styled$changed)
5+
}))
6+
})
7+
8+
test_that("styler can style package and exclude some directories", {
9+
capture_output(
10+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
11+
exclude_dirs = "tests"
12+
)
13+
)
14+
expect_true(nrow(styled) == 1)
15+
expect_false(any(grepl("tests/testthat/test-package-xyz.R", styled$file)))
16+
})
17+
18+
test_that("styler can style package and exclude some sub-directories", {
19+
capture_output(
20+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
21+
exclude_dirs = "tests/testthat"
22+
)
23+
)
24+
expect_true(nrow(styled) == 2)
25+
expect_true(any(grepl("tests/testthat.R", styled$file)))
26+
expect_false(any(grepl("tests/testthat/test-package-xyz.R", styled$file)))
27+
})
28+
29+
30+
31+
test_that("styler can style package and exclude some directories and files", {
32+
capture_output(expect_true({
33+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
34+
exclude_dirs = "tests",
35+
exclude_files = ".Rprofile"
36+
)
37+
nrow(styled) == 1
38+
}))
39+
40+
capture_output(expect_true({
41+
styled <- style_pkg(testthat_file("public-api", "xyzpackage"),
42+
exclude_dirs = "tests",
43+
exclude_files = "./.Rprofile"
44+
)
45+
nrow(styled) == 1
46+
}))
47+
})
48+
49+
50+
test_that("styler can style directory", {
51+
capture_output(expect_false({
52+
styled <- style_dir(testthat_file("public-api", "xyzdir"))
53+
any(styled$changed)
54+
}))
55+
})
56+
57+
test_that("styler can style directories and exclude", {
58+
capture_output(expect_true({
59+
styled <- style_dir(
60+
testthat_file("public-api", "renvpkg"),
61+
exclude_dirs = "renv"
62+
)
63+
nrow(styled) == 2
64+
}))
65+
capture_output(expect_true({
66+
styled <- style_dir(
67+
testthat_file("public-api", "renvpkg"),
68+
exclude_dirs = c("renv", "tests/testthat")
69+
)
70+
nrow(styled) == 1
71+
}))
72+
73+
capture_output(expect_true({
74+
styled <- style_dir(
75+
testthat_file("public-api", "renvpkg"),
76+
exclude_dirs = "./renv"
77+
)
78+
nrow(styled) == 2
79+
}))
80+
81+
capture_output(expect_true({
82+
styled <- style_dir(
83+
testthat_file("public-api", "renvpkg"),
84+
exclude_dirs = "./renv", recursive = FALSE
85+
)
86+
nrow(styled) == 0
87+
}))
88+
89+
capture_output(expect_true({
90+
styled <- style_dir(
91+
testthat_file("public-api", "renvpkg"),
92+
recursive = FALSE
93+
)
94+
nrow(styled) == 0
95+
}))
96+
})

tests/testthat/test-public_api-1.R

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
test_that("styler can style files", {
2+
# just one
3+
capture_output(expect_equal(
4+
{
5+
out <- style_file(c(
6+
testthat_file("public-api", "xyzfile", "random-script.R")
7+
), strict = FALSE)
8+
out$changed
9+
},
10+
rep(FALSE, 1),
11+
ignore_attr = TRUE
12+
))
13+
# multiple not in the same working directory
14+
capture_output(expect_equal(
15+
{
16+
out <- style_file(c(
17+
testthat_file("public-api", "xyzfile", "random-script.R"),
18+
testthat_file("public-api", "xyzfile", "subfolder", "random-script.R")
19+
), strict = FALSE)
20+
out$changed
21+
},
22+
rep(FALSE, 2),
23+
ignore_attr = TRUE
24+
))
25+
})
26+
27+
28+
test_that("styler does not return error when there is no file to style", {
29+
capture_output(expect_error(style_dir(
30+
testthat_file("public-api", "xyzemptydir"),
31+
strict = FALSE
32+
), NA))
33+
})
34+
35+
36+
37+
test_that("styler can style Rmd file", {
38+
expect_false({
39+
out <- style_file(
40+
testthat_file("public-api", "xyzfile_rmd", "random.Rmd"),
41+
strict = FALSE
42+
)
43+
out$changed
44+
})
45+
46+
styled <- style_file(
47+
testthat_file("public-api", "xyzfile_rmd", "random2.Rmd"),
48+
strict = FALSE
49+
)
50+
expect_false(styled$changed)
51+
})
52+
53+
test_that("styler can style Rmarkdown file", {
54+
expect_false({
55+
out <- style_file(
56+
testthat_file("public-api", "xyzfile_rmd", "random.Rmarkdown"),
57+
strict = FALSE
58+
)
59+
out$changed
60+
})
61+
62+
63+
styled <- style_file(
64+
testthat_file("public-api", "xyzfile_rmd", "random2.Rmarkdown"),
65+
strict = FALSE
66+
)
67+
expect_false(styled$changed)
68+
})
69+
70+
71+
test_that("styler can style qmd file", {
72+
expect_false({
73+
out <- style_file(
74+
testthat_file("public-api", "xyzfile_qmd", "new.qmd"),
75+
strict = FALSE
76+
)
77+
out$changed
78+
})
79+
80+
styled <- style_file(
81+
testthat_file("public-api", "xyzfile_rmd", "random2.Rmarkdown"),
82+
strict = FALSE
83+
)
84+
expect_false(styled$changed)
85+
})
86+
87+
test_that("styler handles malformed Rmd file and invalid R code in chunk", {
88+
capture_output(expect_warning(
89+
style_file(testthat_file("public-api", "xyzfile_rmd", "invalid4.Rmd"), strict = FALSE),
90+
"3: "
91+
))
92+
93+
capture_output(expect_warning(
94+
style_file(testthat_file("public-api", "xyzfile_rmd", "invalid7.Rmd"), strict = FALSE),
95+
"Malformed file"
96+
))
97+
})
98+
99+
100+
101+
102+
test_that("messages (via cat()) of style_file are correct", {
103+
for (encoding in ls_testable_encodings()) {
104+
withr::with_options(
105+
list(cli.unicode = encoding == "utf8"),
106+
{
107+
# Message if scope > line_breaks and code changes
108+
expect_snapshot({
109+
cat(catch_style_file_output(file.path(
110+
"public-api",
111+
"xyzdir-dirty",
112+
"dirty-sample-with-scope-tokens.R"
113+
)), sep = "\n")
114+
})
115+
116+
# No message if scope > line_breaks and code does not change
117+
expect_snapshot({
118+
cat(catch_style_file_output(file.path(
119+
"public-api", "xyzdir-dirty", "clean-sample-with-scope-tokens.R"
120+
)), sep = "\n")
121+
})
122+
123+
# No message if scope <= line_breaks even if code is changed.
124+
expect_snapshot({
125+
cat(catch_style_file_output(file.path(
126+
"public-api", "xyzdir-dirty", "dirty-sample-with-scope-spaces.R"
127+
)), sep = "\n")
128+
})
129+
}
130+
)
131+
}
132+
})
133+
134+
test_that("Messages can be suppressed", {
135+
withr::with_options(
136+
list(styler.quiet = TRUE),
137+
{
138+
output <- catch_style_file_output(file.path(
139+
"public-api", "xyzdir-dirty", "dirty-sample-with-scope-spaces.R"
140+
))
141+
expect_equal(output, character(0))
142+
}
143+
)
144+
})

0 commit comments

Comments
 (0)