Debian Codegraph @

tests/test_node_fillcolor.cpp

raw
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 fillcolor",
10 "Test that the Graphviz `fillcolor` attribute is used to set "
11 "the `fill` and `fill-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_fillcolor =
22 GENERATE("#000000", "#ffffff", "#ff0000", "#aa0f55");
23 const auto opacity = GENERATE(0, 200, 255);
24 const auto node_rgba_fillcolor =
25 std::format("{}{:02x}", node_rgb_fillcolor, opacity);
26 INFO("Node fillcolor: " << node_rgba_fillcolor);
27
28 auto dot = std::format(
29 "digraph g1 {{node [shape={} style=filled fillcolor=\"{}\"]; a -> b}}",
30 shape, node_rgba_fillcolor);
31
32 const auto engine = "dot";
33 auto svg_analyzer = SVGAnalyzer::make_from_dot(dot, engine);
34
35 const auto expected_node_fillcolor = [&]() -> const std::string {
36 if (opacity == 255) {
37 return node_rgb_fillcolor;
38 }
39 if (opacity == 0) {
40 return "#00000000"; // transparent/none
41 }
42 return node_rgba_fillcolor;
43 }();
44
45 for (const auto &graph : svg_analyzer.graphs()) {
46 for (const auto &node : graph.nodes()) {
47 if (contains_multiple_shapes_with_different_fill(shape) && opacity != 0) {
48 // FIXME: these node shapes consists of both a transparent shape and a
49 // shape with the specified filllcolor which the node fillcolor
50 // retrieval function cannot yet handle (unless the specified fillcolor
51 // is also transparent) and will throw an exception for
52 REQUIRE_THROWS_AS(node.fillcolor(), std::runtime_error);
53 continue;
54 }
55 CHECK(node.fillcolor() == expected_node_fillcolor);
56 }
57 }
58 }