Skip to content

Commit 4417121

Browse files
committed
Make succeed()/failure() primary
Instead of `expect()`. I think this makes the reasoning a little easier to follow, and will support the transition away from `expect()`.
1 parent 433101d commit 4417121

File tree

8 files changed

+57
-54
lines changed

8 files changed

+57
-54
lines changed

R/expect-that.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,28 @@ expect_that <- function(object, condition, info = NULL, label = NULL) {
5353
fail <- function(
5454
message = "Failure has been forced",
5555
info = NULL,
56-
trace_env = caller_env()
56+
srcref = NULL,
57+
trace_env = caller_env(),
58+
trace = NULL
5759
) {
58-
expect(FALSE, message, info = info, trace_env = trace_env)
60+
if (is.null(trace)) {
61+
trace <- trace_back(top = getOption("testthat_topenv"), bottom = trace_env)
62+
}
63+
# Only show if there's at least one function apart from the expectation
64+
if (trace_length(trace) <= 1) {
65+
trace <- NULL
66+
}
67+
68+
message <- paste(c(message, info), collapse = "\n")
69+
expectation("failure", message, srcref = srcref, trace = trace)
5970
}
6071

6172
#' @rdname fail
6273
#' @export
6374
succeed <- function(message = "Success has been forced", info = NULL) {
64-
expect(TRUE, message, info = info)
75+
message <- paste(c(message, info), collapse = "\n")
76+
77+
expectation("success", message)
6578
}
6679

6780
#' Negate an expectation

R/expectation.R

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,49 +43,28 @@ expect <- function(
4343
trace = NULL,
4444
trace_env = caller_env()
4545
) {
46-
type <- if (ok) "success" else "failure"
47-
48-
# Preserve existing API which appear to be used in package test code
49-
# Can remove in next major release
50-
if (missing(failure_message)) {
51-
warn("`failure_message` is missing, with no default.")
52-
message <- "unknown failure"
46+
if (ok) {
47+
succeed(failure_message)
5348
} else {
54-
# A few packages include code in info that errors on evaluation
55-
if (ok) {
56-
message <- paste(failure_message, collapse = "\n")
57-
} else {
58-
message <- paste(c(failure_message, info), collapse = "\n")
59-
}
60-
}
61-
62-
if (!ok) {
63-
if (is.null(trace)) {
64-
trace <- trace_back(
65-
top = getOption("testthat_topenv"),
66-
bottom = trace_env
67-
)
68-
}
69-
70-
# Only show if there's at least one function apart from the expectation
71-
if (trace_length(trace) <= 1) {
72-
trace <- NULL
73-
}
49+
fail(
50+
failure_message,
51+
info,
52+
srcref = srcref,
53+
trace = trace,
54+
trace_env = trace_env
55+
)
7456
}
75-
76-
exp <- expectation(type, message, srcref = srcref, trace = trace)
77-
exp_signal(exp)
7857
}
7958

80-
8159
#' Construct an expectation object
8260
#'
61+
#' @description
8362
#' For advanced use only. If you are creating your own expectation, you should
8463
#' call [expect()] instead. See `vignette("custom-expectation")` for more
8564
#' details.
8665
#'
87-
#' Create an expectation with `expectation()` or `new_expectation()`
88-
#' and signal it with `exp_signal()`.
66+
#' `new_expectation()` creates an expectation object and `exp_signal()` signals
67+
#' it. `expectation()` does both.
8968
#'
9069
#' @param type Expectation type. Must be one of "success", "failure", "error",
9170
#' "skip", "warning".
@@ -94,7 +73,8 @@ expect <- function(
9473
#' @inheritParams expect
9574
#' @export
9675
expectation <- function(type, message, srcref = NULL, trace = NULL) {
97-
new_expectation(type, message, srcref = srcref, trace = trace)
76+
exp <- new_expectation(type, message, srcref = srcref, trace = trace)
77+
exp_signal(exp)
9878
}
9979
#' @rdname expectation
10080
#' @param ... Additional attributes for the expectation object.
@@ -207,7 +187,7 @@ as.expectation.error <- function(x, srcref = NULL) {
207187
cnd_message(x)
208188
)
209189

210-
expectation("error", msg, srcref, trace = x[["trace"]])
190+
new_expectation("error", msg, srcref = srcref, trace = x[["trace"]])
211191
}
212192

213193

@@ -217,12 +197,17 @@ is_simple_error <- function(x) {
217197

218198
#' @export
219199
as.expectation.warning <- function(x, srcref = NULL) {
220-
expectation("warning", cnd_message(x), srcref, trace = x[["trace"]])
200+
new_expectation(
201+
"warning",
202+
cnd_message(x),
203+
srcref = srcref,
204+
trace = x[["trace"]]
205+
)
221206
}
222207

223208
#' @export
224209
as.expectation.skip <- function(x, ..., srcref = NULL) {
225-
expectation("skip", cnd_message(x), srcref, trace = x[["trace"]])
210+
new_expectation("skip", cnd_message(x), srcref = srcref, trace = x[["trace"]])
226211
}
227212

228213
#' @export

R/test-compiled-code.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ run_cpp_tests <- function(package) {
6060
reporter$add_result(
6161
context = "Catch",
6262
test = "Catch",
63-
result = expectation("failure", e$message)
63+
result = new_expectation("failure", e$message)
6464
)
6565
reporter$end_test(context = "Catch", test = "Catch")
6666
}
@@ -89,7 +89,7 @@ run_cpp_tests <- function(package) {
8989
get_reporter()$start_test(context = context_name, test = test_name)
9090

9191
for (i in seq_len(successes)) {
92-
exp <- expectation("success", "")
92+
exp <- new_expectation("success", "")
9393
exp$test <- test_name
9494
get_reporter()$add_result(
9595
context = context_name,
@@ -122,7 +122,7 @@ run_cpp_tests <- function(package) {
122122
c(line, line, 1, 1)
123123
)
124124

125-
exp <- expectation("failure", org_text, srcref = failure_srcref)
125+
exp <- new_expectation("failure", org_text, srcref = failure_srcref)
126126
exp$test <- test_name
127127

128128
get_reporter()$add_result(
@@ -143,7 +143,7 @@ run_cpp_tests <- function(package) {
143143
c(line, line, 1, 1)
144144
)
145145

146-
exp <- expectation("error", exception_text, srcref = exception_srcref)
146+
exp <- new_expectation("error", exception_text, srcref = exception_srcref)
147147
exp$test <- test_name
148148

149149
get_reporter()$add_result(

R/test-that.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
141141
}
142142
handle_expectation <- function(e) {
143143
handled <<- TRUE
144-
register_expectation(e, 6)
144+
register_expectation(e, 7)
145145
invokeRestart("continue_test")
146146
}
147147
handle_warning <- function(e) {

man/expectation.Rd

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fail.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/reporter-debug.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
1: expect_true(FALSE)
44
2: expect_waldo_constant(act, TRUE, info = info, ignore_attr = TRUE)
55
3: expect(length(comp) == 0, sprintf("%s is not %s\n\n%s", act$lab, deparse(co
6+
4: fail(failure_message, info, srcref = srcref, trace = trace, trace_env = tra
67

78
1: f()
89
2: expect_true(FALSE)
910
3: expect_waldo_constant(act, TRUE, info = info, ignore_attr = TRUE)
1011
4: expect(length(comp) == 0, sprintf("%s is not %s\n\n%s", act$lab, deparse(co
12+
5: fail(failure_message, info, srcref = srcref, trace = trace, trace_env = tra
1113

1214
1: stop("stop")
1315

tests/testthat/test-expectation.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ test_that("expectation contains failure message even when successful", {
33
expect_equal(e$message, "I failed")
44
})
55

6-
test_that("expect warns if no `failure_message`", {
7-
expect_warning(expect(TRUE), "missing, with no default")
8-
})
9-
106
test_that("info only evaluated on failure", {
117
expect_error(expect(TRUE, "fail", info = stop("!")), NA)
128
})

0 commit comments

Comments
 (0)