|
1 | | -#' Do you expect an object with this shape? |
| 1 | +#' Do you expect an object with this length or shape? |
2 | 2 | #' |
3 | | -#' This is a generalization of [expect_length()] to test the "shape" of |
4 | | -#' more general objects like data.frames, matrices, and arrays. |
| 3 | +#' `expect_length()` inspects the [length()] of an object; `expect_shape()` |
| 4 | +#' inspects the "shape" (i.e. [nrow()], [ncol()], or [dim()]) of |
| 5 | +#' higher-dimensional objects like data.frames, matrices, and arrays. |
5 | 6 | #' |
6 | | -#' @seealso [expect_length()] to specifically make assertions about the |
7 | | -#' [length()] of a vector. |
| 7 | +#' @seealso [expect_vector()] to make assertions about the "size" of a vector. |
8 | 8 | #' @inheritParams expect_that |
9 | | -#' @param ... Ignored. |
10 | | -#' @param nrow,ncol Expected [nrow()]/[ncol()] of `object`. |
11 | | -#' @param dim Expected [dim()] of `object`. |
| 9 | +#' @param n Expected length. |
12 | 10 | #' @family expectations |
13 | 11 | #' @export |
14 | 12 | #' @examples |
| 13 | +#' expect_length(1, 1) |
| 14 | +#' expect_length(1:10, 10) |
| 15 | +#' show_failure(expect_length(1:10, 1)) |
| 16 | +#' |
15 | 17 | #' x <- matrix(1:9, nrow = 3) |
16 | 18 | #' expect_shape(x, nrow = 3) |
| 19 | +#' show_failure(expect_shape(x, nrow = 4)) |
17 | 20 | #' expect_shape(x, ncol = 3) |
| 21 | +#' show_failure(expect_shape(x, ncol = 4)) |
18 | 22 | #' expect_shape(x, dim = c(3, 3)) |
| 23 | +#' show_failure(expect_shape(x, dim = c(3, 4, 5))) |
| 24 | +expect_length <- function(object, n) { |
| 25 | + check_number_whole(n, min = 0) |
| 26 | + |
| 27 | + act <- quasi_label(enquo(object)) |
| 28 | + act$n <- length(act$val) |
| 29 | + |
| 30 | + if (act$n != n) { |
| 31 | + msg <- sprintf("%s has length %i, not length %i.", act$lab, act$n, n) |
| 32 | + return(fail(msg)) |
| 33 | + } |
| 34 | + pass(act$val) |
| 35 | +} |
| 36 | + |
| 37 | +#' @param nrow,ncol Expected [nrow()]/[ncol()] of `object`. |
| 38 | +#' @param dim Expected [dim()] of `object`. |
| 39 | +#' @rdname expect_length |
| 40 | +#' @param ... Not used; used to force naming of other arguments. |
| 41 | +#' @export |
19 | 42 | expect_shape = function(object, ..., nrow, ncol, dim) { |
20 | 43 | check_dots_empty() |
21 | 44 | check_exclusive(nrow, ncol, dim) |
|
0 commit comments