From 1e7489d74299192a125291b2e98c6e00e4015dad Mon Sep 17 00:00:00 2001 From: Magnus Jacobsson Date: Sun, 28 Aug 2022 17:36:31 +0200 Subject: [PATCH] tests: test_edge_node_overlap_utilities: add support for writing of SVG files for manual inspection --- tests/test_edge_node_overlap_utilities.cpp | 32 ++++++++++++++++++++-- tests/test_edge_node_overlap_utilities.h | 10 ++++++- tests/test_utilities.cpp | 17 ++++++++++++ tests/test_utilities.h | 7 +++++ 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/tests/test_edge_node_overlap_utilities.cpp b/tests/test_edge_node_overlap_utilities.cpp index 80cf89a21..9698e3eb5 100644 --- a/tests/test_edge_node_overlap_utilities.cpp +++ b/tests/test_edge_node_overlap_utilities.cpp @@ -4,6 +4,7 @@ #include "svg_analyzer.h" #include "test_edge_node_overlap_utilities.h" +#include "test_utilities.h" /// check that edges do not overlap nodes static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer, @@ -66,7 +67,31 @@ static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer, return success; } -void test_edge_node_overlap(const std::string &dot) { +/// write SVG files for manual analysis if any of the above checks failed or if +/// we explicitly have requested it +static void write_svg_files(const SVGAnalyzer &svg_analyzer, + const write_options &write_options) { + const std::filesystem::path test_artifacts_directory = "test_artifacts"; + + if (write_options.write_original_svg) { + // write the original SVG generated by Graphviz to a file + const std::filesystem::path filename = + write_options.filename_base + "_original.svg"; + write_to_file(test_artifacts_directory, filename, + svg_analyzer.original_svg()); + } + + if (write_options.write_recreated_svg) { + // write the SVG recreated by the SVG analyzer to a file + const std::filesystem::path filename = + write_options.filename_base + "_recreated.svg"; + const auto recreated_svg = svg_analyzer.svg_string(); + write_to_file(test_artifacts_directory, filename, recreated_svg); + } +} + +void test_edge_node_overlap(const std::string &dot, + const write_options &write_options) { auto svg_analyzer = SVGAnalyzer::make_from_dot(dot); // The binary search in the bezier_clip function in lib/common/splines.c has a @@ -88,6 +113,7 @@ void test_edge_node_overlap(const std::string &dot) { const auto success = check_analyzed_svg(svg_analyzer, check_options); - // FIXME: add writing of SVG files for manual inspection when not success - REQUIRE(success); + if (!success || write_options.write_svg_on_success) { + write_svg_files(svg_analyzer, write_options); + } } diff --git a/tests/test_edge_node_overlap_utilities.h b/tests/test_edge_node_overlap_utilities.h index bfe1f234b..b77b0c4a4 100644 --- a/tests/test_edge_node_overlap_utilities.h +++ b/tests/test_edge_node_overlap_utilities.h @@ -9,6 +9,14 @@ struct check_options { double svg_rounding_error; }; +struct write_options { + std::string filename_base = "test_edge_node_overlap"; + bool write_svg_on_success = false; + bool write_original_svg = false; + bool write_recreated_svg = true; +}; + /// 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 std::string &dot, + const write_options &write_options = {}); diff --git a/tests/test_utilities.cpp b/tests/test_utilities.cpp index da7bb6eb7..7cc2bf458 100644 --- a/tests/test_utilities.cpp +++ b/tests/test_utilities.cpp @@ -1,6 +1,11 @@ +#include +#include +#include #include #include +#include + #include "test_utilities.h" const std::unordered_set node_shapes_consisting_of_ellipse = { @@ -190,3 +195,15 @@ const std::unordered_set const std::unordered_set all_rank_directions = {"TB", "BT", "LR", "RL"}; +void write_to_file(const std::filesystem::path &directory, + const std::filesystem::path &filename, + const std::string_view text) { + std::filesystem::create_directories(directory); + const std::filesystem::path test_artifacts_path = directory / filename; + std::ofstream outfile{test_artifacts_path}; + if (!outfile.is_open()) { + throw std::runtime_error{fmt::format("Could not create output file \"{}\"", + test_artifacts_path.native())}; + } + outfile << text; +} diff --git a/tests/test_utilities.h b/tests/test_utilities.h index 887bb7a4d..b051f8a62 100644 --- a/tests/test_utilities.h +++ b/tests/test_utilities.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -34,3 +35,9 @@ extern const std::unordered_set /// rank directions extern const std::unordered_set all_rank_directions; + +/// misc utilities + +void write_to_file(const std::filesystem::path &directory, + const std::filesystem::path &filename, + std::string_view text); -- 2.40.0