]> granicus.if.org Git - graphviz/commitdiff
tests: test_edge_node_overlap_utilities: add support for writing of SVG files for...
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Sun, 28 Aug 2022 15:36:31 +0000 (17:36 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Mon, 12 Sep 2022 07:46:17 +0000 (09:46 +0200)
tests/test_edge_node_overlap_utilities.cpp
tests/test_edge_node_overlap_utilities.h
tests/test_utilities.cpp
tests/test_utilities.h

index 80cf89a21b497e736802aeaa130503da02065f94..9698e3eb55781fe3acc5603cd927c50463fc9c93 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "svg_analyzer.h"
 #include "test_edge_node_overlap_utilities.h"
+#include "test_utilities.h"
 
 /// check that edges do not overlap nodes
 static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer,
@@ -66,7 +67,31 @@ static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer,
   return success;
 }
 
-void test_edge_node_overlap(const std::string &dot) {
+/// write SVG files for manual analysis if any of the above checks failed or if
+/// we explicitly have requested it
+static void write_svg_files(const SVGAnalyzer &svg_analyzer,
+                            const write_options &write_options) {
+  const std::filesystem::path test_artifacts_directory = "test_artifacts";
+
+  if (write_options.write_original_svg) {
+    // write the original SVG generated by Graphviz to a file
+    const std::filesystem::path filename =
+        write_options.filename_base + "_original.svg";
+    write_to_file(test_artifacts_directory, filename,
+                  svg_analyzer.original_svg());
+  }
+
+  if (write_options.write_recreated_svg) {
+    // write the SVG recreated by the SVG analyzer to a file
+    const std::filesystem::path filename =
+        write_options.filename_base + "_recreated.svg";
+    const auto recreated_svg = svg_analyzer.svg_string();
+    write_to_file(test_artifacts_directory, filename, recreated_svg);
+  }
+}
+
+void test_edge_node_overlap(const std::string &dot,
+                            const write_options &write_options) {
   auto svg_analyzer = SVGAnalyzer::make_from_dot(dot);
 
   // The binary search in the bezier_clip function in lib/common/splines.c has a
@@ -88,6 +113,7 @@ void test_edge_node_overlap(const std::string &dot) {
 
   const auto success = check_analyzed_svg(svg_analyzer, check_options);
 
-  // FIXME: add writing of SVG files for manual inspection when not success
-  REQUIRE(success);
+  if (!success || write_options.write_svg_on_success) {
+    write_svg_files(svg_analyzer, write_options);
+  }
 }
index bfe1f234b5b94620ce868a4e29365160adc9d62f..b77b0c4a435e68a7a80926558de35e1ca4d58ae5 100644 (file)
@@ -9,6 +9,14 @@ struct check_options {
   double svg_rounding_error;
 };
 
+struct write_options {
+  std::string filename_base = "test_edge_node_overlap";
+  bool write_svg_on_success = false;
+  bool write_original_svg = false;
+  bool write_recreated_svg = true;
+};
+
 /// 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 std::string &dot,
+                            const write_options &write_options = {});
index da7bb6eb728794baae56f0450a081cab6b002513..7cc2bf4585f373a4dccf8d617835055174ac7673 100644 (file)
@@ -1,6 +1,11 @@
+#include <filesystem>
+#include <fstream>
+#include <stdexcept>
 #include <string_view>
 #include <unordered_set>
 
+#include <fmt/format.h>
+
 #include "test_utilities.h"
 
 const std::unordered_set<std::string_view> node_shapes_consisting_of_ellipse = {
@@ -190,3 +195,15 @@ const std::unordered_set<std::string_view>
 
 const std::unordered_set<std::string_view> all_rank_directions = {"TB", "BT",
                                                                   "LR", "RL"};
+void write_to_file(const std::filesystem::path &directory,
+                   const std::filesystem::path &filename,
+                   const std::string_view text) {
+  std::filesystem::create_directories(directory);
+  const std::filesystem::path test_artifacts_path = directory / filename;
+  std::ofstream outfile{test_artifacts_path};
+  if (!outfile.is_open()) {
+    throw std::runtime_error{fmt::format("Could not create output file \"{}\"",
+                                         test_artifacts_path.native())};
+  }
+  outfile << text;
+}
index 887bb7a4d4276f96c03bb00f267029eae66d81f8..b051f8a62add7f35bb5d76d4c3cc730d042d43ae 100644 (file)
@@ -1,5 +1,6 @@
 #pragma once
 
+#include <filesystem>
 #include <string_view>
 #include <unordered_set>
 
@@ -34,3 +35,9 @@ extern const std::unordered_set<std::string_view>
 
 /// rank directions
 extern const std::unordered_set<std::string_view> all_rank_directions;
+
+/// misc utilities
+
+void write_to_file(const std::filesystem::path &directory,
+                   const std::filesystem::path &filename,
+                   std::string_view text);