Skip to content

Commit b29cd06

Browse files
committed
add error_prefix to expect_setequal_() and expect_waldo_equal_()
1 parent 13cc3e2 commit b29cd06

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

R/expect-equality.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ expect_waldo_equal_ <- function(
126126
exp,
127127
info = NULL,
128128
...,
129-
trace_env = caller_env()
129+
trace_env = caller_env(),
130+
error_prefix = NULL
130131
) {
131132
comp <- waldo_compare(
132133
act$val,
@@ -145,6 +146,9 @@ expect_waldo_equal_ <- function(
145146
"`expected`",
146147
paste0(comp, collapse = "\n\n")
147148
)
149+
if (!is.null(error_prefix)) {
150+
msg <- paste0(error_prefix, msg)
151+
}
148152
return(fail(msg, info = info, trace_env = trace_env))
149153
}
150154
pass(act$val)

R/expect-named.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ expect_named <- function(
4949

5050
if (ignore.order) {
5151
act <- labelled_value(act_names, act$lab)
52-
return(expect_setequal_(act, exp, prefix = "Names of "))
52+
return(expect_setequal_(act, exp, error_prefix = "Names of "))
5353
} else {
5454
act <- labelled_value(act_names, act$lab)
55-
return(expect_waldo_equal_("equal", act, exp))
55+
return(expect_waldo_equal_("equal", act, exp, error_prefix = "Names of "))
5656
}
5757

5858
pass(act$val)

R/expect-setequal.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ expect_setequal <- function(object, expected) {
3636
expect_setequal_(act, exp)
3737
}
3838

39-
expect_setequal_ <- function(act, exp, trace_env = caller_env()) {
39+
expect_setequal_ <- function(
40+
act,
41+
exp,
42+
trace_env = caller_env(),
43+
error_prefix = NULL
44+
) {
4045
act_miss <- unique(act$val[!act$val %in% exp$val])
4146
exp_miss <- unique(exp$val[!exp$val %in% act$val])
4247

4348
if (length(exp_miss) || length(act_miss)) {
4449
msg <- paste0(
50+
if (!is.null(error_prefix)) {
51+
error_prefix
52+
},
4553
act$lab,
4654
" (`actual`) and ",
4755
exp$lab,

tests/testthat/_snaps/expect-named.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
# provide useful feedback on failure
22

3-
c(a = 1) (`actual`) and c("a", "b") (`expected`) don't have the same values.
3+
Names of c(a = 1) (`actual`) and c("a", "b") (`expected`) don't have the same values.
44
* Only in `expected`: "b"
55

66

77
---
88

9-
c(a = 1, b = 1) (`actual`) and c("a") (`expected`) don't have the same values.
9+
Names of c(a = 1, b = 1) (`actual`) and c("a") (`expected`) don't have the same values.
1010
* Only in `actual`: "b"
1111

1212

1313
---
1414

15-
c(a = 1) (`actual`) and c("b") (`expected`) don't have the same values.
15+
Names of c(a = 1) (`actual`) and c("b") (`expected`) don't have the same values.
1616
* Only in `actual`: "a"
1717
* Only in `expected`: "b"
1818

1919

20+
---
21+
22+
Names of c(a = 1) (`actual`) is not equal to c("a", "b") (`expected`).
23+
24+
`actual`: "a"
25+
`expected`: "a" "b"
26+
27+
---
28+
29+
Names of c(a = 1, b = 1) (`actual`) is not equal to c("a") (`expected`).
30+
31+
`actual`: "a" "b"
32+
`expected`: "a"
33+
34+
---
35+
36+
Names of c(a = 1) (`actual`) is not equal to c("b") (`expected`).
37+
38+
`actual`: "a"
39+
`expected`: "b"
40+
2041
# expect_named validates its inputs
2142

2243
Code

tests/testthat/test-expect-named.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ test_that("provide useful feedback on failure", {
3030
expect_snapshot_error(
3131
expect_named(c(a = 1), c("b"), ignore.order = TRUE)
3232
)
33+
34+
expect_snapshot_error(
35+
expect_named(c(a = 1), c("a", "b"), ignore.order = FALSE)
36+
)
37+
expect_snapshot_error(
38+
expect_named(c(a = 1, b = 1), c("a"), ignore.order = FALSE)
39+
)
40+
expect_snapshot_error(
41+
expect_named(c(a = 1), c("b"), ignore.order = FALSE)
42+
)
3343
})
3444

3545
test_that("expect_named validates its inputs", {

0 commit comments

Comments
 (0)