Skip to content

Commit 87377ac

Browse files
committed
Push through more expectations
1 parent c7e1f2d commit 87377ac

24 files changed

+161
-102
lines changed

R/expect-inheritance.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ expect_type <- function(object, type) {
7474
sprintf("Expected %s to have type %s.", act$lab, format_class(type)),
7575
sprintf("Actual type: %s", format_class(act_type))
7676
)
77-
return(fail(msg))
77+
fail(msg)
78+
} else {
79+
pass()
7880
}
79-
pass(act$val)
81+
invisible(act$val)
8082
}
8183

8284
#' @export

R/expect-invisible.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ expect_invisible <- function(call, label = NULL) {
2929
sprintf("Expected %s to return invisibly.", lab),
3030
"Actual visibility: visible."
3131
)
32-
return(fail(msg))
32+
fail(msg)
33+
} else {
34+
pass()
3335
}
34-
pass(vis$value)
36+
invisible(vis$value)
3537
}
3638

3739
#' @export
@@ -45,7 +47,9 @@ expect_visible <- function(call, label = NULL) {
4547
sprintf("Expected %s to return visibly.", lab),
4648
"Actual visibility: invisible."
4749
)
48-
return(fail(msg))
50+
fail(msg)
51+
} else {
52+
pass()
4953
}
50-
pass(vis$value)
54+
invisible(vis$value)
5155
}

R/expect-known.R

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,22 @@ expect_known_output <- function(
7373
act$lab <- label %||% quo_label(act$quo)
7474
act <- append(act, eval_with_output(object, print = print, width = width))
7575

76-
compare_file(file, act$out, update = update, info = info, ...)
76+
expect_file_unchanged_(file, act$out, update = update, info = info, ...)
7777
invisible(act$val)
7878
}
7979

80-
compare_file <- function(path, lines, ..., update = TRUE, info = NULL) {
80+
expect_file_unchanged_ <- function(
81+
path,
82+
lines,
83+
...,
84+
update = TRUE,
85+
info = NULL
86+
) {
8187
if (!file.exists(path)) {
8288
cli::cli_warn("Creating reference output.")
8389
brio::write_lines(lines, path)
84-
return(pass(NULL))
90+
pass()
91+
return()
8592
}
8693

8794
old_lines <- brio::read_lines(path)
@@ -108,9 +115,10 @@ compare_file <- function(path, lines, ..., update = TRUE, info = NULL) {
108115
encodeString(path, quote = "'"),
109116
paste0(comp, collapse = "\n\n")
110117
)
111-
return(fail(msg, info = info, trace_env = caller_env()))
118+
fail(msg, info = info, trace_env = caller_env())
119+
} else {
120+
pass()
112121
}
113-
pass(NULL)
114122
}
115123

116124
#' Do you expect the output/result to equal a known good value?
@@ -151,7 +159,7 @@ expect_output_file <- function(
151159
act$lab <- label %||% quo_label(act$quo)
152160
act <- append(act, eval_with_output(object, print = print, width = width))
153161

154-
compare_file(file, act$out, update = update, info = info, ...)
162+
expect_file_unchanged_(file, act$out, update = update, info = info, ...)
155163
invisible(act$val)
156164
}
157165

@@ -180,6 +188,7 @@ expect_known_value <- function(
180188
if (!file.exists(file)) {
181189
cli::cli_warn("Creating reference value.")
182190
saveRDS(object, file, version = version)
191+
pass()
183192
} else {
184193
ref_val <- readRDS(file)
185194
comp <- compare(act$val, ref_val, ...)
@@ -194,11 +203,13 @@ expect_known_value <- function(
194203
encodeString(file, quote = "'"),
195204
comp$message
196205
)
197-
return(fail(msg, info = info))
206+
fail(msg, info = info)
207+
} else {
208+
pass()
198209
}
199210
}
200211

201-
pass(act$value)
212+
invisible(act$val)
202213
}
203214

204215
#' @export
@@ -233,18 +244,21 @@ expect_known_hash <- function(object, hash = NULL) {
233244

234245
if (is.null(hash)) {
235246
cli::cli_warn("No recorded hash: use {substr(act_hash, 1, 10)}.")
247+
pass()
236248
} else {
237249
if (hash != act_hash) {
238250
msg <- sprintf(
239251
"Expected value to hash to %s.\nActual hash: %s",
240252
hash,
241253
act_hash
242254
)
243-
return(fail(msg))
255+
fail(msg)
256+
} else {
257+
pass()
244258
}
245259
}
246260

247-
pass(act$value)
261+
invisible(act$val)
248262
}
249263

