Skip to content

Commit bf46420

Browse files
committed
Eliminate intermediate variable
1 parent 7397403 commit bf46420

File tree

9 files changed

+45
-71
lines changed

9 files changed

+45
-71
lines changed

β€ŽR/expect-inheritance.Rβ€Ž

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@ expect_type <- function(object, type) {
7070
act_type <- typeof(act$val)
7171

7272
if (!identical(act_type, type)) {
73-
msg <- c(
73+
fail(c(
7474
sprintf("Expected %s to have type %s.", act$lab, format_class(type)),
7575
sprintf("Actual type: %s", format_class(act_type))
76-
)
77-
fail(msg)
76+
))
7877
} else {
7978
pass()
8079
}
@@ -95,30 +94,26 @@ expect_s3_class <- function(object, class, exact = FALSE) {
9594

9695
if (identical(class, NA)) {
9796
if (isS3(object)) {
98-
msg <- sprintf("Expected %s not to be an S3 object.", act$lab)
99-
fail(msg)
97+
fail(sprintf("Expected %s not to be an S3 object.", act$lab))
10098
} else {
10199
pass()
102100
}
103101
} else if (is.character(class)) {
104102
if (!isS3(act$val)) {
105-
msg <- c(
103+
fail(c(
106104
sprintf("Expected %s to be an S3 object.", act$lab),
107105
sprintf("Actual OO type: %s.", oo_type(act$val))
108-
)
109-
fail(msg)
106+
))
110107
} else if (exact && !identical(class(act$val), class)) {
111-
msg <- c(
108+
fail(c(
112109
sprintf("Expected %s to have class %s.", act$lab, exp_lab),
113110
sprintf("Actual class: %s.", act$class)
114-
)
115-
fail(msg)
111+
))
116112
} else if (!inherits(act$val, class)) {
117-
msg <- c(
113+
fail(c(
118114
sprintf("Expected %s to inherit from %s.", act$lab, exp_lab),
119115
sprintf("Actual class: %s.", act$class)
120-
)
121-
fail(msg)
116+
))
122117
} else {
123118
pass()
124119
}
@@ -138,24 +133,21 @@ expect_s4_class <- function(object, class) {
138133

139134
if (identical(class, NA)) {
140135
if (isS4(object)) {
141-
msg <- sprintf("Expected %s not to be an S4 object.", act$lab)
142-
fail(msg)
136+
fail(sprintf("Expected %s not to be an S4 object.", act$lab))
143137
} else {
144138
pass()
145139
}
146140
} else if (is.character(class)) {
147141
if (!isS4(act$val)) {
148-
msg <- c(
142+
fail(c(
149143
sprintf("Expected %s to be an S4 object.", act$lab),
150144
sprintf("Actual OO type: %s.", oo_type(act$val))
151-
)
152-
fail(msg)
145+
))
153146
} else if (!methods::is(act$val, class)) {
154-
msg <- c(
147+
fail(c(
155148
sprintf("Expected %s to inherit from %s.", act$lab, exp_lab),
156149
sprintf("Actual class: %s.", act$class)
157-
)
158-
fail(msg)
150+
))
159151
} else {
160152
pass()
161153
}
@@ -173,19 +165,17 @@ expect_r6_class <- function(object, class) {
173165
check_string(class)
174166

175167
if (!inherits(act$val, "R6")) {
176-
msg <- c(
168+
fail(c(
177169
sprintf("Expected %s to be an R6 object.", act$lab),
178170
sprintf("Actual OO type: %s.", oo_type(act$val))
179-
)
180-
fail(msg)
171+
))
181172
} else if (!inherits(act$val, class)) {
182173
act_class <- format_class(class(act$val))
183174
exp_class <- format_class(class)
184-
msg <- c(
175+
fail(c(
185176
sprintf("Expected %s to inherit from %s.", act$lab, exp_class),
186177
sprintf("Actual class: %s.", act_class)
187-
)
188-
fail(msg)
178+
))
189179
} else {
190180
pass()
191181
}
@@ -204,21 +194,19 @@ expect_s7_class <- function(object, class) {
204194
act <- quasi_label(enquo(object))
205195

206196
if (!S7::S7_inherits(object)) {
207-
msg <- c(
197+
fail(c(
208198
sprintf("Expected %s to be an S7 object.", act$lab),
209199
sprintf("Actual OO type: %s.", oo_type(act$val))
210-
)
211-
fail(msg)
200+
))
212201
} else if (!S7::S7_inherits(object, class)) {
213202
exp_class <- attr(class, "name", TRUE)
214203
act_class <- setdiff(base::class(object), "S7_object")
215204
act_class_desc <- paste0("<", act_class, ">", collapse = "/")
216205

217-
msg <- c(
206+
fail(c(
218207
sprintf("Expected %s to inherit from <%s>.", act$lab, exp_class),
219208
sprintf("Actual class: %s.", act_class_desc)
220-
)
221-
fail(msg)
209+
))
222210
} else {
223211
pass()
224212
}

β€ŽR/expect-invisible.Rβ€Ž

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ expect_invisible <- function(call, label = NULL) {
2525
vis <- withVisible(call)
2626

2727
if (!identical(vis$visible, FALSE)) {
28-
msg <- c(
28+
fail(c(
2929
sprintf("Expected %s to return invisibly.", lab),
3030
"Actual visibility: visible."
31-
)
32-
fail(msg)
31+
))
3332
} else {
3433
pass()
3534
}
@@ -43,11 +42,10 @@ expect_visible <- function(call, label = NULL) {
4342
vis <- withVisible(call)
4443

4544
if (!identical(vis$visible, TRUE)) {
46-
msg <- c(
45+
fail(c(
4746
sprintf("Expected %s to return visibly.", lab),
4847
"Actual visibility: invisible."
49-
)
50-
fail(msg)
48+
))
5149
} else {
5250
pass()
5351
}

β€ŽR/expect-known.Rβ€Ž

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,11 @@ expect_known_hash <- function(object, hash = NULL) {
248248
pass()
249249
} else {
250250
if (hash != act_hash) {
251-
msg <- sprintf(
251+
fail(sprintf(
252252
"Expected value to hash to %s.\nActual hash: %s",
253253
hash,
254254
act_hash
255-
)
256-
fail(msg)
255+
))
257256
} else {
258257
pass()
259258
}

β€ŽR/expect-shape.Rβ€Ž

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ expect_length <- function(object, n) {
2828
act$n <- length(act$val)
2929

3030
if (act$n != n) {
31-
msg <- c(
31+
fail(c(
3232
sprintf("Expected %s to have length %i.", act$lab, n),
3333
sprintf("Actual length: %i.", act$n)
34-
)
35-
fail(msg)
34+
))
3635
} else {
3736
pass()
3837
}
@@ -60,31 +59,28 @@ expect_shape = function(object, ..., nrow, ncol, dim) {
6059
act$nrow <- dim_object[1L]
6160

6261
if (!identical(as.integer(act$nrow), as.integer(nrow))) {
63-
msg <- c(
62+
fail(c(
6463
sprintf("Expected %s to have %i rows.", act$lab, nrow),
6564
sprintf("Actual rows: %i.", act$nrow)
66-
)
67-
fail(msg)
65+
))
6866
} else {
6967
pass()
7068
}
7169
} else if (!missing(ncol)) {
7270
check_number_whole(ncol, allow_na = TRUE)
7371

7472
if (length(dim_object) == 1L) {
75-
msg <- sprintf("Expected %s to have two or more dimensions.", act$lab)
76-
fail(msg)
73+
fail(sprintf("Expected %s to have two or more dimensions.", act$lab))
7774
return(invisible(act$val))
7875
}
7976

8077
act$ncol <- dim_object[2L]
8178

8279
if (!identical(as.integer(act$ncol), as.integer(ncol))) {
83-
msg <- c(
80+
fail(c(
8481
sprintf("Expected %s to have %i columns.", act$lab, ncol),
8582
sprintf("Actual columns: %i.", act$ncol)
86-
)
87-
fail(msg)
83+
))
8884
} else {
8985
pass()
9086
}
@@ -96,17 +92,15 @@ expect_shape = function(object, ..., nrow, ncol, dim) {
9692
act$dim <- dim_object
9793

9894
if (length(act$dim) != length(dim)) {
99-
msg <- c(
95+
fail(c(
10096
sprintf("Expected %s to have %i dimensions.", act$lab, length(dim)),
10197
sprintf("Actual dimensions: %i.", length(act$dim))
102-
)
103-
fail(msg)
98+
))
10499
} else if (!identical(as.integer(act$dim), as.integer(dim))) {
105-
msg <- c(
100+
fail(c(
106101
sprintf("Expected %s to have dim (%s).", act$lab, toString(dim)),
107102
sprintf("Actual dim: (%s).", toString(act$dim))
108-
)
109-
fail(msg)
103+
))
110104
} else {
111105
pass()
112106
}

β€ŽR/expect-silent.Rβ€Ž

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ expect_silent <- function(object) {
2727
)
2828

2929
if (length(outputs) != 0) {
30-
msg <- c(
30+
fail(c(
3131
sprintf("Expected %s to run silently.", act$lab),
3232
sprintf("Actual noise: %s.", paste(outputs, collapse = ", "))
33-
)
34-
fail(msg)
33+
))
3534
} else {
3635
pass()
3736
}

β€ŽR/expect-that.Rβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
#'
3030
#' act_n <- length(act$val)
3131
#' if (act_n != n) {
32-
#' msg <- sprintf("%s has length %i, not length %i.", act$lab, act_n, n)
33-
#' fail(msg)
32+
#' fail(sprintf("%s has length %i, not length %i.", act$lab, act_n, n))
3433
#' } else {
3534
#' pass()
3635
#' }

β€ŽR/old-school.Rβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ takes_less_than <- function(amount) {
147147
duration <- system.time(force(expr))["elapsed"]
148148

149149
if (duration >= amount) {
150-
msg <- paste0("took ", duration, " seconds, which is more than ", amount)
151-
fail(msg)
150+
fail(paste0("took ", duration, " seconds, which is more than ", amount))
152151
} else {
153152
pass()
154153
}

β€ŽR/test-compiled-code.Rβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ expect_cpp_tests_pass <- function(package) {
2424
info <- paste(output[-1], collapse = "\n")
2525

2626
if (!tests_passed) {
27-
msg <- paste("C++ unit tests:", info, sep = "\n")
28-
fail(msg)
27+
fail(paste("C++ unit tests:", info, sep = "\n"))
2928
} else {
3029
pass()
3130
}

β€Žman/fail.Rdβ€Ž

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)