Skip to content

Commit 0746701

Browse files
committed
add special case handling in expect_named when ignore.order = TRUE
1 parent 3c45e5c commit 0746701

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

R/expect-named.R

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,42 @@ expect_named <- function(
4141
sprintf("%s does not have names.", act$lab)
4242
)
4343
} else {
44-
exp_names <- normalise_names(expected, ignore.order, ignore.case)
44+
exp <- quasi_label(enquo(expected), arg = "expected")
45+
exp$val <- normalise_names(exp$val, ignore.order, ignore.case)
4546
act$names <- normalise_names(act$names, ignore.order, ignore.case)
4647

47-
expect(
48-
identical(act$names, exp_names),
49-
sprintf(
50-
"Names of %s (%s) don't match %s",
51-
act$lab,
52-
paste0("'", act$names, "'", collapse = ", "),
53-
paste0("'", exp_names, "'", collapse = ", ")
54-
),
55-
info = info
56-
)
48+
if (ignore.order) {
49+
act_miss <- unique(act$names[!act$names %in% exp$val])
50+
exp_miss <- unique(exp$val[!exp$val %in% act$names])
51+
52+
expect(
53+
length(exp_miss) == 0 && length(act_miss) == 0,
54+
paste0(
55+
"Names of ",
56+
act$lab,
57+
" (`actual`) and ",
58+
exp$lab,
59+
" (`expected`) don't have the same values.\n",
60+
if (length(act_miss)) {
61+
paste0("* Only in `actual`: ", values(act_miss), "\n")
62+
},
63+
if (length(exp_miss)) {
64+
paste0("* Only in `expected`: ", values(exp_miss), "\n")
65+
}
66+
)
67+
)
68+
} else {
69+
expect(
70+
identical(act$names, exp$val),
71+
sprintf(
72+
"Names of %s (%s) don't match %s",
73+
act$lab,
74+
paste0("'", act$names, "'", collapse = ", "),
75+
paste0("'", exp$val, "'", collapse = ", ")
76+
),
77+
info = info
78+
)
79+
}
5780
}
5881
invisible(act$val)
5982
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# provide useful feedback on failure
2+
3+
Names of c(a = 1) (`actual`) and c("a", "b") (`expected`) don't have the same values.
4+
* Only in `expected`: "b"
5+
6+
7+
---
8+
9+
Names of c(a = 1, b = 1) (`actual`) and c("a") (`expected`) don't have the same values.
10+
* Only in `actual`: "b"
11+
12+
13+
---
14+
15+
Names of c(a = 1) (`actual`) and c("b") (`expected`) don't have the same values.
16+
* Only in `actual`: "a"
17+
* Only in `expected`: "b"
18+
19+

tests/testthat/test-expect-named.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ test_that("expected_named optionally ignores order", {
1919
ignore.order = TRUE
2020
))
2121
})
22+
23+
test_that("provide useful feedback on failure", {
24+
expect_snapshot_error(
25+
expect_named(c(a = 1), c("a", "b"), ignore.order = TRUE)
26+
)
27+
expect_snapshot_error(
28+
expect_named(c(a = 1, b = 1), c("a"), ignore.order = TRUE)
29+
)
30+
expect_snapshot_error(
31+
expect_named(c(a = 1), c("b"), ignore.order = TRUE)
32+
)
33+
})

0 commit comments

Comments
 (0)