From: Magnus Jacobsson Date: Sat, 23 Jul 2022 10:50:06 +0000 (+0200) Subject: tests: SvgAnalyzer: add handling of the 'class' attribute X-Git-Tag: 5.0.1~7^2~18 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1dc6786c9adab290cbc9e239b5c9241ddff77fc8;p=graphviz tests: SvgAnalyzer: add handling of the 'class' attribute This will be used in an upcoming commit to identify Graphviz graphs, nodes and edges. --- diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index 239ea91ff..e688b7825 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -111,6 +111,10 @@ void SVGAnalyzer::enter_element(SVG::SVGElementType type) { m_elements_in_process.push_back(&element.children.back()); } +void SVGAnalyzer::set_class(std::string_view class_) { + current_element().attributes.class_ = class_; +} + void SVGAnalyzer::set_text(std::string_view text) { auto &element = current_element(); element.text = text; diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index 6a005876c..f8e7a4c9d 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -27,6 +27,7 @@ public: void on_enter_element_text() override; void on_enter_element_title() override; void on_exit_element() override; + void set_class(std::string_view) override; void set_text(std::string_view text) override; std::size_t num_svgs() const { return m_num_svgs; }; std::size_t num_groups() const { return m_num_groups; }; diff --git a/tests/svg_analyzer_interface.h b/tests/svg_analyzer_interface.h index 7ea80454a..a81a5804b 100644 --- a/tests/svg_analyzer_interface.h +++ b/tests/svg_analyzer_interface.h @@ -26,5 +26,6 @@ public: virtual void on_enter_element_text() = 0; virtual void on_enter_element_title() = 0; virtual void on_exit_element() = 0; + virtual void set_class(std::string_view) = 0; virtual void set_text(std::string_view text) = 0; }; diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index c842a4b90..97c4545ef 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -1,3 +1,5 @@ +#include + #include #include "svg_element.h" @@ -57,6 +59,41 @@ void SVG::SVGElement::to_string_impl(std::string &output, output += "<"; output += tag(type); + std::string attributes_str{}; + switch (type) { + case SVG::SVGElementType::Ellipse: + // ignore for now + break; + case SVG::SVGElementType::Group: + attributes_str += fmt::format(R"(class="{}")", attributes.class_); + break; + case SVG::SVGElementType::Path: + // ignore for now + break; + case SVG::SVGElementType::Polygon: + // ignore for now + break; + case SVG::SVGElementType::Polyline: + // ignore for now + break; + case SVG::SVGElementType::Svg: + // ignore for now + break; + case SVG::SVGElementType::Text: + // ignore for now + break; + case SVG::SVGElementType::Title: + // Graphviz doesn't generate attributes on 'title' elements + break; + default: + throw std::runtime_error{fmt::format( + "Attributes on '{}' elements are not yet implemented", tag(type))}; + } + if (!attributes_str.empty()) { + output += " "; + } + output += attributes_str; + if (children.empty() && text.empty()) { output += "/>\n"; } else { diff --git a/tests/svg_element.h b/tests/svg_element.h index 6ef88f1e5..bf3e206b0 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -22,6 +22,10 @@ enum class SVGElementType { std::string_view tag(SVG::SVGElementType type); +struct SVGAttributes { + std::string class_; +}; + /** * @brief The SVGElement class represents an SVG element */ @@ -33,6 +37,7 @@ public: std::string to_string(std::size_t indent_size) const; + SVGAttributes attributes; /// The Graphviz build date std::string graphviz_build_date; std::vector children; diff --git a/tests/svgpp_context.cpp b/tests/svgpp_context.cpp index e87a5cdbc..f09ec6d44 100644 --- a/tests/svgpp_context.cpp +++ b/tests/svgpp_context.cpp @@ -172,6 +172,11 @@ void SvgppContext::set(svgpp::tag::attribute::height a, const double v) { (void)v; } +void SvgppContext::set(svgpp::tag::attribute::class_, + boost::iterator_range v) { + m_svgAnalyzer->set_class({v.begin(), v.end()}); +} + void SvgppContext::set_impl(svgpp::tag::attribute::points &points, const std::any &range) { (void)points; diff --git a/tests/svgpp_context.h b/tests/svgpp_context.h index d23e57804..c66abb169 100644 --- a/tests/svgpp_context.h +++ b/tests/svgpp_context.h @@ -63,6 +63,8 @@ public: void set(svgpp::tag::attribute::y y, double v); void set(svgpp::tag::attribute::width width, double v); void set(svgpp::tag::attribute::height height, double v); + void set(svgpp::tag::attribute::class_ a, + boost::iterator_range v); void set_text(boost::iterator_range v); private: diff --git a/tests/svgpp_document_traverser.cpp b/tests/svgpp_document_traverser.cpp index 93f941f13..c22cfd296 100644 --- a/tests/svgpp_document_traverser.cpp +++ b/tests/svgpp_document_traverser.cpp @@ -24,10 +24,14 @@ void traverseDocumentWithSvgpp(SvgppContext &context, char *text) { svgpp::tag::element::rect, svgpp::tag::element::text, svgpp::tag::element::title>::type; + using processed_attributes_t = + boost::mpl::set::type; + svgpp::document_traversal< svgpp::processed_elements, - svgpp::processed_attributes< - svgpp::traits::shapes_attributes_by_element>, + svgpp::processed_attributes, svgpp::basic_shapes_policy>:: load_document(xml_root_element, context); } diff --git a/tests/test_svg_analyzer.cpp b/tests/test_svg_analyzer.cpp index 84a2f37ee..fb97d6ae2 100644 --- a/tests/test_svg_analyzer.cpp +++ b/tests/test_svg_analyzer.cpp @@ -135,7 +135,9 @@ TEST_CASE( // yet compare with the original SVG CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); - CHECK(recreated_svg.find("") != std::string::npos); + CHECK(recreated_svg.find("") != std::string::npos); + CHECK(recreated_svg.find("") != std::string::npos); + CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("g1") != std::string::npos); CHECK(recreated_svg.find("a") != std::string::npos);