@@ -26,80 +26,3 @@ expect_length <- function(object, n) {
2626
2727  invisible (act $ val )
2828}
29- 
30- # ' Does code return an object with the specified shape?
31- # '
32- # ' By "shape", we mean an object's [dim()], or, for one-dimensional objects,
33- # '   it's [length()]. Thus this is an extension of [expect_length()] to more
34- # '   general objects like [data.frame()], [matrix()], and [array()].
35- # ' To wit, first, the object's `dim()` is checked. If non-`NULL`, it is compared
36- # '   to `shape` (or one/both of `nrow`, `ncol`, if they are supplied, in which
37- # '   case they take precedence). If `dim(object)` is `NULL`, `length(object)`
38- # '   is compared to `shape`.
39- # '
40- # ' @seealso [expect_length()] to specifically make assertions about the
41- # '   [length()] of a vector.
42- # ' @inheritParams expect_that
43- # ' @param shape Expected shape, an integer vector.
44- # ' @param nrow Expected number of rows, numeric.
45- # ' @param ncol Expected number of columns, numeric.
46- # ' @family expectations
47- # ' @export
48- # ' @examples
49- expect_shape  =  function (object , shape , nrow , ncol ) {
50-   stopifnot(
51-     missing(shape ) ||  is.numeric(shape ),
52-     missing(nrow ) ||  is.numeric(nrow ),
53-     missing(ncol ) ||  is.numeric(ncol )
54-   )
55- 
56-   dim_object  <-  dim(object )
57-   if  (is.null(dim_object )) {
58-     if  (missing(shape )) {
59-       stop(" `shape` must be provided for one-dimensional inputs"  )
60-     }
61-     return (expect_length(object , shape ))
62-   }
63- 
64-   act  <-  quasi_label(enquo(object ), arg  =  " object"  )
65- 
66-   if  (missing(nrow ) &&  missing(ncol )) {
67-     #  testing dim
68-     if  (missing(shape )) {
69-       stop(" `shape` must be provided if `nrow` and `ncol` are not"  )
70-     }
71-     act $ shape  <-  dim_object 
72- 
73-     expect(
74-       isTRUE(all.equal(act $ shape , shape )),
75-       sprintf(" %s has shape (%s), not (%s)."  , act $ lab , toString(act $ shape ), toString(shape ))
76-     )
77-   } else  if  (missing(nrow ) &&  ! missing(ncol )) {
78-     #  testing only ncol
79-     act $ ncol  <-  dim_object [2L ]
80- 
81-     expect(
82-       act $ ncol  ==  ncol ,
83-       sprintf(" %s has %i columns, not %i."  , act $ lab , act $ ncol , ncol )
84-     )
85-   } else  if  (! missing(nrow ) &&  missing(ncol )) {
86-     #  testing only nrow
87-     act $ nrow  <-  dim_object [1L ]
88- 
89-     expect(
90-       act $ nrow  ==  nrow ,
91-       sprintf(" %s has %i rows, not %i."  , act $ lab , act $ nrow , nrow )
92-     )
93-   } else  {
94-     #  testing both nrow & ncol (useful, e.g., for testing dim(.)[1:2] for arrays
95-     act $ nrow  <-  dim_object [1L ]
96-     act $ ncol  <-  dim_object [2L ]
97- 
98-     expect(
99-       act $ nrow  ==  nrow  &&  act $ ncol  ==  ncol ,
100-       sprintf(" %s has %i rows and %i columns, not %i rows and %i columns"  , act $ lab , act $ nrow , act $ ncol , nrow , ncol )
101-     )
102-   }
103- 
104-   return (act $ val )
105- }
0 commit comments