Skip to content

Commit 608decc

Browse files
authored
Document expect_length() and expect_shape() together (#2134)
* Document `expect_length()` and `expect_shape()` together * Update news bullet
1 parent 8d08da2 commit 608decc

21 files changed

+89
-138
lines changed

β€ŽNEWS.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* Fixed an issue preventing compilation from succeeding due to deprecation / removal of `std::uncaught_exception()` (@kevinushey, #2047).
4444
* New `skip_unless_r()` to skip running tests on unsuitable versions of R, e.g. `skip_unless_r(">= 4.1.0")` to skip tests that require, say, `...names` (@MichaelChirico, #2022)
4545
* `skip_on_os()` gains an option `"emscripten"` of the `os` argument to skip tests on Emscripten (@eitsupi, #2103).
46-
* New expectation, `expect_shape()`, for testing the shape (i.e., the `length()`, `nrow()`, `ncol()`, or `dim()`), all in one place (#1423, @michaelchirico).
46+
* New expectation, `expect_shape()`, for testing the shape (i.e., the `nrow()`, `ncol()`, or `dim()`), all in one place (#1423, @michaelchirico).
4747

4848
# testthat 3.2.3
4949

β€Ž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-
#' Do you expect an object with this shape?
1+
#' Do you expect an object with this 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+
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
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
@@ -33,7 +33,6 @@ reference:
3333
- expect_named
3434
- expect_null
3535
- expect_setequal
36-
- expect_shape
3736
- expect_type
3837
- expect_vector
3938

β€Ž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.

0 commit comments

Comments
Β (0)