Skip to content

Commit c219d63

Browse files
committed
Always return act$val
1 parent a3189a8 commit c219d63

File tree

7 files changed

+103
-122
lines changed

7 files changed

+103
-122
lines changed

R/expect-equality.R

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,26 @@ expect_equal <- function(
6868
check_number_decimal(tolerance, min = 0, allow_null = TRUE)
6969

7070
if (edition_get() >= 3) {
71-
if (
72-
!expect_waldo_equal_("equal", act, exp, info, ..., tolerance = tolerance)
73-
) {
74-
return()
75-
}
71+
expect_waldo_equal_("equal", act, exp, info, ..., tolerance = tolerance)
7672
} else {
7773
if (!is.null(tolerance)) {
7874
comp <- compare(act$val, exp$val, ..., tolerance = tolerance)
7975
} else {
8076
comp <- compare(act$val, exp$val, ...)
8177
}
8278

83-
if (!comp$equal) {
79+
if (comp$equal) {
80+
pass()
81+
} else {
8482
msg <- c(
8583
sprintf("Expected %s to equal %s.", act$lab, exp$lab),
8684
"Differences:",
8785
comp$message
8886
)
89-
return(fail(msg, info = info))
87+
fail(msg, info = info)
9088
}
9189
}
92-
pass(act$val)
90+
invisible(act$val)
9391
}
9492

9593

@@ -107,12 +105,12 @@ expect_identical <- function(
107105
exp <- quasi_label(enquo(expected), expected.label)
108106

109107
if (edition_get() >= 3) {
110-
if (!expect_waldo_equal_("identical", act, exp, info, ...)) {
111-
return()
112-
}
108+
expect_waldo_equal_("identical", act, exp, info, ...)
113109
} else {
114110
ident <- identical(act$val, exp$val, ...)
115-
if (!ident) {
111+
if (ident) {
112+
pass()
113+
} else {
116114
compare <- compare(act$val, exp$val)
117115
if (compare$equal) {
118116
msg_act <- "Objects equal but not identical"
@@ -129,7 +127,7 @@ expect_identical <- function(
129127
}
130128
}
131129

132-
pass(act$val)
130+
invisible(act$val)
133131
}
134132

135133
expect_waldo_equal_ <- function(
@@ -148,15 +146,17 @@ expect_waldo_equal_ <- function(
148146
y_arg = "expected"
149147
)
150148
if (length(comp) == 0) {
151-
return(TRUE)
149+
pass()
150+
} else {
151+
msg <- c(
152+
sprintf("Expected %s to be %s to %s.", act$lab, type, exp$lab),
153+
"Differences:",
154+
paste0(comp, collpase = "\n")
155+
)
156+
fail(msg, info = info, trace_env = trace_env)
152157
}
153158

154-
msg <- c(
155-
sprintf("Expected %s to be %s to %s.", act$lab, type, exp$lab),
156-
"Differences:",
157-
paste0(comp, collpase = "\n")
158-
)
159-
fail(msg, info = info, trace_env = trace_env)
159+
invisible(act$val)
160160
}
161161

162162
#' Is an object equal to the expected value, ignoring attributes?

R/expect-match.R

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,17 @@ expect_match <- function(
5050
return(fail(msg, info = info))
5151
}
5252

53-
if (
54-
!expect_match_(
55-
act = act,
56-
regexp = regexp,
57-
perl = perl,
58-
fixed = fixed,
59-
...,
60-
all = all,
61-
info = info,
62-
label = label,
63-
negate = FALSE
64-
)
65-
) {
66-
return()
67-
}
68-
69-
pass(act$val)
53+
expect_match_(
54+
act = act,
55+
regexp = regexp,
56+
perl = perl,
57+
fixed = fixed,
58+
...,
59+
all = all,
60+
info = info,
61+
label = label,
62+
negate = FALSE
63+
)
7064
}
7165

7266
#' @describeIn expect_match Check that a string doesn't match a regular
@@ -90,22 +84,17 @@ expect_no_match <- function(
9084
check_bool(fixed)
9185
check_bool(all)
9286

93-
if (
94-
!expect_match_(
95-
act = act,
96-
regexp = regexp,
97-
perl = perl,
98-
fixed = fixed,
99-
...,
100-
all = all,
101-
info = info,
102-
label = label,
103-
negate = TRUE
104-
)
105-
) {
106-
return()
107-
}
108-
pass(act$val)
87+
expect_match_(
88+
act = act,
89+
regexp = regexp,
90+
perl = perl,
91+
fixed = fixed,
92+
...,
93+
all = all,
94+
info = info,
95+
label = label,
96+
negate = TRUE
97+
)
10998
}
11099

111100
expect_match_ <- function(
@@ -126,27 +115,29 @@ expect_match_ <- function(
126115
ok <- if (all) all(condition) else any(condition)
127116

128117
if (ok) {
129-
return(TRUE)
130-
}
131-
132-
values <- show_text(act$val, condition)
133-
if (length(act$val) == 1) {
134-
which <- ""
118+
pass()
135119
} else {
136-
which <- if (all) "every element of " else "some element of "
120+
values <- show_text(act$val, condition)
121+
if (length(act$val) == 1) {
122+
which <- ""
123+
} else {
124+
which <- if (all) "every element of " else "some element of "
125+
}
126+
match <- if (negate) "not to match" else "to match"
127+
128+
msg_exp <- sprintf(
129+
"Expected %s%s %s %s %s.",
130+
which,
131+
act$lab,
132+
match,
133+
if (fixed) "string" else "regexp",
134+
encodeString(regexp, quote = '"')
135+
)
136+
msg_act <- c(paste0("Actual ", title, ':'), values)
137+
fail(c(msg_exp, msg_act), info = info, trace_env = trace_env)
137138
}
138-
match <- if (negate) "not to match" else "to match"
139-
140-
msg_exp <- sprintf(
141-
"Expected %s%s %s %s %s.",
142-
which,
143-
act$lab,
144-
match,
145-
if (fixed) "string" else "regexp",
146-
encodeString(regexp, quote = '"')
147-
)
148-
msg_act <- c(paste0("Actual ", title, ':'), values)
149-
fail(c(msg_exp, msg_act), info = info, trace_env = trace_env)
139+
140+
invisible(act$val)
150141
}
151142

152143

R/expect-named.R

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,13 @@ expect_named <- function(
4848

4949
if (ignore.order) {
5050
act_names <- labelled_value(act_names, paste0("names(", act$lab, ")"))
51-
if (!expect_setequal_(act_names, exp)) {
52-
return()
53-
}
51+
expect_setequal_(act_names, exp)
5452
} else {
5553
act_name <- labelled_value(act_names, paste0("names(", act$lab, ")"))
56-
if (!expect_waldo_equal_("equal", act_name, exp)) {
57-
return()
58-
}
54+
expect_waldo_equal_("equal", act_name, exp)
5955
}
6056

61-
pass(act$val)
57+
invisible(act$val)
6258
}
6359

6460
normalise_names <- function(x, ignore.order = FALSE, ignore.case = FALSE) {

R/expect-output.R

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,21 @@ expect_output <- function(
4040
sprintf("Expected %s to produce no output.", act$lab),
4141
sprintf("Actual output:\n%s", encodeString(act$cap))
4242
)
43-
return(fail(msg, info = info))
43+
fail(msg, info = info)
44+
} else {
45+
pass()
4446
}
4547
} else if (is.null(regexp) || identical(act$cap, "")) {
4648
if (identical(act$cap, "")) {
4749
msg <- sprintf("Expected %s to produce output.", act$lab)
48-
return(fail(msg, info = info))
50+
fail(msg, info = info)
51+
} else {
52+
pass()
4953
}
5054
} else {
5155
act_out <- labelled_value(act$cap, paste0("output from ", act$lab))
52-
if (!expect_match_(act_out, enc2native(regexp), ..., title = "output")) {
53-
return()
54-
}
56+
expect_match_(act_out, enc2native(regexp), ..., title = "output")
5557
}
56-
pass(act$val)
58+
59+
invisible(act$val)
5760
}

R/expect-self-test.R

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,22 @@ expect_failure <- function(expr, message = NULL, ...) {
7575
"Expected one failure.",
7676
sprintf("Actually failed %i times", status$n_failure)
7777
)
78-
return(fail(msg))
79-
}
80-
81-
if (status$n_success != 0) {
78+
fail(msg)
79+
} else if (status$n_success != 0) {
8280
msg <- c(
8381
"Expected zero successes.",
8482
sprintf("Actually succeeded %i times", status$n_success)
8583
)
86-
return(fail(msg))
87-
}
88-
89-
if (!is.null(message)) {
90-
act <- labelled_value(status$last_failure$message, "failure message")
91-
if (!expect_match_(act, message, ..., title = "message")) {
92-
return()
84+
fail(msg)
85+
} else {
86+
if (is.null(message)) {
87+
pass()
88+
} else {
89+
act <- labelled_value(status$last_failure$message, "failure message")
90+
expect_match_(act, message, ..., title = "message")
9391
}
9492
}
95-
pass(NULL)
93+
invisible()
9694
}
9795

9896
#' @export

R/expect-setequal.R

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ expect_setequal <- function(object, expected) {
3434
testthat_warn("expect_setequal() ignores names")
3535
}
3636

37-
if (!expect_setequal_(act, exp)) {
38-
return()
39-
}
40-
41-
pass(act$val)
37+
expect_setequal_(act, exp)
4238
}
4339

