Skip to content

Commit edee55a

Browse files
authored
Improve expect_setequal() message and implementation (#2015)
Fixes #1962
1 parent 477f43c commit edee55a

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `expect_setequal()` correctly identifies what is missing where (#1962).
34
* `expect_true()` and `expect_false()` give better errors if `actual` isn't a vector (#1996).
45
* `expect_no_*()` expectations no longer incorrectly emit a passing test result if they in fact fail (#1997).
56
* Require the latest version of waldo (0.6.0) in order to get the latest goodies (#1955).

R/expect-setequal.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ expect_setequal <- function(object, expected) {
3535
warn("expect_setequal() ignores names")
3636
}
3737

38-
act_miss <- !act$val %in% exp$val
39-
exp_miss <- !exp$val %in% act$val
38+
act_miss <- setdiff(act$val, exp$val)
39+
exp_miss <- setdiff(exp$val, act$val)
4040

41-
if (any(exp_miss) || any(act_miss)) {
41+
if (length(exp_miss) || length(act_miss)) {
4242
fail(paste0(
4343
act$lab, " (`actual`) and ", exp$lab, " (`expected`) don't have the same values.\n",
44-
if (any(act_miss))
45-
paste0("* Only in `expected`: ", values(act$val[act_miss]), "\n"),
46-
if (any(exp_miss))
47-
paste0("* Only in `actual`: ", values(exp$val[exp_miss]), "\n")
44+
if (length(act_miss))
45+
paste0("* Only in `actual`: ", values(act_miss), "\n"),
46+
if (length(exp_miss))
47+
paste0("* Only in `expected`: ", values(exp_miss), "\n")
4848
))
4949
} else {
5050
succeed()

tests/testthat/_snaps/expect-setequal.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
# useful message on failure
22

3+
"actual" (`actual`) and "expected" (`expected`) don't have the same values.
4+
* Only in `actual`: "actual"
5+
* Only in `expected`: "expected"
6+
7+
8+
---
9+
310
1:2 (`actual`) and 2 (`expected`) don't have the same values.
4-
* Only in `expected`: 1
11+
* Only in `actual`: 1
512

613

714
---
815

916
2 (`actual`) and 2:3 (`expected`) don't have the same values.
10-
* Only in `actual`: 3
17+
* Only in `expected`: 3
1118

1219

1320
---
1421

1522
1:2 (`actual`) and 2:3 (`expected`) don't have the same values.
16-
* Only in `expected`: 1
17-
* Only in `actual`: 3
23+
* Only in `actual`: 1
24+
* Only in `expected`: 3
1825

1926

2027
# truncates long vectors
2128

2229
1:2 (`actual`) and 1:50 (`expected`) don't have the same values.
23-
* Only in `actual`: 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
30+
* Only in `expected`: 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
2431

2532

2633
# expect_contains() gives useful message on failure

tests/testthat/test-expect-setequal.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ test_that("error for non-vectors", {
2424
})
2525

2626
test_that("useful message on failure", {
27+
expect_snapshot_failure(expect_setequal("actual", "expected"))
28+
2729
expect_snapshot_failure(expect_setequal(1:2, 2))
2830
expect_snapshot_failure(expect_setequal(2, 2:3))
2931
expect_snapshot_failure(expect_setequal(1:2, 2:3))
@@ -112,4 +114,3 @@ test_that("expect_in() gives useful message on failure", {
112114
expect_snapshot_failure(expect_in(x1, x2))
113115
expect_snapshot_failure(expect_in(x1, x3))
114116
})
115-

0 commit comments

Comments
 (0)