Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions R/operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,14 @@ path <- function(...) {
e2[named]
)

# Abort if adding named vertices to a non-empty unnamed graph
# Allow adding to graphs with no edges (like make_empty_graph(n))
if (!is.null(nn) && !is_named(e1) && ecount(e1) > 0) {
cli::cli_abort(
"Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have {.code NA} names."
)
}

# When adding vertices via +, all unnamed arguments are interpreted as vertex names of the new vertices.
res <- add_vertices(e1, nv = vctrs::vec_size_common(!!!e2), attr = e2)
} else if ("igraph.path" %in% class(e2)) {
Expand Down Expand Up @@ -1188,6 +1196,13 @@ path <- function(...) {
res <- add_vertices(e1, e2)
} else if (is.character(e2)) {
## Adding named vertices
# Abort if adding named vertices to a non-empty unnamed graph
# Allow adding to graphs with no edges (like make_empty_graph(n))
if (!is_named(e1) && ecount(e1) > 0) {
cli::cli_abort(
"Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have {.code NA} names."
)
}
res <- add_vertices(e1, length(e2), name = e2)
} else {
cli::cli_abort("Cannot add {.obj_type_friendly {type}} to igraph graph.")
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@

Duplicate attribute names in `vertices()`: "foo" and "bar".

# adding named vertices to non-empty unnamed graphs errors

Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.

---

Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.

---

Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.

---

Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.

41 changes: 40 additions & 1 deletion tests/testthat/test-operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ test_that("vertices() works", {
expect_s3_class(V(g_none), "igraph.vs")
expect_null(V(g_none)$name)

expect_snapshot_error(make_empty_graph(1) + vertices("a", "b", foo = 5:7))
expect_snapshot_error(
make_empty_graph(1) + vertices("a", "b", foo = 5:7)
)
})

test_that("vertices() errors on duplicate attribute names", {
Expand All @@ -274,6 +276,43 @@ test_that("vertices() errors on duplicate attribute names", {
)
})

test_that("adding named vertices to non-empty unnamed graphs errors", {
# Test with vertex() function
expect_snapshot_error(
make_ring(10) + vertex(1)
)

expect_snapshot_error(
make_ring(10) + vertex("a")
)

expect_snapshot_error(
make_ring(10) + vertices("a", "b")
)

# Test with character vector
expect_snapshot_error(
make_ring(10) + c("a", "b")
)

# No error when adding to named graph
g <- make_ring(10)
V(g)$name <- letters[1:10]
expect_no_error(g + vertex("k"))
expect_no_error(g + c("x", "y"))

# No error when adding unnamed vertices
expect_no_error(make_ring(10) + vertex(foo = 5))
expect_no_error(make_ring(10) + vertices(foo = 1:3))
expect_no_error(make_ring(10) + 5)

# No error when adding to empty graph
expect_no_error(make_empty_graph() + vertex("a"))
expect_no_error(make_empty_graph() + c("a", "b"))
expect_no_error(make_empty_graph() + vertices("a", "b"))
})


test_that("infix operators work", {
g <- make_ring(10)
V(g)$name <- letters[1:10]
Expand Down
Loading