Skip to content

Commit 13d1778

Browse files
authored
use_air() (#2121)
1 parent acbf9d6 commit 13d1778

File tree

135 files changed

+2085
-1054
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+2085
-1054
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@
2020
^\.github/workflows/R\.yaml$
2121
^\.github/workflows/pr-commands\.yaml$
2222
^CRAN-SUBMISSION$
23+
^[\.]?air\.toml$
24+
^\.vscode$

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"Posit.air-vscode"
4+
]
5+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"[r]": {
3+
"editor.formatOnSave": true,
4+
"editor.defaultFormatter": "Posit.air-vscode"
5+
}
6+
}

R/auto-test.R

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828
#' @param hash Passed on to [watch()]. When FALSE, uses less accurate
2929
#' modification time stamps, but those are faster for large files.
3030
#' @keywords debugging
31-
auto_test <- function(code_path, test_path, reporter = default_reporter(),
32-
env = test_env(),
33-
hash = TRUE) {
31+
auto_test <- function(
32+
code_path,
33+
test_path,
34+
reporter = default_reporter(),
35+
env = test_env(),
36+
hash = TRUE
37+
) {
3438
reporter <- find_reporter(reporter)
3539
code_path <- normalizePath(code_path)
3640
test_path <- normalizePath(test_path)
@@ -72,7 +76,11 @@ auto_test <- function(code_path, test_path, reporter = default_reporter(),
7276
#' modification time stamps, but those are faster for large files.
7377
#' @keywords debugging
7478
#' @seealso [auto_test()] for details on how method works
75-
auto_test_package <- function(pkg = ".", reporter = default_reporter(), hash = TRUE) {
79+
auto_test_package <- function(
80+
pkg = ".",
81+
reporter = default_reporter(),
82+
hash = TRUE
83+
) {
7684
reporter <- find_reporter(reporter)
7785

7886
path <- pkgload::pkg_path(pkg)
@@ -86,7 +94,12 @@ auto_test_package <- function(pkg = ".", reporter = default_reporter(), hash = T
8694
# Start by loading all code and running all tests
8795
withr::local_envvar("NOT_CRAN" = "true")
8896
pkgload::load_all(path)
89-
test_dir(test_path, package = package, reporter = reporter$clone(deep = TRUE), stop_on_failure = FALSE)
97+
test_dir(
98+
test_path,
99+
package = package,
100+
reporter = reporter$clone(deep = TRUE),
101+
stop_on_failure = FALSE
102+
)
90103

91104
# Next set up watcher to monitor changes
92105
watcher <- function(added, deleted, modified) {
@@ -106,7 +119,11 @@ auto_test_package <- function(pkg = ".", reporter = default_reporter(), hash = T
106119
cat("Changed code: ", paste0(basename(code), collapse = ", "), "\n")
107120
cat("Rerunning all tests\n")
108121
pkgload::load_all(path, quiet = TRUE)
109-
test_dir(test_path, package = package, reporter = reporter$clone(deep = TRUE))
122+
test_dir(
123+
test_path,
124+
package = package,
125+
reporter = reporter$clone(deep = TRUE)
126+
)
110127
} else if (length(tests) > 0) {
111128
# If test changes, rerun just that test
112129
cat("Rerunning tests: ", paste0(basename(tests), collapse = ", "), "\n")
@@ -115,7 +132,7 @@ auto_test_package <- function(pkg = ".", reporter = default_reporter(), hash = T
115132
test_dir = test_path,
116133
test_package = package,
117134
test_paths = tests,
118-
env = env,
135+
env = env,
119136
reporter = reporter$clone(deep = TRUE)
120137
)
121138
}

R/colour-text.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
colourise <- function(text, as = c("success", "skip", "warning", "failure", "error")) {
1+
colourise <- function(
2+
text,
3+
as = c("success", "skip", "warning", "failure", "error")
4+
) {
25
if (has_colour()) {
36
unclass(cli::make_ansi_style(testthat_style(as))(text))
47
} else {
@@ -11,7 +14,9 @@ has_colour <- function() {
1114
cli::num_ansi_colors() > 1
1215
}
1316

14-
testthat_style <- function(type = c("success", "skip", "warning", "failure", "error")) {
17+
testthat_style <- function(
18+
type = c("success", "skip", "warning", "failure", "error")
19+
) {
1520
type <- match.arg(type)
1621

1722
c(

R/compare.R

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ print_out <- function(x, ...) {
6464
# Common helpers ---------------------------------------------------------------
6565

6666
same_length <- function(x, y) length(x) == length(y)
67-
diff_length <- function(x, y) difference(fmt = "Lengths differ: %i is not %i", length(x), length(y))
67+
diff_length <- function(x, y) {
68+
difference(fmt = "Lengths differ: %i is not %i", length(x), length(y))
69+
}
6870

6971
same_type <- function(x, y) identical(typeof(x), typeof(y))
70-
diff_type <- function(x, y) difference(fmt = "Types not compatible: %s is not %s", typeof(x), typeof(y))
72+
diff_type <- function(x, y) {
73+
difference(fmt = "Types not compatible: %s is not %s", typeof(x), typeof(y))
74+
}
7175

7276
same_class <- function(x, y) {
7377
if (!is.object(x) && !is.object(y)) {
@@ -76,7 +80,11 @@ same_class <- function(x, y) {
7680
identical(class(x), class(y))
7781
}
7882
diff_class <- function(x, y) {
79-
difference(fmt = "Classes differ: %s is not %s", format_class(class(x)), format_class(class(y)))
83+
difference(
84+
fmt = "Classes differ: %s is not %s",
85+
format_class(class(x)),
86+
format_class(class(y))
87+
)
8088
}
8189

8290
same_attr <- function(x, y) {
@@ -91,10 +99,9 @@ vector_equal <- function(x, y) {
9199
(is.na(x) & is.na(y)) | (!is.na(x) & !is.na(y) & x == y)
92100
}
93101

94-
vector_equal_tol <- function(x, y, tolerance = .Machine$double.eps ^ 0.5) {
102+
vector_equal_tol <- function(x, y, tolerance = .Machine$double.eps^0.5) {
95103
(is.na(x) & is.na(y)) |
96104
(!is.na(x) & !is.na(y)) & (x == y | abs(x - y) < tolerance)
97-
98105
}
99106

100107

@@ -125,9 +132,15 @@ vector_equal_tol <- function(x, y, tolerance = .Machine$double.eps ^ 0.5) {
125132
#' compare(x, y)
126133
#' compare(c(x, x), c(y, y))
127134
#'
128-
compare.character <- function(x, y, check.attributes = TRUE, ...,
129-
max_diffs = 5, max_lines = 5,
130-
width = cli::console_width()) {
135+
compare.character <- function(
136+
x,
137+
y,
138+
check.attributes = TRUE,
139+
...,
140+
max_diffs = 5,
141+
max_lines = 5,
142+
width = cli::console_width()
143+
) {
131144
if (identical(x, y)) {
132145
return(no_difference())
133146
}
@@ -174,10 +187,13 @@ mismatch_character <- function(x, y, diff = !vector_equal(x, y)) {
174187
}
175188

176189
#' @export
177-
format.mismatch_character <- function(x, ...,
178-
max_diffs = 5,
179-
max_lines = 5,
180-
width = cli::console_width()) {
190+
format.mismatch_character <- function(
191+
x,
192+
...,
193+
max_diffs = 5,
194+
max_lines = 5,
195+
width = cli::console_width()
196+
) {
181197
width <- width - 6 # allocate space for labels
182198
n_show <- seq_len(min(x$n_diff, max_diffs))
183199

@@ -186,11 +202,16 @@ format.mismatch_character <- function(x, ...,
186202
show_y <- str_trunc(encode(x$y[n_show]), width * max_lines)
187203
show_i <- x$i[n_show]
188204

189-
sidebyside <- Map(function(x, y, pos) {
190-
x <- paste0("x[", pos, "]: ", str_chunk(x, width))
191-
y <- paste0("y[", pos, "]: ", str_chunk(y, width))
192-
paste(c(x, y), collapse = "\n")
193-
}, show_x, show_y, show_i)
205+
sidebyside <- Map(
206+
function(x, y, pos) {
207+
x <- paste0("x[", pos, "]: ", str_chunk(x, width))
208+
y <- paste0("y[", pos, "]: ", str_chunk(y, width))
209+
paste(c(x, y), collapse = "\n")
210+
},
211+
show_x,
212+
show_y,
213+
show_i
214+
)
194215

195216
summary <- paste0(x$n_diff, "/", x$n, " mismatches")
196217
paste0(summary, "\n", paste0(sidebyside, collapse = "\n\n"))
@@ -238,13 +259,20 @@ str_chunk <- function(x, length) {
238259
#' # Compare ignores minor numeric differences in the same way
239260
#' # as all.equal.
240261
#' compare(x, x + 1e-9)
241-
compare.numeric <- function(x, y,
242-
tolerance = testthat_tolerance(),
243-
check.attributes = TRUE,
244-
..., max_diffs = 9) {
262+
compare.numeric <- function(
263+
x,
264+
y,
265+
tolerance = testthat_tolerance(),
266+
check.attributes = TRUE,
267+
...,
268+
max_diffs = 9
269+
) {
245270
all_equal <- all.equal(
246-
x, y, tolerance = tolerance,
247-
check.attributes = check.attributes, ...
271+
x,
272+
y,
273+
tolerance = tolerance,
274+
check.attributes = check.attributes,
275+
...
248276
)
249277
if (isTRUE(all_equal)) {
250278
return(no_difference())
@@ -284,7 +312,7 @@ testthat_tolerance <- function() {
284312
skip("Long doubles not available and `tolerance` not supplied")
285313
}
286314

287-
.Machine$double.eps ^ 0.5
315+
.Machine$double.eps^0.5
288316
}
289317

290318
mismatch_numeric <- function(x, y, diff = !vector_equal(x, y)) {
@@ -312,7 +340,8 @@ format.mismatch_numeric <- function(x, ..., max_diffs = 9, digits = 3) {
312340
n_show <- seq_len(min(x$n_diff, max_diffs))
313341

314342
diffs <- paste0(
315-
format(paste0("[", x$i[n_show], "]")), " ",
343+
format(paste0("[", x$i[n_show], "]")),
344+
" ",
316345
format(x$x[n_show], digits = digits),
317346
" - ",
318347
format(x$y[n_show], digits = digits),
@@ -362,10 +391,11 @@ compare.POSIXt <- function(x, y, tolerance = 0.001, ..., max_diffs = 9) {
362391
}
363392

364393
standardise_tzone <- function(x) {
365-
if (is.null(attr(x, "tzone")) || identical(attr(x, "tzone"), Sys.timezone())) {
394+
if (
395+
is.null(attr(x, "tzone")) || identical(attr(x, "tzone"), Sys.timezone())
396+
) {
366397
attr(x, "tzone") <- ""
367398
}
368399

369400
x
370401
}
371-

R/deprec-condition.R

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
21
new_capture <- function(class) {
32
exiting_handlers <- rep_named(class, list(identity))
43

5-
calling_handlers <- rep_named(class, alist(function(cnd) {
6-
if (can_entrace(cnd)) {
7-
cnd <- cnd_entrace(cnd)
8-
}
9-
return_from(env, cnd)
10-
}))
4+
calling_handlers <- rep_named(
5+
class,
6+
alist(function(cnd) {
7+
if (can_entrace(cnd)) {
8+
cnd <- cnd_entrace(cnd)
9+
}
10+
return_from(env, cnd)
11+
})
12+
)
1113

1214
formals <- pairlist2(code = , entrace = FALSE)
1315

@@ -16,11 +18,23 @@ new_capture <- function(class) {
1618

1719
body <- expr({
1820
if (!entrace) {
19-
return(tryCatch({ code; NULL }, !!!exiting_handlers))
21+
return(tryCatch(
22+
{
23+
code
24+
NULL
25+
},
26+
!!!exiting_handlers
27+
))
2028
}
2129

2230
env <- environment()
23-
withCallingHandlers({ code; NULL }, !!!calling_handlers)
31+
withCallingHandlers(
32+
{
33+
code
34+
NULL
35+
},
36+
!!!calling_handlers
37+
)
2438
})
2539

2640
new_function(formals, body, ns_env("testthat"))

R/edition.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ edition_deprecate <- function(in_edition, what, instead = NULL) {
3434
}
3535

3636
edition_require <- function(in_edition, what) {
37-
if (edition_get() >= in_edition || isTRUE(getOption("testthat.edition_ignore"))) {
37+
if (
38+
edition_get() >= in_edition || isTRUE(getOption("testthat.edition_ignore"))
39+
) {
3840
return()
3941
}
4042

R/example.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ testthat_examples <- function() {
1717
#' @rdname testthat_examples
1818
testthat_example <- function(filename) {
1919
system.file(
20-
"examples", paste0("test-", filename, ".R"),
20+
"examples",
21+
paste0("test-", filename, ".R"),
2122
package = "testthat",
2223
mustWork = TRUE
2324
)

R/expect-comparison.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ expect_compare <- function(operator = c("<", "<=", ">", ">="), act, exp) {
2424
op <- match.fun(operator)
2525

2626
msg <- c(
27-
"<" = "not strictly less than",
27+
"<" = "not strictly less than",
2828
"<=" = "not less than",
29-
">" = "not strictly more than",
29+
">" = "not strictly more than",
3030
">=" = "not more than"
3131
)[[operator]]
3232

@@ -36,7 +36,13 @@ expect_compare <- function(operator = c("<", "<=", ">", ">="), act, exp) {
3636
}
3737
expect(
3838
if (!is.na(cmp)) cmp else FALSE,
39-
sprintf("%s is %s %s. Difference: %.3g", act$lab, msg, exp$lab, act$val - exp$val),
39+
sprintf(
40+
"%s is %s %s. Difference: %.3g",
41+
act$lab,
42+
msg,
43+
exp$lab,
44+
act$val - exp$val
45+
),
4046
trace_env = caller_env()
4147
)
4248
invisible(act$val)

0 commit comments

Comments
 (0)