Skip to content

Commit 963e37b

Browse files
committed
Refactor expect_comparison()
1 parent fb41736 commit 963e37b

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

R/expect-comparison.R

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,25 @@ expect_compare_ <- function(
2828
operator <- match.arg(operator)
2929
op <- match.fun(operator)
3030

31-
actual_op <- switch(operator, "<" = ">=", "<=" = ">", ">" = "<=", ">=" = "<")
32-
3331
cmp <- op(act$val, exp$val)
3432
if (length(cmp) != 1 || !is.logical(cmp)) {
3533
cli::cli_abort(
36-
"Result of comparison must be a single logical value.",
34+
"Result of comparison must be `TRUE`, `FALSE`, or `NA`",
3735
call = trace_env
3836
)
39-
}
40-
if (isTRUE(cmp)) {
37+
} else if (!isTRUE(cmp)) {
38+
msg <- failure_compare(act, exp, operator)
39+
fail(msg, trace_env = trace_env)
40+
} else {
4141
pass()
42-
return(invisible(act$val))
4342
}
4443

44+
invisible(act$val)
45+
}
46+
47+
failure_compare <- function(act, exp, operator) {
48+
actual_op <- switch(operator, "<" = ">=", "<=" = ">", ">" = "<=", ">=" = "<")
49+
4550
diff <- act$val - exp$val
4651
msg_exp <- sprintf("Expected %s %s %s.", act$lab, operator, exp$lab)
4752

@@ -67,9 +72,10 @@ expect_compare_ <- function(
6772
actual_op
6873
)
6974
}
70-
fail(c(msg_exp, msg_act, msg_diff), trace_env = trace_env)
71-
invisible(act$val)
75+
76+
c(msg_exp, msg_act, msg_diff)
7277
}
78+
7379
#' @export
7480
#' @rdname comparison-expectations
7581
expect_lt <- function(object, expected, label = NULL, expected.label = NULL) {

tests/testthat/_snaps/expect-comparison.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,5 @@
8282
expect_lt(1:10, 5)
8383
Condition
8484
Error in `expect_lt()`:
85-
! Result of comparison must be a single logical value.
85+
! Result of comparison must be `TRUE`, `FALSE`, or `NA`
8686

0 commit comments

Comments
 (0)