Skip to content

Commit 9104922

Browse files
committed
More polishing
1 parent 41da073 commit 9104922

14 files changed

+208
-163
lines changed

R/expect-comparison.R

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,30 @@ expect_compare_ <- function(
3939
}
4040
if (!isTRUE(cmp)) {
4141
diff <- act$val - exp$val
42-
msg1 <- sprintf("Expected %s %s %s.", act$lab, operator, exp$lab)
42+
msg_exp <- sprintf("Expected %s %s %s.", act$lab, operator, exp$lab)
4343

4444
if (is.nan(diff)) {
45-
msg2 <- "Actual values are incomparable."
46-
msg3 <- NULL
45+
msg_act <- "Actual values are incomparable."
4746
} else if (is.na(diff)) {
48-
msg2 <- "Actual comparison is NA."
49-
msg3 <- NULL
47+
msg_act <- "Actual comparison is NA."
5048
} else {
5149
digits <- max(
5250
digits(act$val),
5351
digits(exp$val),
5452
min_digits(act$val, exp$val)
5553
)
5654

57-
msg2 <- sprintf(
58-
"Actual %s %s %s",
59-
num_exact(act$val, digits),
60-
actual_op,
61-
num_exact(exp$val, digits)
62-
)
63-
msg3 <- sprintf(
64-
"Difference %s %s 0",
65-
num_exact(act$val - exp$val, digits),
66-
actual_op
55+
msg_act <- c(
56+
sprintf(
57+
"Actual %s %s %s",
58+
num_exact(act$val, digits),
59+
actual_op,
60+
num_exact(exp$val, digits)
61+
),
62+
sprintf("Difference %s %s 0", num_exact(diff, digits), actual_op)
6763
)
6864
}
69-
return(fail(c(msg1, msg2, msg3), trace_env = trace_env))
65+
return(fail(c(msg_exp, msg_act), trace_env = trace_env))
7066
}
7167
pass(act$val)
7268
}

