Debian Codegraph @

tests/test_svg_analyzer.cpp

raw
1 #include <boost/range/adaptor/indexed.hpp>
2 #include <catch2/catch_all.hpp>
3 #include <format>
4
5 #include "svg_analyzer.h"
6 #include "test_utilities.h"
7
8 TEST_CASE("SvgAnalyzer",
9 "Test that the SvgAnalyzer can parse an SVG produced by Graphviz to "
10 "an internal data structure and re-create the original SVG exactly "
11 "from that data structure") {
12
13 const auto shape = GENERATE(from_range(all_node_shapes));
14 INFO("Shape: " << shape);
15
16 auto dot = std::format("digraph g1 {{node [shape={}]; a -> b}}", shape);
17
18 auto svg_analyzer = SVGAnalyzer::make_from_dot(dot);
19
20 const std::size_t expected_num_graphs = 1;
21 const std::size_t expected_num_nodes = 2;
22 const std::size_t expected_num_edges = 1;
23
24 CHECK(svg_analyzer.graphs().size() == expected_num_graphs);
25 for (const auto &graph : svg_analyzer.graphs()) {
26 CHECK(graph.svg_g_element().elem_type == SVG::SVGElementType::Group);
27 CHECK(graph.svg_g_element().attributes.class_ == "graph");
28 CHECK(graph.svg_g_element().graphviz_id == "g1");
29
30 CHECK(graph.nodes().size() == expected_num_nodes);
31 for (const auto &node_it : graph.nodes() | boost::adaptors::indexed(0)) {
32 const auto node = node_it.value();
33 const auto i = node_it.index();
34 CHECK(node.svg_g_element().elem_type == SVG::SVGElementType::Group);
35 CHECK(node.svg_g_element().attributes.class_ == "node");
36 const auto node_id = i == 0 ? "a" : "b";
37 CHECK(node.svg_g_element().graphviz_id == node_id);
38 }
39
40 CHECK(graph.edges().size() == expected_num_edges);
41 for (const auto &edge : graph.edges()) {
42 CHECK(edge.svg_g_element().elem_type == SVG::SVGElementType::Group);
43 CHECK(edge.svg_g_element().attributes.class_ == "edge");
44 CHECK(edge.svg_g_element().graphviz_id == "a->b");
45 }
46
47 const std::size_t expected_num_svgs = expected_num_graphs;
48 const std::size_t expected_num_groups =
49 expected_num_graphs + expected_num_nodes + expected_num_edges;
50 const std::size_t expected_num_circles = 0;
51 const std::size_t expected_num_ellipses = [&]() {
52 if (shape == "doublecircle") {
53 return expected_num_nodes * 2;
54 } else if (contains_ellipse_shape(shape)) {
55 return expected_num_nodes;
56 } else {
57 return 0UL;
58 }
59 }();
60 const std::size_t expected_num_lines = 0;
61 const std::size_t expected_num_paths =
62 expected_num_edges + (shape == "cylinder" ? expected_num_nodes * 2 : 0);
63 const std::size_t expected_num_polygons =
64 expected_num_graphs + expected_num_edges + [&]() {
65 if (shape == "noverhang") {
66 return expected_num_nodes * 4;
67 } else if (shape == "tripleoctagon") {
68 return expected_num_nodes * 3;
69 } else if (shape == "doubleoctagon" || shape == "fivepoverhang" ||
70 shape == "threepoverhang" || shape == "assembly") {
71 return expected_num_nodes * 2;
72 } else if (contains_polygon_shape(shape)) {
73 return expected_num_nodes;
74 } else {
75 return 0UL;
76 }
77 }();
78 const std::size_t expected_num_polylines = [&]() {
79 if (shape == "Mdiamond" || shape == "Msquare") {
80 return expected_num_nodes * 4;
81 } else if (shape == "box3d" || shape == "signature" ||
82 shape == "insulator" || shape == "ribosite" ||
83 shape == "rnastab") {
84 return expected_num_nodes * 3;
85 } else if (shape == "Mcircle" || shape == "note" ||
86 shape == "component" || shape == "restrictionsite" ||
87 shape == "noverhang" || shape == "assembly" ||
88 shape == "proteasesite" || shape == "proteinstab") {
89 return expected_num_nodes * 2;
90 } else if (shape == "underline" || shape == "tab" ||
91 shape == "promoter" || shape == "terminator" ||
92 shape == "utr" || shape == "primersite" ||
93 shape == "fivepoverhang" || shape == "threepoverhang") {
94 return expected_num_nodes;
95 } else {
96 return 0UL;
97 }
98 }();
99 const std::size_t expected_num_rects = 0;
100 const std::size_t expected_num_titles =
101 expected_num_graphs + expected_num_nodes + expected_num_edges;
102 const std::size_t expected_num_texts =
103 shape == "point" ? 0 : expected_num_nodes;
104
105 CHECK(svg_analyzer.num_svgs() == expected_num_svgs);
106 CHECK(svg_analyzer.num_groups() == expected_num_groups);
107 CHECK(svg_analyzer.num_circles() == expected_num_circles);
108 CHECK(svg_analyzer.num_ellipses() == expected_num_ellipses);
109 CHECK(svg_analyzer.num_lines() == expected_num_lines);
110 CHECK(svg_analyzer.num_paths() == expected_num_paths);
111 CHECK(svg_analyzer.num_polygons() == expected_num_polygons);
112 CHECK(svg_analyzer.num_polylines() == expected_num_polylines);
113 CHECK(svg_analyzer.num_rects() == expected_num_rects);
114 CHECK(svg_analyzer.num_titles() == expected_num_titles);
115 CHECK(svg_analyzer.num_texts() == expected_num_texts);
116
117 svg_analyzer.re_create_and_verify_svg();
118 }
119 }