| 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("Node color", |
| 10 |
"Test that the Graphviz `color` attribute is used to set the " |
| 11 |
"`stroke` and `stroke-opacity` attributes correctly for nodes in the " |
| 12 |
"generated SVG") { |
| 13 |
|
| 14 |
const auto shape = GENERATE(filter( |
| 15 |
[](std::string_view s) { |
| 16 |
return !node_shapes_without_svg_shape.contains(s); |
| 17 |
}, |
| 18 |
from_range(all_node_shapes))); |
| 19 |
INFO("Shape: " << shape); |
| 20 |
|
| 21 |
const auto node_rgb_color = GENERATE("#000000", "#ffffff", "#5580aa"); |
| 22 |
const auto opacity = GENERATE(0, 100, 255); |
| 23 |
const auto node_rgba_color = std::format("{}{:02x}", node_rgb_color, opacity); |
| 24 |
INFO("Node color: " << node_rgba_color); |
| 25 |
|
| 26 |
auto dot = std::format("digraph g1 {{node [shape={} color=\"{}\"]; a -> b}}", |
| 27 |
shape, node_rgba_color); |
| 28 |
|
| 29 |
const auto engine = "dot"; |
| 30 |
auto svg_analyzer = SVGAnalyzer::make_from_dot(dot, engine); |
| 31 |
|
| 32 |
const auto expected_node_color = [&]() -> const std::string { |
| 33 |
if (opacity == 255) { |
| 34 |
return node_rgb_color; |
| 35 |
} |
| 36 |
if (opacity == 0) { |
| 37 |
return "#00000000"; // transparent/none |
| 38 |
} |
| 39 |
return node_rgba_color; |
| 40 |
}(); |
| 41 |
|
| 42 |
for (const auto &graph : svg_analyzer.graphs()) { |
| 43 |
for (const auto &node : graph.nodes()) { |
| 44 |
if (shape == "underline" && opacity != 0) { |
| 45 |
// FIXME: The 'underline' node shape consists of a transparent polygon |
| 46 |
// and a polyline with the specified color which the node color |
| 47 |
// retrieval function cannot yet handle (unless the specified color is |
| 48 |
// also transparent) and will throw an exception for |
| 49 |
REQUIRE_THROWS_AS(node.color(), std::runtime_error); |
| 50 |
continue; |
| 51 |
} |
| 52 |
CHECK(node.color() == expected_node_color); |
| 53 |
} |
| 54 |
} |
| 55 |
} |