Skip to content

Commit 8723eee

Browse files
Copilotkrlmlr
andcommitted
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>
1 parent 1ac70d5 commit 8723eee

File tree

6 files changed

+68
-61
lines changed

6 files changed

+68
-61
lines changed

R/operators.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,10 +1161,11 @@ path <- function(...) {
11611161
e2[named]
11621162
)
11631163

1164-
# Warn if adding named vertices to an unnamed graph
1165-
if (!is.null(nn) && !is_named(e1)) {
1166-
cli::cli_warn(
1167-
"Adding named vertices to an unnamed graph. Existing vertices will have {.code NA} names."
1164+
# Abort if adding named vertices to a non-empty unnamed graph
1165+
# Allow adding to graphs with no edges (like make_empty_graph(n))
1166+
if (!is.null(nn) && !is_named(e1) && ecount(e1) > 0) {
1167+
cli::cli_abort(
1168+
"Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have {.code NA} names."
11681169
)
11691170
}
11701171

@@ -1195,10 +1196,11 @@ path <- function(...) {
11951196
res <- add_vertices(e1, e2)
11961197
} else if (is.character(e2)) {
11971198
## Adding named vertices
1198-
# Warn if adding named vertices to an unnamed graph
1199-
if (!is_named(e1)) {
1200-
cli::cli_warn(
1201-
"Adding named vertices to an unnamed graph. Existing vertices will have {.code NA} names."
1199+
# Abort if adding named vertices to a non-empty unnamed graph
1200+
# Allow adding to graphs with no edges (like make_empty_graph(n))
1201+
if (!is_named(e1) && ecount(e1) > 0) {
1202+
cli::cli_abort(
1203+
"Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have {.code NA} names."
12021204
)
12031205
}
12041206
res <- add_vertices(e1, length(e2), name = e2)

tests/testthat/_snaps/operators.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,19 @@
1818

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

21+
# adding named vertices to non-empty unnamed graphs errors
22+
23+
Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.
24+
25+
---
26+
27+
Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.
28+
29+
---
30+
31+
Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.
32+
33+
---
34+
35+
Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.
36+

tests/testthat/test-make.R

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,17 @@ test_that("make_graph works for numeric edges and isolates", {
231231

232232
test_that("make_graph handles names", {
233233
graph_make_names <- make_graph(letters[1:10])
234-
graph_elist_names <- suppressWarnings(
235-
make_empty_graph() +
236-
vertices(letters[1:10]) +
237-
edges(letters[1:10])
238-
)
234+
graph_elist_names <- make_empty_graph() +
235+
vertices(letters[1:10]) +
236+
edges(letters[1:10])
239237
expect_identical_graphs(graph_make_names, graph_elist_names)
240238
})
241239

242240
test_that("make_graph handles names and isolates", {
243241
graph_make_iso <- make_graph(letters[1:10], isolates = letters[11:20])
244-
graph_elist_iso <- suppressWarnings(
245-
make_empty_graph() +
246-
vertices(letters[1:20]) +
247-
edges(letters[1:10])
248-
)
242+
graph_elist_iso <- make_empty_graph() +
243+
vertices(letters[1:20]) +
244+
edges(letters[1:10])
249245
expect_identical_graphs(graph_make_iso, graph_elist_iso)
250246
})
251247

tests/testthat/test-operators.R

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -214,28 +214,24 @@ test_that("t() is aliased to edge reversal for graphs", {
214214
})
215215

216216
test_that("vertices() works", {
217-
g_all_unnamed <- suppressWarnings(make_empty_graph(1) + vertices("a", "b"))
217+
g_all_unnamed <- make_empty_graph(1) + vertices("a", "b")
218218
expect_s3_class(V(g_all_unnamed), "igraph.vs")
219219
expect_identical(V(g_all_unnamed)$name, c(NA, "a", "b"))
220220

221-
g_mix_named_unnamed <- suppressWarnings(
222-
make_empty_graph(1) + vertices("a", "b", foo = 5)
223-
)
221+
g_mix_named_unnamed <- make_empty_graph(1) + vertices("a", "b", foo = 5)
224222
expect_s3_class(V(g_mix_named_unnamed), "igraph.vs")
225223
expect_true(is.na(V(g_mix_named_unnamed)$name[1]))
226224
expect_identical(V(g_mix_named_unnamed)$name[-1], c("a", "b"))
227225
expect_equal(V(g_mix_named_unnamed)$foo, c(NA, 5, 5))
228226

229-
g_mix_bigger_attribute <- suppressWarnings(
230-
make_empty_graph(1) +
231-
vertices("a", "b", "c", foo = 5:7, bar = 8)
232-
)
227+
g_mix_bigger_attribute <- make_empty_graph(1) +
228+
vertices("a", "b", "c", foo = 5:7, bar = 8)
233229
expect_s3_class(V(g_mix_bigger_attribute), "igraph.vs")
234230
expect_identical(V(g_mix_bigger_attribute)$name, c(NA, "a", "b", "c"))
235231
expect_equal(V(g_mix_bigger_attribute)$foo, c(NA, 5, 6, 7))
236232
expect_equal(V(g_mix_bigger_attribute)$bar, c(NA, 8, 8, 8))
237233

238-
g_one_unnamed <- suppressWarnings(make_empty_graph(1) + vertices(letters))
234+
g_one_unnamed <- make_empty_graph(1) + vertices(letters)
239235
expect_s3_class(V(g_one_unnamed), "igraph.vs")
240236
expect_identical(V(g_one_unnamed)$name, c(NA, letters))
241237

@@ -253,9 +249,9 @@ test_that("vertices() works", {
253249
expect_s3_class(V(g_none), "igraph.vs")
254250
expect_null(V(g_none)$name)
255251

256-
expect_snapshot_error(suppressWarnings(
252+
expect_snapshot_error(
257253
make_empty_graph(1) + vertices("a", "b", foo = 5:7)
258-
))
254+
)
259255
})
260256

261257
test_that("vertices() errors on duplicate attribute names", {
@@ -280,39 +276,40 @@ test_that("vertices() errors on duplicate attribute names", {
280276
)
281277
})
282278

283-
test_that("adding named vertices to unnamed graphs warns", {
279+
test_that("adding named vertices to non-empty unnamed graphs errors", {
284280
# Test with vertex() function
285-
expect_warning(
286-
make_ring(10) + vertex(1),
287-
"Adding named vertices to an unnamed graph"
281+
expect_snapshot_error(
282+
make_ring(10) + vertex(1)
288283
)
289284

290-
expect_warning(
291-
make_ring(10) + vertex("a"),
292-
"Adding named vertices to an unnamed graph"
285+
expect_snapshot_error(
286+
make_ring(10) + vertex("a")
293287
)
294288

295-
expect_warning(
296-
make_ring(10) + vertices("a", "b"),
297-
"Adding named vertices to an unnamed graph"
289+
expect_snapshot_error(
290+
make_ring(10) + vertices("a", "b")
298291
)
299292

300293
# Test with character vector
301-
expect_warning(
302-
make_ring(10) + c("a", "b"),
303-
"Adding named vertices to an unnamed graph"
294+
expect_snapshot_error(
295+
make_ring(10) + c("a", "b")
304296
)
305297

306-
# No warning when adding to named graph
298+
# No error when adding to named graph
307299
g <- make_ring(10)
308300
V(g)$name <- letters[1:10]
309-
expect_no_warning(g + vertex("k"))
310-
expect_no_warning(g + c("x", "y"))
311-
312-
# No warning when adding unnamed vertices
313-
expect_no_warning(make_ring(10) + vertex(foo = 5))
314-
expect_no_warning(make_ring(10) + vertices(foo = 1:3))
315-
expect_no_warning(make_ring(10) + 5)
301+
expect_no_error(g + vertex("k"))
302+
expect_no_error(g + c("x", "y"))
303+
304+
# No error when adding unnamed vertices
305+
expect_no_error(make_ring(10) + vertex(foo = 5))
306+
expect_no_error(make_ring(10) + vertices(foo = 1:3))
307+
expect_no_error(make_ring(10) + 5)
308+
309+
# No error when adding to empty graph
310+
expect_no_error(make_empty_graph() + vertex("a"))
311+
expect_no_error(make_empty_graph() + c("a", "b"))
312+
expect_no_error(make_empty_graph() + vertices("a", "b"))
316313
})
317314

318315

tests/testthat/test-print.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test_that("print.igraph() works", {
4646
})
4747
expect_output(print(g6), " ")
4848

49-
kite <- suppressWarnings(make_empty_graph(directed = FALSE) + LETTERS[1:10])
49+
kite <- make_empty_graph(directed = FALSE) + LETTERS[1:10]
5050
kite <- kite +
5151
edges(
5252
"A", "B",

tests/testthat/test-topology.R

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ test_that("automorphism_group works with colored graphs", {
4646
})
4747

4848
test_that("isomorphisms() works", {
49-
motif <- suppressWarnings(
50-
make_empty_graph(directed = FALSE) +
51-
vertices("D1", "D2", type = c("type1", "type1")) +
52-
edges("D1", "D2", type = c("type2"))
53-
)
49+
motif <- make_empty_graph(directed = FALSE) +
50+
vertices("D1", "D2", type = c("type1", "type1")) +
51+
edges("D1", "D2", type = c("type2"))
5452
motif_iso <- isomorphisms(
5553
motif,
5654
motif,
@@ -63,11 +61,9 @@ test_that("isomorphisms() works", {
6361
})
6462

6563
test_that("subgraph_isomorphisms works", {
66-
motif <- suppressWarnings(
67-
make_empty_graph(directed = FALSE) +
68-
vertices("D1", "D2", type = c("type1", "type1")) +
69-
edges("D1", "D2", type = c("type2"))
70-
)
64+
motif <- make_empty_graph(directed = FALSE) +
65+
vertices("D1", "D2", type = c("type1", "type1")) +
66+
edges("D1", "D2", type = c("type2"))
7167
out <- subgraph_isomorphisms(
7268
target = motif,
7369
pattern = motif,

0 commit comments

Comments
 (0)