From: Magnus Jacobsson Date: Tue, 23 Aug 2022 07:11:02 +0000 (+0200) Subject: tests: SVGAnalyzer: add 'make_from_dot' static method and use in test_rankdir X-Git-Tag: 6.0.1~10^2~15 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=975c10754b2d782e936655758165f4558b7cd362;p=graphviz tests: SVGAnalyzer: add 'make_from_dot' static method and use in test_rankdir Upcoming commits will add several new test cases doing exactly that. This change will keep those tests short and DRY. --- diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 08bbf5b3a..0b962959b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -34,6 +34,7 @@ set_target_properties(test_common PROPERTIES CXX_STANDARD 20) set_target_properties(test_common PROPERTIES CXX_STANDARD_REQUIRED ON) target_include_directories(test_common PRIVATE ../lib + ../lib/cgraph++ ) target_include_directories(test_common SYSTEM PRIVATE ${Boost_INCLUDE_DIRS} diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index 3f4017487..3e5db3edf 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -5,6 +5,10 @@ #include "svg_analyzer.h" #include "svgpp_context.h" #include "svgpp_document_traverser.h" +#include +#include +#include +#include SVGAnalyzer::SVGAnalyzer(char *text) : m_svg(SVG::SVGElement(SVG::SVGElementType::Svg)) { @@ -242,6 +246,19 @@ void SVGAnalyzer::set_x(double x) { current_element().attributes.x = x; } void SVGAnalyzer::set_y(double y) { current_element().attributes.y = y; } +SVGAnalyzer SVGAnalyzer::make_from_dot(const std::string &dot_source, + const std::string &engine) { + auto g = CGraph::AGraph{dot_source}; + + const auto demand_loading = false; + auto gvc = GVC::GVContext{lt_preloaded_symbols, demand_loading}; + + const auto layout = GVC::GVLayout(std::move(gvc), std::move(g), engine); + + const auto result = layout.render("svg"); + return SVGAnalyzer{result.c_str()}; +} + void SVGAnalyzer::set_transform(double a, double b, double c, double d, double e, double f) { current_element().attributes.transform = {a, b, c, d, e, f}; diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index 033b4ee68..22f7e3be8 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -55,6 +55,12 @@ public: void set_width(double width) override; void set_x(double x) override; void set_y(double y) override; + /// Create an SVGAnalyzer from DOT source using the `engine` layout engine. + /// + /// \param dot_source The DOT source + /// \param engine The Graphviz layout engine + static SVGAnalyzer make_from_dot(const std::string &dot_source, + const std::string &engine = "dot"); std::size_t num_svgs() const { return m_num_svgs; }; std::size_t num_groups() const { return m_num_groups; }; std::size_t num_circles() const { return m_num_circles; }; diff --git a/tests/test_rankdir.cpp b/tests/test_rankdir.cpp index 7e17c37c7..34c194cfd 100644 --- a/tests/test_rankdir.cpp +++ b/tests/test_rankdir.cpp @@ -5,10 +5,6 @@ #include "svg_analyzer.h" #include "test_utilities.h" -#include -#include -#include -#include TEST_CASE("Graph rankdir", "Test that the Graphviz `rankdir` attribute affects " "the relative placement of nodes and edges " @@ -27,15 +23,9 @@ TEST_CASE("Graph rankdir", "Test that the Graphviz `rankdir` attribute affects " auto dot = fmt::format( "digraph g1 {{rankdir={}; node [shape={} fontname=Courier]; a -> b}}", rankdir, shape); - auto g = CGraph::AGraph{dot}; - const auto demand_loading = false; - auto gvc = GVC::GVContext{lt_preloaded_symbols, demand_loading}; - - const auto layout = GVC::GVLayout(std::move(gvc), std::move(g), "dot"); - - const auto result = layout.render("svg"); - SVGAnalyzer svg_analyzer{result.c_str()}; + const auto engine = "dot"; + auto svg_analyzer = SVGAnalyzer::make_from_dot(dot, engine); REQUIRE(svg_analyzer.graphs().size() == 1); const auto &graph = svg_analyzer.graphs().back();