From f53789e7d1addfe3518064f1ae13556fd7ae84b8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Oct 2025 21:27:22 +0000 Subject: [PATCH 1/5] Initial plan From 50ec62209dc57f33ed3e62a40f4c822c4a477fb7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Oct 2025 21:45:51 +0000 Subject: [PATCH 2/5] feat: warn when adding named vertices to unnamed graphs Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com> --- R/operators.R | 13 ++++++++ tests/testthat/test-operators.R | 54 +++++++++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/R/operators.R b/R/operators.R index 361775f32d3..f541b946481 100644 --- a/R/operators.R +++ b/R/operators.R @@ -1161,6 +1161,13 @@ path <- function(...) { e2[named] ) + # Warn if adding named vertices to an unnamed graph + if (!is.null(nn) && !is_named(e1)) { + cli::cli_warn( + "Adding named vertices to an 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)) { @@ -1188,6 +1195,12 @@ path <- function(...) { res <- add_vertices(e1, e2) } else if (is.character(e2)) { ## Adding named vertices + # Warn if adding named vertices to an unnamed graph + if (!is_named(e1)) { + cli::cli_warn( + "Adding named vertices to an 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.") diff --git a/tests/testthat/test-operators.R b/tests/testthat/test-operators.R index d38e3ae5a1a..16e3604d27b 100644 --- a/tests/testthat/test-operators.R +++ b/tests/testthat/test-operators.R @@ -214,24 +214,28 @@ test_that("t() is aliased to edge reversal for graphs", { }) test_that("vertices() works", { - g_all_unnamed <- make_empty_graph(1) + vertices("a", "b") + g_all_unnamed <- suppressWarnings(make_empty_graph(1) + vertices("a", "b")) expect_s3_class(V(g_all_unnamed), "igraph.vs") expect_identical(V(g_all_unnamed)$name, c(NA, "a", "b")) - g_mix_named_unnamed <- make_empty_graph(1) + vertices("a", "b", foo = 5) + g_mix_named_unnamed <- suppressWarnings( + make_empty_graph(1) + vertices("a", "b", foo = 5) + ) expect_s3_class(V(g_mix_named_unnamed), "igraph.vs") expect_true(is.na(V(g_mix_named_unnamed)$name[1])) expect_identical(V(g_mix_named_unnamed)$name[-1], c("a", "b")) expect_equal(V(g_mix_named_unnamed)$foo, c(NA, 5, 5)) - g_mix_bigger_attribute <- make_empty_graph(1) + - vertices("a", "b", "c", foo = 5:7, bar = 8) + g_mix_bigger_attribute <- suppressWarnings( + make_empty_graph(1) + + vertices("a", "b", "c", foo = 5:7, bar = 8) + ) expect_s3_class(V(g_mix_bigger_attribute), "igraph.vs") expect_identical(V(g_mix_bigger_attribute)$name, c(NA, "a", "b", "c")) expect_equal(V(g_mix_bigger_attribute)$foo, c(NA, 5, 6, 7)) expect_equal(V(g_mix_bigger_attribute)$bar, c(NA, 8, 8, 8)) - g_one_unnamed <- make_empty_graph(1) + vertices(letters) + g_one_unnamed <- suppressWarnings(make_empty_graph(1) + vertices(letters)) expect_s3_class(V(g_one_unnamed), "igraph.vs") expect_identical(V(g_one_unnamed)$name, c(NA, letters)) @@ -249,7 +253,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(suppressWarnings( + make_empty_graph(1) + vertices("a", "b", foo = 5:7) + )) }) test_that("vertices() errors on duplicate attribute names", { @@ -274,6 +280,42 @@ test_that("vertices() errors on duplicate attribute names", { ) }) +test_that("adding named vertices to unnamed graphs warns", { + # Test with vertex() function + expect_warning( + make_ring(10) + vertex(1), + "Adding named vertices to an unnamed graph" + ) + + expect_warning( + make_ring(10) + vertex("a"), + "Adding named vertices to an unnamed graph" + ) + + expect_warning( + make_ring(10) + vertices("a", "b"), + "Adding named vertices to an unnamed graph" + ) + + # Test with character vector + expect_warning( + make_ring(10) + c("a", "b"), + "Adding named vertices to an unnamed graph" + ) + + # No warning when adding to named graph + g <- make_ring(10) + V(g)$name <- letters[1:10] + expect_no_warning(g + vertex("k")) + expect_no_warning(g + c("x", "y")) + + # No warning when adding unnamed vertices + expect_no_warning(make_ring(10) + vertex(foo = 5)) + expect_no_warning(make_ring(10) + vertices(foo = 1:3)) + expect_no_warning(make_ring(10) + 5) +}) + + test_that("infix operators work", { g <- make_ring(10) V(g)$name <- letters[1:10] From 1ac70d55ed9f1bfe69e1093a98b7376efb1f6079 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Oct 2025 21:49:48 +0000 Subject: [PATCH 3/5] test: suppress expected warnings in tests Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com> --- tests/testthat/test-make.R | 16 ++++++++++------ tests/testthat/test-print.R | 2 +- tests/testthat/test-topology.R | 16 ++++++++++------ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/tests/testthat/test-make.R b/tests/testthat/test-make.R index eafcc645d31..264ec1612f4 100644 --- a/tests/testthat/test-make.R +++ b/tests/testthat/test-make.R @@ -231,17 +231,21 @@ test_that("make_graph works for numeric edges and isolates", { test_that("make_graph handles names", { graph_make_names <- make_graph(letters[1:10]) - graph_elist_names <- make_empty_graph() + - vertices(letters[1:10]) + - edges(letters[1:10]) + graph_elist_names <- suppressWarnings( + make_empty_graph() + + vertices(letters[1:10]) + + edges(letters[1:10]) + ) expect_identical_graphs(graph_make_names, graph_elist_names) }) test_that("make_graph handles names and isolates", { graph_make_iso <- make_graph(letters[1:10], isolates = letters[11:20]) - graph_elist_iso <- make_empty_graph() + - vertices(letters[1:20]) + - edges(letters[1:10]) + graph_elist_iso <- suppressWarnings( + make_empty_graph() + + vertices(letters[1:20]) + + edges(letters[1:10]) + ) expect_identical_graphs(graph_make_iso, graph_elist_iso) }) diff --git a/tests/testthat/test-print.R b/tests/testthat/test-print.R index eb880eed5ef..e6aa4cf2aeb 100644 --- a/tests/testthat/test-print.R +++ b/tests/testthat/test-print.R @@ -46,7 +46,7 @@ test_that("print.igraph() works", { }) expect_output(print(g6), " ") - kite <- make_empty_graph(directed = FALSE) + LETTERS[1:10] + kite <- suppressWarnings(make_empty_graph(directed = FALSE) + LETTERS[1:10]) kite <- kite + edges( "A", "B", diff --git a/tests/testthat/test-topology.R b/tests/testthat/test-topology.R index 12dec658e91..973e1f2a544 100644 --- a/tests/testthat/test-topology.R +++ b/tests/testthat/test-topology.R @@ -46,9 +46,11 @@ test_that("automorphism_group works with colored graphs", { }) test_that("isomorphisms() works", { - motif <- make_empty_graph(directed = FALSE) + - vertices("D1", "D2", type = c("type1", "type1")) + - edges("D1", "D2", type = c("type2")) + motif <- suppressWarnings( + make_empty_graph(directed = FALSE) + + vertices("D1", "D2", type = c("type1", "type1")) + + edges("D1", "D2", type = c("type2")) + ) motif_iso <- isomorphisms( motif, motif, @@ -61,9 +63,11 @@ test_that("isomorphisms() works", { }) test_that("subgraph_isomorphisms works", { - motif <- make_empty_graph(directed = FALSE) + - vertices("D1", "D2", type = c("type1", "type1")) + - edges("D1", "D2", type = c("type2")) + motif <- suppressWarnings( + make_empty_graph(directed = FALSE) + + vertices("D1", "D2", type = c("type1", "type1")) + + edges("D1", "D2", type = c("type2")) + ) out <- subgraph_isomorphisms( target = motif, pattern = motif, From 8723eee43d3d8a454f5ccaab0ebd0b5afe4388f7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Oct 2025 22:15:58 +0000 Subject: [PATCH 4/5] refactor: abort instead of warn, allow adding to empty graphs - Changed from cli_warn to cli_abort for non-empty unnamed graphs - Allow adding named vertices to graphs with no edges (empty graphs) - Added snapshot tests for error messages - Removed suppressWarnings from tests that should now work Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com> --- R/operators.R | 18 +++++---- tests/testthat/_snaps/operators.md | 16 ++++++++ tests/testthat/test-make.R | 16 +++----- tests/testthat/test-operators.R | 61 ++++++++++++++---------------- tests/testthat/test-print.R | 2 +- tests/testthat/test-topology.R | 16 +++----- 6 files changed, 68 insertions(+), 61 deletions(-) diff --git a/R/operators.R b/R/operators.R index f541b946481..4ef8bd55e26 100644 --- a/R/operators.R +++ b/R/operators.R @@ -1161,10 +1161,11 @@ path <- function(...) { e2[named] ) - # Warn if adding named vertices to an unnamed graph - if (!is.null(nn) && !is_named(e1)) { - cli::cli_warn( - "Adding named vertices to an unnamed graph. Existing vertices will have {.code NA} names." + # 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." ) } @@ -1195,10 +1196,11 @@ path <- function(...) { res <- add_vertices(e1, e2) } else if (is.character(e2)) { ## Adding named vertices - # Warn if adding named vertices to an unnamed graph - if (!is_named(e1)) { - cli::cli_warn( - "Adding named vertices to an unnamed graph. Existing vertices will have {.code NA} names." + # 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) diff --git a/tests/testthat/_snaps/operators.md b/tests/testthat/_snaps/operators.md index c60e2dc767a..f1074f1c5d7 100644 --- a/tests/testthat/_snaps/operators.md +++ b/tests/testthat/_snaps/operators.md @@ -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. + diff --git a/tests/testthat/test-make.R b/tests/testthat/test-make.R index 264ec1612f4..eafcc645d31 100644 --- a/tests/testthat/test-make.R +++ b/tests/testthat/test-make.R @@ -231,21 +231,17 @@ test_that("make_graph works for numeric edges and isolates", { test_that("make_graph handles names", { graph_make_names <- make_graph(letters[1:10]) - graph_elist_names <- suppressWarnings( - make_empty_graph() + - vertices(letters[1:10]) + - edges(letters[1:10]) - ) + graph_elist_names <- make_empty_graph() + + vertices(letters[1:10]) + + edges(letters[1:10]) expect_identical_graphs(graph_make_names, graph_elist_names) }) test_that("make_graph handles names and isolates", { graph_make_iso <- make_graph(letters[1:10], isolates = letters[11:20]) - graph_elist_iso <- suppressWarnings( - make_empty_graph() + - vertices(letters[1:20]) + - edges(letters[1:10]) - ) + graph_elist_iso <- make_empty_graph() + + vertices(letters[1:20]) + + edges(letters[1:10]) expect_identical_graphs(graph_make_iso, graph_elist_iso) }) diff --git a/tests/testthat/test-operators.R b/tests/testthat/test-operators.R index 16e3604d27b..faaf646a5d5 100644 --- a/tests/testthat/test-operators.R +++ b/tests/testthat/test-operators.R @@ -214,28 +214,24 @@ test_that("t() is aliased to edge reversal for graphs", { }) test_that("vertices() works", { - g_all_unnamed <- suppressWarnings(make_empty_graph(1) + vertices("a", "b")) + g_all_unnamed <- make_empty_graph(1) + vertices("a", "b") expect_s3_class(V(g_all_unnamed), "igraph.vs") expect_identical(V(g_all_unnamed)$name, c(NA, "a", "b")) - g_mix_named_unnamed <- suppressWarnings( - make_empty_graph(1) + vertices("a", "b", foo = 5) - ) + g_mix_named_unnamed <- make_empty_graph(1) + vertices("a", "b", foo = 5) expect_s3_class(V(g_mix_named_unnamed), "igraph.vs") expect_true(is.na(V(g_mix_named_unnamed)$name[1])) expect_identical(V(g_mix_named_unnamed)$name[-1], c("a", "b")) expect_equal(V(g_mix_named_unnamed)$foo, c(NA, 5, 5)) - g_mix_bigger_attribute <- suppressWarnings( - make_empty_graph(1) + - vertices("a", "b", "c", foo = 5:7, bar = 8) - ) + g_mix_bigger_attribute <- make_empty_graph(1) + + vertices("a", "b", "c", foo = 5:7, bar = 8) expect_s3_class(V(g_mix_bigger_attribute), "igraph.vs") expect_identical(V(g_mix_bigger_attribute)$name, c(NA, "a", "b", "c")) expect_equal(V(g_mix_bigger_attribute)$foo, c(NA, 5, 6, 7)) expect_equal(V(g_mix_bigger_attribute)$bar, c(NA, 8, 8, 8)) - g_one_unnamed <- suppressWarnings(make_empty_graph(1) + vertices(letters)) + g_one_unnamed <- make_empty_graph(1) + vertices(letters) expect_s3_class(V(g_one_unnamed), "igraph.vs") expect_identical(V(g_one_unnamed)$name, c(NA, letters)) @@ -253,9 +249,9 @@ test_that("vertices() works", { expect_s3_class(V(g_none), "igraph.vs") expect_null(V(g_none)$name) - expect_snapshot_error(suppressWarnings( + expect_snapshot_error( make_empty_graph(1) + vertices("a", "b", foo = 5:7) - )) + ) }) test_that("vertices() errors on duplicate attribute names", { @@ -280,39 +276,40 @@ test_that("vertices() errors on duplicate attribute names", { ) }) -test_that("adding named vertices to unnamed graphs warns", { +test_that("adding named vertices to non-empty unnamed graphs errors", { # Test with vertex() function - expect_warning( - make_ring(10) + vertex(1), - "Adding named vertices to an unnamed graph" + expect_snapshot_error( + make_ring(10) + vertex(1) ) - expect_warning( - make_ring(10) + vertex("a"), - "Adding named vertices to an unnamed graph" + expect_snapshot_error( + make_ring(10) + vertex("a") ) - expect_warning( - make_ring(10) + vertices("a", "b"), - "Adding named vertices to an unnamed graph" + expect_snapshot_error( + make_ring(10) + vertices("a", "b") ) # Test with character vector - expect_warning( - make_ring(10) + c("a", "b"), - "Adding named vertices to an unnamed graph" + expect_snapshot_error( + make_ring(10) + c("a", "b") ) - # No warning when adding to named graph + # No error when adding to named graph g <- make_ring(10) V(g)$name <- letters[1:10] - expect_no_warning(g + vertex("k")) - expect_no_warning(g + c("x", "y")) - - # No warning when adding unnamed vertices - expect_no_warning(make_ring(10) + vertex(foo = 5)) - expect_no_warning(make_ring(10) + vertices(foo = 1:3)) - expect_no_warning(make_ring(10) + 5) + 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")) }) diff --git a/tests/testthat/test-print.R b/tests/testthat/test-print.R index e6aa4cf2aeb..eb880eed5ef 100644 --- a/tests/testthat/test-print.R +++ b/tests/testthat/test-print.R @@ -46,7 +46,7 @@ test_that("print.igraph() works", { }) expect_output(print(g6), " ") - kite <- suppressWarnings(make_empty_graph(directed = FALSE) + LETTERS[1:10]) + kite <- make_empty_graph(directed = FALSE) + LETTERS[1:10] kite <- kite + edges( "A", "B", diff --git a/tests/testthat/test-topology.R b/tests/testthat/test-topology.R index 973e1f2a544..12dec658e91 100644 --- a/tests/testthat/test-topology.R +++ b/tests/testthat/test-topology.R @@ -46,11 +46,9 @@ test_that("automorphism_group works with colored graphs", { }) test_that("isomorphisms() works", { - motif <- suppressWarnings( - make_empty_graph(directed = FALSE) + - vertices("D1", "D2", type = c("type1", "type1")) + - edges("D1", "D2", type = c("type2")) - ) + motif <- make_empty_graph(directed = FALSE) + + vertices("D1", "D2", type = c("type1", "type1")) + + edges("D1", "D2", type = c("type2")) motif_iso <- isomorphisms( motif, motif, @@ -63,11 +61,9 @@ test_that("isomorphisms() works", { }) test_that("subgraph_isomorphisms works", { - motif <- suppressWarnings( - make_empty_graph(directed = FALSE) + - vertices("D1", "D2", type = c("type1", "type1")) + - edges("D1", "D2", type = c("type2")) - ) + motif <- make_empty_graph(directed = FALSE) + + vertices("D1", "D2", type = c("type1", "type1")) + + edges("D1", "D2", type = c("type2")) out <- subgraph_isomorphisms( target = motif, pattern = motif, From a4aa43d37bf9035b6a56c0d9428cd1cac1c87f49 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Oct 2025 22:35:42 +0000 Subject: [PATCH 5/5] fix: use vertex count instead of edge count for empty graph check - Changed check from ecount(e1) > 0 to vcount(e1) > 0 - Empty graphs are now correctly defined as having 0 vertices, not 0 edges - Updated tests to use make_empty_graph() instead of make_empty_graph(1) - Added tests for make_empty_graph(1) to verify it now errors correctly Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com> --- R/operators.R | 8 +++--- tests/testthat/_snaps/operators.md | 12 +++++++++ tests/testthat/test-operators.R | 39 +++++++++++++++++++----------- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/R/operators.R b/R/operators.R index 4ef8bd55e26..d65a1087b8a 100644 --- a/R/operators.R +++ b/R/operators.R @@ -1162,8 +1162,8 @@ path <- function(...) { ) # 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) { + # Empty graphs have zero vertices + if (!is.null(nn) && !is_named(e1) && vcount(e1) > 0) { cli::cli_abort( "Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have {.code NA} names." ) @@ -1197,8 +1197,8 @@ path <- function(...) { } 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) { + # Empty graphs have zero vertices + if (!is_named(e1) && vcount(e1) > 0) { cli::cli_abort( "Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have {.code NA} names." ) diff --git a/tests/testthat/_snaps/operators.md b/tests/testthat/_snaps/operators.md index f1074f1c5d7..a4c0f7e8eb4 100644 --- a/tests/testthat/_snaps/operators.md +++ b/tests/testthat/_snaps/operators.md @@ -2,6 +2,10 @@ Can't recycle `name` (size 2) to match `foo` (size 3). +--- + + Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names. + # vertices() errors on duplicate attribute names Duplicate attribute name in `vertices()`: "name". @@ -34,3 +38,11 @@ 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. + diff --git a/tests/testthat/test-operators.R b/tests/testthat/test-operators.R index faaf646a5d5..c8e48886a0f 100644 --- a/tests/testthat/test-operators.R +++ b/tests/testthat/test-operators.R @@ -214,27 +214,28 @@ test_that("t() is aliased to edge reversal for graphs", { }) test_that("vertices() works", { - g_all_unnamed <- make_empty_graph(1) + vertices("a", "b") + # Test adding named vertices to empty graph (0 vertices) + g_all_unnamed <- make_empty_graph() + vertices("a", "b") expect_s3_class(V(g_all_unnamed), "igraph.vs") - expect_identical(V(g_all_unnamed)$name, c(NA, "a", "b")) + expect_identical(V(g_all_unnamed)$name, c("a", "b")) - g_mix_named_unnamed <- make_empty_graph(1) + vertices("a", "b", foo = 5) + g_mix_named_unnamed <- make_empty_graph() + vertices("a", "b", foo = 5) expect_s3_class(V(g_mix_named_unnamed), "igraph.vs") - expect_true(is.na(V(g_mix_named_unnamed)$name[1])) - expect_identical(V(g_mix_named_unnamed)$name[-1], c("a", "b")) - expect_equal(V(g_mix_named_unnamed)$foo, c(NA, 5, 5)) + expect_identical(V(g_mix_named_unnamed)$name, c("a", "b")) + expect_equal(V(g_mix_named_unnamed)$foo, c(5, 5)) - g_mix_bigger_attribute <- make_empty_graph(1) + + g_mix_bigger_attribute <- make_empty_graph() + vertices("a", "b", "c", foo = 5:7, bar = 8) expect_s3_class(V(g_mix_bigger_attribute), "igraph.vs") - expect_identical(V(g_mix_bigger_attribute)$name, c(NA, "a", "b", "c")) - expect_equal(V(g_mix_bigger_attribute)$foo, c(NA, 5, 6, 7)) - expect_equal(V(g_mix_bigger_attribute)$bar, c(NA, 8, 8, 8)) + expect_identical(V(g_mix_bigger_attribute)$name, c("a", "b", "c")) + expect_equal(V(g_mix_bigger_attribute)$foo, c(5, 6, 7)) + expect_equal(V(g_mix_bigger_attribute)$bar, c(8, 8, 8)) - g_one_unnamed <- make_empty_graph(1) + vertices(letters) + g_one_unnamed <- make_empty_graph() + vertices(letters) expect_s3_class(V(g_one_unnamed), "igraph.vs") - expect_identical(V(g_one_unnamed)$name, c(NA, letters)) + expect_identical(V(g_one_unnamed)$name, letters) + # Test adding unnamed vertices (attributes only) - should work with any graph g_all_named <- make_empty_graph(1) + vertices(foo = 5:7) expect_s3_class(V(g_all_named), "igraph.vs") expect_null(V(g_all_named)$name) @@ -249,8 +250,14 @@ test_that("vertices() works", { expect_s3_class(V(g_none), "igraph.vs") expect_null(V(g_none)$name) + # Test that adding named vertices to non-empty graph with mismatched attributes errors expect_snapshot_error( - make_empty_graph(1) + vertices("a", "b", foo = 5:7) + make_empty_graph() + vertices("a", "b", foo = 5:7) + ) + + # Test that adding named vertices to non-empty unnamed graph errors + expect_snapshot_error( + make_empty_graph(1) + vertices("a", "b") ) }) @@ -306,10 +313,14 @@ test_that("adding named vertices to non-empty unnamed graphs errors", { expect_no_error(make_ring(10) + vertices(foo = 1:3)) expect_no_error(make_ring(10) + 5) - # No error when adding to empty graph + # No error when adding to empty graph (0 vertices) 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")) + + # Error when adding to non-empty unnamed graph (even with 1 vertex) + expect_snapshot_error(make_empty_graph(1) + vertex("a")) + expect_snapshot_error(make_empty_graph(1) + c("a", "b")) })