-
Notifications
You must be signed in to change notification settings - Fork 339
Improved composition #2250
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
Improved composition #2250
Changes from all commits
a3189a8
c219d63
7bb8bff
c4a6e27
c7e1f2d
87377ac
832e5d3
be37cf5
9bba39e
a530c63
6c40b18
22d01a9
f3126ca
7397403
bf46420
0281227
fb41736
963e37b
5d8887d
8294b85
1a048b4
843e34d
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 |
---|---|---|
|
@@ -28,52 +28,60 @@ expect_compare_ <- function( | |
operator <- match.arg(operator) | ||
op <- match.fun(operator) | ||
|
||
actual_op <- switch(operator, "<" = ">=", "<=" = ">", ">" = "<=", ">=" = "<") | ||
|
||
cmp <- op(act$val, exp$val) | ||
if (length(cmp) != 1 || !is.logical(cmp)) { | ||
cli::cli_abort( | ||
"Result of comparison must be a single logical value.", | ||
"Result of comparison must be `TRUE`, `FALSE`, or `NA`", | ||
call = trace_env | ||
) | ||
} else if (!isTRUE(cmp)) { | ||
msg <- failure_compare(act, exp, operator) | ||
fail(msg, trace_env = trace_env) | ||
} else { | ||
pass() | ||
} | ||
if (!isTRUE(cmp)) { | ||
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'm totally with you on no longer wanting to do an early return anymore Like, I think seeing an if/else construct where one branch ends in a In other words, I prefer this version of expect_compare_ <- function(
operator = c("<", "<=", ">", ">="),
act,
exp,
trace_env = caller_env()
) {
operator <- match.arg(operator)
op <- match.fun(operator)
actual_op <- switch(operator, "<" = ">=", "<=" = ">", ">" = "<=", ">=" = "<")
cmp <- op(act$val, exp$val)
if (length(cmp) != 1 || !is.logical(cmp)) {
cli::cli_abort(
"Result of comparison must be a single logical value.",
call = trace_env
)
}
if (isTRUE(cmp)) {
pass()
} else {
diff <- act$val - exp$val
msg_exp <- sprintf("Expected %s %s %s.", act$lab, operator, exp$lab)
digits <- max(
digits(act$val),
digits(exp$val),
min_digits(act$val, exp$val)
)
msg_act <- sprintf(
"Actual comparison: %s %s %s",
num_exact(act$val, digits),
actual_op,
num_exact(exp$val, digits)
)
if (is.na(diff)) {
msg_diff <- NULL
} else {
msg_diff <- sprintf(
"Difference: %s %s 0",
num_exact(diff, digits),
actual_op
)
}
fail(c(msg_exp, msg_act, msg_diff), trace_env = trace_env)
}
invisible(act$val)
} 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, I was thinking that instead of an early return, pulling out a helper to generate the failure message would be the way to go. 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. yea everything except the fail(), i.e. this bit
|
||
diff <- act$val - exp$val | ||
msg_exp <- sprintf("Expected %s %s %s.", act$lab, operator, exp$lab) | ||
|
||
digits <- max( | ||
digits(act$val), | ||
digits(exp$val), | ||
min_digits(act$val, exp$val) | ||
) | ||
} | ||
|
||
msg_act <- sprintf( | ||
"Actual comparison: %s %s %s", | ||
num_exact(act$val, digits), | ||
actual_op, | ||
num_exact(exp$val, digits) | ||
) | ||
failure_compare <- function(act, exp, operator) { | ||
actual_op <- switch(operator, "<" = ">=", "<=" = ">", ">" = "<=", ">=" = "<") | ||
|
||
if (is.na(diff)) { | ||
msg_diff <- NULL | ||
} else { | ||
msg_diff <- sprintf( | ||
"Difference: %s %s 0", | ||
num_exact(diff, digits), | ||
actual_op | ||
) | ||
} | ||
return(fail(c(msg_exp, msg_act, msg_diff), trace_env = trace_env)) | ||
diff <- act$val - exp$val | ||
msg_exp <- sprintf("Expected %s %s %s.", act$lab, operator, exp$lab) | ||
|
||
digits <- max( | ||
digits(act$val), | ||
digits(exp$val), | ||
min_digits(act$val, exp$val) | ||
) | ||
|
||
msg_act <- sprintf( | ||
"Actual comparison: %s %s %s", | ||
num_exact(act$val, digits), | ||
actual_op, | ||
num_exact(exp$val, digits) | ||
) | ||
|
||
if (is.na(diff)) { | ||
msg_diff <- NULL | ||
} else { | ||
msg_diff <- sprintf( | ||
"Difference: %s %s 0", | ||
num_exact(diff, digits), | ||
actual_op | ||
) | ||
} | ||
pass(act$val) | ||
|
||
c(msg_exp, msg_act, msg_diff) | ||
} | ||
|
||
#' @export | ||
#' @rdname comparison-expectations | ||
expect_lt <- function(object, expected, label = NULL, expected.label = NULL) { | ||
act <- quasi_label(enquo(object), label) | ||
exp <- quasi_label(enquo(expected), expected.label) | ||
|
||
expect_compare_("<", act, exp) | ||
invisible(act$val) | ||
} | ||
|
||
#' @export | ||
|
@@ -83,6 +91,7 @@ expect_lte <- function(object, expected, label = NULL, expected.label = NULL) { | |
exp <- quasi_label(enquo(expected), expected.label) | ||
|
||
expect_compare_("<=", act, exp) | ||
invisible(act$val) | ||
} | ||
|
||
#' @export | ||
|
@@ -92,6 +101,7 @@ expect_gt <- function(object, expected, label = NULL, expected.label = NULL) { | |
exp <- quasi_label(enquo(expected), expected.label) | ||
|
||
expect_compare_(">", act, exp) | ||
invisible(act$val) | ||
} | ||
|
||
#' @export | ||
|
@@ -101,6 +111,7 @@ expect_gte <- function(object, expected, label = NULL, expected.label = NULL) { | |
exp <- quasi_label(enquo(expected), expected.label) | ||
|
||
expect_compare_(">=", act, exp) | ||
invisible(act$val) | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,9 +143,11 @@ expect_error <- function( | |
# Access error fields with `[[` rather than `$` because the | ||
# `$.Throwable` from the rJava package throws with unknown fields | ||
if (!is.null(msg)) { | ||
return(fail(msg, info = info, trace = act$cap[["trace"]])) | ||
fail(msg, info = info, trace = act$cap[["trace"]]) | ||
} else { | ||
pass() | ||
} | ||
Comment on lines
145
to
149
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. My brain really wants if (is.null(msg)) {
pass()
} else {
fail(msg, info = info, trace = act$cap[["trace"]])
} i.e. always 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'm actually starting to think it 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.
yea i noted that as my exception here |
||
pass(act$val %||% act$cap) | ||
invisible(act$val %||% act$cap) | ||
} | ||
} | ||
|
||
|
@@ -198,9 +200,11 @@ expect_warning <- function( | |
cond_type = "warnings" | ||
) | ||
if (!is.null(msg)) { | ||
return(fail(msg, info = info)) | ||
fail(msg, info = info) | ||
} else { | ||
pass() | ||
} | ||
pass(act$val) | ||
invisible(act$val) | ||
} | ||
} | ||
|
||
|
@@ -236,9 +240,11 @@ expect_message <- function( | |
act <- quasi_capture(enquo(object), label, capture_messages) | ||
msg <- compare_messages(act$cap, act$lab, regexp = regexp, all = all, ...) | ||
if (!is.null(msg)) { | ||
return(fail(msg, info = info)) | ||
fail(msg, info = info) | ||
} else { | ||
pass() | ||
} | ||
pass(act$val) | ||
invisible(act$val) | ||
} | ||
} | ||
|
||
|
@@ -285,9 +291,11 @@ expect_condition <- function( | |
cond_type = "condition" | ||
) | ||
if (!is.null(msg)) { | ||
return(fail(msg, info = info, trace = act$cap[["trace"]])) | ||
fail(msg, info = info, trace = act$cap[["trace"]]) | ||
} else { | ||
pass() | ||
} | ||
pass(act$val %||% act$cap) | ||
invisible(act$val %||% act$cap) | ||
} | ||
} | ||
|
||
|
@@ -327,16 +335,18 @@ expect_condition_matching_ <- function( | |
# Access error fields with `[[` rather than `$` because the | ||
# `$.Throwable` from the rJava package throws with unknown fields | ||
if (!is.null(msg)) { | ||
return(fail( | ||
fail( | ||
msg, | ||
info = info, | ||
trace = act$cap[["trace"]], | ||
trace_env = trace_env | ||
)) | ||
) | ||
} else { | ||
pass() | ||
} | ||
# If a condition was expected, return it. Otherwise return the value | ||
# of the expression. | ||
pass(if (expected) act$cap else act$val) | ||
invisible(if (expected) act$cap else act$val) | ||
} | ||
|
||
# ------------------------------------------------------------------------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,16 +76,18 @@ expect_equal <- function( | |
comp <- compare(act$val, exp$val, ...) | ||
} | ||
|
||
if (!comp$equal) { | ||
if (comp$equal) { | ||
pass() | ||
} else { | ||
msg <- c( | ||
sprintf("Expected %s to equal %s.", act$lab, exp$lab), | ||
"Differences:", | ||
comp$message | ||
) | ||
return(fail(msg, info = info)) | ||
fail(msg, info = info) | ||
} | ||
pass(act$val) | ||
} | ||
invisible(act$val) | ||
} | ||
|
||
|
||
|
@@ -105,28 +107,25 @@ expect_identical <- function( | |
if (edition_get() >= 3) { | ||
expect_waldo_equal_("identical", act, exp, info, ...) | ||
} else { | ||
ident <- identical(act$val, exp$val, ...) | ||
if (ident) { | ||
msg_act <- NULL | ||
if (identical(act$val, exp$val, ...)) { | ||
pass() | ||
} else { | ||
compare <- compare(act$val, exp$val) | ||
if (compare$equal) { | ||
msg_act <- "Objects equal but not identical" | ||
} else { | ||
msg_act <- compare$message | ||
} | ||
} | ||
|
||
if (!ident) { | ||
msg <- c( | ||
sprintf("Expected %s to be identical to %s.", act$lab, exp$lab), | ||
"Differences:", | ||
msg_act | ||
) | ||
return(fail(msg, info = info)) | ||
fail(msg, info = info) | ||
} | ||
pass(act$val) | ||
} | ||
invisible(act$val) | ||
} | ||
|
||
expect_waldo_equal_ <- function( | ||
|
@@ -144,15 +143,16 @@ expect_waldo_equal_ <- function( | |
x_arg = "actual", | ||
y_arg = "expected" | ||
) | ||
if (length(comp) != 0) { | ||
if (length(comp) == 0) { | ||
pass() | ||
} else { | ||
msg <- c( | ||
sprintf("Expected %s to be %s to %s.", act$lab, type, exp$lab), | ||
"Differences:", | ||
paste0(comp, collpase = "\n") | ||
) | ||
return(fail(msg, info = info, trace_env = trace_env)) | ||
fail(msg, info = info, trace_env = trace_env) | ||
} | ||
pass(act$val) | ||
} | ||
|
||
#' Is an object equal to the expected value, ignoring attributes? | ||
|
@@ -203,7 +203,9 @@ expect_equivalent <- function( | |
exp$lab, | ||
comp$message | ||
) | ||
return(fail(msg, info = info)) | ||
fail(msg, info = info) | ||
} else { | ||
pass() | ||
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. flip pass fail order? |
||
} | ||
pass(act$val) | ||
invisible(act$val) | ||
} |
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.