From a498b7ef34f0bc47908775810c55cd71c62bc38d Mon Sep 17 00:00:00 2001 From: Magnus Jacobsson Date: Tue, 16 Aug 2022 15:55:53 +0200 Subject: [PATCH] tests: SvgAnalyzer: GraphvizGraph: add retrieval of node by node_id --- tests/graphviz_graph.cpp | 14 ++++++++++++++ tests/graphviz_graph.h | 2 ++ tests/graphviz_node.cpp | 4 +++- tests/graphviz_node.h | 6 ++++++ tests/test_rankdir.cpp | 3 +++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/graphviz_graph.cpp b/tests/graphviz_graph.cpp index 183a723ad..434daec13 100644 --- a/tests/graphviz_graph.cpp +++ b/tests/graphviz_graph.cpp @@ -1,3 +1,7 @@ +#include + +#include + #include "graphviz_graph.h" GraphvizGraph::GraphvizGraph(SVG::SVGElement &svg_g_element) @@ -15,6 +19,16 @@ const SVG::SVGElement &GraphvizGraph::svg_g_element() const { return m_svg_g_element; } +const GraphvizNode &GraphvizGraph::node(std::string_view node_id) const { + for (auto &node : m_nodes) { + if (node.node_id() == node_id) { + return node; + } + } + throw std::runtime_error{ + fmt::format("Unknown node '{}' in graph '{}'", node_id, m_graph_id)}; +} + void GraphvizGraph::add_edge(SVG::SVGElement &svg_g_element) { m_edges.emplace_back(svg_g_element); } diff --git a/tests/graphviz_graph.h b/tests/graphviz_graph.h index e55551fa3..317ed3287 100644 --- a/tests/graphviz_graph.h +++ b/tests/graphviz_graph.h @@ -23,6 +23,8 @@ public: void add_node(SVG::SVGElement &svg_g_element); /// Return a non-mutable reference to the list of Graphviz edges const std::vector &edges() const; + /// Return the node with the specified `node_id` + const GraphvizNode &node(std::string_view node_id) const; /// Return a non-mutable reference to the list of Graphviz nodes const std::vector &nodes() const; /// Return a non-mutable reference to the SVG `g` element corresponding to the diff --git a/tests/graphviz_node.cpp b/tests/graphviz_node.cpp index fe1f486b7..b1857c041 100644 --- a/tests/graphviz_node.cpp +++ b/tests/graphviz_node.cpp @@ -1,7 +1,9 @@ #include "graphviz_node.h" GraphvizNode::GraphvizNode(SVG::SVGElement &svg_element) - : m_svg_g_element(svg_element) {} + : m_node_id(svg_element.graphviz_id), m_svg_g_element(svg_element) {} + +std::string_view GraphvizNode::node_id() const { return m_node_id; } const SVG::SVGElement &GraphvizNode::svg_g_element() const { return m_svg_g_element; diff --git a/tests/graphviz_node.h b/tests/graphviz_node.h index 857494370..2de2c6c0c 100644 --- a/tests/graphviz_node.h +++ b/tests/graphviz_node.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "svg_element.h" @@ -14,11 +15,16 @@ public: GraphvizNode() = delete; explicit GraphvizNode(SVG::SVGElement &svg_element); + /// Return the node's `node_id` as defined by the DOT language + std::string_view node_id() const; /// Return a non-mutable reference to the SVG `g` element corresponding to the /// node const SVG::SVGElement &svg_g_element() const; private: + /// The `node_id` according to the DOT language specification. Note that this + /// is not the same as the `id` attribute of a node + std::string m_node_id; /// The SVG `g` element corresponding to the node SVG::SVGElement &m_svg_g_element; }; diff --git a/tests/test_rankdir.cpp b/tests/test_rankdir.cpp index 77cdd0604..2b39d5276 100644 --- a/tests/test_rankdir.cpp +++ b/tests/test_rankdir.cpp @@ -38,4 +38,7 @@ TEST_CASE("Graph rankdir", "Test that the Graphviz `rankdir` attribute affects " SVGAnalyzer svgAnalyzer{result.c_str()}; REQUIRE(svgAnalyzer.graphs().size() == 1); + const auto &graph = svgAnalyzer.graphs().back(); + const auto node_a = graph.node("a"); + const auto node_b = graph.node("b"); } -- 2.40.0