]> granicus.if.org Git - graphviz/commitdiff
tests: test_edge_node_overlap_simple: generate DOT based on options
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Tue, 13 Sep 2022 11:00:51 +0000 (13:00 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Tue, 20 Sep 2022 15:53:06 +0000 (17:53 +0200)
Generate DOT source based on given options which are also passed to
the check function. Upcoming commits will add checks which need to
know details about how the graph was generated.

tests/test_edge_node_overlap_simple.cpp
tests/test_edge_node_overlap_utilities.cpp
tests/test_edge_node_overlap_utilities.h

index 78321fac22cfd197293dc230ef525628b6dda477..21a88b29986a0838a1fea8988910501f4a59a299 100644 (file)
@@ -9,10 +9,13 @@ TEST_CASE(
     "Overlap",
     "[!shouldfail] An edge connected to a node shall not overlap that node") {
 
-  std::string dot =
-      "digraph {node[shape=polygon penwidth=2 fontname=Courier] a -> b}";
+  const graph_options graph_options = {
+      .node_shape = "polygon",
+      .node_penwidth = 2,
+      .edge_penwidth = 2,
+  };
 
   const auto filename_base = AUTO_NAME();
 
-  test_edge_node_overlap(dot, {.filename_base = filename_base});
+  test_edge_node_overlap(graph_options, {.filename_base = filename_base});
 }
index 52b780fd3ff95550537f6f2d518a35c45d0ed039..3597ea72a20ab6e5e7ef729680ee8e8fdd540af7 100644 (file)
@@ -1,3 +1,6 @@
+#include <cassert>
+#include <string>
+
 #include <catch2/catch.hpp>
 #include <cmath>
 #include <fmt/format.h>
@@ -8,8 +11,11 @@
 
 /// check that edges do not overlap nodes
 static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer,
+                               const graph_options &graph_options,
                                const check_options &check_options) {
 
+  const auto rankdir = graph_options.rankdir;
+
   REQUIRE(svg_analyzer.graphs().size() == 1);
   auto &recreated_graph = svg_analyzer.graphs().back();
 
@@ -39,6 +45,8 @@ static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer,
     INFO(fmt::format("  height: {:.3f}", overlap_bbox.height));
     // FIXME: add support for rank direction "LR" and "RL". For now assume
     // "TB" or "BT" and check only in the vertical direction
+    assert((rankdir == "TB" || rankdir == "BT") &&
+           "Only rankdir=TB or rankdir=BT is supported");
     const auto head_node_edge_overlap = overlap_bbox.height;
 
     // check maximum head node and edge overlap
@@ -56,6 +64,8 @@ static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer,
     INFO(fmt::format("  height: {:.6f}", overlap_bbox.height));
     // FIXME: add support for rank direction "LR" and "RL". For now assume
     // "TB" or "BT" and check only in the vertical direction
+    assert((rankdir == "TB" || rankdir == "BT") &&
+           "Only rankdir=TB or rankdir=BT is supported");
     const auto tail_node_edge_overlap = overlap_bbox.height;
 
     // check maximum tail node and edge overlap
@@ -104,8 +114,22 @@ static void write_svg_files(SVGAnalyzer &svg_analyzer,
   }
 }
 
-void test_edge_node_overlap(const std::string &dot,
+/// generate DOT source based on given options
+static std::string generate_dot(const graph_options &graph_options) {
+  return fmt::format("digraph g1 {{"
+                     "  graph [rankdir={}]"
+                     "  node [penwidth={} shape={} fontname=Courier]"
+                     "  edge [penwidth={}]"
+                     "  a -> b"
+                     "}}",
+                     graph_options.rankdir, graph_options.node_penwidth,
+                     graph_options.node_shape, graph_options.edge_penwidth);
+}
+
+void test_edge_node_overlap(const graph_options &graph_options,
                             const write_options &write_options) {
+  const auto dot = generate_dot(graph_options);
+
   auto svg_analyzer = SVGAnalyzer::make_from_dot(dot);
 
   // The binary search in the bezier_clip function in lib/common/splines.c has a
@@ -125,7 +149,8 @@ void test_edge_node_overlap(const std::string &dot,
       .svg_rounding_error = graphviz_max_svg_rounding_error,
   };
 
-  const auto success = check_analyzed_svg(svg_analyzer, check_options);
+  const auto success =
+      check_analyzed_svg(svg_analyzer, graph_options, check_options);
 
   if (!success || write_options.write_svg_on_success) {
     write_svg_files(svg_analyzer, check_options, write_options);
index 395bedd649afbfb4a3601fb4caa9c5db343bc4b5..8fa656de48c18e8f301cc51cdf7761d6dda28274 100644 (file)
@@ -9,6 +9,13 @@ struct check_options {
   double svg_rounding_error;
 };
 
+struct graph_options {
+  std::string_view rankdir = "TB";
+  std::string_view node_shape = "polygon";
+  double node_penwidth = 1;
+  double edge_penwidth = 1;
+};
+
 struct write_options {
   std::string filename_base = "test_edge_node_overlap";
   bool write_svg_on_success = false;
@@ -19,5 +26,5 @@ struct write_options {
 
 /// generate an SVG graph from the `dot` source and check that edges don't
 /// overlap nodes
-void test_edge_node_overlap(const std::string &dot,
+void test_edge_node_overlap(const graph_options &graph_options = {},
                             const write_options &write_options = {});