Skip to content

Commit 5a93c98

Browse files
Copilotkrlmlr
andauthored
refactor: standardize error snapshot testing with expect_snapshot_igraph_error() (#2528)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
1 parent 9e4fb9e commit 5a93c98

25 files changed

Lines changed: 124 additions & 102 deletions

src/rinterface_extra.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7760,13 +7760,6 @@ SEXP Rx_igraph_create_bipartite(SEXP types, SEXP edges, SEXP directed) {
77607760
return R_igraph_create_bipartite(types, edges, directed);
77617761
}
77627762

7763-
/* Test function to verify error formatting with file and line information */
7764-
attribute_visible SEXP Rx_igraph_test_error_with_source(void) {
7765-
igraph_errorf("Test error message for verifying source location formatting",
7766-
__FILE__, __LINE__, IGRAPH_EINVAL);
7767-
return R_NilValue;
7768-
}
7769-
77707763
SEXP Rx_igraph_finalizer(void) {
77717764
return R_igraph_finalizer();
77727765
}

src/sources-glue-c.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
GLUE_C_SOURCES=rcallback.o rinterface.o rinterface_extra.o rrandom.o uuid.o
1+
GLUE_C_SOURCES=rcallback.o rinterface.o rinterface_extra.o rrandom.o test_error_with_source.o uuid.o

src/test_error_with_source.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* -*- mode: C -*- */
2+
/*
3+
IGraph library R interface.
4+
Copyright (C) 2013 Gabor Csardi <csardi.gabor@gmail.com>
5+
334 Harvard street, Cambridge, MA 02139 USA
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation; either version 2 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20+
02110-1301 USA
21+
22+
*/
23+
24+
#include "rinterface.h"
25+
26+
#include <R_ext/Visibility.h>
27+
28+
/* Test function to verify error formatting with file and line information */
29+
attribute_visible SEXP Rx_igraph_test_error_with_source(void) {
30+
igraph_errorf("Test error message for verifying source location formatting",
31+
__FILE__, __LINE__, IGRAPH_EINVAL);
32+
return R_NilValue;
33+
}

src/test_error_with_source.dd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Generated by deps.mk, do not edit by hand and do not add dependencies to system headers
2+
test_error_with_source.o: \
3+
rinterface.h \
4+
test_error_with_source.c \

tests/testthat/_snaps/error-formatting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
Condition
66
Error in `test_error_with_source()`:
77
! Test error message for verifying source location formatting. Invalid value
8-
Source: rinterface_extra.c:7766
8+
Source: test_error_with_source.c:31
99

tests/testthat/test-adjacency.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ test_that("sparse/dense matrices min/max/plus", {
787787

788788
test_that("graph_from_adjacency_matrix errors for NAs", {
789789
A <- matrix(c(1, 1, NA, 1), 2, 2)
790-
expect_snapshot(graph_from_adjacency_matrix(A), error = TRUE)
790+
expect_snapshot_igraph_error(graph_from_adjacency_matrix(A))
791791
})
792792

793793
test_that("graph_from_adjacency_matrix handles add.colnames and add.rownames = FALSE correctly", {

tests/testthat/test-attributes.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,13 @@ test_that("adding and removing attributes", {
436436

437437
test_that("error messages work", {
438438
g <- make_full_graph(5)
439-
expect_snapshot(set_vertex_attr(g, "test", value = c(1, 2)), error = TRUE)
440-
expect_snapshot(set_edge_attr(g, "test", value = c(1, 2)), error = TRUE)
441-
expect_snapshot(delete_graph_attr(g, "a"), error = TRUE)
442-
expect_snapshot(delete_vertex_attr(g, "a"), error = TRUE)
443-
expect_snapshot(delete_edge_attr(g, "a"), error = TRUE)
444-
expect_snapshot(assert_named_list("a"), error = TRUE)
445-
expect_snapshot(assert_named_list(list("a", "b")), error = TRUE)
439+
expect_snapshot_igraph_error(set_vertex_attr(g, "test", value = c(1, 2)))
440+
expect_snapshot_igraph_error(set_edge_attr(g, "test", value = c(1, 2)))
441+
expect_snapshot_igraph_error(delete_graph_attr(g, "a"))
442+
expect_snapshot_igraph_error(delete_vertex_attr(g, "a"))
443+
expect_snapshot_igraph_error(delete_edge_attr(g, "a"))
444+
expect_snapshot_igraph_error(assert_named_list("a"))
445+
expect_snapshot_igraph_error(assert_named_list(list("a", "b")))
446446
})
447447

448448
test_that("empty returns work", {
@@ -465,7 +465,7 @@ test_that("assign data.frame attributes works", {
465465

466466
test_that("good error message when not using character", {
467467
ring <- graph_from_literal(A - B - C - D - E - F - G - A)
468-
expect_snapshot(error = TRUE, {
468+
expect_snapshot_igraph_error({
469469
set_graph_attr(ring, 1, 1)
470470
})
471471
})
@@ -476,7 +476,7 @@ test_that("set_vertex_attrs() works", {
476476
expect_equal(V(g)$color, rep("blue", vcount(g)))
477477
expect_equal(V(g)$size, rep(10, vcount(g)))
478478
expect_equal(V(g)$name, LETTERS[1:10])
479-
expect_snapshot(error = TRUE, {
479+
expect_snapshot_igraph_error({
480480
set_vertex_attrs(g)
481481
})
482482

tests/testthat/test-centrality.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ test_that("arpack() errors well", {
848848
expect_snapshot_igraph_error({
849849
arpack(f, options = list(nev = 2, ncv = 4), sym = TRUE)
850850
})
851-
expect_snapshot(error = TRUE, {
851+
expect_snapshot_igraph_error({
852852
arpack(
853853
f,
854854
options = list(unknown_thing1 = 2, unknown_thing2 = 4),

tests/testthat/test-conversion.R

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ test_that("as_adjacency_matrix() works -- sparse + not both", {
142142

143143
test_that("as_adjacency_matrix() errors well -- sparse", {
144144
g <- make_graph(c(1, 2, 2, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4, 2, 4, 2, 4, 2), directed = TRUE)
145-
expect_snapshot(as_adjacency_matrix(g, attr = "bla"), error = TRUE)
145+
expect_snapshot_igraph_error(as_adjacency_matrix(g, attr = "bla"))
146146

147147
E(g)$bla <- letters[1:ecount(g)]
148-
expect_snapshot(as_adjacency_matrix(g, attr = "bla"), error = TRUE)
148+
expect_snapshot_igraph_error(as_adjacency_matrix(g, attr = "bla"))
149149
})
150150

151151
test_that("as_adjacency_matrix() works -- sparse undirected", {
@@ -197,15 +197,13 @@ test_that("as_adjacency_matrix() works -- dense", {
197197

198198
test_that("as_adjacency_matrix() errors well -- dense", {
199199
g <- make_graph(c(1, 2, 2, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4, 2, 4, 2, 4, 2), directed = TRUE)
200-
expect_snapshot(
201-
as_adjacency_matrix(g, attr = "bla", sparse = FALSE),
202-
error = TRUE
200+
expect_snapshot_igraph_error(
201+
as_adjacency_matrix(g, attr = "bla", sparse = FALSE)
203202
)
204203

205204
E(g)$bla <- letters[1:ecount(g)]
206-
expect_snapshot(
207-
as_adjacency_matrix(g, attr = "bla", sparse = FALSE),
208-
error = TRUE
205+
expect_snapshot_igraph_error(
206+
as_adjacency_matrix(g, attr = "bla", sparse = FALSE)
209207
)
210208
})
211209

@@ -679,7 +677,7 @@ test_that("edge names work", {
679677

680678
test_that("graph_from_edgelist errors for NAs", {
681679
A <- matrix(c(1, 2, NA, 1), 2, 2)
682-
expect_snapshot(graph_from_edgelist(A), error = TRUE)
680+
expect_snapshot_igraph_error(graph_from_edgelist(A))
683681
})
684682

685683
test_that("graph_from_data_frame works with factors", {

tests/testthat/test-error-formatting.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
test_that("error messages include source file and line information", {
2+
# Note: We use expect_snapshot(error = TRUE) here instead of
3+
# expect_snapshot_igraph_error() because we need to test the actual
4+
# line numbers in the error message, which expect_snapshot_igraph_error()
5+
# would transform to "xx".
26
expect_snapshot(
37
error = TRUE,
48
{

0 commit comments

Comments
 (0)