- 
                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
          
     Merged
      
      
    
  
     Merged
                    Expect shape #1469
Changes from 26 commits
      Commits
    
    
            Show all changes
          
          
            32 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      66b166a
              
                New expectation function, expect_shape()
              
              
                MichaelChirico d4853ff
              
                revert spurious NAMESPACE edits
              
              
                MichaelChirico 69c410a
              
                really this time
              
              
                MichaelChirico e89fff9
              
                finish documentation
              
              
                MichaelChirico cdb15a2
              
                refactor for clarity, add tests
              
              
                MichaelChirico 268aee8
              
                Merge branch 'main' into expect-shape
              
              
                MichaelChirico 89c0c82
              
                restyle NEWS
              
              
                MichaelChirico b74737f
              
                move to own file
              
              
                MichaelChirico 769ccc1
              
                tighter wording: integer-->numeric
              
              
                MichaelChirico 9aede6f
              
                completeness comment
              
              
                MichaelChirico dd23081
              
                new file in pkgdown ref
              
              
                MichaelChirico eb0dc5a
              
                Merge branch 'main' into expect-shape
              
              
                MichaelChirico 1a020dc
              
                reorder news after merge
              
              
                MichaelChirico ffde38b
              
                Rearrange to desired signature, adjust tests
              
              
                MichaelChirico 0661094
              
                tweak NEWS
              
              
                MichaelChirico ac0acbc
              
                copy-paste roxygenize...
              
              
                MichaelChirico 150425b
              
                expect_snapshot() over expect_error()
              
              
                MichaelChirico 53b0ee8
              
                expect_failure() -> expect_snapshot_failure()
              
              
                MichaelChirico a72708b
              
                manual roxygenize continues
              
              
                MichaelChirico da7fdf2
              
                manual \description too
              
              
                MichaelChirico d873766
              
                rlang::check_exclusive
              
              
                MichaelChirico 3710870
              
                Check length(dim) to get better errors
              
              
                MichaelChirico 170543e
              
                new edge tests, improve structure
              
              
                MichaelChirico a88340d
              
                more 0-dimension edge case checks
              
              
                MichaelChirico e35122a
              
                S3 dispatch check
              
              
                MichaelChirico ff4d7c5
              
                Robustness: dim() can return NA
              
              
                MichaelChirico 4bc32fc
              
                Use fail() + early return, which exposed faulty logic
              
              
                MichaelChirico 1bdf1cd
              
                update snapshots
              
              
                MichaelChirico 3ed14c4
              
                remove unneeded early return
              
              
                MichaelChirico 58654fb
              
                Don't get generic 'object' label by passing to expect_length(); corre…
              
              
                MichaelChirico ff4c894
              
                Re-document
              
              
                hadley 2ca461b
              
                Style tweaks; validate more inputs
              
              
                hadley File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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) | ||
| 
     | 
||
| # Re-use expect_length() to ensure they stay in sync. | ||
| if (!missing(length)) { | ||
| return(expect_length(object, length)) | ||
| } | ||
| 
     | 
||
| # need base:: qualification or we might trigger an error for missing(length) | ||
| length <- base::length | ||
| 
     | 
||
| act <- quasi_label(enquo(object), arg = "object") | ||
| dim_object <- base::dim(object) | ||
| 
     | 
||
| expect( | ||
| !is.null(dim_object), | ||
| 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)) { | ||
| expect( | ||
| length(dim_object) >= 2L, | ||
| 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 | ||
| 
     | 
||
| expect( | ||
| length(act$dim) == length(dim), | ||
                
      
                  MichaelChirico marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 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 shape (%s), not (%s).", act$lab, toString(act$dim), toString(dim)) | ||
| ) | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # length compared correctly | ||
| 
     | 
||
| `object` has length 1, not length 2. | ||
| 
     | 
||
| # dim compared correctly | ||
| 
     | 
||
| matrix(nrow = 6, ncol = 3) has shape (6, 3), not (6, 2). | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| matrix(nrow = 6, ncol = 3) has shape (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() has only one dimension. | ||
| 
     | 
||
| # NA handling (e.g. dbplyr) | ||
| 
     | 
||
| `x` has NA rows, not 10. | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| `x` has 10 columns, not NA. | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| `x` has shape (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. | ||
| 
     | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| 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)) | ||
| }) | ||
| 
     | 
||
| 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(), dim = 0L)) | ||
| dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L) | ||
| expect_success(expect_shape(array(dim = dd), dim = dd)) | ||
| }) | ||
| 
     | 
||
| 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(), nrow = 0L)) | ||
| dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L) | ||
| expect_success(expect_shape(array(dim = dd), nrow = 0L)) | ||
| }) | ||
| 
     | 
||
| 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(), ncol = 0L)) | ||
| dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L) | ||
| expect_success(expect_shape(array(dim = dd), ncol = 0L)) | ||
| }) | ||
| 
     | 
||
| 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("returns input", { | ||
| x <- list(1:10, letters) | ||
| out <- expect_shape(x, length = 2) | ||
| expect_identical(out, x) | ||
| }) | ||
| 
     | 
||
| 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 | ||
| }) | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.