- 
                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 all 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,11 +17,16 @@ 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( | ||
| act$n == n, | ||
| sprintf("%s has length %i, not length %i.", act$lab, act$n, n) | ||
| sprintf("%s has length %i, not length %i.", act$lab, act$n, n), | ||
| trace_env = parent.frame() | ||
| ) | ||
| 
     | 
||
| invisible(act$val) | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #' 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,nrow Expected [nrow()]/[ncol()] of `object`. | ||
| #' @param dim Expected [dim()] of `object`. | ||
| #' @family expectations | ||
| #' @export | ||
| #' @examples | ||
| #' x <- matrix(1:9, nrow = 3) | ||
| #' expect_shape(x, length = 9) | ||
| #' expect_shape(x, nrow = 3) | ||
| #' expect_shape(x, ncol = 3) | ||
| #' expect_shape(x, dim = c(3, 3)) | ||
| expect_shape = function(object, ..., length, nrow, ncol, dim) { | ||
| check_dots_empty() | ||
| 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)) | ||
| } | ||
| # now that we've handled the length argument, revert to usual base function | ||
| 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
         | 
||
| check_number_whole(nrow, allow_na = TRUE) | ||
| 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)) { | ||
| check_number_whole(ncol, allow_na = TRUE) | ||
| 
     | 
||
| 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) | ||
| if (!is.numeric(dim) && !is.integer(dim)) { | ||
| stop_input_type(dim, "a numeric vector") | ||
| } | ||
| 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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,89 @@ | ||
| # 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). | ||
| 
     | 
||
| # checks inputs arguments, | ||
| 
     | 
||
| 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, 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. | ||
| Code | ||
| expect_shape(1:10, 2) | ||
| Condition | ||
| Error in `expect_shape()`: | ||
| ! `...` must be empty. | ||
| x Problematic argument: | ||
| * ..1 = 2 | ||
| i Did you forget to name an argument? | ||
| Code | ||
| expect_shape(array(1), nrow = "x") | ||
| Condition | ||
| Error in `expect_shape()`: | ||
| ! `nrow` must be a whole number or `NA`, not the string "x". | ||
| Code | ||
| expect_shape(array(1), ncol = "x") | ||
| Condition | ||
| Error in `expect_shape()`: | ||
| ! `ncol` must be a whole number or `NA`, not the string "x". | ||
| Code | ||
| expect_shape(array(1), dim = "x") | ||
| Condition | ||
| Error in `expect_shape()`: | ||
| ! `dim` must be a numeric vector, not the string "x". | ||
| 
     | 
Uh oh!
There was an error while loading. Please reload this page.