R/expect-condition.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,9 @@ compare_condition_3e <- function(cond_type, cond_class, cond, lab, expected) {
454454
}
455455
} else {
456456
if (!is.null(cond)) {
457-
sprintf(
458-
"Expected %s to not throw a %s.\nActual <%s>:\n%s",
459-
lab,
460-
cond_type,
461-
paste(class(cond), collapse = "/"),
462-
cnd_message(cond)
457+
c(
458+
sprintf("Expected %s to not throw a %s.", lab, cond_type),
459+
actual_condition(cond)
463460
)
464461
} else {
465462
NULL
@@ -630,3 +627,11 @@ check_condition_dots <- function(
630627
call = error_call
631628
)
632629
}
630+
631+
actual_condition <- function(cond) {
632+
sprintf(
633+
"Actual <%s>:\n%s",
634+
paste(class(cond), collapse = "/"),
635+
cnd_message(cond)
636+
)
637+
}

R/expect-no-condition.R

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

110110
if (!is.null(first_match)) {
111-
expected <- paste0(
111+
exp_msg <- paste0(
112112
"Expected ",
113-
quo_label(enquo(object)),
113+
act$lab,
114114
" to run without any ",
115115
base_class,
116116
"s",
117117
if (!is.null(class)) paste0(" of class '", class, "'"),
118118
if (!is.null(regexp)) paste0(" matching pattern '", regexp, "'"),
119119
"."
120120
)
121-
actual <- paste0(
121+
act_msg <- paste0(
122122
"Actually got a <",
123123
class(first_match)[[1]],
124-
"> with text:\n",
124+
"> with message:\n",
125125
indent_lines(rlang::cnd_message(first_match))
126126
)
127-
message <- format_error_bullets(c(expected, i = actual))
128-
return(fail(message, trace_env = trace_env))
127+
return(fail(c(exp_msg, act_msg), trace_env = trace_env))
129128
}
130129

131130
pass(act$val)

R/expect-output.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ expect_output <- function(
5252
pass(act$val)
5353
} else {
5454
act <- labelled_value(act$cap, act$lab)
55-
expect_match_(act, enc2native(regexp), ..., title = "Output")
55+
expect_match_(act, enc2native(regexp), ..., title = "output")
5656
}
5757
}

R/expect-setequal.R

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,17 @@ expect_setequal_ <- function(
4646
exp_miss <- unique(exp$val[!exp$val %in% act$val])
4747

4848
if (length(exp_miss) || length(act_miss)) {
49-
msg <- paste0(
49+
msg_exp <- sprintf(
50+
"Expected %s to have the same values as %s.",
5051
act$lab,
51-
" doesn't have the same values as ",
52-
exp$lab,
53-
".\n",
54-
if (length(act_miss)) {
55-
paste0("* Only in `actual`: ", values(act_miss), "\n")
56-
},
57-
if (length(exp_miss)) {
58-
paste0("* Only in `expected`: ", values(exp_miss), "\n")
59-
}
52+
exp$lab
6053
)
61-
return(fail(msg, trace_env = trace_env))
54+
msg_act <- c(
55+
if (length(act_miss)) sprintf("Needs: %s", values(act_miss)),
56+
if (length(exp_miss)) sprintf("Extra: %s", values(exp_miss))
57+
)
58+
59+
return(fail(c(msg_exp, msg_act), trace_env = trace_env))
6260
}
6361
pass(act$val)
6462
}
@@ -101,14 +99,13 @@ expect_contains <- function(object, expected) {
10199

102100
exp_miss <- !exp$val %in% act$val
103101
if (any(exp_miss)) {
104-
return(fail(paste0(
102+
msg_exp <- sprintf(
103+
"Expected %s to contain all values in %s.",
105104
act$lab,
106-
" doesn't fully contain all the values in ",
107-
exp$lab,
108-
".\n",
109-
paste0("* Missing from ", act$lab, ": ", values(exp$val[exp_miss]), "\n"),
110-
paste0("* Present in ", act$lab, ": ", values(act$val), "\n")
111-
)))
105+
exp$lab
106+
)
107+
msg_act <- sprintf("Missing: %s", values(exp$val[exp_miss]))
108+
fail(c(msg_exp, msg_act))
112109
}
113110

114111
pass(act$val)
@@ -125,14 +122,13 @@ expect_in <- function(object, expected) {
125122

126123
act_miss <- !act$val %in% exp$val
127124
if (any(act_miss)) {
128-
return(fail(paste0(
125+
msg_exp <- sprintf(
126+
"Expected all values in %s to be in %s.",
129127
act$lab,
130-
" isn't fully contained within ",
131-
exp$lab,
132-
".\n",
133-
paste0("* Missing from ", act$lab, ": ", values(act$val[act_miss]), "\n"),
134-
paste0("* Present in ", act$lab, ": ", values(exp$val), "\n")
135-
)))
128+
exp$lab
129+
)
130+
msg_act <- sprintf("Extra: %s", values(act$val[act_miss]))
131+
fail(c(msg_exp, msg_act))
136132
}
137133

138134
pass(act$val)

R/expect-shape.R

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

3030
if (act$n != n) {
31-
msg <- sprintf(
32-
"Expected %s to have length %i.\nActual length: %i",
33-
act$lab,
34-
n,
35-
act$n
31+
msg <- c(
32+
sprintf("Expected %s to have length %i.", act$lab, n),
33+
sprintf("Actual length: %i.", act$n)
3634
)
3735
return(fail(msg))
3836
}
@@ -59,32 +57,26 @@ expect_shape = function(object, ..., nrow, ncol, dim) {
5957
act$nrow <- dim_object[1L]
6058

6159
if (!identical(as.integer(act$nrow), as.integer(nrow))) {
62-
msg <- sprintf(
63-
"Expected %s to have %i rows.\nActual rows: %i",
64-
act$lab,
65-
nrow,
66-
act$nrow
60+
msg <- c(
61+
sprintf("Expected %s to have %i rows.", act$lab, nrow),
62+
sprintf("Actual rows: %i.", act$nrow)
6763
)
6864
return(fail(msg))
6965
}
7066
} else if (!missing(ncol)) {
7167
check_number_whole(ncol, allow_na = TRUE)
7268

7369
if (length(dim_object) == 1L) {
74-
return(fail(sprintf(
75-
"Expected %s to have more than one dimension.",
76-
act$lab
77-
)))
70+
msg <- sprintf("Expected %s to have more than one dimension.", act$lab)
71+
return(fail(msg))
7872
}
7973

8074
act$ncol <- dim_object[2L]
8175

8276
if (!identical(as.integer(act$ncol), as.integer(ncol))) {
83-
msg <- sprintf(
84-
"Expected %s to have %i columns.\nActual columns: %i",
85-
act$lab,
86-
ncol,
87-
act$ncol
77+
msg <- c(
78+
sprintf("Expected %s to have %i columns.", act$lab, ncol),
79+
sprintf("Actual columns: %i.", act$ncol)
8880
)
8981
return(fail(msg))
9082
}
@@ -96,20 +88,16 @@ expect_shape = function(object, ..., nrow, ncol, dim) {
9688
act$dim <- dim_object
9789

9890
if (length(act$dim) != length(dim)) {
99-
return(fail(sprintf(
100-
"Expected %s to have %i dimensions.\nActual dimensions: %i",
101-
act$lab,
102-
length(dim),
103-
length(act$dim)
104-
)))
91+
msg <- c(
92+
sprintf("Expected %s to have %i dimensions.", act$lab, length(dim)),
93+
sprintf("Actual dimensions: %i.", length(act$dim))
94+
)
10595
}
10696

10797
if (!identical(as.integer(act$dim), as.integer(dim))) {
108-
msg <- sprintf(
109-
"Expected %s to have dim (%s).\nActual dim: (%s)",
110-
act$lab,
111-
toString(dim),
112-
toString(act$dim)
98+
msg <- c(
99+
sprintf("Expected %s to have dim (%s).", act$lab, toString(dim)),
100+
sprintf("Actual dim: (%s).", toString(act$dim))
113101
)
114102
return(fail(msg))
115103
}

R/expect-silent.R

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

2929
if (length(outputs) != 0) {
30-
msg <- sprintf(
31-
"Expected %s to run silently.\nActually produced: %s",
32-
act$lab,
33-
paste(outputs, collapse = ", ")
30+
msg <- c(
31+
sprintf("Expected %s to run silently.", act$lab),
32+
sprintf("Actually produced: %s.", paste(outputs, collapse = ", "))
3433
)
3534
return(fail(msg))
3635
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
# expect_no_* conditions behave as expected
22

33
Expected `stop("error")` to run without any errors.
4-
i Actually got a <simpleError> with text:
4+
Actually got a <simpleError> with text:
55
error
66

77
---
88

99
Expected `warning("warning")` to run without any warnings.
10-
i Actually got a <simpleWarning> with text:
10+
Actually got a <simpleWarning> with text:
1111
warning
1212

1313
---
1414

1515
Expected `message("message")` to run without any messages.
16-
i Actually got a <simpleMessage> with text:
16+
Actually got a <simpleMessage> with text:
1717
message
1818
1919

2020
---
2121

2222
Expected `abort("error")` to run without any errors.
23-
i Actually got a <rlang_error> with text:
23+
Actually got a <rlang_error> with text:
2424
error
2525

2626
---
2727

2828
Expected `warn("warning")` to run without any warnings.
29-
i Actually got a <rlang_warning> with text:
29+
Actually got a <rlang_warning> with text:
3030
warning
3131

3232
---
3333

3434
Expected `inform("message")` to run without any messages.
35-
i Actually got a <rlang_message> with text:
35+
Actually got a <rlang_message> with text:
3636
message
3737

3838
# expect_no_ continues execution
3939

4040
Expected `{ ... }` to run without any warnings.
41-
i Actually got a <simpleWarning> with text:
41+
Actually got a <simpleWarning> with text:
4242
x
4343

4444
# expect_no_* don't emit success when they fail
4545

4646
Expected `stop("!")` to run without any errors.
47-
i Actually got a <simpleError> with text:
47+
Actually got a <simpleError> with text:
4848
!
4949

5050
# matched conditions give informative message
@@ -54,27 +54,27 @@
5454
Condition
5555
Error:
5656
! Expected `foo()` to run without any warnings.
57-
i Actually got a <test> with text:
57+
Actually got a <test> with text:
5858
This is a problem!
5959
Code
6060
expect_no_warning(foo(), message = "problem")
6161
Condition
6262
Error:
6363
! Expected `foo()` to run without any warnings matching pattern 'problem'.
64-
i Actually got a <test> with text:
64+
Actually got a <test> with text:
6565
This is a problem!
6666
Code
6767
expect_no_warning(foo(), class = "test")
6868
Condition
6969
Error:
7070
! Expected `foo()` to run without any warnings of class 'test'.
71-
i Actually got a <test> with text:
71+
Actually got a <test> with text:
7272
This is a problem!
7373
Code
7474
expect_no_warning(foo(), message = "problem", class = "test")
7575
Condition
7676
Error:
7777
! Expected `foo()` to run without any warnings of class 'test' matching pattern 'problem'.
78-
i Actually got a <test> with text:
78+
Actually got a <test> with text:
7979
This is a problem!
8080

0 commit comments

Comments
 (0)