expect_length()
inspects the length()
of an object; expect_shape()
inspects the "shape" (i.e. nrow()
, ncol()
, or dim()
) of
higher-dimensional objects like data.frames, matrices, and arrays.
Arguments
- object
Object to test.
Supports limited unquoting to make it easier to generate readable failures within a function or for loop. See quasi_label for more details.
- n
Expected length.
- ...
Not used; used to force naming of other arguments.
- nrow, ncol
- dim
Expected
dim()
ofobject
.
See also
expect_vector()
to make assertions about the "size" of a vector.
Other expectations:
comparison-expectations
,
equality-expectations
,
expect_error()
,
expect_match()
,
expect_named()
,
expect_null()
,
expect_output()
,
expect_reference()
,
expect_silent()
,
inheritance-expectations
,
logical-expectations
Examples
expect_length(1, 1)
expect_length(1:10, 10)
show_failure(expect_length(1:10, 1))
#> Failed expectation:
#> Expected `1:10` to have length 1.
#> Actual length: 10.
x <- matrix(1:9, nrow = 3)
expect_shape(x, nrow = 3)
show_failure(expect_shape(x, nrow = 4))
#> Failed expectation:
#> Expected `x` to have 4 rows.
#> Actual rows: 3.
expect_shape(x, ncol = 3)
show_failure(expect_shape(x, ncol = 4))
#> Failed expectation:
#> Expected `x` to have 4 columns.
#> Actual columns: 3.
expect_shape(x, dim = c(3, 3))
show_failure(expect_shape(x, dim = c(3, 4, 5)))
#> Failed expectation:
#> Expected `x` to have dim (3, 4, 5).
#> Actual dim: (3, 3).