You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Would come in handy. For list-extensions like data.frame, of course expect_length works, but an expect_ncol would also be more readable for this case.
Does that make sense to include in testthat itself? Happy to file the PR if so.
From a quick glance at our particular subset of CRAN, I see 413 usages of expect_equal(nrow(, 98 for expect_equal(ncol(, and 130 of expect_equal(dim(, so it's clearly used a decent amount already by downstreams.
Reacted by Watal M. Iwasaki, André Veríssimo and Lluís Revilla
My main concern is whether this is a "slippery slope" — i.e. does adding expect_shape() imply there are a large number of other expectations we also need to add? (Or that other folks might argue we should add). But I think expect_shape() seems defensible because it's around testing a key attribute that is really built-in into base R.
OTOH, I think expect_dim() would be a little easier to explain, since while I like the notion of shape, it's not something that currently exists in base R, so would need to be explained somewhere. But that would imply separate expect_nrow() and expect_ncol() which I don't love just because it increases the API surface by 3 functions for relatively rarely used cases. So all-in-all, I think I would prefer expect_shape() with multiple arguments.
Maybe it would be better to leave length as a separate expectation, then drop dim in favour of a single shape argument? You can always use expect_equal(dim(x), ...) if you want to match exactly, and this has some precedence in that expect_s3_class() is not always sufficiently precise so you use expect_equal(class(x), ...).
So I think I'm suggesting expect_shape = function(x, shape, nrow, ncol) {}. If that sounds good, a PR would be much appreciated.
Activity
MichaelChirico commentedon Sep 8, 2021
Alternatively we might try something like
expect_shape(x, .)with signatureAnd
dimcan be fudged to work aslengthforNULL-diminput. That would unify things a bit:And so on.
Maybe the signature could be
instead so that explicitly setting
dim=doesn't unintentionally match onlength(), only ondim().hadley commentedon Sep 8, 2021
My main concern is whether this is a "slippery slope" — i.e. does adding
expect_shape()imply there are a large number of other expectations we also need to add? (Or that other folks might argue we should add). But I thinkexpect_shape()seems defensible because it's around testing a key attribute that is really built-in into base R.OTOH, I think
expect_dim()would be a little easier to explain, since while I like the notion of shape, it's not something that currently exists in base R, so would need to be explained somewhere. But that would imply separateexpect_nrow()andexpect_ncol()which I don't love just because it increases the API surface by 3 functions for relatively rarely used cases. So all-in-all, I think I would preferexpect_shape()with multiple arguments.Maybe it would be better to leave length as a separate expectation, then drop
dimin favour of a singleshapeargument? You can always useexpect_equal(dim(x), ...)if you want to match exactly, and this has some precedence in thatexpect_s3_class()is not always sufficiently precise so you useexpect_equal(class(x), ...).So I think I'm suggesting
expect_shape = function(x, shape, nrow, ncol) {}. If that sounds good, a PR would be much appreciated.MichaelChirico commentedon Oct 4, 2021
SGTM! I'll work on a PR. If it's possible to assign to me, feel free to do so.
MichaelChirico commentedon Oct 4, 2021
As noted at r-lib/vctrs#1470, we could re-use
expect_vector()in some cases:But that's pretty abstruse IMO; the proposed
expect_shape()would be much clearer.julianbarg commentedon Jun 10, 2025
Just bumping this up again. IMO expect_nrow is the one I am most surprised is one we do not have.
Implement `expect_shape()` (#1469)