-
Notifications
You must be signed in to change notification settings - Fork 340
Expect shape #1469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expect shape #1469
Changes from 11 commits
66b166a
d4853ff
69c410a
e89fff9
cdb15a2
268aee8
89c0c82
b74737f
769ccc1
9aede6f
dd23081
eb0dc5a
1a020dc
ffde38b
0661094
ac0acbc
150425b
53b0ee8
a72708b
da7fdf2
d873766
3710870
170543e
a88340d
e35122a
ff4d7c5
4bc32fc
1bdf1cd
3ed14c4
58654fb
ff4c894
2ca461b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #' Does code return an object with the specified shape? | ||
| #' | ||
| #' By "shape", we mean an object's [dim()], or, for one-dimensional objects, | ||
| #' it's [length()]. Thus this is an extension of [expect_length()] to more | ||
| #' general objects like [data.frame()], [matrix()], and [array()]. | ||
| #' To wit, first, the object's `dim()` is checked. If non-`NULL`, it is compared | ||
| #' to `shape` (or one/both of `nrow`, `ncol`, if they are supplied, in which | ||
| #' case they take precedence). If `dim(object)` is `NULL`, `length(object)` | ||
| #' is compared to `shape`. | ||
| #' | ||
| #' @seealso [expect_length()] to specifically make assertions about the | ||
| #' [length()] of a vector. | ||
| #' @inheritParams expect_that | ||
| #' @param shape Expected shape, a numeric vector. | ||
| #' @param nrow Expected number of rows, numeric. | ||
| #' @param ncol Expected number of columns, numeric. | ||
| #' @family expectations | ||
| #' @export | ||
| #' @examples | ||
| expect_shape = function(object, shape, nrow, ncol) { | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| stopifnot( | ||
| missing(shape) || is.numeric(shape), | ||
| missing(nrow) || is.numeric(nrow), | ||
| missing(ncol) || is.numeric(ncol) | ||
| ) | ||
|
|
||
| dim_object <- dim(object) | ||
| if (is.null(dim_object)) { | ||
| if (missing(shape)) { | ||
| stop("`shape` must be provided for one-dimensional inputs") | ||
| } | ||
| return(expect_length(object, shape)) | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| act <- quasi_label(enquo(object), arg = "object") | ||
|
|
||
| if (missing(nrow) && missing(ncol)) { | ||
| # testing dim | ||
| if (missing(shape)) { | ||
| stop("`shape` must be provided if `nrow` and `ncol` are not") | ||
| } | ||
| act$shape <- dim_object | ||
|
|
||
| expect( | ||
| isTRUE(all.equal(act$shape, shape)), | ||
| sprintf("%s has shape (%s), not (%s).", act$lab, toString(act$shape), toString(shape)) | ||
| ) | ||
| } else if (missing(nrow) && !missing(ncol)) { | ||
| # testing only ncol | ||
| act$ncol <- dim_object[2L] | ||
|
|
||
| expect( | ||
| act$ncol == ncol, | ||
| sprintf("%s has %i columns, not %i.", act$lab, act$ncol, ncol) | ||
| ) | ||
| } else if (!missing(nrow) && missing(ncol)) { | ||
| # testing only nrow | ||
| act$nrow <- dim_object[1L] | ||
|
|
||
| expect( | ||
| act$nrow == nrow, | ||
| sprintf("%s has %i rows, not %i.", act$lab, act$nrow, nrow) | ||
| ) | ||
| } else { # !missing(nrow) && !missing(ncol) | ||
| # testing both nrow & ncol (useful, e.g., for testing dim(.)[1:2] for arrays | ||
| act$nrow <- dim_object[1L] | ||
| act$ncol <- dim_object[2L] | ||
|
|
||
| expect( | ||
| act$nrow == nrow && act$ncol == ncol, | ||
| sprintf("%s has %i rows and %i columns, not %i rows and %i columns", act$lab, act$nrow, act$ncol, nrow, ncol) | ||
| ) | ||
| } | ||
|
|
||
| return(act$val) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| test_that("shape computed correctly", { | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # equivalent to expect_length | ||
| expect_success(expect_shape(1, 1)) | ||
| expect_failure(expect_shape(1, 2), "has length 1, not length 2.") | ||
| expect_success(expect_shape(1:10, 10)) | ||
| expect_success(expect_shape(letters[1:5], 5)) | ||
|
|
||
| # testing dim() | ||
| expect_success(expect_shape(matrix(nrow = 5, ncol = 4), c(5L, 4L))) | ||
| expect_failure(expect_shape(matrix(nrow = 6, ncol = 3), c(6L, 2L))) | ||
|
||
| expect_failure(expect_shape(matrix(nrow = 6, ncol = 3), c(7L, 3L))) | ||
| expect_success(expect_shape(data.frame(1:10, 11:20), c(10, 2))) | ||
| expect_success(expect_shape(array(dim = 1:3), 1:3)) | ||
|
|
||
| # testing nrow= | ||
| expect_success(expect_shape(matrix(nrow = 5, ncol = 4), nrow = 5L)) | ||
| expect_failure(expect_shape(matrix(nrow = 5, ncol = 5), nrow = 6L)) | ||
| expect_success(expect_shape(data.frame(1:10, 11:20), nrow = 10L)) | ||
|
|
||
| # testing ncol= | ||
| expect_success(expect_shape(matrix(nrow = 5, ncol = 4), ncol = 4L)) | ||
| expect_failure(expect_shape(matrix(nrow = 5, ncol = 5), ncol = 7L)) | ||
| expect_success(expect_shape(data.frame(1:10, 11:20), ncol = 2L)) | ||
|
|
||
| # testing nrow= and ncol= | ||
| expect_success(expect_shape(matrix(nrow = 5, ncol = 4), nrow = 5L, ncol = 4L)) | ||
| expect_failure(expect_shape(matrix(nrow = 5, ncol = 5), nrow = 6L, ncol = 5L)) | ||
| expect_success(expect_shape(data.frame(1:10, 11:20), nrow = 10L, ncol = 2L)) | ||
| expect_success(expect_shape(array(dim = 5:7), nrow = 5L, ncol = 6L)) | ||
|
|
||
| # precedence of manual nrow/ncol over shape | ||
| expect_success(expect_shape(matrix(nrow = 7, ncol = 10), 1:2, nrow = 7L)) | ||
| expect_success(expect_shape(matrix(nrow = 7, ncol = 10), 1:2, ncol = 10L)) | ||
| }) | ||
|
|
||
| test_that("uses S4 dim method", { | ||
| A <- setClass("ExpectShapeA", slots = c(x = "numeric", y = "numeric")) | ||
| setMethod("dim", "ExpectShapeA", function(x) 8:10) | ||
| expect_success(expect_shape(A(x = 1:9, y = 3), 8:10)) | ||
| }) | ||
|
|
||
| test_that("returns input", { | ||
| x <- list(1:10, letters) | ||
| out <- expect_shape(x, 2) | ||
| expect_identical(out, x) | ||
| }) | ||
|
|
||
| test_that("at least one argument is required", { | ||
| expect_error(expect_shape(1:10), "`shape` must be provided for one-dimensional inputs", fixed = TRUE) | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expect_error(expect_shape(cbind(1:2)), "`shape` must be provided if `nrow` and `ncol` are not") | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.