From 9fe5b8d4c32955bc69382f4c80fa38ac234f7e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 24 Feb 2025 09:45:31 +0100 Subject: [PATCH 1/8] test: merge all tests for `foreign.R` into one test file --- tests/testthat/{test-read_graph.R => test-foreign.R} | 12 ++++++++++++ tests/testthat/test-pajek.R | 11 ----------- 2 files changed, 12 insertions(+), 11 deletions(-) rename tests/testthat/{test-read_graph.R => test-foreign.R} (54%) delete mode 100644 tests/testthat/test-pajek.R diff --git a/tests/testthat/test-read_graph.R b/tests/testthat/test-foreign.R similarity index 54% rename from tests/testthat/test-read_graph.R rename to tests/testthat/test-foreign.R index 2f671f872e..169eae2f7c 100644 --- a/tests/testthat/test-read_graph.R +++ b/tests/testthat/test-foreign.R @@ -1,3 +1,15 @@ +test_that("writing Pajek files works", { + g <- make_ring(9) + V(g)$color <- rep(c("red", "green", "yellow"), 3) + + tc <- rawConnection(raw(0), "w") + write_graph(g, format = "pajek", file = tc) + out <- rawToChar(rawConnectionValue(tc)) + close(tc) + + expect_equal(out, "*Vertices 9\n1 \"1\" ic \"red\"\n2 \"2\" ic \"green\"\n3 \"3\" ic \"yellow\"\n4 \"4\" ic \"red\"\n5 \"5\" ic \"green\"\n6 \"6\" ic \"yellow\"\n7 \"7\" ic \"red\"\n8 \"8\" ic \"green\"\n9 \"9\" ic \"yellow\"\n*Edges\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n1 9\n") +}) + test_that("reading GraphML file works", { skip_if_no_graphml() diff --git a/tests/testthat/test-pajek.R b/tests/testthat/test-pajek.R deleted file mode 100644 index 8a44c41f97..0000000000 --- a/tests/testthat/test-pajek.R +++ /dev/null @@ -1,11 +0,0 @@ -test_that("writing Pajek files works", { - g <- make_ring(9) - V(g)$color <- rep(c("red", "green", "yellow"), 3) - - tc <- rawConnection(raw(0), "w") - write_graph(g, format = "pajek", file = tc) - out <- rawToChar(rawConnectionValue(tc)) - close(tc) - - expect_equal(out, "*Vertices 9\n1 \"1\" ic \"red\"\n2 \"2\" ic \"green\"\n3 \"3\" ic \"yellow\"\n4 \"4\" ic \"red\"\n5 \"5\" ic \"green\"\n6 \"6\" ic \"yellow\"\n7 \"7\" ic \"red\"\n8 \"8\" ic \"green\"\n9 \"9\" ic \"yellow\"\n*Edges\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n1 9\n") -}) From c19d47d776d5c4f3cf198cb738c0f839e6cc4b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 24 Feb 2025 09:47:13 +0100 Subject: [PATCH 2/8] refactor: use `rep_len()` -- more readable? --- tests/testthat/test-foreign.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-foreign.R b/tests/testthat/test-foreign.R index 169eae2f7c..0798df75f1 100644 --- a/tests/testthat/test-foreign.R +++ b/tests/testthat/test-foreign.R @@ -1,6 +1,6 @@ test_that("writing Pajek files works", { g <- make_ring(9) - V(g)$color <- rep(c("red", "green", "yellow"), 3) + V(g)$color <- rep_len(c("red", "green", "yellow"), length.out = 9) tc <- rawConnection(raw(0), "w") write_graph(g, format = "pajek", file = tc) From a4a56c90203fff25d64df00f5a14b2ab86f3cad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 24 Feb 2025 09:47:58 +0100 Subject: [PATCH 3/8] chore: multiline --- tests/testthat/test-foreign.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-foreign.R b/tests/testthat/test-foreign.R index 0798df75f1..d33ccbb0bf 100644 --- a/tests/testthat/test-foreign.R +++ b/tests/testthat/test-foreign.R @@ -7,7 +7,10 @@ test_that("writing Pajek files works", { out <- rawToChar(rawConnectionValue(tc)) close(tc) - expect_equal(out, "*Vertices 9\n1 \"1\" ic \"red\"\n2 \"2\" ic \"green\"\n3 \"3\" ic \"yellow\"\n4 \"4\" ic \"red\"\n5 \"5\" ic \"green\"\n6 \"6\" ic \"yellow\"\n7 \"7\" ic \"red\"\n8 \"8\" ic \"green\"\n9 \"9\" ic \"yellow\"\n*Edges\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n1 9\n") + expect_equal( + out, + "*Vertices 9\n1 \"1\" ic \"red\"\n2 \"2\" ic \"green\"\n3 \"3\" ic \"yellow\"\n4 \"4\" ic \"red\"\n5 \"5\" ic \"green\"\n6 \"6\" ic \"yellow\"\n7 \"7\" ic \"red\"\n8 \"8\" ic \"green\"\n9 \"9\" ic \"yellow\"\n*Edges\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n1 9\n" + ) }) test_that("reading GraphML file works", { From ed8ea42206d8af2b6d746520844d679a19229707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 24 Feb 2025 09:51:52 +0100 Subject: [PATCH 4/8] refactor: use `withr::local_connection()` --- tests/testthat/test-foreign.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/testthat/test-foreign.R b/tests/testthat/test-foreign.R index d33ccbb0bf..f48f569aa7 100644 --- a/tests/testthat/test-foreign.R +++ b/tests/testthat/test-foreign.R @@ -2,10 +2,9 @@ test_that("writing Pajek files works", { g <- make_ring(9) V(g)$color <- rep_len(c("red", "green", "yellow"), length.out = 9) - tc <- rawConnection(raw(0), "w") + tc <- withr::local_connection(rawConnection(raw(0), "w")) write_graph(g, format = "pajek", file = tc) out <- rawToChar(rawConnectionValue(tc)) - close(tc) expect_equal( out, From 6c8d9a0429d3c41cc2b088c8e5648930ea360fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 24 Feb 2025 09:52:13 +0100 Subject: [PATCH 5/8] chore: simpler --- tests/testthat/test-foreign.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-foreign.R b/tests/testthat/test-foreign.R index f48f569aa7..78c75161d1 100644 --- a/tests/testthat/test-foreign.R +++ b/tests/testthat/test-foreign.R @@ -3,11 +3,11 @@ test_that("writing Pajek files works", { V(g)$color <- rep_len(c("red", "green", "yellow"), length.out = 9) tc <- withr::local_connection(rawConnection(raw(0), "w")) + write_graph(g, format = "pajek", file = tc) - out <- rawToChar(rawConnectionValue(tc)) expect_equal( - out, + rawToChar(rawConnectionValue(tc)), "*Vertices 9\n1 \"1\" ic \"red\"\n2 \"2\" ic \"green\"\n3 \"3\" ic \"yellow\"\n4 \"4\" ic \"red\"\n5 \"5\" ic \"green\"\n6 \"6\" ic \"yellow\"\n7 \"7\" ic \"red\"\n8 \"8\" ic \"green\"\n9 \"9\" ic \"yellow\"\n*Edges\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n1 9\n" ) }) From 879a9ca3d4d7045f0c817ac2faeb3736acb0904f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 24 Feb 2025 09:56:39 +0100 Subject: [PATCH 6/8] test: isomorphic expectations helpers --- tests/testthat/helper.R | 8 ++++++-- tests/testthat/test-components.R | 6 +++--- tests/testthat/test-conversion.R | 8 ++++---- tests/testthat/test-foreign.R | 2 +- tests/testthat/test-isomorphism.R | 16 ++++++++-------- tests/testthat/test-operators.R | 2 +- tests/testthat/test-trees.R | 8 ++++---- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index c0d7e6309c..03a7eabe58 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -20,8 +20,12 @@ local_rng_version <- function(version, .local_envir = parent.frame()) { orig } -expect_isomorphic <- function(g1, g2) { - expect_true(isomorphic(g1, g2)) +expect_isomorphic <- function(g1, g2, ...) { + expect_true(isomorphic(g1, g2, ...)) +} + +expect_not_isomorphic <- function(g1, g2, ...) { + expect_false(isomorphic(g1, g2, ...)) } expect_vcount <- function(graph, expected, ...) { diff --git a/tests/testthat/test-components.R b/tests/testthat/test-components.R index 50733ffd31..a30bde3338 100644 --- a/tests/testthat/test-components.R +++ b/tests/testthat/test-components.R @@ -111,7 +111,7 @@ test_that("largest strongly and weakly components are correct", { B - +C, C - +A ) - expect_true(isomorphic(largest_component(g, "strong"), strongly)) + expect_isomorphic(largest_component(g, "strong"), strongly) weakly <- graph_from_literal( A - +B, @@ -119,13 +119,13 @@ test_that("largest strongly and weakly components are correct", { C - +A, C - +D ) - expect_true(isomorphic(largest_component(g, "weak"), weakly)) + expect_isomorphic(largest_component(g, "weak"), weakly) }) test_that("the largest component of a null graph is a valid null graph", { nullgraph <- make_empty_graph(0) - expect_true(isomorphic(largest_component(make_empty_graph(0)), nullgraph)) + expect_isomorphic(largest_component(make_empty_graph(0)), nullgraph) }) test_that("articulation_points works", { diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 93016bde07..f127975035 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -306,11 +306,11 @@ test_that("as_adj_list works", { expect_s3_class(adj_list[[1]], "igraph.vs") g_same <- graph_from_adj_list(adj_list, mode = "all") expect_isomorphic(g, g_same) - expect_true(isomorphic(g, g_same, + expect_isomorphic(g, g_same, method = "vf2", vertex.color1 = seq_len(vcount(g)), vertex.color2 = seq_len(vcount(g_same)) - )) + ) adj_el_list <- as_adj_edge_list(g) expect_s3_class(adj_el_list[[1]], "igraph.es") @@ -345,11 +345,11 @@ test_that("as_adj_list works when return.vs.es is FALSE", { expect_s3_class(adj_list[[1]], NA) g2 <- graph_from_adj_list(adj_list, mode = "all") expect_isomorphic(g, g2) - expect_true(isomorphic(g, g2, + expect_isomorphic(g, g2, method = "vf2", vertex.color1 = 1:vcount(g), vertex.color2 = 1:vcount(g2) - )) + ) adj_el_list <- as_adj_edge_list(g) for (i in seq_len(vcount(g))) { diff --git a/tests/testthat/test-foreign.R b/tests/testthat/test-foreign.R index 78c75161d1..bbb1a39420 100644 --- a/tests/testthat/test-foreign.R +++ b/tests/testthat/test-foreign.R @@ -18,7 +18,7 @@ test_that("reading GraphML file works", { g <- read_graph(f <- gzfile("zachary.graphml.gz"), format = "graphml") g2 <- make_graph("zachary") - expect_true(isomorphic(g2, g)) + expect_isomorphic(g2, g) }) test_that("reading graph in NCOL format", { diff --git a/tests/testthat/test-isomorphism.R b/tests/testthat/test-isomorphism.R index cbb2b6cf11..c4a867d6da 100644 --- a/tests/testthat/test-isomorphism.R +++ b/tests/testthat/test-isomorphism.R @@ -1,15 +1,15 @@ test_that("isomorphic", { g <- graph_from_literal(A - B - C - A) - expect_true(isomorphic(g, g)) - expect_true(isomorphic(g, g, method = "direct")) - expect_true(isomorphic(g, g, method = "vf2")) - expect_true(isomorphic(g, g, method = "bliss")) + expect_isomorphic(g, g) + expect_isomorphic(g, g, method = "direct") + expect_isomorphic(g, g, method = "vf2") + expect_isomorphic(g, g, method = "bliss") g2 <- graph_from_literal(A - B - C) - expect_false(isomorphic(g, g2)) - expect_false(isomorphic(g, g2, method = "direct")) - expect_false(isomorphic(g, g2, method = "vf2")) - expect_false(isomorphic(g, g2, method = "bliss")) + expect_not_isomorphic(g, g2) + expect_not_isomorphic(g, g2, method = "direct") + expect_not_isomorphicg, g2, method = "vf2") + expect_not_isomorphic(g, g2, method = "bliss") }) test_that("subgraph_isomorphic", { diff --git a/tests/testthat/test-operators.R b/tests/testthat/test-operators.R index 28a7020b5a..6821d69b23 100644 --- a/tests/testthat/test-operators.R +++ b/tests/testthat/test-operators.R @@ -87,7 +87,7 @@ test_that("edge reversal works", { directed_graph <- make_graph(~ 1 -+ 2, 1 -+ 3, 1 -+ 4, 2 -+ 3, 3 -+ 4) reverse_directed_graph <- reverse_edges(directed_graph, 1:3) expected <- make_graph(~ 1 +- 2, 1 +- 3, 1 +- 4, 2 -+ 3, 3 -+ 4) - expect_true(isomorphic(reverse_directed_graph, expected)) + expect_isomorphic(reverse_directed_graph, expected) reverse_all_directed_graph <- reverse_edges(directed_graph) expect_vcount(reverse_all_directed_graph, vcount(directed_graph)) diff --git a/tests/testthat/test-trees.R b/tests/testthat/test-trees.R index 1dba898ba5..b02ee6a4d7 100644 --- a/tests/testthat/test-trees.R +++ b/tests/testthat/test-trees.R @@ -73,26 +73,26 @@ test_that("to_prufer and make_from_prufer works for trees", { g <- make_tree(13, 3, mode = "undirected") seq <- to_prufer(g) g2 <- make_from_prufer(seq) - expect_true(isomorphic(g, g2)) + expect_isomorphic(g, g2) g <- make_tree(13, 3, mode = "out") seq <- to_prufer(g) g2 <- make_from_prufer(seq) g3 <- as_undirected(g) - expect_true(isomorphic(g2, g3)) + expect_isomorphic(g2, g3) g <- make_tree(13, 3, mode = "in") seq <- to_prufer(g) g2 <- make_from_prufer(seq) g3 <- as_undirected(g) - expect_true(isomorphic(g2, g3)) + expect_isomorphic(g2, g3) }) test_that("make_(from_prufer(...)) works", { g <- make_tree(13, 3, mode = "undirected") seq <- to_prufer(g) g2 <- make_(from_prufer(seq)) - expect_true(isomorphic(g, g2)) + expect_isomorphic(g, g2) }) test_that("to_prufer prints an error for non-trees", { From 6124395d3223374bf0a1f81311fad8f2e1ae7098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 24 Feb 2025 10:00:54 +0100 Subject: [PATCH 7/8] test: we can expect more than no error, and we can use withr --- tests/testthat/_snaps/foreign.md | 20 ++++++++++++++++++++ tests/testthat/test-foreign.R | 12 ++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/_snaps/foreign.md diff --git a/tests/testthat/_snaps/foreign.md b/tests/testthat/_snaps/foreign.md new file mode 100644 index 0000000000..1f4d040c57 --- /dev/null +++ b/tests/testthat/_snaps/foreign.md @@ -0,0 +1,20 @@ +# reading graph in NCOL format + + Code + read_graph(ncol_path, "ncol") + Output + IGRAPH UN-- 3 2 -- + + attr: name (v/c) + + edges (vertex names): + [1] 0--1 1--2 + +# reading graph in LGL format + + Code + read_graph(lgl_path, "lgl") + Output + IGRAPH UN-- 3 2 -- + + attr: name (v/c) + + edges (vertex names): + [1] 0--1 1--2 + diff --git a/tests/testthat/test-foreign.R b/tests/testthat/test-foreign.R index bbb1a39420..31713d0d00 100644 --- a/tests/testthat/test-foreign.R +++ b/tests/testthat/test-foreign.R @@ -22,15 +22,19 @@ test_that("reading GraphML file works", { }) test_that("reading graph in NCOL format", { - ncol_path <- tempfile("testfile", fileext = ".ncol") + local_igraph_options(print.id = FALSE) + + ncol_path <- withr::local_tempfile(pattern = "testfile", fileext = ".ncol") g <- make_graph(c(1, 2, 2, 3)) write_graph(g, ncol_path, "ncol") - expect_no_error(g2 <- read_graph(ncol_path, "ncol")) + expect_snapshot(read_graph(ncol_path, "ncol")) }) test_that("reading graph in LGL format", { - lgl_path <- tempfile("testfile", fileext = ".lgl") + local_igraph_options(print.id = FALSE) + + lgl_path <- withr::local_tempfile(pattern = "testfile", fileext = ".lgl") g <- make_graph(c(1, 2, 2, 3)) write_graph(g, lgl_path, "lgl") - expect_no_error(g2 <- read_graph(lgl_path, "lgl")) + expect_snapshot(read_graph(lgl_path, "lgl")) }) From 1fd4e63c4f9e5bd4e39b3fc7418110ce6cc4ce3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 24 Feb 2025 10:22:04 +0100 Subject: [PATCH 8/8] oops --- tests/testthat/test-isomorphism.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-isomorphism.R b/tests/testthat/test-isomorphism.R index c4a867d6da..4fb497ccfa 100644 --- a/tests/testthat/test-isomorphism.R +++ b/tests/testthat/test-isomorphism.R @@ -8,7 +8,7 @@ test_that("isomorphic", { g2 <- graph_from_literal(A - B - C) expect_not_isomorphic(g, g2) expect_not_isomorphic(g, g2, method = "direct") - expect_not_isomorphicg, g2, method = "vf2") + expect_not_isomorphic(g, g2, method = "vf2") expect_not_isomorphic(g, g2, method = "bliss") })