Skip to content

Commit b74737f

Browse files
move to own file
1 parent 89c0c82 commit b74737f

File tree

2 files changed

+76
-77
lines changed

2 files changed

+76
-77
lines changed

R/expect-length.R

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
}

R/expect-shape.R

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#' Does code return an object with the specified shape?
2+
#'
3+
#' By "shape", we mean an object's [dim()], or, for one-dimensional objects,
4+
#' it's [length()]. Thus this is an extension of [expect_length()] to more
5+
#' general objects like [data.frame()], [matrix()], and [array()].
6+
#' To wit, first, the object's `dim()` is checked. If non-`NULL`, it is compared
7+
#' to `shape` (or one/both of `nrow`, `ncol`, if they are supplied, in which
8+
#' case they take precedence). If `dim(object)` is `NULL`, `length(object)`
9+
#' is compared to `shape`.
10+
#'
11+
#' @seealso [expect_length()] to specifically make assertions about the
12+
#' [length()] of a vector.
13+
#' @inheritParams expect_that
14+
#' @param shape Expected shape, an integer vector.
15+
#' @param nrow Expected number of rows, numeric.
16+
#' @param ncol Expected number of columns, numeric.
17+
#' @family expectations
18+
#' @export
19+
#' @examples
20+
expect_shape = function(object, shape, nrow, ncol) {
21+
stopifnot(
22+
missing(shape) || is.numeric(shape),
23+
missing(nrow) || is.numeric(nrow),
24+
missing(ncol) || is.numeric(ncol)
25+
)
26+
27+
dim_object <- dim(object)
28+
if (is.null(dim_object)) {
29+
if (missing(shape)) {
30+
stop("`shape` must be provided for one-dimensional inputs")
31+
}
32+
return(expect_length(object, shape))
33+
}
34+
35+
act <- quasi_label(enquo(object), arg = "object")
36+
37+
if (missing(nrow) && missing(ncol)) {
38+
# testing dim
39+
if (missing(shape)) {
40+
stop("`shape` must be provided if `nrow` and `ncol` are not")
41+
}
42+
act$shape <- dim_object
43+
44+
expect(
45+
isTRUE(all.equal(act$shape, shape)),
46+
sprintf("%s has shape (%s), not (%s).", act$lab, toString(act$shape), toString(shape))
47+
)
48+
} else if (missing(nrow) && !missing(ncol)) {
49+
# testing only ncol
50+
act$ncol <- dim_object[2L]
51+
52+
expect(
53+
act$ncol == ncol,
54+
sprintf("%s has %i columns, not %i.", act$lab, act$ncol, ncol)
55+
)
56+
} else if (!missing(nrow) && missing(ncol)) {
57+
# testing only nrow
58+
act$nrow <- dim_object[1L]
59+
60+
expect(
61+
act$nrow == nrow,
62+
sprintf("%s has %i rows, not %i.", act$lab, act$nrow, nrow)
63+
)
64+
} else {
65+
# testing both nrow & ncol (useful, e.g., for testing dim(.)[1:2] for arrays
66+
act$nrow <- dim_object[1L]
67+
act$ncol <- dim_object[2L]
68+
69+
expect(
70+
act$nrow == nrow && act$ncol == ncol,
71+
sprintf("%s has %i rows and %i columns, not %i rows and %i columns", act$lab, act$nrow, act$ncol, nrow, ncol)
72+
)
73+
}
74+
75+
return(act$val)
76+
}

0 commit comments

Comments
 (0)