From: Magnus Jacobsson Date: Wed, 27 Jul 2022 09:05:08 +0000 (+0200) Subject: tests: SvgAnalyzer: add handling of the 'id' attribute X-Git-Tag: 5.0.1~7^2~13 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b33b48d44dd19b9605c7587c987c550bb5c0c354;p=graphviz tests: SvgAnalyzer: add handling of the 'id' attribute --- diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index 118f869af..cd1e88a99 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -119,6 +119,10 @@ void SVGAnalyzer::set_height(double height) { current_element().attributes.height = height; } +void SVGAnalyzer::set_id(std::string_view id) { + current_element().attributes.id = id; +} + 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 541d600cd..26c6bd633 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -28,6 +28,7 @@ public: void on_enter_element_title() override; void on_exit_element() override; void set_height(double height) override; + void set_id(std::string_view id) override; void set_class(std::string_view) override; void set_text(std::string_view text) override; void set_viewBox(double x, double y, double width, double height) override; diff --git a/tests/svg_analyzer_interface.h b/tests/svg_analyzer_interface.h index ac186e953..48a10f2c4 100644 --- a/tests/svg_analyzer_interface.h +++ b/tests/svg_analyzer_interface.h @@ -28,6 +28,7 @@ public: virtual void on_exit_element() = 0; virtual void set_class(std::string_view) = 0; virtual void set_height(double height) = 0; + virtual void set_id(std::string_view id) = 0; virtual void set_text(std::string_view text) = 0; virtual void set_viewBox(double x, double y, double width, double height) = 0; virtual void set_width(double width) = 0; diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index 639fa9554..2a50161c5 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -36,6 +36,25 @@ static std::string xml_encode(const std::string &text) { return out; } +void SVG::SVGElement::append_attribute(std::string &output, + const std::string &attribute) const { + if (attribute.empty()) { + return; + } + if (!output.empty()) { + output += " "; + } + output += attribute; +} + +std::string SVG::SVGElement::id_attribute_to_string() const { + if (attributes.id.empty()) { + return ""; + } + + return fmt::format(R"(id="{}")", attributes.id); +} + std::string SVG::SVGElement::to_string(std::size_t indent_size = 2) const { std::string output; output += R"()" @@ -71,12 +90,13 @@ void SVG::SVGElement::to_string_impl(std::string &output, output += tag(type); std::string attributes_str{}; + append_attribute(attributes_str, id_attribute_to_string()); switch (type) { case SVG::SVGElementType::Ellipse: // ignore for now break; case SVG::SVGElementType::Group: - attributes_str += fmt::format(R"(class="{}")", attributes.class_); + attributes_str += fmt::format(R"( class="{}")", attributes.class_); break; case SVG::SVGElementType::Path: // ignore for now diff --git a/tests/svg_element.h b/tests/svg_element.h index 628bede7c..e910a7cb1 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -33,6 +33,7 @@ std::string_view tag(SVG::SVGElementType type); struct SVGAttributes { std::string class_; double height; + std::string id; SVGRect viewBox; double width; }; @@ -65,6 +66,11 @@ public: const SVGElementType type; private: + /// append a string possibly containing an attribute to another string, + /// handling space separation + void append_attribute(std::string &output, + const std::string &attribute) const; + std::string id_attribute_to_string() const; void to_string_impl(std::string &output, std::size_t indent_size, std::size_t current_indent) const; }; diff --git a/tests/svgpp_context.cpp b/tests/svgpp_context.cpp index 1a311a220..24d33c394 100644 --- a/tests/svgpp_context.cpp +++ b/tests/svgpp_context.cpp @@ -170,6 +170,11 @@ void SvgppContext::set(svgpp::tag::attribute::height, const double v) { m_svgAnalyzer->set_height(v); } +void SvgppContext::set(svgpp::tag::attribute::id, + boost::iterator_range v) { + m_svgAnalyzer->set_id({v.begin(), v.end()}); +} + void SvgppContext::set(svgpp::tag::attribute::class_, boost::iterator_range v) { m_svgAnalyzer->set_class({v.begin(), v.end()}); diff --git a/tests/svgpp_context.h b/tests/svgpp_context.h index d52053720..ec9a5b68b 100644 --- a/tests/svgpp_context.h +++ b/tests/svgpp_context.h @@ -63,6 +63,7 @@ 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::id a, boost::iterator_range v); void set(svgpp::tag::attribute::class_ a, boost::iterator_range v); void set(svgpp::tag::attribute::viewBox a, double v1, double v2, double v3, diff --git a/tests/svgpp_document_traverser.cpp b/tests/svgpp_document_traverser.cpp index 824834bd2..ad63276c1 100644 --- a/tests/svgpp_document_traverser.cpp +++ b/tests/svgpp_document_traverser.cpp @@ -28,6 +28,7 @@ void traverseDocumentWithSvgpp(SvgppContext &context, char *text) { boost::mpl::set::type; diff --git a/tests/test_svg_analyzer.cpp b/tests/test_svg_analyzer.cpp index a77530f4f..786681f62 100644 --- a/tests/test_svg_analyzer.cpp +++ b/tests/test_svg_analyzer.cpp @@ -123,7 +123,7 @@ TEST_CASE( 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()); - if (recreated_svg_lines[i] == "") { + if (recreated_svg_lines[i] == "") { // stop comparison here since we do not yet handle all attributes on the // 'g' element break; @@ -133,9 +133,14 @@ TEST_CASE( // do some sanity checks of the parts of the recreated SVG that we cannot // 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("") != std::string::npos); CHECK(recreated_svg.find("g1") != std::string::npos); CHECK(recreated_svg.find("a") != std::string::npos);