Skip to content

Commit 63f39e4

Browse files
committed
Document expect_length() and expect_shape() together
1 parent e4ab555 commit 63f39e4

18 files changed

+73
-123
lines changed

β€ŽR/expect-length.Rβ€Ž

Lines changed: 0 additions & 27 deletions
This file was deleted.

β€ŽR/expect-shape.Rβ€Ž

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
1-
#' Does code return an object with the specified shape?
1+
#' Does code return a vector with the specified length or shape?
22
#'
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.
56
#'
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.
88
#' @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.
1210
#' @family expectations
1311
#' @export
1412
#' @examples
13+
#' expect_length(1, 1)
14+
#' expect_length(1:10, 10)
15+
#' show_failure(expect_length(1:10, 1))
16+
#'
1517
#' x <- matrix(1:9, nrow = 3)
1618
#' expect_shape(x, nrow = 3)
19+
#' show_failure(expect_shape(x, nrow = 4))
1720
#' expect_shape(x, ncol = 3)
21+
#' show_failure(expect_shape(x, ncol = 4))
1822
#' 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+
stopifnot(is.numeric(n), length(n) == 1)
26+
27+
act <- quasi_label(enquo(object), arg = "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, trace_env = parent.frame()))
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
1942
expect_shape = function(object, ..., nrow, ncol, dim) {
2043
check_dots_empty()
2144
check_exclusive(nrow, ncol, dim)

β€Ž_pkgdown.ymlβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ reference:
2626
- subtitle: Vectors
2727
contents:
2828
- expect_length
29-
- expect_shape
3029
- expect_gt
3130
- expect_match
3231
- expect_named

β€Žman/comparison-expectations.Rdβ€Ž

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žman/equality-expectations.Rdβ€Ž

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žman/expect_error.Rdβ€Ž

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žman/expect_length.Rdβ€Ž

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

β€Žman/expect_match.Rdβ€Ž

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žman/expect_named.Rdβ€Ž

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žman/expect_null.Rdβ€Ž

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)