11test_that(" shape computed correctly" , {
22 # equivalent to expect_length
3- expect_success(expect_shape(1 , 1 ))
4- expect_failure(expect_shape(1 , 2 ), " has length 1, not length 2." )
5- expect_success(expect_shape(1 : 10 , 10 ))
6- expect_success(expect_shape(letters [1 : 5 ], 5 ))
3+ expect_success(expect_shape(1 , length = 1 ))
4+ expect_failure(expect_shape(1 , length = 2 ), " has length 1, not length 2." )
5+ expect_success(expect_shape(1 : 10 , length = 10 ))
6+ expect_success(expect_shape(letters [1 : 5 ], length = 5 ))
77
88 # testing dim()
9- expect_success(expect_shape(matrix (nrow = 5 , ncol = 4 ), c(5L , 4L )))
10- expect_failure(expect_shape(matrix (nrow = 6 , ncol = 3 ), c(6L , 2L )))
11- expect_failure(expect_shape(matrix (nrow = 6 , ncol = 3 ), c(7L , 3L )))
12- expect_success(expect_shape(data.frame (1 : 10 , 11 : 20 ), c(10 , 2 )))
13- expect_success(expect_shape(array (dim = 1 : 3 ), 1 : 3 ))
9+ expect_success(expect_shape(matrix (nrow = 5 , ncol = 4 ), dim = c(5L , 4L )))
10+ expect_failure(expect_shape(matrix (nrow = 6 , ncol = 3 ), dim = c(6L , 2L )))
11+ expect_failure(expect_shape(matrix (nrow = 6 , ncol = 3 ), dim = c(7L , 3L )))
12+ expect_success(expect_shape(data.frame (1 : 10 , 11 : 20 ), dim = c(10 , 2 )))
13+ expect_success(expect_shape(array (dim = 1 : 3 ), dim = 1 : 3 ))
1414
1515 # testing nrow=
1616 expect_success(expect_shape(matrix (nrow = 5 , ncol = 4 ), nrow = 5L ))
@@ -21,31 +21,23 @@ test_that("shape computed correctly", {
2121 expect_success(expect_shape(matrix (nrow = 5 , ncol = 4 ), ncol = 4L ))
2222 expect_failure(expect_shape(matrix (nrow = 5 , ncol = 5 ), ncol = 7L ))
2323 expect_success(expect_shape(data.frame (1 : 10 , 11 : 20 ), ncol = 2L ))
24-
25- # testing nrow= and ncol=
26- expect_success(expect_shape(matrix (nrow = 5 , ncol = 4 ), nrow = 5L , ncol = 4L ))
27- expect_failure(expect_shape(matrix (nrow = 5 , ncol = 5 ), nrow = 6L , ncol = 5L ))
28- expect_success(expect_shape(data.frame (1 : 10 , 11 : 20 ), nrow = 10L , ncol = 2L ))
29- expect_success(expect_shape(array (dim = 5 : 7 ), nrow = 5L , ncol = 6L ))
30-
31- # precedence of manual nrow/ncol over shape
32- expect_success(expect_shape(matrix (nrow = 7 , ncol = 10 ), 1 : 2 , nrow = 7L ))
33- expect_success(expect_shape(matrix (nrow = 7 , ncol = 10 ), 1 : 2 , ncol = 10L ))
3424})
3525
3626test_that(" uses S4 dim method" , {
3727 A <- setClass ("ExpectShapeA ", slots = c(x = "numeric", y = "numeric"))
3828 setMethod ("dim ", "ExpectShapeA", function(x) 8:10)
39- expect_success(expect_shape(A(x = 1 : 9 , y = 3 ), 8 : 10 ))
29+ expect_success(expect_shape(A(x = 1 : 9 , y = 3 ), dim = 8 : 10 ))
4030})
4131
4232test_that(" returns input" , {
4333 x <- list (1 : 10 , letters )
44- out <- expect_shape(x , 2 )
34+ out <- expect_shape(x , length = 2 )
4535 expect_identical(out , x )
4636})
4737
4838test_that(" at least one argument is required" , {
49- expect_error(expect_shape(1 : 10 ), " `shape` must be provided for one-dimensional inputs" , fixed = TRUE )
50- expect_error(expect_shape(cbind(1 : 2 )), " `shape` must be provided if `nrow` and `ncol` are not" )
39+ err_msg <- " Exactly one of `length`, `nrow`, `ncol`, or `dim` must be provided."
40+ expect_error(expect_shape(1 : 10 ), err_msg , fixed = TRUE ) # no args
41+ expect_error(expect_shape(1 : 10 , 2 ), err_msg , fixed = TRUE ) # no named args
42+ expect_error(expect_shape(1 : 10 , nrow = 1L , ncol = 2L ), err_msg , fixed = TRUE ) # multiple named args
5143})
0 commit comments