| 1 |
#include <format> |
| 2 |
#include <string_view> |
| 3 |
|
| 4 |
#include <catch2/catch_all.hpp> |
| 5 |
|
| 6 |
#include "svg_analyzer.h" |
| 7 |
#include "test_utilities.h" |
| 8 |
|
| 9 |
TEST_CASE("Graph rankdir", "Test that the Graphviz `rankdir` attribute affects " |
| 10 |
"the relative placement of nodes and edges " |
| 11 |
"correctly when the 'dot` layout engine is used") { |
| 12 |
|
| 13 |
const auto rankdir = GENERATE(from_range(all_rank_directions)); |
| 14 |
INFO("Rankdir: " << rankdir); |
| 15 |
|
| 16 |
const auto shape = GENERATE(filter( |
| 17 |
[](std::string_view s) { |
| 18 |
return !node_shapes_without_svg_shape.contains(s); |
| 19 |
}, |
| 20 |
from_range(all_node_shapes))); |
| 21 |
INFO("Shape: " << shape); |
| 22 |
|
| 23 |
auto dot = std::format( |
| 24 |
"digraph g1 {{rankdir={}; node [shape={} fontname=Courier]; a -> b}}", |
| 25 |
rankdir, shape); |
| 26 |
|
| 27 |
const auto engine = "dot"; |
| 28 |
auto svg_analyzer = SVGAnalyzer::make_from_dot(dot, engine); |
| 29 |
|
| 30 |
REQUIRE(svg_analyzer.graphs().size() == 1); |
| 31 |
const auto &graph = svg_analyzer.graphs().back(); |
| 32 |
const auto node_a = graph.node("a"); |
| 33 |
const auto node_b = graph.node("b"); |
| 34 |
const auto edge_ab = graph.edge("a->b"); |
| 35 |
if (rankdir == "TB") { |
| 36 |
CHECK(node_a.center().is_higher_than(node_b.center())); |
| 37 |
CHECK(node_a.center().is_higher_than(edge_ab.center())); |
| 38 |
CHECK(edge_ab.center().is_higher_than(node_b.center())); |
| 39 |
} else if (rankdir == "BT") { |
| 40 |
CHECK(node_a.center().is_lower_than(node_b.center())); |
| 41 |
CHECK(node_a.center().is_lower_than(edge_ab.center())); |
| 42 |
CHECK(edge_ab.center().is_lower_than(node_b.center())); |
| 43 |
} else if (rankdir == "LR") { |
| 44 |
CHECK(node_a.center().is_more_left_than(node_b.center())); |
| 45 |
CHECK(node_a.center().is_more_left_than(edge_ab.center())); |
| 46 |
CHECK(edge_ab.center().is_more_left_than(node_b.center())); |
| 47 |
} else if (rankdir == "RL") { |
| 48 |
CHECK(node_a.center().is_more_right_than(node_b.center())); |
| 49 |
CHECK(node_a.center().is_more_right_than(edge_ab.center())); |
| 50 |
CHECK(edge_ab.center().is_more_right_than(node_b.center())); |
| 51 |
} |
| 52 |
} |