Skip to content

Commit bfb1d41

Browse files
committed
Use testthat_print() instead of cnd_cat() for error snapshots
See r-lib/testthat#1200
1 parent 6b78803 commit bfb1d41

File tree

7 files changed

+67
-30
lines changed

7 files changed

+67
-30
lines changed

R/aaa.R

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
on_load <- function(expr, env = topenv(parent.frame())) {
3+
callback <- function() eval_bare(expr, env)
4+
env$.__rlang_hook__. <- c(env$.__rlang_hook__., list(callback))
5+
}
6+
7+
run_on_load <- function(env = topenv(caller_env())) {
8+
hook <- env$.__rlang_hook__.
9+
env_unbind(env, ".__rlang_hook__.")
10+
11+
for (callback in hook) {
12+
callback()
13+
}
14+
15+
env$.__rlang_hook__. <- NULL
16+
}

R/cnd-error.R

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ print.rlang_error <- function(x,
3434
simplify = c("branch", "collapse", "none"),
3535
fields = FALSE) {
3636
simplify <- arg_match(simplify)
37-
cat_line(format(x, simplify = simplify, fields = fields))
37+
cat_line(format(x, simplify = simplify, fields = fields, ...))
3838
invisible(x)
3939
}
4040

@@ -45,6 +45,7 @@ is_rlang_error <- function(x) {
4545
#' @export
4646
format.rlang_error <- function(x,
4747
...,
48+
backtrace = TRUE,
4849
child = NULL,
4950
simplify = c("branch", "collapse", "none"),
5051
fields = FALSE) {
@@ -109,7 +110,7 @@ format.rlang_error <- function(x,
109110

110111
simplify <- arg_match(simplify)
111112

112-
if (!is_null(trace) && trace_length(trace)) {
113+
if (backtrace && !is_null(trace) && trace_length(trace)) {
113114
out <- paste_line(out, bold("Backtrace:"))
114115
trace_lines <- format(trace, ..., simplify = simplify)
115116
out <- paste_line(out, trace_lines)
@@ -188,3 +189,7 @@ rlang_error_header <- function(cnd, child = NULL) {
188189
bold(sprintf("<parent: %s>", class))
189190
}
190191
}
192+
193+
on_load(s3_register("testthat::testthat_print", "rlang_error", function(x) {
194+
print(x, backtrace = FALSE)
195+
}))

R/rlang.R

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ base_pkg_env <- NULL
2828
.Call(r_init_library)
2929
.Call(rlang_library_load, ns_env("rlang"))
3030

31+
run_on_load()
32+
3133
s3_register("pillar::pillar_shaft", "quosures", pillar_shaft.quosures)
3234
s3_register("pillar::type_sum", "quosures", type_sum.quosures)
3335

tests/testthat/_snaps/arg.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
# `arg_match()` has informative error messages
22

33
Code
4-
cnd_cat(expect_error(arg_match0("continuuos", c("discrete", "continuous"))))
4+
(expect_error(arg_match0("continuuos", c("discrete", "continuous"))))
55
Output
6+
<error/rlang_error>
67
`"continuuos"` must be one of "discrete" or "continuous".
78
Did you mean "continuous"?
89
Code
9-
cnd_cat(expect_error(arg_match0("fou", c("bar", "foo"))))
10+
(expect_error(arg_match0("fou", c("bar", "foo"))))
1011
Output
12+
<error/rlang_error>
1113
`"fou"` must be one of "bar" or "foo".
1214
Did you mean "foo"?
1315
Code
14-
cnd_cat(expect_error(arg_match0("fu", c("ba", "fo"))))
16+
(expect_error(arg_match0("fu", c("ba", "fo"))))
1517
Output
18+
<error/rlang_error>
1619
`"fu"` must be one of "ba" or "fo".
1720
Did you mean "fo"?
1821
Code
19-
cnd_cat(expect_error(arg_match0("baq", c("foo", "baz", "bas"), arg_nm = "arg")))
22+
(expect_error(arg_match0("baq", c("foo", "baz", "bas"), arg_nm = "arg")))
2023
Output
24+
<error/rlang_error>
2125
`arg` must be one of "foo", "baz", or "bas".
2226
Did you mean "baz"?
2327
Code
24-
cnd_cat(expect_error(arg_match0("", character())))
28+
(expect_error(arg_match0("", character())))
2529
Output
30+
<error/rlang_error>
2631
`values` must have at least one element.
2732

2833
# `arg_match()` provides no suggestion when the edit distance is too large
2934

3035
Code
31-
cnd_cat(expect_error(arg_match0("foobaz", c("fooquxs", "discrete"))))
36+
(expect_error(arg_match0("foobaz", c("fooquxs", "discrete"))))
3237
Output
38+
<error/rlang_error>
3339
`"foobaz"` must be one of "fooquxs" or "discrete".
3440
Code
35-
cnd_cat(expect_error(arg_match0("a", c("b", "c"))))
41+
(expect_error(arg_match0("a", c("b", "c"))))
3642
Output
43+
<error/rlang_error>
3744
`"a"` must be one of "b" or "c".
3845

tests/testthat/_snaps/operators.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
# %|% fails with wrong types
22

33
Code
4-
cnd_cat(expect_error(c(1L, NA) %|% 2))
4+
(expect_error(c(1L, NA) %|% 2))
55
Output
6+
<error/rlang_error>
67
Replacement values must have type integer, not type double
78
Code
8-
cnd_cat(expect_error(c(1, NA) %|% ""))
9+
(expect_error(c(1, NA) %|% ""))
910
Output
11+
<error/rlang_error>
1012
Replacement values must have type double, not type character
1113
Code
12-
cnd_cat(expect_error(c(1, NA) %|% call("fn")))
14+
(expect_error(c(1, NA) %|% call("fn")))
1315
Output
16+
<error/rlang_error>
1417
Replacement values must have type double, not type language
1518
Code
16-
cnd_cat(expect_error(call("fn") %|% 1))
19+
(expect_error(call("fn") %|% 1))
1720
Output
21+
<error/rlang_error>
1822
Cannot replace missing values in an object of type language
1923

2024
# %|% fails with wrong length
2125

2226
Code
23-
cnd_cat(expect_error(c(1L, NA) %|% 1:3))
27+
(expect_error(c(1L, NA) %|% 1:3))
2428
Output
29+
<error/rlang_error>
2530
The replacement values must have size 1 or 2, not 3
2631
Code
27-
cnd_cat(expect_error(1:10 %|% 1:4))
32+
(expect_error(1:10 %|% 1:4))
2833
Output
34+
<error/rlang_error>
2935
The replacement values must have size 1 or 10, not 4
3036
Code
31-
cnd_cat(expect_error(1L %|% 1:4))
37+
(expect_error(1L %|% 1:4))
3238
Output
39+
<error/rlang_error>
3340
The replacement values must have size 1, not 4
3441

tests/testthat/test-arg.R

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ test_that("informative error message on partial match", {
5252

5353
test_that("`arg_match()` has informative error messages", {
5454
expect_snapshot({
55-
cnd_cat(expect_error(arg_match0("continuuos", c("discrete", "continuous"))))
56-
cnd_cat(expect_error(arg_match0("fou", c("bar", "foo"))))
57-
cnd_cat(expect_error(arg_match0("fu", c("ba", "fo"))))
58-
cnd_cat(expect_error(arg_match0("baq", c("foo", "baz", "bas"), arg_nm = "arg")))
59-
cnd_cat(expect_error(arg_match0("", character())))
55+
(expect_error(arg_match0("continuuos", c("discrete", "continuous"))))
56+
(expect_error(arg_match0("fou", c("bar", "foo"))))
57+
(expect_error(arg_match0("fu", c("ba", "fo"))))
58+
(expect_error(arg_match0("baq", c("foo", "baz", "bas"), arg_nm = "arg")))
59+
(expect_error(arg_match0("", character())))
6060
})
6161
})
6262

6363
test_that("`arg_match()` provides no suggestion when the edit distance is too large", {
6464
expect_snapshot({
65-
cnd_cat(expect_error(arg_match0("foobaz", c("fooquxs", "discrete"))))
66-
cnd_cat(expect_error(arg_match0("a", c("b", "c"))))
65+
(expect_error(arg_match0("foobaz", c("fooquxs", "discrete"))))
66+
(expect_error(arg_match0("a", c("b", "c"))))
6767
})
6868
})
6969

tests/testthat/test-operators.R

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ test_that("%|% also works when y is of same length as x", {
3434

3535
test_that("%|% fails with wrong types", {
3636
expect_snapshot({
37-
cnd_cat(expect_error(c(1L, NA) %|% 2))
38-
cnd_cat(expect_error(c(1, NA) %|% ""))
39-
cnd_cat(expect_error(c(1, NA) %|% call("fn")))
40-
cnd_cat(expect_error(call("fn") %|% 1))
37+
(expect_error(c(1L, NA) %|% 2))
38+
(expect_error(c(1, NA) %|% ""))
39+
(expect_error(c(1, NA) %|% call("fn")))
40+
(expect_error(call("fn") %|% 1))
4141
})
4242
})
4343

4444
test_that("%|% fails with wrong length", {
4545
expect_snapshot({
46-
cnd_cat(expect_error(c(1L, NA) %|% 1:3))
47-
cnd_cat(expect_error(1:10 %|% 1:4))
48-
cnd_cat(expect_error(1L %|% 1:4))
46+
(expect_error(c(1L, NA) %|% 1:3))
47+
(expect_error(1:10 %|% 1:4))
48+
(expect_error(1L %|% 1:4))
4949
})
5050
})
5151

0 commit comments

Comments
 (0)