From: Magnus Jacobsson Date: Wed, 27 Jul 2022 14:17:28 +0000 (+0200) Subject: tests: SvgAnalyzer: add handling of the 'points' attribute X-Git-Tag: 5.0.1~7^2~8 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=232290fd7d58d6fb7129150256102c1868ec25ac;p=graphviz tests: SvgAnalyzer: add handling of the 'points' attribute --- diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index 501171ec2..3c004959b 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -131,6 +131,10 @@ void SVGAnalyzer::set_id(std::string_view id) { current_element().attributes.id = id; } +void SVGAnalyzer::set_point(std::pair point) { + current_element().attributes.points.emplace_back(point.first, point.second); +} + 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 212fe66a6..a21fec1af 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -32,6 +32,7 @@ public: void set_id(std::string_view id) override; void set_class(std::string_view) override; void set_stroke(std::string_view stroke) override; + void set_point(std::pair point) override; void set_text(std::string_view text) override; void set_transform(double a, double b, double c, double d, double e, double f) override; diff --git a/tests/svg_analyzer_interface.h b/tests/svg_analyzer_interface.h index 086b9ff1d..907c2112a 100644 --- a/tests/svg_analyzer_interface.h +++ b/tests/svg_analyzer_interface.h @@ -30,6 +30,7 @@ public: virtual void set_fill(std::string_view fill) = 0; virtual void set_height(double height) = 0; virtual void set_id(std::string_view id) = 0; + virtual void set_point(std::pair point) = 0; virtual void set_stroke(std::string_view stroke) = 0; virtual void set_text(std::string_view text) = 0; virtual void set_transform(double a, double b, double c, double d, double e, diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index 2d1702331..3a8084abc 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -82,6 +82,18 @@ std::string SVG::SVGElement::id_attribute_to_string() const { return fmt::format(R"(id="{}")", attributes.id); } +std::string SVG::SVGElement::points_attribute_to_string() const { + std::string points_attribute_str = R"|(points=")|"; + const char *separator = ""; + for (const auto &point : attributes.points) { + points_attribute_str += separator + fmt::format("{},{}", point.x, point.y); + separator = " "; + } + points_attribute_str += '"'; + + return points_attribute_str; +} + std::string SVG::SVGElement::stroke_attribute_to_string() const { if (attributes.stroke.empty()) { return ""; @@ -148,10 +160,12 @@ void SVG::SVGElement::to_string_impl(std::string &output, case SVG::SVGElementType::Polygon: append_attribute(attributes_str, fill_attribute_to_string()); append_attribute(attributes_str, stroke_attribute_to_string()); + append_attribute(attributes_str, points_attribute_to_string()); break; case SVG::SVGElementType::Polyline: append_attribute(attributes_str, fill_attribute_to_string()); append_attribute(attributes_str, stroke_attribute_to_string()); + append_attribute(attributes_str, points_attribute_to_string()); break; case SVG::SVGElementType::Svg: attributes_str += fmt::format( diff --git a/tests/svg_element.h b/tests/svg_element.h index 4e701c527..a23014212 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -8,6 +8,11 @@ namespace SVG { +struct SVGPoint { + double x; + double y; +}; + struct SVGRect { double x; double y; @@ -45,6 +50,7 @@ struct SVGAttributes { std::string fill; double height; std::string id; + std::vector points; std::string stroke; std::optional transform; SVGRect viewBox; @@ -85,6 +91,7 @@ private: const std::string &attribute) const; std::string id_attribute_to_string() const; std::string fill_attribute_to_string() const; + std::string points_attribute_to_string() const; std::string stroke_attribute_to_string() const; std::string stroke_to_graphviz_color(const std::string &color) const; void to_string_impl(std::string &output, std::size_t indent_size, diff --git a/tests/svgpp_context.cpp b/tests/svgpp_context.cpp index e75a7cf81..6b17f1d09 100644 --- a/tests/svgpp_context.cpp +++ b/tests/svgpp_context.cpp @@ -235,11 +235,11 @@ void SvgppContext::set(svgpp::tag::attribute::viewBox, const double v1, m_svgAnalyzer->set_viewBox(v1, v2, v3, v4); } -void SvgppContext::set_impl(svgpp::tag::attribute::points &points, - const std::any &range) { - (void)points; - (void)range; - // ignore for now +void SvgppContext::set(svgpp::tag::attribute::points, + const SvgppContext::PointsRange &range) { + for (auto &it : range) { + m_svgAnalyzer->set_point(it); + } } void SvgppContext::set_text(boost::iterator_range v) { diff --git a/tests/svgpp_context.h b/tests/svgpp_context.h index 35b88d275..cfbbfb60a 100644 --- a/tests/svgpp_context.h +++ b/tests/svgpp_context.h @@ -1,8 +1,8 @@ #pragma once -#include #include +#include #include #include @@ -157,10 +157,11 @@ public: void set(svgpp::tag::attribute::y1 y1, double v); void set(svgpp::tag::attribute::x2 x2, double v); void set(svgpp::tag::attribute::y2 y2, double v); - template - void set(svgpp::tag::attribute::points points, const Range &range) { - set_impl(points, range); - } + typedef boost::any_range, + boost::single_pass_traversal_tag, + const std::pair &, std::ptrdiff_t> + PointsRange; + void set(svgpp::tag::attribute::points points, const PointsRange &range); void set(svgpp::tag::attribute::x a, double v); void set(svgpp::tag::attribute::y y, double v); void set(svgpp::tag::attribute::width width, double v); @@ -173,7 +174,5 @@ public: void set_text(boost::iterator_range v); private: - void set_impl(svgpp::tag::attribute::points &points, const std::any &range); - ISVGAnalyzer *m_svgAnalyzer = nullptr; }; diff --git a/tests/svgpp_document_traverser.cpp b/tests/svgpp_document_traverser.cpp index 3894ded48..9dfed9dfb 100644 --- a/tests/svgpp_document_traverser.cpp +++ b/tests/svgpp_document_traverser.cpp @@ -30,6 +30,7 @@ void traverseDocumentWithSvgpp(SvgppContext &context, char *text) { svgpp::tag::attribute::fill, // svgpp::tag::attribute::height, // svgpp::tag::attribute::id, // + svgpp::tag::attribute::points, // svgpp::tag::attribute::stroke, // svgpp::tag::attribute::transform, // svgpp::tag::attribute::viewBox, // diff --git a/tests/test_svg_analyzer.cpp b/tests/test_svg_analyzer.cpp index 32197d7ea..cbb1d2a68 100644 --- a/tests/test_svg_analyzer.cpp +++ b/tests/test_svg_analyzer.cpp @@ -123,10 +123,20 @@ 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].starts_with("") { - // stop comparison here since we do not yet handle all attributes on the - // 'polygon' element + ("")) { + // stop comparison here for the 'cylinder' node shape since we do not + // yet handle all attributes on the 'path' element + break; + } + if (recreated_svg_lines[i] == "a") { + // stop comparison here for polygon based shapes since we do not yet + // handle all attributes on the 'text' element break; } REQUIRE(recreated_svg_lines[i] == original_svg_lines[i]); @@ -141,8 +151,6 @@ TEST_CASE( CHECK(recreated_svg.find("a") != std::string::npos); CHECK(recreated_svg.find("b") != std::string::npos); } - CHECK(recreated_svg.find("") != - std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos);