Skip to content

Commit

Permalink
test: merged and refactored iterators.R and operators.R tests (#1723)
Browse files Browse the repository at this point in the history
  • Loading branch information
schochastics authored Feb 27, 2025
1 parent 39fb16d commit 8064dea
Show file tree
Hide file tree
Showing 13 changed files with 1,438 additions and 1,446 deletions.
44 changes: 22 additions & 22 deletions R/iterators.R
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ E <- function(graph, P = NULL, path = NULL, directed = TRUE) {
update_es_ref(graph)

if (!is.null(P) && !is.null(path)) {
stop("Cannot give both `P' and `path' at the same time")
cli::cli_abort("Cannot use both {.arg P} and {.arg path} at the same time.")
}

if (is.null(P) && is.null(path)) {
Expand Down Expand Up @@ -386,7 +386,7 @@ create_es <- function(graph, idx, na_ok = FALSE) {

simple_vs_index <- function(x, i, na_ok = FALSE) {
res <- unclass(x)[i]
if (!na_ok && any(is.na(res))) stop("Unknown vertex selected")
if (!na_ok && any(is.na(res))) cli::cli_abort("Unknown vertex selected.")
class(res) <- "igraph.vs"
res
}
Expand Down Expand Up @@ -500,14 +500,14 @@ simple_vs_index <- function(x, i, na_ok = FALSE) {
#'
#' # -----------------------------------------------------------------
#' # The same with vertex names
#' g <- make_graph(~ A -+ B, B -+ C:D, D -+ B)
#' g <- make_graph(~ A - +B, B - +C:D, D - +B)
#' V(g)[.nei(c("B", "D"))]
#' V(g)[.nei(c("B", "D"), "in")]
#' V(g)[.nei(c("B", "D"), "out")]
#'
#' # -----------------------------------------------------------------
#' # Resolving attributes
#' g <- make_graph(~ A -+ B, B -+ C:D, D -+ B)
#' g <- make_graph(~ A - +B, B - +C:D, D - +B)
#' V(g)$color <- c("red", "red", "green", "green")
#' V(g)[color == "red"]
#'
Expand Down Expand Up @@ -785,7 +785,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
} else {
res <- unclass(x)[i]
}
if (!na_ok && any(is.na(res))) stop("Unknown edge selected")
if (!na_ok && any(is.na(res))) cli::cli_abort("Unknown edge selected")
attr(res, "env") <- attr(x, "env")
attr(res, "graph") <- attr(x, "graph")
class(res) <- "igraph.es"
Expand Down Expand Up @@ -1054,9 +1054,9 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
`[[<-.igraph.vs` <- function(x, i, value) {
if (!"name" %in% names(attributes(value)) ||
!"value" %in% names(attributes(value))) {
stop("invalid indexing")
cli::cli_abort("Invalid indexing.")
}
if (is.null(get_vs_graph(x))) stop("Graph is unknown")
if (is.null(get_vs_graph(x))) stop("Graph is unknown.")
value
}

Expand All @@ -1072,9 +1072,9 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
`[[<-.igraph.es` <- function(x, i, value) {
if (!"name" %in% names(attributes(value)) ||
!"value" %in% names(attributes(value))) {
stop("invalid indexing")
stop("Invalid indexing.")
}
if (is.null(get_es_graph(x))) stop("Graph is unknown")
if (is.null(get_es_graph(x))) stop("Graph is unknown.")
value
}

Expand Down Expand Up @@ -1138,7 +1138,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
#' plot(g)
`$.igraph.vs` <- function(x, name) {
graph <- get_vs_graph(x)
if (is.null(graph)) stop("Graph is unknown")
if (is.null(graph)) cli::cli_abort("Graph is unknown")
res <- vertex_attr(graph, name, x)
if (is_single_index(x)) {
res[[1]]
Expand Down Expand Up @@ -1189,7 +1189,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
#' plot(g)
`$.igraph.es` <- function(x, name) {
graph <- get_es_graph(x)
if (is.null(graph)) stop("Graph is unknown")
if (is.null(graph)) cli::cli_abort("Graph is unknown")
res <- edge_attr(graph, name, x)
if (is_single_index(x)) {
res[[1]]
Expand All @@ -1205,7 +1205,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
#' @name igraph-vs-attributes
#' @export
`$<-.igraph.vs` <- function(x, name, value) {
if (is.null(get_vs_graph(x))) stop("Graph is unknown")
if (is.null(get_vs_graph(x))) cli::cli_abort("Graph is unknown")
attr(x, "name") <- name
attr(x, "value") <- value
x
Expand All @@ -1218,7 +1218,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
#' @export
#' @family vertex and edge sequences
`$<-.igraph.es` <- function(x, name, value) {
if (is.null(get_es_graph(x))) stop("Graph is unknown")
if (is.null(get_es_graph(x))) cli::cli_abort("Graph is unknown")
attr(x, "name") <- name
attr(x, "value") <- value
x
Expand All @@ -1230,7 +1230,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
ensure_igraph(x)
if (!"name" %in% names(attributes(value)) ||
!"value" %in% names(attributes(value))) {
stop("invalid indexing")
cli::cli_abort("invalid indexing")
}
i_set_vertex_attr(x, attr(value, "name"),
index = value,
Expand All @@ -1249,7 +1249,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) {
ensure_igraph(x)
if (!"name" %in% names(attributes(value)) ||
!"value" %in% names(attributes(value))) {
stop("invalid indexing")
cli::cli_abort("invalid indexing")
}
i_set_edge_attr(x, attr(value, "name"),
index = value,
Expand Down Expand Up @@ -1419,13 +1419,13 @@ as_igraph_vs <- function(graph, v, na.ok = FALSE) {
if (inherits(v, "igraph.vs") && !is.null(graph) &&
!warn_version(graph)) {
if (get_graph_id(graph) != get_vs_graph_id(v)) {
stop("Cannot use a vertex sequence from another graph.")
cli::cli_abort("Cannot use a vertex sequence from another graph.")
}
}
if (is.character(v) && "name" %in% vertex_attr_names(graph)) {
v <- as.numeric(match(v, V(graph)$name))
if (!na.ok && any(is.na(v))) {
stop("Invalid vertex names")
cli::cli_abort("Invalid vertex names")
}
v
} else {
Expand All @@ -1437,7 +1437,7 @@ as_igraph_vs <- function(graph, v, na.ok = FALSE) {
res <- as.numeric(v)
}
if (!na.ok && any(is.na(res))) {
stop("Invalid vertex name(s)")
cli::cli_abort("Invalid vertex name(s)")
}
res
}
Expand All @@ -1447,7 +1447,7 @@ as_igraph_es <- function(graph, e) {
if (inherits(e, "igraph.es") && !is.null(graph) &&
!warn_version(graph)) {
if (get_graph_id(graph) != get_es_graph_id(e)) {
stop("Cannot use an edge sequence from another graph.")
cli::cli_abort("Cannot use an edge sequence from another graph.")
}
}
if (is.character(e)) {
Expand All @@ -1460,7 +1460,7 @@ as_igraph_es <- function(graph, e) {
vv <- strsplit(e[Pairs], "|", fixed = TRUE)
vl <- sapply(vv, length)
if (any(vl != 2)) {
stop("Invalid edge name: ", e[Pairs][vl != 2][1])
cli::cli_abort("Invalid edge name: ", e[Pairs][vl != 2][1])
}
vp <- unlist(vv)
if (!"name" %in% vertex_attr_names(graph)) {
Expand All @@ -1481,7 +1481,7 @@ as_igraph_es <- function(graph, e) {
res <- as.numeric(e)
}
if (any(is.na(res))) {
stop("Invalid edge names")
cli::cli_abort("Invalid edge names")
}
res
}
Expand All @@ -1500,7 +1500,7 @@ is_igraph_es <- function(x) {
parse_op_args <- function(..., what, is_fun, as_fun, check_graph = TRUE) {
args <- list(...)

if (any(!sapply(args, is_fun))) stop("Not ", what, " sequence")
if (any(!sapply(args, is_fun))) cli::cli_abort("Not {what} sequence")

## get the ids of all graphs
graph_id <- sapply(args, get_vs_graph_id) %>%
Expand Down
24 changes: 12 additions & 12 deletions R/operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ disjoint_union <- function(...) {
}))
lapply(graphs, ensure_igraph)
if (byname != "auto" && !is.logical(byname)) {
stop("`bynam' must be \"auto\", or logical")
cli::cli_abort("{.arg bynam} must be \"auto\", or \"logical\".")
}
nonamed <- sum(sapply(graphs, is_named))
if (byname == "auto") {
Expand All @@ -286,7 +286,7 @@ disjoint_union <- function(...) {
cli::cli_warn("Some, but not all graphs are named, not using vertex names.")
}
} else if (byname && nonamed != length(graphs)) {
stop("Some graphs are not named")
cli::cli_abort("Some graphs are not named.")
}

edgemaps <- length(unlist(lapply(graphs, edge_attr_names))) != 0
Expand Down Expand Up @@ -613,7 +613,7 @@ difference.igraph <- function(big, small, byname = "auto", ...) {
ensure_igraph(big)
ensure_igraph(small)
if (byname != "auto" && !is.logical(byname)) {
stop("`bynam' must be \"auto\", or logical")
cli::cli_abort("{.arg bynam} must be \"auto\", or \"logical\".")
}
nonamed <- is_named(big) + is_named(small)
if (byname == "auto") {
Expand All @@ -622,7 +622,7 @@ difference.igraph <- function(big, small, byname = "auto", ...) {
cli::cli_warn("One, but not both graphs are named, not using vertex names.")
}
} else if (byname && nonamed != 2) {
stop("Some graphs are not named")
cli::cli_abort("Some graphs are not named.")
}

if (byname) {
Expand Down Expand Up @@ -762,7 +762,7 @@ compose <- function(g1, g2, byname = "auto") {
ensure_igraph(g2)

if (byname != "auto" && !is.logical(byname)) {
stop("`byname' must be \"auto\", or logical")
cli::cli_abort("{.arg bynam} must be \"auto\", or \"logical\".")
}
nonamed <- is_named(g1) + is_named(g2)
if (byname == "auto") {
Expand All @@ -771,7 +771,7 @@ compose <- function(g1, g2, byname = "auto") {
cli::cli_warn("One, but not both graphs are named, not using vertex names.")
}
} else if (byname && nonamed != 2) {
stop("Some graphs are not named")
cli::cli_abort("Some graphs are not named.")
}

if (byname) {
Expand Down Expand Up @@ -1115,7 +1115,7 @@ path <- function(...) {
## Adding named vertices
res <- add_vertices(e1, length(e2), name = e2)
} else {
stop("Cannot add unknown type to igraph graph")
cli::cli_abort("Cannot add {.obj_type_friendly type} to igraph graph.")
}
res
}
Expand Down Expand Up @@ -1171,7 +1171,7 @@ path <- function(...) {
#' @export
`-.igraph` <- function(e1, e2) {
if (missing(e2)) {
stop("Non-numeric argument to negation operator")
cli::cli_abort("Non-numeric argument to negation operator")
}
if (is_igraph(e2)) {
res <- difference(e1, e2)
Expand All @@ -1195,7 +1195,7 @@ path <- function(...) {
} else if (is.numeric(e2) || is.character(e2)) {
res <- delete_vertices(e1, e2)
} else {
stop("Cannot substract unknown type from igraph graph")
cli::cli_abort("Cannot substract {.obj_type_friendly type} from igraph graph.")
}
res
}
Expand All @@ -1220,7 +1220,7 @@ path <- function(...) {
#' @examples
#' rings <- make_ring(5) * 5
rep.igraph <- function(x, n, mark = TRUE, ...) {
if (n < 0) stop("Number of replications must be positive")
if (n < 0) cli::cli_abort("Number of replications must be positive")

res <- do_call(disjoint_union,
.args =
Expand All @@ -1245,7 +1245,7 @@ rep.igraph <- function(x, n, mark = TRUE, ...) {
if (is.numeric(n) && length(n) == 1) {
rep.igraph(x, n)
} else {
stop("Cannot multiply igraph graph with this type")
cli::cli_abort("Cannot multiply igraph graph with {.obj_type_friendly type}.")
}
}

Expand All @@ -1263,7 +1263,7 @@ rep.igraph <- function(x, n, mark = TRUE, ...) {
#'
#' @examples
#'
#' g <- make_graph(~ 1 -+ 2, 2 -+ 3, 3 -+ 4)
#' g <- make_graph(~ 1 - +2, 2 - +3, 3 - +4)
#' reverse_edges(g, 2)
#' @family functions for manipulating graph structure
#' @export
Expand Down
File renamed without changes.
61 changes: 0 additions & 61 deletions tests/testthat/test-graph-ids.R

This file was deleted.

6 changes: 0 additions & 6 deletions tests/testthat/test-graph.complementer.R

This file was deleted.

10 changes: 0 additions & 10 deletions tests/testthat/test-graph.compose.R

This file was deleted.

22 changes: 0 additions & 22 deletions tests/testthat/test-identical_graphs.R

This file was deleted.

Loading

0 comments on commit 8064dea

Please sign in to comment.