expect_setequal(x, y)
tests that every element ofx
occurs iny
, and that every element ofy
occurs inx
.expect_contains(x, y)
tests thatx
contains every element ofy
(i.e.y
is a subset ofx
).expect_in(x, y)
tests every element ofx
is iny
(i.e.x
is a subset ofy
).expect_mapequal(x, y)
treats lists as if they are mappings between names and values. Concretely, this dropsNULL
s in both objects and sorts named components.
Usage
expect_setequal(object, expected)
expect_mapequal(object, expected)
expect_contains(object, expected)
expect_in(object, expected)
Arguments
- object, expected
Computation and value to compare it to.
Both arguments supports limited unquoting to make it easier to generate readable failures within a function or for loop. See quasi_label for more details.
Details
Note that expect_setequal()
ignores names, and you will be warned if both
object
and expected
have them.
Examples
expect_setequal(letters, rev(letters))
show_failure(expect_setequal(letters[-1], rev(letters)))
#> Failed expectation:
#> letters[-1] (`actual`) and rev(letters) (`expected`) don't have the same values.
#> * Only in `expected`: "a"
#>
x <- list(b = 2, a = 1)
expect_mapequal(x, list(a = 1, b = 2))
show_failure(expect_mapequal(x, list(a = 1)))
#> Failed expectation:
#> `x` (`actual`) is not equal to list(a = 1) (`expected`).
#>
#> `actual` is length 2
#> `expected` is length 1
#>
#> `names(actual)`: "a" "b"
#> `names(expected)`: "a"
#>
#> `actual$b` is a double vector (2)
#> `expected$b` is absent
show_failure(expect_mapequal(x, list(a = 1, b = "x")))
#> Failed expectation:
#> `x` (`actual`) is not equal to list(a = 1, b = "x") (`expected`).
#>
#> `actual$b` is a double vector (2)
#> `expected$b` is a character vector ('x')
show_failure(expect_mapequal(x, list(a = 1, b = 2, c = 3)))
#> Failed expectation:
#> `x` (`actual`) is not equal to list(a = 1, b = 2, c = 3) (`expected`).
#>
#> `actual` is length 2
#> `expected` is length 3
#>
#> `names(actual)`: "a" "b"
#> `names(expected)`: "a" "b" "c"
#>
#> `actual$c` is absent
#> `expected$c` is a double vector (3)