-
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 30 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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| #' Does code return a vector with the specified length? | ||
| #' | ||
| #' @seealso [expect_vector()] to make assertions about the "size" of a vector | ||
| #' @seealso [expect_vector()] to make assertions about the "size" of a vector, | ||
| #' [expect_shape()] for more general assertions about object "shape". | ||
| #' @inheritParams expect_that | ||
| #' @param n Expected length. | ||
| #' @family expectations | ||
|
|
@@ -16,6 +17,10 @@ expect_length <- function(object, n) { | |
| stopifnot(is.numeric(n), length(n) == 1) | ||
|
|
||
| act <- quasi_label(enquo(object), arg = "object") | ||
| expect_length_impl_(act, n) | ||
| } | ||
|
|
||
| expect_length_impl_ <- function(act, n) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a case where you need to pass |
||
| act$n <- length(act$val) | ||
|
|
||
| expect( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #' Does code return an object with the specified shape? | ||
| #' | ||
| #' This is a generalization of [expect_length()] to test the "shape" of | ||
| #' more general objects like data.frames, matrices, and arrays. | ||
| #' | ||
| #' @seealso [expect_length()] to specifically make assertions about the | ||
| #' [length()] of a vector. | ||
| #' @inheritParams expect_that | ||
| #' @param ... Ignored. | ||
| #' @param length Expected [length()] of `object`. | ||
| #' @param nrow Expected [nrow()] of `object`. | ||
| #' @param ncol Expected [ncol()] of `object`. | ||
| #' @param dim Expected [dim()] of `object`. | ||
| #' @family expectations | ||
| #' @export | ||
| #' @examples | ||
| expect_shape = function(object, ..., length, nrow, ncol, dim) { | ||
| rlang::check_exclusive(length, nrow, ncol, dim) | ||
|
|
||
| act <- quasi_label(enquo(object), arg = "object") | ||
|
|
||
| # Re-use expect_length() to ensure they stay in sync. | ||
| if (!missing(length)) { | ||
| return(expect_length_impl_(act, length)) | ||
| } | ||
|
|
||
| # need base:: qualification or we might trigger an error for missing(length) | ||
| length <- base::length | ||
|
|
||
| dim_object <- base::dim(object) | ||
|
|
||
| if (is.null(dim_object)) { | ||
| fail(sprintf("%s has no dimensions.", act$lab)) | ||
| } | ||
|
|
||
| if (!missing(nrow)) { | ||
MichaelChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| act$nrow <- dim_object[1L] | ||
|
|
||
| expect( | ||
| identical(as.integer(act$nrow), as.integer(nrow)), | ||
| sprintf("%s has %i rows, not %i.", act$lab, act$nrow, nrow) | ||
| ) | ||
| } else if (!missing(ncol)) { | ||
| if (length(dim_object) == 1L) { | ||
| fail(sprintf("%s has only one dimension.", act$lab)) | ||
| } | ||
|
|
||
| act$ncol <- dim_object[2L] | ||
|
|
||
| expect( | ||
| identical(as.integer(act$ncol), as.integer(ncol)), | ||
| sprintf("%s has %i columns, not %i.", act$lab, act$ncol, ncol) | ||
| ) | ||
| } else { # !missing(dim) | ||
| act$dim <- dim_object | ||
|
|
||
| if (length(act$dim) != length(dim)) { | ||
| fail(sprintf("%s has %i dimensions, not %i", act$lab, length(act$dim), length(dim))) | ||
| } | ||
|
|
||
| expect( | ||
| identical(as.integer(act$dim), as.integer(dim)), | ||
| sprintf("%s has dim (%s), not (%s).", act$lab, toString(act$dim), toString(dim)) | ||
| ) | ||
| } | ||
|
|
||
| invisible(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,77 @@ | ||
| # length compared correctly | ||
|
|
||
| 1 has length 1, not length 2. | ||
|
|
||
| # dim compared correctly | ||
|
|
||
| matrix(nrow = 6, ncol = 3) has dim (6, 3), not (6, 2). | ||
|
|
||
| --- | ||
|
|
||
| matrix(nrow = 6, ncol = 3) has dim (6, 3), not (7, 3). | ||
|
|
||
| --- | ||
|
|
||
| array(dim = 1:3) has 3 dimensions, not 2 | ||
|
|
||
| --- | ||
|
|
||
| array(dim = 1:3) has 3 dimensions, not 4 | ||
|
|
||
| # nrow compared correctly | ||
|
|
||
| matrix(nrow = 5, ncol = 5) has 5 rows, not 6. | ||
|
|
||
| --- | ||
|
|
||
| 1 has no dimensions. | ||
|
|
||
| # ncol compared correctly | ||
|
|
||
| matrix(nrow = 5, ncol = 5) has 5 columns, not 7. | ||
|
|
||
| --- | ||
|
|
||
| array(1) has only one dimension. | ||
|
|
||
| --- | ||
|
|
||
| array(integer()) has only one dimension. | ||
|
|
||
| # NA handling (e.g. dbplyr) | ||
|
|
||
| `x` has NA rows, not 10. | ||
|
|
||
| --- | ||
|
|
||
| `x` has 10 columns, not NA. | ||
|
|
||
| --- | ||
|
|
||
| `x` has dim (NA, 10), not (10, NA). | ||
|
|
||
| # at least one argument is required | ||
|
|
||
| Code | ||
| expect_shape(1:10) | ||
| Condition | ||
| Error in `expect_shape()`: | ||
| ! One of `length`, `nrow`, `ncol`, or `dim` must be supplied. | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| expect_shape(1:10, 2) | ||
| Condition | ||
| Error in `expect_shape()`: | ||
| ! One of `length`, `nrow`, `ncol`, or `dim` must be supplied. | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| expect_shape(1:10, nrow = 1L, ncol = 2L) | ||
| Condition | ||
| Error in `expect_shape()`: | ||
| ! Exactly one of `length`, `nrow`, `ncol`, or `dim` must be supplied. | ||
| x `nrow` and `ncol` were supplied together. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| test_that("length compared correctly", { | ||
| expect_success(expect_shape(1, length = 1)) | ||
| expect_snapshot_failure(expect_shape(1, length = 2)) | ||
| expect_success(expect_shape(1:10, length = 10)) | ||
| expect_success(expect_shape(letters[1:5], length = 5)) | ||
| expect_success(expect_shape(integer(), length = 0)) | ||
|
|
||
| x <- list(1:10, letters) | ||
| out <- expect_shape(x, length = 2) | ||
| expect_identical(out, x) | ||
| }) | ||
|
|
||
| test_that("dim compared correctly", { | ||
| expect_success(expect_shape(matrix(nrow = 5, ncol = 4), dim = c(5L, 4L))) | ||
| expect_snapshot_failure(expect_shape(matrix(nrow = 6, ncol = 3), dim = c(6L, 2L))) | ||
| expect_snapshot_failure(expect_shape(matrix(nrow = 6, ncol = 3), dim = c(7L, 3L))) | ||
| expect_success(expect_shape(data.frame(1:10, 11:20), dim = c(10, 2))) | ||
| expect_success(expect_shape(array(dim = 1:3), dim = 1:3)) | ||
| expect_snapshot_failure(expect_shape(array(dim = 1:3), dim = 1:2)) | ||
| expect_snapshot_failure(expect_shape(array(dim = 1:3), dim = 1:4)) | ||
| expect_success(expect_shape(array(integer()), dim = 0L)) | ||
| dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L) | ||
| expect_success(expect_shape(array(dim = dd), dim = dd)) | ||
|
|
||
| x <- cbind(1:2, 3:4) | ||
| out <- expect_shape(x, dim = c(2L, 2L)) | ||
| expect_identical(out, x) | ||
| }) | ||
|
|
||
| test_that("nrow compared correctly", { | ||
| expect_success(expect_shape(matrix(nrow = 5, ncol = 4), nrow = 5L)) | ||
| expect_snapshot_failure(expect_shape(matrix(nrow = 5, ncol = 5), nrow = 6L)) | ||
| expect_success(expect_shape(data.frame(1:10, 11:20), nrow = 10L)) | ||
| expect_snapshot_failure(expect_shape(1, nrow = 1)) | ||
| expect_success(expect_shape(array(integer()), nrow = 0L)) | ||
| dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L) | ||
| expect_success(expect_shape(array(dim = dd), nrow = 0L)) | ||
|
|
||
| x <- cbind(1:2, 3:4) | ||
| out <- expect_shape(x, dim = c(2L, 2L)) | ||
| expect_identical(out, x) | ||
| }) | ||
|
|
||
| test_that("ncol compared correctly", { | ||
| expect_success(expect_shape(matrix(nrow = 5, ncol = 4), ncol = 4L)) | ||
| expect_snapshot_failure(expect_shape(matrix(nrow = 5, ncol = 5), ncol = 7L)) | ||
| expect_success(expect_shape(data.frame(1:10, 11:20), ncol = 2L)) | ||
| expect_snapshot_failure(expect_shape(array(1), ncol = 1)) | ||
| expect_snapshot_failure(expect_shape(array(integer()), ncol = 0L)) | ||
| dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L) | ||
| expect_success(expect_shape(array(dim = dd), ncol = 0L)) | ||
|
|
||
| x <- cbind(1:2, 3:4) | ||
| out <- expect_shape(x, dim = c(2L, 2L)) | ||
| expect_identical(out, x) | ||
| }) | ||
|
|
||
| test_that("uses S3 dim method", { | ||
| dim.testthat_expect_shape <- function(x) 1:2 | ||
| x <- integer() | ||
| class(x) <- "testthat_expect_shape" | ||
| registerS3method("dim", "testthat_expect_shape", dim.testthat_expect_shape) | ||
|
|
||
| expect_success(expect_shape(x, dim = 1:2)) | ||
| }) | ||
|
|
||
| test_that("NA handling (e.g. dbplyr)", { | ||
| dim.testthat_expect_shape_missing <- function(x) c(NA_integer_, 10L) | ||
| x <- integer() | ||
| class(x) <- "testthat_expect_shape_missing" | ||
| registerS3method("dim", "testthat_expect_shape_missing", dim.testthat_expect_shape_missing) | ||
|
|
||
| expect_success(expect_shape(x, nrow = NA_integer_)) | ||
| expect_success(expect_shape(x, ncol = 10L)) | ||
| expect_success(expect_shape(x, dim = c(NA_integer_, 10L))) | ||
|
|
||
| expect_snapshot_failure(expect_shape(x, nrow = 10L)) | ||
| expect_snapshot_failure(expect_shape(x, ncol = NA_integer_)) | ||
| expect_snapshot_failure(expect_shape(x, dim = c(10L, NA_integer_))) | ||
| }) | ||
|
|
||
| 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), dim = 8:10)) | ||
| }) | ||
|
|
||
| test_that("at least one argument is required", { | ||
| err_msg <- "Exactly one of `length`, `nrow`, `ncol`, or `dim` must be provided." | ||
| expect_snapshot(expect_shape(1:10), error = TRUE) # no args | ||
| expect_snapshot(expect_shape(1:10, 2), error = TRUE) # no named args | ||
| expect_snapshot(expect_shape(1:10, nrow = 1L, ncol = 2L), error = TRUE) # multiple named args | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.