]> granicus.if.org Git - graphviz/commitdiff
tests: SVGAnalyzer: add 're_create_and_verify_svg' method and use in test_svg_analyzer
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Sat, 20 Aug 2022 16:12:54 +0000 (18:12 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Mon, 5 Sep 2022 06:10:57 +0000 (08:10 +0200)
Upcoming commits will extend the SVG analyzer's ability to handle new
SVG attributes and add new test cases verifying its ability to
re-create correct SVG containing those attributes. This change will
facilitate the creation of those tests and keep the code DRY.

tests/svg_analyzer.cpp
tests/svg_analyzer.h
tests/test_svg_analyzer.cpp

index eb17b8de4865c53fff7368bdb08b9f2ed07c8a5d..8b1d1ae9f164b80de7a927a11d9deeb39bb5de8c 100644 (file)
@@ -1,6 +1,8 @@
 #include <memory>
 #include <stdexcept>
 
+#include <boost/algorithm/string.hpp>
+#include <catch2/catch.hpp>
 #include <fmt/format.h>
 
 #include "svg_analyzer.h"
@@ -179,6 +181,28 @@ void SVGAnalyzer::retrieve_graphviz_components_impl(
   }
 }
 
+void SVGAnalyzer::re_create_and_verify_svg() {
+
+  const auto indent_size = 0;
+  auto recreated_svg = svg_string(indent_size);
+
+  // compare the recreated SVG with the original SVG
+  if (recreated_svg != m_original_svg) {
+    std::vector<std::string> original_svg_lines;
+    boost::split(original_svg_lines, m_original_svg, boost::is_any_of("\n"));
+
+    std::vector<std::string> recreated_svg_lines;
+    boost::split(recreated_svg_lines, recreated_svg, boost::is_any_of("\n"));
+
+    for (std::size_t i = 0; i < original_svg_lines.size(); i++) {
+      REQUIRE(i < recreated_svg_lines.size());
+      REQUIRE(recreated_svg_lines[i] == original_svg_lines[i]);
+    }
+
+    REQUIRE(recreated_svg_lines.size() == original_svg_lines.size());
+  }
+}
+
 void SVGAnalyzer::set_cx(double cx) { current_element().attributes.cx = cx; }
 
 void SVGAnalyzer::set_cy(double cy) { current_element().attributes.cy = cy; }
index dfbf39be44f53717981776ea00a423fcec7d1b13..8c48dcbe6f3fe15cd6403208d8b29e32c0be0b40 100644 (file)
@@ -74,6 +74,9 @@ public:
   std::size_t num_titles() const { return m_num_titles; };
   /// Return a view of the original SVG
   std::string_view original_svg() const;
+  /// Re-create the SVG from the internal data structure and verify it against
+  /// the original SVG produced by Graphviz
+  void re_create_and_verify_svg();
   void set_graphviz_version(std::string_view version);
   void set_graphviz_build_date(std::string_view build_date);
   std::string svg_string(std::size_t indent_size = 2) const;
index 3543b135c8ec16f92b874bb22ad18c3b0eb2b891..3e83be88d72ec916c0e4c25b67bb1cebe4e408a9 100644 (file)
@@ -1,4 +1,3 @@
-#include <boost/algorithm/string.hpp>
 #include <boost/range/adaptor/indexed.hpp>
 #include <catch2/catch.hpp>
 #include <fmt/format.h>
@@ -115,21 +114,6 @@ TEST_CASE("SvgAnalyzer",
     CHECK(svg_analyzer.num_titles() == expected_num_titles);
     CHECK(svg_analyzer.num_texts() == expected_num_texts);
 
-    auto original_svg = svg_analyzer.original_svg();
-    const auto indent_size = 0;
-    auto recreated_svg = svg_analyzer.svg_string(indent_size);
-
-    // compare the recreated SVG with the original SVG
-    if (recreated_svg != original_svg) {
-      std::vector<std::string> original_svg_lines;
-      boost::split(original_svg_lines, original_svg, boost::is_any_of("\n"));
-      std::vector<std::string> recreated_svg_lines;
-      boost::split(recreated_svg_lines, recreated_svg, boost::is_any_of("\n"));
-      for (std::size_t i = 0; i < original_svg_lines.size(); i++) {
-        REQUIRE(i < recreated_svg_lines.size());
-        REQUIRE(recreated_svg_lines[i] == original_svg_lines[i]);
-      }
-      REQUIRE(recreated_svg_lines.size() == original_svg_lines.size());
-    }
+    svg_analyzer.re_create_and_verify_svg();
   }
 }