-
Notifications
You must be signed in to change notification settings - Fork 327
Use conditions for reporting results #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7339f0d
5448986
92aeb39
6953645
68249e8
5e105d6
3d6dd33
f1018a8
46795a6
12e8d57
00d4d90
8481fd1
0deb2e0
77d51dc
e450fbc
58ab45c
ca3b8b8
8424b89
84abb46
485cbcb
371f500
d9cf5d4
ed95622
29372d8
67c91c9
20ac9ea
72ba350
c3e6137
26b3c41
2b5c28c
5b45d70
9d62f77
e6b0cd6
cfd0489
116e60d
d1fb341
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,19 +10,76 @@ | |
#' @keywords internal | ||
#' @export | ||
expectation <- function(passed, failure_msg, srcref = NULL) { | ||
structure( | ||
new_expectation(passed = passed, failure_msg = failure_msg, | ||
srcref = srcref) | ||
} | ||
|
||
new_expectation <- function(failure_msg, srcref, ..., | ||
passed = FALSE, error = FALSE, skipped = FALSE) { | ||
if (passed) { | ||
class = c("expectation", "condition") | ||
} else { | ||
class = c("expectation", "error", "condition") | ||
} | ||
|
||
exp <- structure( | ||
list( | ||
passed = passed, | ||
error = FALSE, | ||
skipped = FALSE, | ||
failure_msg = failure_msg, | ||
srcref = srcref | ||
error = error, | ||
skipped = skipped, | ||
failure_msg = failure_msg | ||
), | ||
class = "expectation" | ||
class = class | ||
) | ||
|
||
update_expectation(exp, srcref) | ||
} | ||
|
||
update_expectation <- function(exp, srcref, info = NULL, label = NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like we should be able to eventually eliminate this function, wrapping it into the constructor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Difficult without finally deprecating info and label arguments (#218). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, lets look again once we've eliminated those args and refactored the expectations |
||
exp$srcref <- srcref | ||
|
||
if (!is.null(label)) { | ||
exp$failure_msg <- paste0(label, " ", exp$failure_msg) | ||
} | ||
|
||
if (!is.null(info)) { | ||
exp$failure_msg <- paste0(exp$failure_msg, "\n", info) | ||
} | ||
|
||
# TODO: Get rid of failure_msg in favor of message | ||
exp$message <- exp$failure_msg | ||
|
||
exp | ||
} | ||
|
||
expectation_error <- function(error, calls) { | ||
expectation_ok <- function(exp) { | ||
isTRUE(exp$passed) | ||
} | ||
|
||
|
||
as.expectation <- function(x, ...) UseMethod("as.expectation", x) | ||
|
||
#' @export | ||
as.expectation.default <- function(x, ...) { | ||
stop("Don't know how to convert '", paste(class(x), collapse = "', '"), | ||
"' to expectation.", call. = FALSE) | ||
} | ||
|
||
#' @export | ||
as.expectation.expectation <- function(x, ...) x | ||
|
||
#' @export | ||
as.expectation.logical <- function(x, message, ...) { | ||
expectation(passed = x, failure_msg = message, srcref = find_test_srcref()) | ||
} | ||
|
||
#' @export | ||
as.expectation.error <- function(x, ...) { | ||
error <- x$message | ||
calls <- x$calls | ||
# TODO: Collect srcref in test_code() | ||
srcref <- x$srcref | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The srcref handling needs to be improved. This is currently just a placeholder, should be filled later by the calling handler to support file+line reporting. |
||
|
||
msg <- gsub("Error.*?: ", "", as.character(error)) | ||
|
||
if (length(calls) > 0) { | ||
|
@@ -35,29 +92,16 @@ expectation_error <- function(error, calls) { | |
msg <- gsub("\n$", "", msg) | ||
} | ||
|
||
structure( | ||
list( | ||
passed = FALSE, | ||
error = TRUE, | ||
skipped = FALSE, | ||
failure_msg = msg | ||
), | ||
class = "expectation" | ||
) | ||
new_expectation(msg, srcref, error = TRUE) | ||
} | ||
|
||
expectation_skipped <- function(error) { | ||
#' @export | ||
as.expectation.skip <- function(x, ...) { | ||
error <- x$message | ||
srcref <- x$srcref | ||
msg <- gsub("Error.*?: ", "", as.character(error)) | ||
|
||
structure( | ||
list( | ||
passed = FALSE, | ||
error = FALSE, | ||
skipped = TRUE, | ||
failure_msg = msg | ||
), | ||
class = "expectation" | ||
) | ||
new_expectation(msg, srcref, skipped = TRUE) | ||
} | ||
|
||
#' @export | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,6 @@ expect_gte(3, 3) | |
expect_less_than(2, 3) | ||
expect_lt(2, 3) | ||
expect_lte(2, 2) | ||
|
||
expect_error(expect_false(FALSE), NA) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you accidentally re-added this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was intentional, the commented code that was below is now gone. |
||
expect_error(expect_false(TRUE)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
passed && !error
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be better to have
type = c("error", "failure", "skip", "warning", "message")
etc?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
error
, thenpassed
isFALSE
by construction (although not enforced). I agree that a type is better, but I'd like to deal with this in a new batch of changes, and also change the reporters -- this PR is already big enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I think I just introduced some merge conflicts, but if you squash this, I'll fix.