Skip to content

Commit ef98a3e

Browse files
committed
Always return failure message
And other minor refactorings Fixes #836
1 parent c630f38 commit ef98a3e

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

NAMESPACE

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ S3method(as.data.frame,testthat_results)
44
S3method(as.expectation,default)
55
S3method(as.expectation,error)
66
S3method(as.expectation,expectation)
7-
S3method(as.expectation,logical)
87
S3method(as.expectation,skip)
98
S3method(as.expectation,warning)
109
S3method(compare,POSIXt)

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# testthat (development version)
22

3+
* Expectation object always contains the failure message, even when succesful
4+
(#836)
5+
36
* New `expect_vector()` is a wrapper around `vctrs::vec_assert()` making it
47
easy to test against the vctrs definitions of prototype and size (#846).
58

R/expectation.R

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' The building block of all `expect_` functions
22
#'
33
#' Use this if you are writing your own expectation. See
4-
#' `vignette("custom-expectation")` for details
4+
#' `vignette("custom-expectation")` for details.
55
#'
66
#' @param ok Was the expectation successful?
77
#' @param failure_message What message should be shown if the expectation was
@@ -12,7 +12,9 @@
1212
#' forward a srcref captured elsewhere.
1313
#' @export
1414
expect <- function(ok, failure_message, info = NULL, srcref = NULL) {
15-
exp <- as.expectation.logical(ok, failure_message, info = info, srcref = srcref)
15+
type <- if (ok) "success" else "failure"
16+
message <- paste(c(failure_message, info), collapse = "\n")
17+
exp <- expectation(type, message, srcref = srcref)
1618

1719
withRestarts(
1820
if (expectation_broken(exp)) {
@@ -28,8 +30,14 @@ expect <- function(ok, failure_message, info = NULL, srcref = NULL) {
2830

2931
#' Construct an expectation object
3032
#'
31-
#' For advanced use only.
33+
#' For advanced use only. If you are creating your own expectation, you should
34+
#' call `expect()` instead. See `vignette("custom-expectation")` for more
35+
#' details.
3236
#'
37+
#' @param type Expectation type. Must be one of "success", "failure", "error",
38+
#' "skip", "warning".
39+
#' @param message Message describing test failure
40+
#' @param srcref Optional `srcref` giving location of test.
3341
#' @keywords internal
3442
#' @export
3543
expectation <- function(type, message, srcref = NULL) {
@@ -184,17 +192,6 @@ as.expectation.expectation <- function(x, ..., srcref = NULL) {
184192
x
185193
}
186194

187-
#' @export
188-
as.expectation.logical <- function(x, message, ..., srcref = NULL, info = NULL) {
189-
type <- if (x) "success" else "failure"
190-
message <- if (x) "success" else add_info(message, info)
191-
expectation(type, message, srcref = srcref)
192-
}
193-
194-
add_info <- function(message, info = NULL) {
195-
paste(c(message, info), collapse = "\n")
196-
}
197-
198195
#' @export
199196
as.expectation.error <- function(x, ..., srcref = NULL) {
200197
error <- x$message
@@ -227,7 +224,9 @@ as.expectation.skip <- function(x, ..., srcref = NULL) {
227224
}
228225

229226
#' @export
230-
print.expectation <- function(x, ...) cat(format(x), "\n")
227+
print.expectation <- function(x, ...) {
228+
cat(format(x), "\n")
229+
}
231230

232231
#' @export
233232
format.expectation_success <- function(x, ...) {

man/expect.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/expectation.Rd

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-expectation.R

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
context("test-expectation")
2+
3+
test_that("expectation contains failure message even when successful", {
4+
e <- expect(TRUE, "I failed")
5+
expect_equal(e$message, "I failed")
6+
})

0 commit comments

Comments
 (0)