-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathdeprec-condition.R
161 lines (136 loc) Β· 3.76 KB
/
deprec-condition.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
new_capture <- function(class) {
exiting_handlers <- rep_named(class, list(identity))
calling_handlers <- rep_named(class, alist(function(cnd) {
if (can_entrace(cnd)) {
cnd <- cnd_entrace(cnd)
}
return_from(env, cnd)
}))
formals <- pairlist2(code = , entrace = FALSE)
# R CMD check global variable NOTE
code <- entrace <- NULL
body <- expr({
if (!entrace) {
return(tryCatch({ code; NULL }, !!!exiting_handlers))
}
env <- environment()
withCallingHandlers({ code; NULL }, !!!calling_handlers)
})
new_function(formals, body, ns_env("testthat"))
}
#' Capture conditions, including messages, warnings, expectations, and errors.
#'
#' @description
#' `r lifecycle::badge("superseded")`
#'
#' These functions allow you to capture the side-effects of a function call
#' including printed output, messages and warnings. We no longer recommend
#' that you use these functions, instead relying on the [expect_message()]
#' and friends to bubble up unmatched conditions. If you just want to silence
#' unimportant warnings, use [suppressWarnings()].
#'
#' @param code Code to evaluate
#' @param entrace Whether to add a [backtrace][rlang::trace_back] to
#' the captured condition.
#' @return Singular functions (`capture_condition`, `capture_expectation` etc)
#' return a condition object. `capture_messages()` and `capture_warnings`
#' return a character vector of message text.
#' @keywords internal
#' @export
#' @examples
#' f <- function() {
#' message("First")
#' warning("Second")
#' message("Third")
#' }
#'
#' capture_message(f())
#' capture_messages(f())
#'
#' capture_warning(f())
#' capture_warnings(f())
#'
#' # Condition will capture anything
#' capture_condition(f())
capture_condition <- new_capture("condition")
#' @export
#' @rdname capture_condition
capture_error <- new_capture("error")
#' @export
#' @rdname capture_condition
capture_expectation <- new_capture("expectation")
#' @export
#' @rdname capture_condition
capture_message <- new_capture("condition")
#' @export
#' @rdname capture_condition
capture_warning <- new_capture("warning")
#' @export
#' @rdname capture_condition
capture_messages <- function(code) {
out <- Stack$new()
withCallingHandlers(
code,
message = function(condition) {
out$push(condition)
maybe_restart("muffleMessage")
}
)
get_messages(out$as_list())
}
#' @export
#' @rdname capture_condition
capture_warnings <- function(code) {
out <- Stack$new()
withCallingHandlers(
code,
warning = function(condition) {
out$push(condition)
maybe_restart("muffleWarning")
}
)
get_messages(out$as_list())
}
get_messages <- function(x) {
vapply(x, cnd_message, FUN.VALUE = character(1))
}
#' Is an error informative?
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `is_informative_error()` is a generic predicate that indicates
#' whether testthat users should explicitly test for an error
#' class. Since we no longer recommend you do that, this generic
#' has been deprecated.
#'
#' @param x An error object.
#' @inheritParams ellipsis::dots_empty
#'
#' @details
#' A few classes are hard-coded as uninformative:
#' - `simpleError`
#' - `rlang_error` unless a subclass is detected
#' - `Rcpp::eval_error`
#' - `Rcpp::exception`
#'
#' @keywords internal
#' @export
is_informative_error <- function(x, ...) {
lifecycle::deprecate_warn("3.0.0", "is_informative_error()")
ellipsis::check_dots_empty()
if (!inherits(x, "error")) {
return(TRUE)
}
if (inherits(x, c("simpleError", "Rcpp::eval_error", "Rcpp::exception"))) {
return(FALSE)
}
if (inherits_only(x, c("rlang_error", "error", "condition"))) {
return(FALSE)
}
UseMethod("is_informative_error")
}
#' @export
is_informative_error.default <- function(x, ...) {
TRUE
}