Skip to content

Commit 58654fb

Browse files
Don't get generic 'object' label by passing to expect_length(); correct return values & corresponding tests
1 parent 3ed14c4 commit 58654fb

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

R/expect-length.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ expect_length <- function(object, n) {
1717
stopifnot(is.numeric(n), length(n) == 1)
1818

1919
act <- quasi_label(enquo(object), arg = "object")
20+
expect_length_impl_(act, n)
21+
}
22+
23+
expect_length_impl_ <- function(act, n) {
2024
act$n <- length(act$val)
2125

2226
expect(

R/expect-shape.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
expect_shape = function(object, ..., length, nrow, ncol, dim) {
1818
rlang::check_exclusive(length, nrow, ncol, dim)
1919

20+
act <- quasi_label(enquo(object), arg = "object")
21+
2022
# Re-use expect_length() to ensure they stay in sync.
2123
if (!missing(length)) {
22-
return(expect_length(object, length))
24+
return(expect_length_impl_(act, length))
2325
}
2426

2527
# need base:: qualification or we might trigger an error for missing(length)
2628
length <- base::length
2729

28-
act <- quasi_label(enquo(object), arg = "object")
2930
dim_object <- base::dim(object)
3031

3132
if (is.null(dim_object)) {
@@ -62,4 +63,6 @@ expect_shape = function(object, ..., length, nrow, ncol, dim) {
6263
sprintf("%s has dim (%s), not (%s).", act$lab, toString(act$dim), toString(dim))
6364
)
6465
}
66+
67+
invisible(act$val)
6568
}

tests/testthat/_snaps/expect-shape.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# length compared correctly
22

3-
`object` has length 1, not length 2.
3+
1 has length 1, not length 2.
44

55
# dim compared correctly
66

tests/testthat/test-expect-shape.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ test_that("length compared correctly", {
44
expect_success(expect_shape(1:10, length = 10))
55
expect_success(expect_shape(letters[1:5], length = 5))
66
expect_success(expect_shape(integer(), length = 0))
7+
8+
x <- list(1:10, letters)
9+
out <- expect_shape(x, length = 2)
10+
expect_identical(out, x)
711
})
812

913
test_that("dim compared correctly", {
@@ -17,6 +21,10 @@ test_that("dim compared correctly", {
1721
expect_success(expect_shape(array(integer()), dim = 0L))
1822
dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L)
1923
expect_success(expect_shape(array(dim = dd), dim = dd))
24+
25+
x <- cbind(1:2, 3:4)
26+
out <- expect_shape(x, dim = c(2L, 2L))
27+
expect_identical(out, x)
2028
})
2129

2230
test_that("nrow compared correctly", {
@@ -27,6 +35,10 @@ test_that("nrow compared correctly", {
2735
expect_success(expect_shape(array(integer()), nrow = 0L))
2836
dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L)
2937
expect_success(expect_shape(array(dim = dd), nrow = 0L))
38+
39+
x <- cbind(1:2, 3:4)
40+
out <- expect_shape(x, dim = c(2L, 2L))
41+
expect_identical(out, x)
3042
})
3143

3244
test_that("ncol compared correctly", {
@@ -37,6 +49,10 @@ test_that("ncol compared correctly", {
3749
expect_snapshot_failure(expect_shape(array(integer()), ncol = 0L))
3850
dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L)
3951
expect_success(expect_shape(array(dim = dd), ncol = 0L))
52+
53+
x <- cbind(1:2, 3:4)
54+
out <- expect_shape(x, dim = c(2L, 2L))
55+
expect_identical(out, x)
4056
})
4157

4258
test_that("uses S3 dim method", {
@@ -69,12 +85,6 @@ test_that("uses S4 dim method", {
6985
expect_success(expect_shape(A(x = 1:9, y = 3), dim = 8:10))
7086
})
7187

72-
test_that("returns input", {
73-
x <- list(1:10, letters)
74-
out <- expect_shape(x, length = 2)
75-
expect_identical(out, x)
76-
})
77-
7888
test_that("at least one argument is required", {
7989
err_msg <- "Exactly one of `length`, `nrow`, `ncol`, or `dim` must be provided."
8090
expect_snapshot(expect_shape(1:10), error = TRUE) # no args

0 commit comments

Comments
 (0)