Skip to content

Commit 8e09c6e

Browse files
committed
Improve consistency
1 parent fa69ee8 commit 8e09c6e

31 files changed

+239
-298
lines changed

R/expect-condition.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,10 @@ check_condition_dots <- function(
629629
}
630630

631631
actual_condition <- function(cond) {
632-
sprintf(
633-
"Actual <%s>:\n%s",
634-
paste(class(cond), collapse = "/"),
635-
cnd_message(cond)
632+
paste0(
633+
"Actually got a <",
634+
class(cond)[[1]],
635+
"> with message:\n",
636+
indent_lines(cnd_message(cond))
636637
)
637638
}

R/expect-equality.R

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,20 @@ expect_identical <- function(
107107
} else {
108108
ident <- identical(act$val, exp$val, ...)
109109
if (ident) {
110-
msg <- ""
110+
msg_act <- NULL
111111
} else {
112112
compare <- compare(act$val, exp$val)
113113
if (compare$equal) {
114-
msg <- "Objects equal but not identical"
114+
msg_act <- "Objects equal but not identical"
115115
} else {
116-
msg <- compare$message
116+
msg_act <- compare$message
117117
}
118118
}
119119

120120
if (!ident) {
121-
msg <- sprintf(
122-
"Expected %s to be identical to %s.\n%s",
123-
act$lab,
124-
exp$lab,
125-
msg
121+
msg <- c(
122+
sprintf("Expected %s to be identical to %s.", act$lab, exp$lab),
123+
msg_act
126124
)
127125
return(fail(msg, info = info))
128126
}
@@ -146,12 +144,10 @@ expect_waldo_equal_ <- function(
146144
y_arg = "expected"
147145
)
148146
if (length(comp) != 0) {
149-
msg <- sprintf(
150-
"Expected %s to be %s to %s.\n\n%s",
151-
act$lab,
152-
type,
153-
exp$lab,
154-
paste0(comp, collapse = "\n\n")
147+
msg <- c(
148+
sprintf("Expected %s to be %s to %s.", act$lab, type, exp$lab),
149+
"Differences:",
150+
paste0(comp, collpase = "\n")
155151
)
156152
return(fail(msg, info = info, trace_env = trace_env))
157153
}

R/expect-inheritance.R

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

7272
if (!identical(act_type, type)) {
73-
msg <- sprintf(
74-
"Expected %s to have type %s.\nActual type: %s",
75-
act$lab,
76-
format_class(type),
77-
format_class(act_type)
73+
msg <- c(
74+
sprintf("Expected %s to have type %s.", act$lab, format_class(type)),
75+
sprintf("Actual type: %s", format_class(act_type))
7876
)
7977
return(fail(msg))
8078
}
@@ -102,16 +100,14 @@ expect_s3_class <- function(object, class, exact = FALSE) {
102100
if (!isS3(act$val)) {
103101
msg <- c(
104102
sprintf("Expected %s to be an S3 object.", act$lab),
105-
sprintf("Actually is a %s object.", oo_type(act$val))
103+
sprintf("Actual OO type: %s.", oo_type(act$val))
106104
)
107105
return(fail(msg))
108106
} else if (exact) {
109107
if (!identical(class(act$val), class)) {
110-
msg <- sprintf(
111-
"Expected %s to have class %s.\nActual class: %s",
112-
act$lab,
113-
exp_lab,
114-
act$class
108+
msg <- c(
109+
sprintf("Expected %s to have class %s.", act$lab, exp_lab),
110+
sprintf("Actual class: %s", act$class)
115111
)
116112
return(fail(msg))
117113
}
@@ -147,7 +143,7 @@ expect_s4_class <- function(object, class) {
147143
if (!isS4(act$val)) {
148144
msg <- c(
149145
sprintf("Expected %s to be an S4 object.", act$lab),
150-
sprintf("Actually is a %s object.", oo_type(act$val))
146+
sprintf("Actual OO type: %s.", oo_type(act$val))
151147
)
152148
return(fail(msg))
153149
} else {
@@ -175,7 +171,7 @@ expect_r6_class <- function(object, class) {
175171
if (!inherits(act$val, "R6")) {
176172
msg <- c(
177173
sprintf("Expected %s to be an R6 object.", act$lab),
178-
sprintf("Actually is a %s object.", oo_type(act$val))
174+
sprintf("Actual OO type: %s.", oo_type(act$val))
179175
)
180176
return(fail(msg))
181177
}
@@ -206,7 +202,7 @@ expect_s7_class <- function(object, class) {
206202
if (!S7::S7_inherits(object)) {
207203
msg <- c(
208204
sprintf("Expected %s to be an S7 object.", act$lab),
209-
sprintf("Actually is a %s object.", oo_type(act$val))
205+
sprintf("Actual OO type: %s.", oo_type(act$val))
210206
)
211207
return(fail(msg))
212208
}
@@ -279,20 +275,17 @@ format_class <- function(x) {
279275

280276
oo_type <- function(x) {
281277
if (!is.object(x)) {
282-
"base"
283-
} else if (!isS4(x)) {
278+
return("none")
279+
}
280+
if (isS4(x)) {
281+
"S4"
282+
} else {
284283
if (inherits(x, "R6")) {
285284
"R6"
286285
} else if (inherits(x, "S7")) {
287286
"S7"
288287
} else {
289288
"S3"
290289
}
291-
} else {
292-
if (!is(x, "refClass")) {
293-
"S4"
294-
} else {
295-
"RC"
296-
}
297290
}
298291
}

R/expect-invisible.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ expect_invisible <- function(call, label = NULL) {
2727
if (!identical(vis$visible, FALSE)) {
2828
msg <- c(
2929
sprintf("Expected %s to return invisibly.", lab),
30-
"Actually returned visibly."
30+
"Actual visibility: visible."
3131
)
3232
return(fail(msg))
3333
}
@@ -43,7 +43,7 @@ expect_visible <- function(call, label = NULL) {
4343
if (!identical(vis$visible, TRUE)) {
4444
msg <- c(
4545
sprintf("Expected %s to return visibly.", lab),
46-
"Actually returned invisibly."
46+
"Actual visibility: invisible."
4747
)
4848
return(fail(msg))
4949
}

R/expect-match.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ expect_match_ <- function(
132132
}
133133
match <- if (negate) "not to match" else "to match"
134134

135-
exp_msg <- sprintf(
135+
msg_exp <- sprintf(
136136
"Expected %s%s %s %s %s.",
137137
which,
138138
act$lab,
139139
match,
140140
if (fixed) "string" else "regexp",
141141
encodeString(regexp, quote = '"')
142142
)
143-
act_msg <- c(paste0("Actual ", title, ':'), text)
144-
return(fail(c(exp_msg, act_msg), info = info, trace_env = trace_env))
143+
msg_act <- c(paste0("Actual ", title, ':'), text)
144+
return(fail(c(msg_exp, msg_act), info = info, trace_env = trace_env))
145145
}

R/expect-no-condition.R

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ expect_no_ <- function(
108108
act <- quasi_capture(enquo(object), NULL, capture)
109109

110110
if (!is.null(first_match)) {
111-
exp_msg <- paste0(
111+
msg_exp <- paste0(
112112
"Expected ",
113113
act$lab,
114114
" to run without any ",
@@ -118,13 +118,8 @@ expect_no_ <- function(
118118
if (!is.null(regexp)) paste0(" matching pattern '", regexp, "'"),
119119
"."
120120
)
121-
act_msg <- paste0(
122-
"Actually got a <",
123-
class(first_match)[[1]],
124-
"> with message:\n",
125-
indent_lines(rlang::cnd_message(first_match))
126-
)
127-
return(fail(c(exp_msg, act_msg), trace_env = trace_env))
121+
msg_act <- actual_condition(first_match)
122+
return(fail(c(msg_exp, msg_act), trace_env = trace_env))
128123
}
129124

130125
pass(act$val)

R/expect-output.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ expect_output <- function(
3636

3737
if (identical(regexp, NA)) {
3838
if (!identical(act$cap, "")) {
39-
msg <- sprintf(
40-
"Expected %s to produce no output.\nActual output:\n%s",
41-
act$lab,
42-
encodeString(act$cap)
39+
msg <- c(
40+
sprintf("Expected %s to produce no output.", act$lab),
41+
sprintf("Actual output:\n%s", encodeString(act$cap))
4342
)
4443
return(fail(msg, info = info))
4544
}

R/expect-shape.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ expect_shape = function(object, ..., nrow, ncol, dim) {
6767
check_number_whole(ncol, allow_na = TRUE)
6868

6969
if (length(dim_object) == 1L) {
70-
msg <- sprintf("Expected %s to have more than one dimension.", act$lab)
70+
msg <- sprintf("Expected %s to have two or more dimensions.", act$lab)
7171
return(fail(msg))
7272
}
7373

tests/testthat/_snaps/expect-comparison.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# basic comparisons work
22

33
Code
4-
expect_lt(10, 10)
4+
expect_lt(x, 10)
55
Condition
66
Error:
7-
! Expected 10 < 10.
7+
! Expected `x` < 10.
88
Actual 10.0 >= 10.0
99
Difference 0.0 >= 0
1010

1111
---
1212

1313
Code
14-
expect_gt(10, 10)
14+
expect_gt(x, 10)
1515
Condition
1616
Error:
17-
! Expected 10 > 10.
17+
! Expected `x` > 10.
1818
Actual 10.0 <= 10.0
1919
Difference 0.0 <= 0
2020

tests/testthat/_snaps/expect-condition.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
Condition
1414
Error:
1515
! Expected `stop("Yes")` to not throw a error.
16-
Actual <simpleError/error/condition>:
17-
Yes
16+
Actually got a <simpleError> with message:
17+
Yes
1818

1919
# regexp = string matches for error message
2020

@@ -49,8 +49,8 @@
4949
Condition
5050
Error:
5151
! Expected `fb()` to not throw a error.
52-
Actual <foobar/rlang_error/error/condition>:
53-
dispatched!
52+
Actually got a <foobar> with message:
53+
dispatched!
5454

5555
# expect_warning validates its inputs
5656

@@ -82,8 +82,9 @@
8282
Condition
8383
Error:
8484
! Expected `message("!")` to not throw a message.
85-
Actual <simpleMessage/message/condition>:
86-
!
85+
Actually got a <simpleMessage> with message:
86+
!
87+
8788

8889
# expect_message validates its inputs
8990

0 commit comments

Comments
 (0)