|
1 | 1 | test_that("dot layouts produce appropriate errors and output classes", { |
2 | | - if (require(Rgraphviz, quietly = TRUE)) { |
3 | | - specs <- mp_tmb_entire_library() |
4 | | - n_flows = (specs |
5 | | - |> lapply(mp_flow_frame, topological_sort = FALSE) |
6 | | - |> vapply(nrow, integer(1L)) |
7 | | - ) |
8 | | - no_mpflows <- which(n_flows == 0L) |> names() |
9 | | - for (s in no_mpflows) { |
10 | | - expect_error(dot_layout(specs[[s]]), "was spec defined") |
11 | | - } |
12 | | - plts = list() |
13 | | - for (s in setdiff(names(specs), no_mpflows)) { |
14 | | - plts[[s]] <- dot_layout(specs[[s]]) |
15 | | - expect_s4_class(plts[[s]], "graphAM") |
16 | | - } |
17 | | - if (interactive()) lapply(plts, plot) |
18 | | - } |
| 2 | + specs <- mp_tmb_entire_library() |
| 3 | + n_flows = (specs |
| 4 | + |> lapply(mp_flow_frame, topological_sort = FALSE) |
| 5 | + |> vapply(nrow, integer(1L)) |
| 6 | + ) |
| 7 | + no_mpflows <- which(n_flows == 0L) |> names() |
| 8 | + for (s in no_mpflows) { |
| 9 | + expect_error( |
| 10 | + mp_dot_layout(specs[[s]], action = "layout") |
| 11 | + , "was spec defined" |
| 12 | + ) |
| 13 | + } |
| 14 | + plts = list() |
| 15 | + for (s in setdiff(names(specs), no_mpflows)) { |
| 16 | + plts[[s]] <- mp_dot_layout(specs[[s]], action = "layout") |
| 17 | + expect_s4_class(plts[[s]], "graphAM") |
| 18 | + } |
| 19 | + if (interactive()) for (plt in plts) Rgraphviz::renderGraph(plt) |
| 20 | + |
| 21 | + ## copied from https://testthat.r-lib.org/reference/expect_snapshot_file.html |
| 22 | + save_png <- function(code, width = 400, height = 400) { |
| 23 | + path <- tempfile(fileext = ".png") |
| 24 | + png(path, width = width, height = height) |
| 25 | + on.exit(dev.off()) |
| 26 | + code |
| 27 | + |
| 28 | + path |
| 29 | + } |
| 30 | + expect_snapshot_plot <- function(name, code) { |
| 31 | + # Other packages might affect results |
| 32 | + skip_if_not_installed("ggplot2", "2.0.0") |
| 33 | + # Or maybe the output is different on some operation systems |
| 34 | + skip_on_os("windows") |
| 35 | + # You'll need to carefully think about and experiment with these skips |
| 36 | + |
| 37 | + name <- paste0(name, ".png") |
| 38 | + |
| 39 | + # Announce the file before touching `code`. This way, if `code` |
| 40 | + # unexpectedly fails or skips, testthat will not auto-delete the |
| 41 | + # corresponding snapshot file. |
| 42 | + announce_snapshot_file(name = name) |
| 43 | + |
| 44 | + path <- save_png(code) |
| 45 | + expect_snapshot_file(path, name) |
| 46 | + } |
| 47 | + |
| 48 | + macpan_base <- mp_official_library("macpan_base") |
| 49 | + seir <- mp_official_library("seir") |
| 50 | + shiver <- mp_official_library("shiver") |
| 51 | + |
| 52 | + graph3 = mp_dot_layout(macpan_base, action = "define") |
| 53 | + graph2 = mp_dot_layout(seir, action = "layout") |
| 54 | + expect_snapshot_plot("shiver1", {graph1 <- mp_dot_layout(shiver, action = "render")}) |
| 55 | + expect_snapshot_plot("base", {graph3 |> Rgraphviz::layoutGraph() |> Rgraphviz::renderGraph()}) |
| 56 | + expect_snapshot_plot("seir", {graph2 |> Rgraphviz::renderGraph()}) |
| 57 | + expect_snapshot_plot("shiver2", {graph1 |> Rgraphviz::renderGraph()}) |
| 58 | + |
| 59 | + expect_s4_class(graph1, "graphAM") |
| 60 | + expect_s4_class(graph2, "graphAM") |
| 61 | + expect_s4_class(graph3, "graphAM") |
19 | 62 | }) |
0 commit comments