Skip to content

Commit 4bc32fc

Browse files
Use fail() + early return, which exposed faulty logic
1 parent ff4d7c5 commit 4bc32fc

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

R/expect-shape.R

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ expect_shape = function(object, ..., length, nrow, ncol, dim) {
2828
act <- quasi_label(enquo(object), arg = "object")
2929
dim_object <- base::dim(object)
3030

31-
expect(
32-
!is.null(dim_object),
33-
sprintf("%s has no dimensions.", act$lab)
34-
)
31+
if (is.null(dim_object)) {
32+
return(fail(sprintf("%s has no dimensions.", act$lab)))
33+
}
3534

3635
if (!missing(nrow)) {
3736
act$nrow <- dim_object[1L]
@@ -41,11 +40,10 @@ expect_shape = function(object, ..., length, nrow, ncol, dim) {
4140
sprintf("%s has %i rows, not %i.", act$lab, act$nrow, nrow)
4241
)
4342
} else if (!missing(ncol)) {
44-
expect(
45-
length(dim_object) >= 2L,
46-
sprintf("%s has only one dimension.", act$lab)
47-
)
48-
43+
if (length(dim_object) == 1L) {
44+
return(fail(sprintf("%s has only one dimension.", act$lab)))
45+
}
46+
4947
act$ncol <- dim_object[2L]
5048

5149
expect(
@@ -55,14 +53,13 @@ expect_shape = function(object, ..., length, nrow, ncol, dim) {
5553
} else { # !missing(dim)
5654
act$dim <- dim_object
5755

58-
expect(
59-
length(act$dim) == length(dim),
60-
sprintf("%s has %i dimensions, not %i", act$lab, length(act$dim), length(dim))
61-
)
56+
if (length(act$dim) != length(dim)) {
57+
return(fail(sprintf("%s has %i dimensions, not %i", act$lab, length(act$dim), length(dim))))
58+
}
6259

6360
expect(
6461
identical(as.integer(act$dim), as.integer(dim)),
65-
sprintf("%s has shape (%s), not (%s).", act$lab, toString(act$dim), toString(dim))
62+
sprintf("%s has dim (%s), not (%s).", act$lab, toString(act$dim), toString(dim))
6663
)
6764
}
6865
}

tests/testthat/_snaps/expect-shape.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
---
3838

39-
array() has only one dimension.
39+
array(integer()) has only one dimension.
4040

4141
# NA handling (e.g. dbplyr)
4242

tests/testthat/test-expect-shape.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test_that("dim compared correctly", {
1414
expect_success(expect_shape(array(dim = 1:3), dim = 1:3))
1515
expect_snapshot_failure(expect_shape(array(dim = 1:3), dim = 1:2))
1616
expect_snapshot_failure(expect_shape(array(dim = 1:3), dim = 1:4))
17-
expect_success(expect_shape(array(), dim = 0L))
17+
expect_success(expect_shape(array(integer()), dim = 0L))
1818
dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L)
1919
expect_success(expect_shape(array(dim = dd), dim = dd))
2020
})
@@ -24,7 +24,7 @@ test_that("nrow compared correctly", {
2424
expect_snapshot_failure(expect_shape(matrix(nrow = 5, ncol = 5), nrow = 6L))
2525
expect_success(expect_shape(data.frame(1:10, 11:20), nrow = 10L))
2626
expect_snapshot_failure(expect_shape(1, nrow = 1))
27-
expect_success(expect_shape(array(), nrow = 0L))
27+
expect_success(expect_shape(array(integer()), nrow = 0L))
2828
dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L)
2929
expect_success(expect_shape(array(dim = dd), nrow = 0L))
3030
})
@@ -34,7 +34,7 @@ test_that("ncol compared correctly", {
3434
expect_snapshot_failure(expect_shape(matrix(nrow = 5, ncol = 5), ncol = 7L))
3535
expect_success(expect_shape(data.frame(1:10, 11:20), ncol = 2L))
3636
expect_snapshot_failure(expect_shape(array(1), ncol = 1))
37-
expect_snapshot_failure(expect_shape(array(), ncol = 0L))
37+
expect_snapshot_failure(expect_shape(array(integer()), ncol = 0L))
3838
dd <- c(0L, 0L, 0L, 5L, 0L, 0L, 0L)
3939
expect_success(expect_shape(array(dim = dd), ncol = 0L))
4040
})

0 commit comments

Comments
 (0)