Skip to content

Commit

Permalink
fix: Mermaid render of graph views was empty (#175)
Browse files Browse the repository at this point in the history
Fixes #174
  • Loading branch information
aborgna-q authored Jan 20, 2025
1 parent 8f46ae0 commit fbd65b7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ impl EdgeStyle {
#[cfg(test)]
mod test {
use std::fmt::Display;
use std::sync::OnceLock;

use rstest::{fixture, rstest};

use crate::view::Region;
use crate::{Hierarchy, LinkMut, PortGraph, PortMut, PortView, Weights};

use super::{DotFormat, MermaidFormat};
Expand Down Expand Up @@ -247,11 +249,45 @@ mod test {
("weighted", graph, None, Some(weights))
}

#[allow(clippy::type_complexity)]
type RegionViewResult = (
&'static str,
Region<'static, PortGraph>,
Option<Hierarchy>,
Option<Weights<String, String>>,
);
#[fixture]
fn region_view() -> RegionViewResult {
let mut graph = PortGraph::new();
let other = graph.add_node(42, 0);
let root = graph.add_node(1, 0);
let a = graph.add_node(1, 2);
let b = graph.add_node(0, 0);
let c = graph.add_node(0, 0);
graph.link_nodes(a, 0, other, 0).unwrap();
graph.link_nodes(a, 1, root, 0).unwrap();

static HIERARCHY: OnceLock<Hierarchy> = OnceLock::new();
let hierarchy = HIERARCHY.get_or_init(|| {
let mut hierarchy = Hierarchy::new();
hierarchy.push_child(root, other).unwrap();
hierarchy.push_child(a, root).unwrap();
hierarchy.push_child(b, root).unwrap();
hierarchy.push_child(c, b).unwrap();
hierarchy
});

let region = Region::new(graph, hierarchy, root);

("region_view", region, Some(hierarchy.clone()), None)
}

#[rstest]
#[case::flat(flat_graph())]
#[case::hierarchy(hierarchy_graph())]
#[case::interregional(hierarchy_interregional_graph())]
#[case::weighted(weighted_graph())]
#[case::region_view(region_view())]
#[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri
fn mermaid_output<WN: Display + Clone, WP>(
#[case] graph_elems: (
Expand Down
8 changes: 7 additions & 1 deletion src/render/mermaid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ where

/// Helper function to check if a node is a leaf in the hierarchy.
fn is_root(&self, node: NodeIndex) -> bool {
self.forest.map_or(true, |f| f.is_root(node))
let Some(f) = &self.forest else {
return true;
};
f.is_root(node)
// Special case for filtered graphs.
|| f.parent(node)
.map_or(true, |p| !self.graph.contains_node(p))
}

/// Helper function to check if a node is a leaf in the hierarchy.
Expand Down
14 changes: 14 additions & 0 deletions src/snapshots/portgraph__render__test__region_view__mermaid.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: src/render.rs
expression: mermaid
---
graph LR
subgraph 1 [1]
direction LR
2[2]
subgraph 3 [3]
direction LR
4[4]
end
end
2-->1

0 comments on commit fbd65b7

Please sign in to comment.