Skip to content

Commit 9cc5103

Browse files
schochasticsmaelle
andauthored
test: merged and refactored indexing.R tests (#1687)
Co-authored-by: Maëlle Salmon <[email protected]>
1 parent 9172630 commit 9cc5103

9 files changed

+397
-356
lines changed

R/indexing.R

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) {
5959
if (missing(i)) {
6060
i_seq <- seq_len(vcount(x))
6161
has_i <- FALSE
62-
} else{
62+
} else {
6363
i_seq <- i
6464
has_i <- TRUE
6565
}
@@ -78,10 +78,10 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) {
7878
to_id <- unlist(adj)
7979

8080
edge_list <- data.frame(from = as.integer(from_id), to = as.integer(to_id))
81-
if(has_j){
81+
if (has_j) {
8282
edge_list <- edge_list[edge_list$to %in% j_seq, ]
8383
}
84-
84+
8585
row_indices <- edge_list[[1]]
8686
col_indices <- edge_list[[2]]
8787

@@ -214,21 +214,21 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) {
214214
## Argument checks
215215
if ((!missing(from) || !missing(to)) &&
216216
(!missing(i) || !missing(j))) {
217-
stop("Cannot give 'from'/'to' together with regular indices")
217+
cli::cli_abort("Cannot use {.arg from}/{.arg to} together with regular indices")
218218
}
219219
if ((!missing(from) && missing(to)) ||
220220
(missing(from) && !missing(to))) {
221-
stop("Cannot give 'from'/'to' without the other")
221+
cli::cli_abort("Cannot use {.arg from}/{.arg to} without the other")
222222
}
223223
if (!missing(from)) {
224224
if ((!is.numeric(from) && !is.character(from)) || any(is.na(from))) {
225-
stop("'from' must be a numeric or character vector without NAs")
225+
cli::cli_abort("{.arg from} must be a numeric or character vector without NAs")
226226
}
227227
if ((!is.numeric(to) && !is.character(to)) || any(is.na(to))) {
228-
stop("'to' must be a numeric or character vector without NAs")
228+
cli::cli_abort("{.arg to} must be a numeric or character vector without NAs")
229229
}
230230
if (length(from) != length(to)) {
231-
stop("'from' and 'to' must have the same length")
231+
cli::cli_abort("{.arg from} and {.arg to} must have the same length")
232232
}
233233
}
234234

@@ -286,10 +286,9 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) {
286286

287287
if (!sparse) {
288288
as.matrix(sub_adjmat[, , drop = drop])
289-
} else{
289+
} else {
290290
sub_adjmat[, , drop = drop]
291-
}
292-
291+
}
293292
}
294293

295294
#' Query and manipulate a graph as it were an adjacency list
@@ -353,8 +352,8 @@ get_adjacency_submatrix <- function(x, i, j, attr = NULL) {
353352
edges = FALSE, exact = TRUE) {
354353
getfun <- if (edges) as_adj_edge_list else as_adj_list
355354

356-
if (!missing(i) && !missing(from)) stop("Cannot give both 'i' and 'from'")
357-
if (!missing(j) && !missing(to)) stop("Cannot give both 'j' and 'to'")
355+
if (!missing(i) && !missing(from)) cli::cli_abort("Cannot use both {.arg i} and {.arg from}")
356+
if (!missing(j) && !missing(to)) cli::cli_abort("Cannot use both {.arg j} and {.arg to}")
358357
if (missing(i) && !missing(from)) i <- from
359358
if (missing(j) && !missing(to)) j <- to
360359

@@ -425,28 +424,28 @@ expand.grid.unordered <- function(i, j, loops = FALSE, directed = FALSE) {
425424
## Argument checks
426425
if ((!missing(from) || !missing(to)) &&
427426
(!missing(i) || !missing(j))) {
428-
stop("Cannot give 'from'/'to' together with regular indices")
427+
cli::cli_abort("Cannot use {.arg from}/{.arg to} together with regular indices")
429428
}
430429
if ((!missing(from) && missing(to)) ||
431430
(missing(from) && !missing(to))) {
432-
stop("Cannot give 'from'/'to' without the other")
431+
cli::cli_abort("Cannot use {.arg from}/{.arg to} without the other")
433432
}
434433
if (is.null(attr) &&
435434
(!is.null(value) && !is.numeric(value) && !is.logical(value))) {
436-
stop("New value should be NULL, numeric or logical")
435+
cli::cli_abort("New value should be NULL, numeric or logical")
437436
}
438437
if (is.null(attr) && !is.null(value) && length(value) != 1) {
439-
stop("Logical or numeric value must be of length 1")
438+
cli::cli_abort("Logical or numeric value must be of length 1")
440439
}
441440
if (!missing(from)) {
442441
if ((!is.numeric(from) && !is.character(from)) || any(is.na(from))) {
443-
stop("'from' must be a numeric or character vector without NAs")
442+
cli::cli_abort("{.arg from} must be a numeric or character vector without NAs")
444443
}
445444
if ((!is.numeric(to) && !is.character(to)) || any(is.na(to))) {
446-
stop("'to' must be a numeric or character vector without NAs")
445+
cli::cli_abort("{.arg to} must be a numeric or character vector without NAs")
447446
}
448447
if (length(from) != length(to)) {
449-
stop("'from' and 'to' must have the same length")
448+
cli::cli_abort("{.arg from} and {.arg to} must have the same length")
450449
}
451450
}
452451

@@ -498,7 +497,7 @@ expand.grid.unordered <- function(i, j, loops = FALSE, directed = FALSE) {
498497

499498
if (is.null(attr)) {
500499
if (value > 1) {
501-
cli::cli_abort("value greater than one but graph is not weighted and no attribute was specified.")
500+
cli::cli_abort("{.arg value} greater than one but graph is not weighted and {.arg attr} was not specified.")
502501
}
503502
x <- add_edges(x, toadd)
504503
} else {

tests/testthat/helper-indexing.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ vector_to_square_matrix <- function(...) {
33
matrix(v, nrow = sqrt(length(v)))
44
}
55

6-
canonicalize_matrix <- function(x) {
6+
as_unnamed_dense_matrix <- function(x) {
77
x <- as.matrix(x)
88
dimnames(x) <- NULL
99
x

tests/testthat/test-bug-1073705-indexing.R

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/testthat/test-conversion.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ test_that("as_adjacency_matrix() works -- dense + weights", {
282282
mat[lower.tri(mat)] <- 1:10
283283
mat <- mat + t(mat)
284284
A <- as_adjacency_matrix(g, attr = "weight", sparse = FALSE)
285-
expect_equal(canonicalize_matrix(A), mat)
285+
expect_equal(as_unnamed_dense_matrix(A), mat)
286286
})
287287

288288
test_that("as_biadjacency_matrix() works -- dense + weights", {
@@ -295,5 +295,5 @@ test_that("as_biadjacency_matrix() works -- dense + weights", {
295295
ncol = 2L,
296296
dimnames = list(c("1", "3", "5", "6"), c("2", "4"))
297297
)
298-
expect_equal(canonicalize_matrix(A), canonicalize_matrix(mat))
298+
expect_equal(as_unnamed_dense_matrix(A), as_unnamed_dense_matrix(mat))
299299
})

tests/testthat/test-deprecated_indexing_functions.R

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/testthat/test-index-es.R

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)