Skip to content

Commit 29256b5

Browse files
committed
Make labels more flexible
1 parent eb51b50 commit 29256b5

File tree

7 files changed

+33
-30
lines changed

7 files changed

+33
-30
lines changed

R/expect-equality.R

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

7070
if (edition_get() >= 3) {
71-
expect_waldo_equal_("equal", act, exp, info, ..., tolerance = tolerance)
71+
msg <- "Expected %s to equal %s."
72+
expect_waldo_equal_(msg, act, exp, info, ..., tolerance = tolerance)
7273
} else {
7374
if (!is.null(tolerance)) {
7475
comp <- compare(act$val, exp$val, ..., tolerance = tolerance)
@@ -105,7 +106,8 @@ expect_identical <- function(
105106
exp <- quasi_label(enquo(expected), expected.label)
106107

107108
if (edition_get() >= 3) {
108-
expect_waldo_equal_("identical", act, exp, info, ...)
109+
msg <- "Expected %s to be identical to %s."
110+
expect_waldo_equal_(msg, act, exp, info, ...)
109111
} else {
110112
if (identical(act$val, exp$val, ...)) {
111113
pass()
@@ -129,7 +131,7 @@ expect_identical <- function(
129131
}
130132

131133
expect_waldo_equal_ <- function(
132-
type,
134+
msg,
133135
act,
134136
exp,
135137
info = NULL,
@@ -147,7 +149,7 @@ expect_waldo_equal_ <- function(
147149
pass()
148150
} else {
149151
msg <- c(
150-
sprintf("Expected %s to be %s to %s.", act$lab, type, exp$lab),
152+
sprintf(msg, act$lab, exp$lab),
151153
"Differences:",
152154
paste0(comp, collpase = "\n")
153155
)

R/expect-named.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ expect_named <- function(
5151
exp$val <- normalise_names(exp$val, ignore.order, ignore.case)
5252
act_names <- labelled_value(
5353
normalise_names(names(act$val), ignore.order, ignore.case),
54-
paste0("names of ", act$lab)
54+
act$lab
5555
)
5656

57+
msg <- "Expected %s to have names %s."
5758
if (ignore.order) {
58-
expect_setequal_(act_names, exp)
59+
expect_setequal_(msg, act_names, exp)
5960
} else {
60-
expect_waldo_equal_("equal", act_names, exp)
61+
expect_waldo_equal_(msg, act_names, exp)
6162
}
6263
}
6364

R/expect-setequal.R

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

37-
expect_setequal_(act, exp)
37+
expect_setequal_("Expected %s to have the same values as %s.", act, exp)
3838
}
3939

4040
expect_setequal_ <- function(
41+
msg,
4142
act,
4243
exp,
4344
trace_env = caller_env()
@@ -48,11 +49,7 @@ expect_setequal_ <- function(
4849
if (length(exp_miss) == 0 && length(act_miss) == 0) {
4950
pass()
5051
} else {
51-
msg_exp <- sprintf(
52-
"Expected %s to have the same values as %s.",
53-
act$lab,
54-
exp$lab
55-
)
52+
msg_exp <- sprintf(msg, act$lab, exp$lab)
5653
msg_act <- c(
5754
sprintf("Actual: %s", values(act$val)),
5855
sprintf("Expected: %s", values(exp$val)),
@@ -103,13 +100,16 @@ expect_mapequal <- function(object, expected) {
103100
pass()
104101
} else {
105102
if (!setequal(act_nms, exp_nms)) {
106-
act_names <- labelled_value(names(act$val), paste0("names of ", act$lab))
107-
exp_names <- labelled_value(names(exp$val), paste0("names of ", exp$lab))
108-
expect_setequal_(act_names, exp_names)
103+
msg <- "Expected %s to have the same names as %s."
104+
act_names <- labelled_value(names(act$val), act$lab)
105+
exp_names <- labelled_value(names(exp$val), exp$lab)
106+
expect_setequal_(msg, act_names, exp_names)
109107
} else {
110108
if (edition_get() >= 3) {
111109
act <- labelled_value(act$val[exp_nms], act$lab)
112-
expect_waldo_equal_("equal", act, exp)
110+
111+
msg <- "Expected %s to contain same values as %s."
112+
expect_waldo_equal_(msg, act, exp)
113113
} else {
114114
# Packages depend on 2e behaviour, but the expectation isn't written
115115
# to be reused, and we don't want to bother

tests/testthat/_snaps/expect-equality.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
expect_equal(x, "a")
1616
Condition
1717
Error:
18-
! Expected `x` to be equal to "a".
18+
! Expected `x` to equal "a".
1919
Differences:
2020
`actual` is a double vector (1)
2121
`expected` is a character vector ('a')
@@ -66,7 +66,7 @@
6666
expect_equal(x, !!y)
6767
Condition
6868
Error:
69-
! Expected `x` to be equal to 2.
69+
! Expected `x` to equal 2.
7070
Differences:
7171
`actual`: 1.0
7272
`expected`: 2.0

tests/testthat/_snaps/expect-named.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
expect_named(x, "b")
1313
Condition
1414
Error:
15-
! Expected names of `x` to be equal to "b".
15+
! Expected `x` to have names "b".
1616
Differences:
1717
`actual`: "a"
1818
`expected`: "b"
@@ -23,7 +23,7 @@
2323
expect_named(x1, c("a", "b"), ignore.order = TRUE)
2424
Condition
2525
Error:
26-
! Expected names of `x1` to have the same values as `c("a", "b")`.
26+
! Expected `x1` to have names `c("a", "b")`.
2727
Actual: "a"
2828
Expected: "a", "b"
2929
Absent: "b"
@@ -34,7 +34,7 @@
3434
expect_named(x2, "a", ignore.order = TRUE)
3535
Condition
3636
Error:
37-
! Expected names of `x2` to have the same values as "a".
37+
! Expected `x2` to have names "a".
3838
Actual: "a", "b"
3939
Expected: "a"
4040
Needs: "b"
@@ -45,7 +45,7 @@
4545
expect_named(x1, "b", ignore.order = TRUE)
4646
Condition
4747
Error:
48-
! Expected names of `x1` to have the same values as "b".
48+
! Expected `x1` to have names "b".
4949
Actual: "a"
5050
Expected: "b"
5151
Needs: "a"
@@ -57,7 +57,7 @@
5757
expect_named(x1, c("a", "b"), ignore.order = FALSE)
5858
Condition
5959
Error:
60-
! Expected names of `x1` to be equal to `c("a", "b")`.
60+
! Expected `x1` to have names `c("a", "b")`.
6161
Differences:
6262
`actual`: "a"
6363
`expected`: "a" "b"
@@ -68,7 +68,7 @@
6868
expect_named(x2, "a", ignore.order = FALSE)
6969
Condition
7070
Error:
71-
! Expected names of `x2` to be equal to "a".
71+
! Expected `x2` to have names "a".
7272
Differences:
7373
`actual`: "a" "b"
7474
`expected`: "a"
@@ -79,7 +79,7 @@
7979
expect_named(x1, "b", ignore.order = FALSE)
8080
Condition
8181
Error:
82-
! Expected names of `x1` to be equal to "b".
82+
! Expected `x1` to have names "b".
8383
Differences:
8484
`actual`: "a"
8585
`expected`: "b"

tests/testthat/_snaps/expect-setequal.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
expect_mapequal(x, y)
116116
Condition
117117
Error:
118-
! Expected names of `x` to have the same values as names of `y`.
118+
! Expected `x` to have the same names as `y`.
119119
Actual: "a", "b"
120120
Expected: "a"
121121
Needs: "b"
@@ -126,7 +126,7 @@
126126
expect_mapequal(y, x)
127127
Condition
128128
Error:
129-
! Expected names of `y` to have the same values as names of `x`.
129+
! Expected `y` to have the same names as `x`.
130130
Actual: "a"
131131
Expected: "a", "b"
132132
Absent: "b"
@@ -137,7 +137,7 @@
137137
expect_mapequal(x, y)
138138
Condition
139139
Error:
140-
! Expected `x` to be equal to `y`.
140+
! Expected `x` to contain same values as `y`.
141141
Differences:
142142
`actual$b`: 2.0
143143
`expected$b`: 3.0

tests/testthat/_snaps/try-again.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
i Expectation failed; trying again (1)...
1515
Condition
1616
Error:
17-
! Expected `i` to be equal to 0.
17+
! Expected `i` to equal 0.
1818
Differences:
1919
`actual`: 1.0
2020
`expected`: 0.0

0 commit comments

Comments
 (0)