Skip to content

Commit

Permalink
Add error messages to stopifnot() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mojaveazure committed Mar 4, 2024
1 parent 15f50f9 commit a1b24a5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
12 changes: 10 additions & 2 deletions apis/r/R/BlockwiseIter.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ BlockwiseReadIterBase <- R6::R6Class(
private$.axis <- axis

Check warning on line 42 in apis/r/R/BlockwiseIter.R

View check run for this annotation

Codecov / codecov/patch

apis/r/R/BlockwiseIter.R#L42

Added line #L42 was not covered by tests
# Check coords
stopifnot(
is_named_list(coords) && all(vapply_lgl(coords, inherits, "CoordsStrider")),
self$array$dimnames()[self$axis + 1L] %in% names(coords)
"'coords' must be a named list of 'CoordsStrider' objects" = is_named_list(coords) &&
all(vapply_lgl(coords, inherits, "CoordsStrider"))

Check warning on line 46 in apis/r/R/BlockwiseIter.R

View check run for this annotation

Codecov / codecov/patch

apis/r/R/BlockwiseIter.R#L44-L46

Added lines #L44 - L46 were not covered by tests
)
axname <- self$array$dimnames()[self$axis + 1L]
if (!axname %in% names(coords)) {
stop(
"'coords' must include an entry for ",
sQuote(axname, FALSE),
call. = FALSE

Check warning on line 53 in apis/r/R/BlockwiseIter.R

View check run for this annotation

Codecov / codecov/patch

apis/r/R/BlockwiseIter.R#L48-L53

Added lines #L48 - L53 were not covered by tests
)
}
private$.coords <- coords

Check warning on line 56 in apis/r/R/BlockwiseIter.R

View check run for this annotation

Codecov / codecov/patch

apis/r/R/BlockwiseIter.R#L56

Added line #L56 was not covered by tests
# Check reindex_disable_on_axis
if (!is.null(reindex_disable_on_axis)) {
Expand Down
22 changes: 13 additions & 9 deletions apis/r/R/CoordsStrider.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,29 @@ CoordsStrider <- R6::R6Class(
initialize = function(coords, ..., stride = NULL, start = NULL, end = NULL) {
if (missing(coords)) {
stopifnot(
rlang::is_integerish(start, 1L, TRUE) ||
"'start' must be a single integer value" = rlang::is_integerish(start, 1L, TRUE) ||
(inherits(start, "integer64") && length(start) == 1L && is.finite(start)),
rlang::is_integerish(end, 1L, TRUE) ||
"'end' must be a single integer value" = rlang::is_integerish(end, 1L, TRUE) ||
(inherits(end, "integer64") && length(end) == 1L && is.finite(end)),
start <= end
"'start' must be less than or equal to 'end'" = start <= end
)
private$.start <- bit64::as.integer64(start)
private$.end <- bit64::as.integer64(end)
stride <- stride %||% bit64::abs.integer64(self$end - self$start + 1L)
private$.index <- 0L
} else {
stopifnot(inherits(coords, c("integer64", "numeric", "integer")))
stopifnot(
"'coords' must be a vector of integer-like values" = inherits(coords, c("integer64", "numeric", "integer"))
)
private$.coords <- bit64::as.integer64(coords)
stride <- stride %||% length(coords)
stopifnot(stride <= .Machine$integer.max)
stopifnot(
"'stride' must be less than `Machine$integer.max`" = stride <= .Machine$integer.max
)
private$.index <- 1L
}
stopifnot(
rlang::is_integerish(stride, 1L, TRUE) ||
"'stride' must be a single integer value" = rlang::is_integerish(stride, 1L, TRUE) ||
(inherits(stride, "integer64") && length(stride == 1L) && is.finite(stride)),
stride > 0L
)
Expand Down Expand Up @@ -153,7 +157,7 @@ CoordsStrider <- R6::R6Class(
return(private$.stride)
}
stopifnot(
(rlang::is_integerish(value, n = 1L, finite = TRUE) ||
"'stride' must be a single integer value" = (rlang::is_integerish(value, n = 1L, finite = TRUE) ||
(inherits(value, "integer64") && length(value) == 1L && is.finite(value))

Check warning on line 161 in apis/r/R/CoordsStrider.R

View check run for this annotation

Codecov / codecov/patch

apis/r/R/CoordsStrider.R#L159-L161

Added lines #L159 - L161 were not covered by tests
) &&
value > 0L

Check warning on line 163 in apis/r/R/CoordsStrider.R

View check run for this annotation

Codecov / codecov/patch

apis/r/R/CoordsStrider.R#L163

Added line #L163 was not covered by tests
Expand Down Expand Up @@ -214,8 +218,8 @@ hasNext.CoordsStrider <- function(obj, ...) obj$has_next()

unlist64 <- function(x) {
stopifnot(
is.list(x),
all(vapply_lgl(x, inherits, what = 'integer64'))
"'x' must be a list" = is.list(x),
"'x' must contain 'integer64' values" = all(vapply_lgl(x, inherits, what = 'integer64'))
)
res <- bit64::integer64(sum(vapply_int(x, length)))
idx <- 1L
Expand Down
4 changes: 2 additions & 2 deletions apis/r/R/SOMASparseNDArrayRead.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ SOMASparseNDArrayBlockwiseRead <- R6::R6Class(
) {
super$initialize(sr, array, coords)
stopifnot(
is.null(size) ||
"'size' must be a single integer value" = is.null(size) ||
rlang::is_integerish(size, 1L, finite = TRUE) ||
(inherits(size, 'integer64') && length(size) == 1L && is.finite(size)),
is.null(reindex_disable_on_axis) ||
"'reindex_disable_on_axis' must be avector of integers" = is.null(reindex_disable_on_axis) ||
rlang::is_integerish(reindex_disable_on_axis, finite = TRUE) ||
(inherits(reindex_disable_on_axis, 'integer64') && all(is.finite(reindex_disable_on_axis)))

Check warning on line 222 in apis/r/R/SOMASparseNDArrayRead.R

View check run for this annotation

Codecov / codecov/patch

apis/r/R/SOMASparseNDArrayRead.R#L215-L222

Added lines #L215 - L222 were not covered by tests
)
Expand Down
7 changes: 5 additions & 2 deletions apis/r/R/utils-readerTransformers.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ soma_array_to_arrow_table <- function(x) {
}

soma_array_to_arrow_table_concat <- function(it) {
stopifnot(inherits(it, 'ReadIter'))
stopifnot("'it' must be a 'ReadIter' object" = inherits(it, 'ReadIter'))
tbl <- it$read_next()
while (!it$read_complete()) {
nxt <- it$read_next()
Expand All @@ -23,7 +23,10 @@ soma_array_to_arrow_table_concat <- function(it) {
}

soma_array_to_sparse_matrix_concat <- function(it, zero_based = FALSE) {
stopifnot(inherits(it, 'ReadIter'), isTRUE(zero_based) || isFALSE(zero_based))
stopifnot(
"'it' must be a 'ReadIter' object" = inherits(it, 'ReadIter'),
"'zero_based' must be TRUE or FALSE" = isTRUE(zero_based) || isFALSE(zero_based)
)
mat <- it$read_next()
while (!it$read_complete()) {
mat <- if (isTRUE(zero_based)) {
Expand Down

0 comments on commit a1b24a5

Please sign in to comment.