250264
all_utf8 <- function(x) {

R/expect-named.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ normalise_names <- function(x, ignore.order = FALSE, ignore.case = FALSE) {
7474

7575
expect_has_names_ <- function(act, trace_env = caller_env()) {
7676
act_names <- names(act$val)
77-
if (identical(act_names, NULL)) {
77+
if (is.null(act_names)) {
7878
msg <- sprintf("Expected %s to have names.", act$lab)
79-
return(fail(msg, trace_env = trace_env))
79+
fail(msg, trace_env = trace_env)
80+
} else {
81+
pass()
8082
}
81-
return(pass(act$val))
83+
invisible(act$val)
8284
}

R/expect-no-condition.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ expect_no_ <- function(
119119
"."
120120
)
121121
msg_act <- actual_condition(first_match)
122-
return(fail(c(msg_exp, msg_act), trace_env = trace_env))
122+
fail(c(msg_exp, msg_act), trace_env = trace_env)
123+
} else {
124+
pass()
123125
}
124126

125-
pass(act$val)
127+
invisible(act$val)
126128
}
127129

128130
indent_lines <- function(x) {

R/expect-reference.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ expect_reference <- function(
2828

2929
if (!is_reference(act$val, exp$val)) {
3030
msg <- sprintf("Expected %s to be a reference to %s.", act$lab, exp$lab)
31-
return(fail(msg, info = info))
31+
fail(msg, info = info)
32+
} else {
33+
pass()
3234
}
33-
pass(act$val)
35+
invisible(act$val)
3436
}
3537

3638
# expect_reference() needs dev version of rlang

R/expect-self-test.R

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ expect_success <- function(expr) {
5151
"Expected one success.",
5252
sprintf("Actually succeeded %i times", status$n_success)
5353
)
54-
return(fail(msg))
55-
}
56-
57-
if (status$n_failure > 0) {
54+
fail(msg)
55+
} else if (status$n_failure > 0) {
5856
msg <- c(
5957
"Expected zero failures.",
6058
sprintf("Actually failed %i times", status$n_failure)
6159
)
62-
return(fail(msg))
60+
fail(msg)
61+
} else {
62+
pass()
6363
}
6464

65-
pass(NULL)
65+
invisible()
6666
}
6767

6868
#' @export
@@ -116,9 +116,11 @@ expect_no_success <- function(expr) {
116116
status <- capture_success_failure(expr)
117117

118118
if (status$n_success > 0) {
119-
return(fail("Expectation succeeded"))
119+
fail("Expectation succeeded")
120+
} else {
121+
pass()
120122
}
121-
pass(NULL)
123+
invisible()
122124
}
123125

124126
#' @export
@@ -128,9 +130,11 @@ expect_no_failure <- function(expr) {
128130
status <- capture_success_failure(expr)
129131

130132
if (status$n_failure > 0) {
131-
return(fail("Expectation failed"))
133+
fail("Expectation failed")
134+
} else {
135+
pass()
132136
}
133-
pass(NULL)
137+
invisible()
134138
}
135139

136140
expect_snapshot_skip <- function(x, cran = FALSE) {

R/expect-setequal.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ expect_contains <- function(object, expected) {
114114
sprintf("Missing: %s", values(exp$val[exp_miss]))
115115
)
116116
fail(c(msg_exp, msg_act))
117+
} else {
118+
pass()
117119
}
118120

119-
pass(act$val)
121+
invisible(act$val)
120122
}
121123

122124
#' @export
@@ -141,9 +143,11 @@ expect_in <- function(object, expected) {
141143
sprintf("Invalid: %s", values(act$val[act_miss]))
142144
)
143145
fail(c(msg_exp, msg_act))
146+
} else {
147+
pass()
144148
}
145149

146-
pass(act$val)
150+
invisible(act$val)
147151
}
148152

149153
# Helpers ----------------------------------------------------------------------

R/expect-shape.R

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ expect_length <- function(object, n) {
3232
sprintf("Expected %s to have length %i.", act$lab, n),
3333
sprintf("Actual length: %i.", act$n)
3434
)
35-
return(fail(msg))
35+
fail(msg)
36+
} else {
37+
pass()
3638
}
37-
pass(act$val)
39+
invisible(act$val)
3840
}
3941

4042
#' @param nrow,ncol Expected [nrow()]/[ncol()] of `object`.
@@ -61,14 +63,17 @@ expect_shape = function(object, ..., nrow, ncol, dim) {
6163
sprintf("Expected %s to have %i rows.", act$lab, nrow),
6264
sprintf("Actual rows: %i.", act$nrow)
6365
)
64-
return(fail(msg))
66+
fail(msg)
67+
} else {
68+
pass()
6569
}
6670
} else if (!missing(ncol)) {
6771
check_number_whole(ncol, allow_na = TRUE)
6872

6973
if (length(dim_object) == 1L) {
7074
msg <- sprintf("Expected %s to have two or more dimensions.", act$lab)
71-
return(fail(msg))
75+
fail(msg)
76+
return(invisible(act$val))
7277
}
7378

7479
act$ncol <- dim_object[2L]
@@ -78,7 +83,9 @@ expect_shape = function(object, ..., nrow, ncol, dim) {
7883
sprintf("Expected %s to have %i columns.", act$lab, ncol),
7984
sprintf("Actual columns: %i.", act$ncol)
8085
)
81-
return(fail(msg))
86+
fail(msg)
87+
} else {
88+
pass()
8289
}
8390
} else {
8491
# !missing(dim)
@@ -92,16 +99,17 @@ expect_shape = function(object, ..., nrow, ncol, dim) {
9299
sprintf("Expected %s to have %i dimensions.", act$lab, length(dim)),
93100
sprintf("Actual dimensions: %i.", length(act$dim))
94101
)
95-
}
96-
97-
if (!identical(as.integer(act$dim), as.integer(dim))) {
102+
fail(msg)
103+
} else if (!identical(as.integer(act$dim), as.integer(dim))) {
98104
msg <- c(
99105
sprintf("Expected %s to have dim (%s).", act$lab, toString(dim)),
100106
sprintf("Actual dim: (%s).", toString(act$dim))
101107
)
102-
return(fail(msg))
108+
fail(msg)
109+
} else {
110+
pass()
103111
}
104112
}
105113

106-
pass(act$val)
114+
invisible(act$val)
107115
}

R/expect-silent.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ expect_silent <- function(object) {
3131
sprintf("Expected %s to run silently.", act$lab),
3232
sprintf("Actual noise: %s.", paste(outputs, collapse = ", "))
3333
)
34-
return(fail(msg))
34+
fail(msg)
35+
} else {
36+
pass()
3537
}
36-
pass(act$cap$result)
38+
invisible(act$cap$result)
3739
}

0 commit comments

Comments
 (0)