From 514eff0f03c2f092bd3b1ecc396c5282610429d0 Mon Sep 17 00:00:00 2001 From: Magnus Jacobsson Date: Tue, 13 Sep 2022 13:00:51 +0200 Subject: [PATCH] tests: test_edge_node_overlap_simple: generate DOT based on options Generate DOT source based on given options which are also passed to the check function. Upcoming commits will add checks which need to know details about how the graph was generated. --- tests/test_edge_node_overlap_simple.cpp | 9 ++++--- tests/test_edge_node_overlap_utilities.cpp | 29 ++++++++++++++++++++-- tests/test_edge_node_overlap_utilities.h | 9 ++++++- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/tests/test_edge_node_overlap_simple.cpp b/tests/test_edge_node_overlap_simple.cpp index 78321fac2..21a88b299 100644 --- a/tests/test_edge_node_overlap_simple.cpp +++ b/tests/test_edge_node_overlap_simple.cpp @@ -9,10 +9,13 @@ TEST_CASE( "Overlap", "[!shouldfail] An edge connected to a node shall not overlap that node") { - std::string dot = - "digraph {node[shape=polygon penwidth=2 fontname=Courier] a -> b}"; + const graph_options graph_options = { + .node_shape = "polygon", + .node_penwidth = 2, + .edge_penwidth = 2, + }; const auto filename_base = AUTO_NAME(); - test_edge_node_overlap(dot, {.filename_base = filename_base}); + test_edge_node_overlap(graph_options, {.filename_base = filename_base}); } diff --git a/tests/test_edge_node_overlap_utilities.cpp b/tests/test_edge_node_overlap_utilities.cpp index 52b780fd3..3597ea72a 100644 --- a/tests/test_edge_node_overlap_utilities.cpp +++ b/tests/test_edge_node_overlap_utilities.cpp @@ -1,3 +1,6 @@ +#include +#include + #include #include #include @@ -8,8 +11,11 @@ /// check that edges do not overlap nodes static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer, + const graph_options &graph_options, const check_options &check_options) { + const auto rankdir = graph_options.rankdir; + REQUIRE(svg_analyzer.graphs().size() == 1); auto &recreated_graph = svg_analyzer.graphs().back(); @@ -39,6 +45,8 @@ static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer, INFO(fmt::format(" height: {:.3f}", overlap_bbox.height)); // FIXME: add support for rank direction "LR" and "RL". For now assume // "TB" or "BT" and check only in the vertical direction + assert((rankdir == "TB" || rankdir == "BT") && + "Only rankdir=TB or rankdir=BT is supported"); const auto head_node_edge_overlap = overlap_bbox.height; // check maximum head node and edge overlap @@ -56,6 +64,8 @@ static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer, INFO(fmt::format(" height: {:.6f}", overlap_bbox.height)); // FIXME: add support for rank direction "LR" and "RL". For now assume // "TB" or "BT" and check only in the vertical direction + assert((rankdir == "TB" || rankdir == "BT") && + "Only rankdir=TB or rankdir=BT is supported"); const auto tail_node_edge_overlap = overlap_bbox.height; // check maximum tail node and edge overlap @@ -104,8 +114,22 @@ static void write_svg_files(SVGAnalyzer &svg_analyzer, } } -void test_edge_node_overlap(const std::string &dot, +/// generate DOT source based on given options +static std::string generate_dot(const graph_options &graph_options) { + return fmt::format("digraph g1 {{" + " graph [rankdir={}]" + " node [penwidth={} shape={} fontname=Courier]" + " edge [penwidth={}]" + " a -> b" + "}}", + graph_options.rankdir, graph_options.node_penwidth, + graph_options.node_shape, graph_options.edge_penwidth); +} + +void test_edge_node_overlap(const graph_options &graph_options, const write_options &write_options) { + const auto dot = generate_dot(graph_options); + auto svg_analyzer = SVGAnalyzer::make_from_dot(dot); // The binary search in the bezier_clip function in lib/common/splines.c has a @@ -125,7 +149,8 @@ void test_edge_node_overlap(const std::string &dot, .svg_rounding_error = graphviz_max_svg_rounding_error, }; - const auto success = check_analyzed_svg(svg_analyzer, check_options); + const auto success = + check_analyzed_svg(svg_analyzer, graph_options, check_options); if (!success || write_options.write_svg_on_success) { write_svg_files(svg_analyzer, check_options, write_options); diff --git a/tests/test_edge_node_overlap_utilities.h b/tests/test_edge_node_overlap_utilities.h index 395bedd64..8fa656de4 100644 --- a/tests/test_edge_node_overlap_utilities.h +++ b/tests/test_edge_node_overlap_utilities.h @@ -9,6 +9,13 @@ struct check_options { double svg_rounding_error; }; +struct graph_options { + std::string_view rankdir = "TB"; + std::string_view node_shape = "polygon"; + double node_penwidth = 1; + double edge_penwidth = 1; +}; + struct write_options { std::string filename_base = "test_edge_node_overlap"; bool write_svg_on_success = false; @@ -19,5 +26,5 @@ struct write_options { /// generate an SVG graph from the `dot` source and check that edges don't /// overlap nodes -void test_edge_node_overlap(const std::string &dot, +void test_edge_node_overlap(const graph_options &graph_options = {}, const write_options &write_options = {}); -- 2.40.0