From 09821c4050ddbd76a9f36f12858cd6bfd9ebc2e4 Mon Sep 17 00:00:00 2001 From: Magnus Jacobsson Date: Sat, 20 Aug 2022 18:12:54 +0200 Subject: [PATCH] tests: SVGAnalyzer: add 're_create_and_verify_svg' method and use in test_svg_analyzer 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 | 24 ++++++++++++++++++++++++ tests/svg_analyzer.h | 3 +++ tests/test_svg_analyzer.cpp | 18 +----------------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index eb17b8de4..8b1d1ae9f 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -1,6 +1,8 @@ #include #include +#include +#include #include #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 original_svg_lines; + boost::split(original_svg_lines, m_original_svg, boost::is_any_of("\n")); + + std::vector 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; } diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index dfbf39be4..8c48dcbe6 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -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; diff --git a/tests/test_svg_analyzer.cpp b/tests/test_svg_analyzer.cpp index 3543b135c..3e83be88d 100644 --- a/tests/test_svg_analyzer.cpp +++ b/tests/test_svg_analyzer.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -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 original_svg_lines; - boost::split(original_svg_lines, original_svg, boost::is_any_of("\n")); - std::vector 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(); } } -- 2.50.1