4440
expect_setequal_ <- function(
@@ -50,22 +46,23 @@ expect_setequal_ <- function(
5046
exp_miss <- unique(exp$val[!exp$val %in% act$val])
5147

5248
if (length(exp_miss) == 0 && length(act_miss) == 0) {
53-
return(TRUE)
49+
pass()
50+
} else {
51+
msg_exp <- sprintf(
52+
"Expected %s to have the same values as %s.",
53+
act$lab,
54+
exp$lab
55+
)
56+
msg_act <- c(
57+
sprintf("Actual: %s", values(act$val)),
58+
sprintf("Expected: %s", values(exp$val)),
59+
if (length(act_miss)) sprintf("Needs: %s", values(act_miss)),
60+
if (length(exp_miss)) sprintf("Absent: %s", values(exp_miss))
61+
)
62+
fail(c(msg_exp, msg_act), trace_env = trace_env)
5463
}
5564

56-
msg_exp <- sprintf(
57-
"Expected %s to have the same values as %s.",
58-
act$lab,
59-
exp$lab
60-
)
61-
msg_act <- c(
62-
sprintf("Actual: %s", values(act$val)),
63-
sprintf("Expected: %s", values(exp$val)),
64-
if (length(act_miss)) sprintf("Needs: %s", values(act_miss)),
65-
if (length(exp_miss)) sprintf("Absent: %s", values(exp_miss))
66-
)
67-
68-
fail(c(msg_exp, msg_act), trace_env = trace_env)
65+
invisible(act$value)
6966
}
7067

7168
values <- function(x) {
@@ -92,11 +89,7 @@ expect_mapequal <- function(object, expected) {
9289
act <- quasi_label(enquo(object))
9390
exp <- quasi_label(enquo(expected))
9491

95-
if (!expect_waldo_equal_("equal", act, exp, list_as_map = TRUE)) {
96-
return()
97-
}
98-
99-
pass(act$val)
92+
expect_waldo_equal_("equal", act, exp, list_as_map = TRUE)
10093
}
10194

10295
#' @export

R/expect-that.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ capture_trace <- function(trace_env) {
6767
#' @param value Value to return, typically the result of evaluating the
6868
#' `object` argument to the expectation.
6969
#' @export
70-
pass <- function(value) {
70+
pass <- function(value = NULL) {
7171
expectation("success", "success")
7272
invisible(value)
7373
}

0 commit comments

Comments
 (0)