Skip to content

Commit 8a06d57

Browse files
committed
Use waldo::compare(list_as_map) in expect_mapequal()
This weakens the expectation so it shouldn't cause any existing tests to fail. Fixes #1521
1 parent 13e102e commit 8a06d57

File tree

5 files changed

+22
-59
lines changed

5 files changed

+22
-59
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+
* Power `expect_mapequal()` with `waldo::compare(list_as_map = TRUE)` (#1521).
34
* `expect_snapshot_value()` can now handle expressions that generate `-` (#1678) or zero length atomic vectors (#2042).
45
* `expect_matches()` failures should be a little easier to read (#2135).
56
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).

R/expect-equality.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ expect_waldo_equal_ <- function(
123123
type,
124124
act,
125125
exp,
126-
info,
126+
info = NULL,
127127
...,
128128
trace_env = caller_env()
129129
) {

R/expect-setequal.R

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
#' (i.e. `y` is a subset of `x`).
77
#' * `expect_in(x, y)` tests every element of `x` is in `y`
88
#' (i.e. `x` is a subset of `y`).
9-
#' * `expect_mapequal(x, y)` tests that `x` and `y` have the same names, and
10-
#' that `x[names(y)]` equals `y`.
9+
#' * `expect_mapequal(x, y)` treats lists as if they are mappings between names
10+
#' and values. Concretely, this drops `NULL`s in both objects and sorts
11+
#' named components.
1112
#'
1213
#' Note that `expect_setequal()` ignores names, and you will be warned if both
1314
#' `object` and `expected` have them.
@@ -79,39 +80,7 @@ expect_mapequal <- function(object, expected) {
7980
act <- quasi_label(enquo(object), arg = "object")
8081
exp <- quasi_label(enquo(expected), arg = "expected")
8182

82-
if (!is_vector(act$val) || !is_vector(exp$val)) {
83-
abort("`object` and `expected` must both be vectors")
84-
}
85-
86-
# Length-0 vectors are OK whether named or unnamed.
87-
if (length(act$val) == 0 && length(exp$val) == 0) {
88-
warn("`object` and `expected` are empty lists")
89-
return(pass(act$val))
90-
}
91-
92-
act_nms <- names(act$val)
93-
exp_nms <- names(exp$val)
94-
95-
check_names_ok(act_nms, "object")
96-
check_names_ok(exp_nms, "expected")
97-
98-
if (setequal(act_nms, exp_nms)) {
99-
return(expect_equal(act$val[exp_nms], exp$val))
100-
}
101-
102-
act_miss <- setdiff(exp_nms, act_nms)
103-
if (length(act_miss) > 0) {
104-
vals <- paste0(encodeString(act_miss, quote = '"'), ", ")
105-
return(fail(paste0("Names absent from `object`: ", vals)))
106-
}
107-
108-
exp_miss <- setdiff(act_nms, exp_nms)
109-
if (length(exp_miss) > 0) {
110-
vals <- paste0(encodeString(exp_miss, quote = '"'), ", ")
111-
return(fail(paste0("Names absent from `expected`: ", vals)))
112-
}
113-
114-
pass(act$val)
83+
expect_waldo_equal_("equal", act, exp, list_as_map = TRUE)
11584
}
11685

11786
check_names_ok <- function(x, label) {

man/expect_setequal.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-expect-setequal.R

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,17 @@ test_that("ignores order", {
5050
expect_success(expect_mapequal(list(a = 1, b = 2), list(b = 2, a = 1)))
5151
})
5252

53+
test_that("ignores order recursively", {
54+
local_edition(3)
55+
x <- list(outer_1 = 1, outer_2 = list(inner_1 = 1, inner_2 = 2))
56+
y <- list(outer_2 = list(inner_2 = 2, inner_1 = 1), outer_1 = 1)
57+
expect_success(expect_mapequal(x, y))
58+
})
59+
5360
test_that("error if any names are duplicated", {
54-
expect_error(expect_mapequal(list(a = 1, b = 2, b = 3), list(b = 2, a = 1)))
55-
expect_error(expect_mapequal(list(a = 1, b = 2), list(b = 3, b = 2, a = 1)))
56-
expect_error(expect_mapequal(
61+
expect_failure(expect_mapequal(list(a = 1, b = 2, b = 3), list(b = 2, a = 1)))
62+
expect_failure(expect_mapequal(list(a = 1, b = 2), list(b = 3, b = 2, a = 1)))
63+
expect_failure(expect_mapequal(
5764
list(a = 1, b = 2, b = 3),
5865
list(b = 3, b = 2, a = 1)
5966
))
@@ -72,24 +79,9 @@ test_that("fails if values don't match", {
7279
expect_failure(expect_mapequal(list(a = 1, b = 2), list(a = 1, b = 3)))
7380
})
7481

75-
test_that("error for non-vectors", {
76-
expect_error(expect_mapequal(sum, sum), "be vectors")
77-
expect_error(expect_mapequal(NULL, NULL), "be vectors")
78-
})
79-
80-
test_that("error if any unnamed values", {
81-
expect_error(expect_mapequal(list(1, b = 2), list(1, b = 2)))
82-
expect_error(expect_mapequal(list(1, b = 2), list(b = 2, 1)))
83-
})
84-
85-
test_that("succeeds if comparing empty named and unnamed vectors", {
86-
x1 <- list()
87-
x2 <- setNames(list(), character())
88-
89-
expect_warning(expect_success(expect_mapequal(x1, x1)))
90-
expect_warning(expect_success(expect_mapequal(x1, x2)))
91-
expect_warning(expect_success(expect_mapequal(x2, x1)))
92-
expect_warning(expect_success(expect_mapequal(x2, x2)))
82+
test_that("fails if unnamed values in different location if any unnamed values", {
83+
expect_success(expect_mapequal(list(1, b = 2, c = 3), list(1, c = 3, b = 2)))
84+
expect_failure(expect_mapequal(list(1, b = 2, c = 3), list(b = 2, 1, c = 3)))
9385
})
9486

9587
# contains ----------------------------------------------------------------

0 commit comments

Comments
 (0)