Skip to content

Commit d899e12

Browse files
committed
Merged origin/main into new-snapshot-fail-on-ci
2 parents eca73da + 7eb22e8 commit d899e12

Some content is hidden

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

79 files changed

+5590
-762
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ export(skip_on_os)
192192
export(skip_on_travis)
193193
export(skip_unless_r)
194194
export(snapshot_accept)
195+
export(snapshot_reject)
195196
export(snapshot_review)
196197
export(source_dir)
197198
export(source_file)

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# testthat (development version)
22

3+
* `local_mock()` and `with_mock()` have been deprecated because they are no longer permitted in R 4.5.
4+
* `snapshot_review()` now passes `...` on to `shiny::runApp()` (#1928).
5+
* `expect_named()` now gives more informative errors (#2091).
6+
* `expect_*()` functions consistently and rigorously check their inputs (#1754).
7+
* `test_that()` no longer warns about the absence of `{}` since it no longer seems to be necessary.
8+
* `test_that()`, `describe()`, and `it()` can now be arbitrarily nested. Each component will skip only if it and its subtests don't contain any expectations. The interactive stop reporter has been fixed so it doesn't duplicate failures. (#2063, #2188).
9+
* Test filtering now works with `it()`, and the `desc` argument can take a character vector in order to recursively filter subtests (i.e. `it()` nested inside of `describe()`) (#2118).
10+
* New `snapshot_reject()` rejects all modified snapshots by deleting the `.new` variants (#1923).
311
* New `SlowReporter` makes it easier to find the slowest tests in your package. The easiest way to run it is with `devtools::test(reporter = "slow")` (#1466).
412
* Power `expect_mapequal()` with `waldo::compare(list_as_map = TRUE)` (#1521).
513
* On CRAN, `test_that()` now automatically skips if a package is not installed (#1585). Practically, this means that you no longer need to check that suggested packages are installed. (We don't do this in the tidyverse because we think it has limited payoff, but other styles advise differently.)

R/describe.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
describe <- function(description, code) {
6161
local_description_push(description)
6262

63-
test_code(code, parent.frame(), skip_on_empty = FALSE)
63+
code <- substitute(code)
64+
test_code(code, parent.frame())
6465
}
6566

6667
#' @export
@@ -69,5 +70,5 @@ it <- function(description, code = NULL) {
6970
local_description_push(description)
7071

7172
code <- substitute(code)
72-
test_code(code, env = parent.frame(), skip_on_empty = FALSE)
73+
test_code(code, parent.frame())
7374
}

R/edition.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ edition_deprecate <- function(in_edition, what, instead = NULL) {
2727
return()
2828
}
2929

30-
warn(c(
31-
paste0("`", what, "` was deprecated in ", edition_name(in_edition), "."),
30+
cli::cli_warn(c(
31+
"{.code {what}} was deprecated in {edition_name(in_edition)}.",
3232
i = instead
3333
))
3434
}
@@ -40,7 +40,7 @@ edition_require <- function(in_edition, what) {
4040
return()
4141
}
4242

43-
stop(paste0("`", what, "` requires ", edition_name(in_edition), "."))
43+
cli::cli_abort("{.code {what}} requires {edition_name(in_edition)}.")
4444
}
4545

4646
edition_name <- function(x) {

R/expect-comparison.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ expect_compare_ <- function(
3939

4040
cmp <- op(act$val, exp$val)
4141
if (length(cmp) != 1 || !is.logical(cmp)) {
42-
abort(
43-
"Result of comparison must be a single logical value",
42+
cli::cli_abort(
43+
"Result of comparison must be a single logical value.",
4444
call = trace_env
4545
)
4646
}
@@ -112,14 +112,14 @@ expect_gte <- function(object, expected, label = NULL, expected.label = NULL) {
112112
#' @param ... All arguments passed on to `expect_lt()`/`expect_gt()`.
113113
#' @keywords internal
114114
expect_less_than <- function(...) {
115-
warning("Deprecated: please use `expect_lt()` instead", call. = FALSE)
115+
cli::cli_warn("Deprecated: please use {.fn expect_lt} instead.")
116116
expect_lt(...)
117117
}
118118

119119
#' @rdname expect_less_than
120120
#' @export
121121
expect_more_than <- function(...) {
122-
warning("Deprecated: please use `expect_gt()` instead", call. = FALSE)
122+
cli::cli_warn("Deprecated: please use {.fn expect_gt} instead.")
123123
expect_gt(...)
124124
}
125125

R/expect-condition.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ expect_warning <- function(
169169

170170
if (edition_get() >= 3) {
171171
if (!missing(all)) {
172-
warn("The `all` argument is deprecated")
172+
cli::cli_warn("The {.arg all} argument is deprecated.")
173173
}
174174

175175
expect_condition_matching_(

R/expect-equality.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ expect_waldo_equal_ <- function(
126126
exp,
127127
info = NULL,
128128
...,
129-
trace_env = caller_env()
129+
trace_env = caller_env(),
130+
error_prefix = NULL
130131
) {
131132
comp <- waldo_compare(
132133
act$val,
@@ -145,6 +146,7 @@ expect_waldo_equal_ <- function(
145146
"`expected`",
146147
paste0(comp, collapse = "\n\n")
147148
)
149+
msg <- paste0(error_prefix, msg)
148150
return(fail(msg, info = info, trace_env = trace_env))
149151
}
150152
pass(act$val)

R/expect-known.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ expect_known_output <- function(
7979

8080
compare_file <- function(path, lines, ..., update = TRUE, info = NULL) {
8181
if (!file.exists(path)) {
82-
warning("Creating reference output", call. = FALSE)
82+
cli::cli_warn("Creating reference output.")
8383
brio::write_lines(lines, path)
8484
return(pass(NULL))
8585
}
@@ -88,11 +88,11 @@ compare_file <- function(path, lines, ..., update = TRUE, info = NULL) {
8888
if (update) {
8989
brio::write_lines(lines, path)
9090
if (!all_utf8(lines)) {
91-
warning("New reference output is not UTF-8 encoded", call. = FALSE)
91+
cli::cli_warn("New reference output is not UTF-8 encoded.")
9292
}
9393
}
9494
if (!all_utf8(old_lines)) {
95-
warning("Reference output is not UTF-8 encoded", call. = FALSE)
95+
cli::cli_warn("Reference output is not UTF-8 encoded.")
9696
}
9797

9898
comp <- waldo_compare(
@@ -178,7 +178,7 @@ expect_known_value <- function(
178178
act <- quasi_label(enquo(object), label)
179179

180180
if (!file.exists(file)) {
181-
warning("Creating reference value", call. = FALSE)
181+
cli::cli_warn("Creating reference value.")
182182
saveRDS(object, file, version = version)
183183
} else {
184184
ref_val <- readRDS(file)
@@ -232,7 +232,7 @@ expect_known_hash <- function(object, hash = NULL) {
232232
}
233233

234234
if (is.null(hash)) {
235-
warning(paste0("No recorded hash: use ", substr(act_hash, 1, 10)))
235+
cli::cli_warn("No recorded hash: use {substr(act_hash, 1, 10)}.")
236236
} else {
237237
if (hash != act_hash) {
238238
msg <- sprintf("Value hashes to %s, not %s", act_hash, hash)

R/expect-named.R

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,24 @@ expect_named <- function(
3636
check_bool(ignore.case)
3737

3838
act <- quasi_label(enquo(object), label)
39-
act$names <- names(act$val)
4039

4140
if (missing(expected)) {
42-
if (identical(act$names, NULL)) {
43-
msg <- sprintf("%s does not have names.", act$lab)
44-
return(fail(msg))
45-
}
46-
} else {
47-
exp_names <- normalise_names(expected, ignore.order, ignore.case)
48-
act$names <- normalise_names(act$names, ignore.order, ignore.case)
41+
return(expect_has_names_(act))
42+
}
43+
44+
exp <- quasi_label(enquo(expected), arg = "expected")
4945

50-
if (!identical(act$names, exp_names)) {
51-
msg <- sprintf(
52-
"Names of %s (%s) don't match %s",
53-
act$lab,
54-
paste0("'", act$names, "'", collapse = ", "),
55-
paste0("'", exp_names, "'", collapse = ", ")
56-
)
57-
return(fail(msg, info = info))
58-
}
46+
exp$val <- normalise_names(exp$val, ignore.order, ignore.case)
47+
act_names <- normalise_names(names(act$val), ignore.order, ignore.case)
48+
49+
if (ignore.order) {
50+
act <- labelled_value(act_names, act$lab)
51+
return(expect_setequal_(act, exp, error_prefix = "Names of "))
52+
} else {
53+
act <- labelled_value(act_names, act$lab)
54+
return(expect_waldo_equal_("equal", act, exp, error_prefix = "Names of "))
5955
}
56+
6057
pass(act$val)
6158
}
6259

@@ -74,3 +71,12 @@ normalise_names <- function(x, ignore.order = FALSE, ignore.case = FALSE) {
7471

7572
x
7673
}
74+
75+
expect_has_names_ <- function(act, trace_env = caller_env()) {
76+
act_names <- names(act$val)
77+
if (identical(act_names, NULL)) {
78+
msg <- sprintf("%s does not have names.", act$lab)
79+
return(fail(msg, trace_env = trace_env))
80+
}
81+
return(pass(act$val))
82+
}

R/expect-setequal.R

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,23 @@ expect_setequal <- function(object, expected) {
3434
testthat_warn("expect_setequal() ignores names")
3535
}
3636

37+
expect_setequal_(act, exp)
38+
}
39+
40+
expect_setequal_ <- function(
41+
act,
42+
exp,
43+
trace_env = caller_env(),
44+
error_prefix = NULL
45+
) {
3746
act_miss <- unique(act$val[!act$val %in% exp$val])
3847
exp_miss <- unique(exp$val[!exp$val %in% act$val])
3948

4049
if (length(exp_miss) || length(act_miss)) {
41-
return(fail(paste0(
50+
msg <- paste0(
51+
if (!is.null(error_prefix)) {
52+
error_prefix
53+
},
4254
act$lab,
4355
" (`actual`) and ",
4456
exp$lab,
@@ -49,7 +61,8 @@ expect_setequal <- function(object, expected) {
4961
if (length(exp_miss)) {
5062
paste0("* Only in `expected`: ", values(exp_miss), "\n")
5163
}
52-
)))
64+
)
65+
return(fail(msg, trace_env = trace_env))
5366
}
5467
pass(act$val)
5568
}
@@ -131,35 +144,6 @@ expect_in <- function(object, expected) {
131144

132145
# Helpers ----------------------------------------------------------------------
133146

134-
check_map_names <- function(
135-
x,
136-
error_arg = caller_arg(x),
137-
error_call = caller_env()
138-
) {
139-
nms <- names2(x)
140-
141-
if (anyDuplicated(nms)) {
142-
dups <- unique(nms[duplicated(nms)])
143-
cli::cli_abort(
144-
c(
145-
"All elements in {.arg {error_arg}} must have unique names.",
146-
x = "Duplicate names: {.str {dups}}"
147-
),
148-
call = error_call
149-
)
150-
}
151-
if (any(nms == "")) {
152-
empty <- which(nms == "")
153-
cli::cli_abort(
154-
c(
155-
"All elements in {.arg {error_arg}} must have names.",
156-
x = "Empty names at position{?s}: {empty}"
157-
),
158-
call = error_call
159-
)
160-
}
161-
}
162-
163147
check_vector <- function(
164148
x,
165149
error_arg = caller_arg(x),

0 commit comments

Comments
 (0)