diff --git a/src/builder.rs b/src/builder.rs index a893fb44a..c0cecfc65 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -178,8 +178,8 @@ pub(crate) mod test { use super::handle::BuildHandle; use super::{ - BuildError, Container, DFGBuilder, Dataflow, DataflowHugr, FuncID, FunctionBuilder, - ModuleBuilder, + BuildError, CFGBuilder, Container, DFGBuilder, Dataflow, DataflowHugr, FuncID, + FunctionBuilder, ModuleBuilder, }; use super::{DataflowSubContainer, HugrBuilder}; @@ -214,6 +214,14 @@ pub(crate) mod test { dfg_builder.finish_prelude_hugr_with_outputs([i1]).unwrap() } + #[fixture] + pub(crate) fn simple_cfg_hugr() -> Hugr { + let mut cfg_builder = + CFGBuilder::new(FunctionType::new(type_row![NAT], type_row![NAT])).unwrap(); + super::cfg::test::build_basic_cfg(&mut cfg_builder).unwrap(); + cfg_builder.finish_prelude_hugr().unwrap() + } + /// A helper method which creates a DFG rooted hugr with closed resources, /// for tests which want to avoid having open extension variables after /// inference. Using DFGBuilder will default to a root node with an open diff --git a/src/builder/cfg.rs b/src/builder/cfg.rs index a9a94da1d..b9b7e2621 100644 --- a/src/builder/cfg.rs +++ b/src/builder/cfg.rs @@ -404,7 +404,7 @@ impl BlockBuilder { } #[cfg(test)] -mod test { +pub(crate) mod test { use crate::builder::build_traits::HugrBuilder; use crate::builder::{DataflowSubContainer, ModuleBuilder}; @@ -453,7 +453,7 @@ mod test { Ok(()) } - fn build_basic_cfg + AsRef>( + pub(crate) fn build_basic_cfg + AsRef>( cfg_builder: &mut CFGBuilder, ) -> Result<(), BuildError> { let sum2_variants = vec![type_row![NAT], type_row![NAT]]; diff --git a/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_cfg.snap b/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_cfg.snap new file mode 100644 index 000000000..328baa096 --- /dev/null +++ b/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_cfg.snap @@ -0,0 +1,5 @@ +--- +source: src/hugr/views/tests.rs +expression: h.dot_string() +--- +"digraph {\n0 [shape=plain label=<
(0) CFG
>]\n1 [shape=plain label=<
0
(1) ExitBlock
>]\n2 [shape=plain label=<
0
(2) DataflowBlock
01
>]\n2:out0 -> 7:in0 [style=\"dashed\"]\n2:out1 -> 1:in0 [style=\"dashed\"]\n3 [shape=plain label=<
(3) Input
0: usize
>]\n3:out0 -> 5:in0 [style=\"\"]\n4 [shape=plain label=<
0: Sum([Tuple([usize]), Tuple([usize])])
(4) Output
>]\n5 [shape=plain label=<
0: usize
(5) MakeTuple
0: Tuple([usize])
>]\n5:out0 -> 6:in0 [style=\"\"]\n6 [shape=plain label=<
0: Tuple([usize])
(6) Tag
0: Sum([Tuple([usize]), Tuple([usize])])
>]\n6:out0 -> 4:in0 [style=\"\"]\n7 [shape=plain label=<
0
(7) DataflowBlock
0
>]\n7:out0 -> 1:in0 [style=\"dashed\"]\n8 [shape=plain label=<
(8) Input
0: usize
>]\n8:out0 -> 9:in1 [style=\"\"]\n9 [shape=plain label=<
0: Sum(UnitSum(1))1: usize
(9) Output
>]\n10 [shape=plain label=<
(10) const:sum:{tag:0, val:const:seq:{}}
0: Sum(UnitSum(1))
>]\n10:out0 -> 11:in0 [style=\"\"]\n11 [shape=plain label=<
0: Sum(UnitSum(1))
(11) LoadConstant
0: Sum(UnitSum(1))
>]\n11:out0 -> 9:in0 [style=\"\"]\nhier0 [shape=plain label=\"0\"]\nhier0 -> hier2 [style = \"dashed\"] \nhier0 -> hier1 [style = \"dashed\"] \nhier0 -> hier7 [style = \"dashed\"] \nhier1 [shape=plain label=\"1\"]\nhier2 [shape=plain label=\"2\"]\nhier2 -> hier3 [style = \"dashed\"] \nhier2 -> hier4 [style = \"dashed\"] \nhier2 -> hier5 [style = \"dashed\"] \nhier2 -> hier6 [style = \"dashed\"] \nhier3 [shape=plain label=\"3\"]\nhier4 [shape=plain label=\"4\"]\nhier5 [shape=plain label=\"5\"]\nhier6 [shape=plain label=\"6\"]\nhier7 [shape=plain label=\"7\"]\nhier7 -> hier8 [style = \"dashed\"] \nhier7 -> hier9 [style = \"dashed\"] \nhier7 -> hier10 [style = \"dashed\"] \nhier7 -> hier11 [style = \"dashed\"] \nhier8 [shape=plain label=\"8\"]\nhier9 [shape=plain label=\"9\"]\nhier10 [shape=plain label=\"10\"]\nhier11 [shape=plain label=\"11\"]\n}\n" diff --git a/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_string.snap b/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_dfg.snap similarity index 99% rename from src/hugr/views/snapshots/hugr__hugr__views__tests__dot_string.snap rename to src/hugr/views/snapshots/hugr__hugr__views__tests__dot_dfg.snap index 2a04e5b4e..5e7d86242 100644 --- a/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_string.snap +++ b/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_dfg.snap @@ -3,4 +3,3 @@ source: src/hugr/views/tests.rs expression: h.dot_string() --- "digraph {\n0 [shape=plain label=<
(0) DFG
>]\n1 [shape=plain label=<
(1) Input
0: qubit1: qubit
>]\n1:out0 -> 3:in0 [style=\"\"]\n1:out1 -> 3:in1 [style=\"\"]\n2 [shape=plain label=<
0: qubit1: qubit
(2) Output
>]\n3 [shape=plain label=<
0: qubit1: qubit
(3) test.quantum.CX
0: qubit1: qubit
>]\n3:out0 -> 4:in1 [style=\"\"]\n3:out1 -> 4:in0 [style=\"\"]\n3:out2 -> 4:in2 [style=\"dotted\"]\n4 [shape=plain label=<
0: qubit1: qubit
(4) test.quantum.CX
0: qubit1: qubit
>]\n4:out0 -> 2:in0 [style=\"\"]\n4:out1 -> 2:in1 [style=\"\"]\nhier0 [shape=plain label=\"0\"]\nhier0 -> hier1 [style = \"dashed\"] \nhier0 -> hier2 [style = \"dashed\"] \nhier0 -> hier3 [style = \"dashed\"] \nhier0 -> hier4 [style = \"dashed\"] \nhier1 [shape=plain label=\"1\"]\nhier2 [shape=plain label=\"2\"]\nhier3 [shape=plain label=\"3\"]\nhier4 [shape=plain label=\"4\"]\n}\n" - diff --git a/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_empty_dfg.snap b/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_empty_dfg.snap new file mode 100644 index 000000000..7180af21f --- /dev/null +++ b/src/hugr/views/snapshots/hugr__hugr__views__tests__dot_empty_dfg.snap @@ -0,0 +1,5 @@ +--- +source: src/hugr/views/tests.rs +expression: h.dot_string() +--- +"digraph {\n0 [shape=plain label=<
(0) DFG
>]\n1 [shape=plain label=<
(1) Input
0: Sum(UnitSum(2))
>]\n1:out0 -> 2:in0 [style=\"\"]\n2 [shape=plain label=<
0: Sum(UnitSum(2))
(2) Output
>]\nhier0 [shape=plain label=\"0\"]\nhier0 -> hier1 [style = \"dashed\"] \nhier0 -> hier2 [style = \"dashed\"] \nhier1 [shape=plain label=\"1\"]\nhier2 [shape=plain label=\"2\"]\n}\n" diff --git a/src/hugr/views/snapshots/hugr__hugr__views__tests__mmd_cfg.snap b/src/hugr/views/snapshots/hugr__hugr__views__tests__mmd_cfg.snap new file mode 100644 index 000000000..a626002e3 --- /dev/null +++ b/src/hugr/views/snapshots/hugr__hugr__views__tests__mmd_cfg.snap @@ -0,0 +1,32 @@ +--- +source: src/hugr/views/tests.rs +expression: h.mermaid_string() +--- +graph LR + subgraph 0 ["(0) CFG"] + direction LR + subgraph 2 ["(2) DataflowBlock"] + direction LR + 3["(3) Input"] + 3--"0:0
usize"-->5 + 4["(4) Output"] + 5["(5) MakeTuple"] + 5--"0:0
Tuple([usize])"-->6 + 6["(6) Tag"] + 6--"0:0
Sum([Tuple([usize]), Tuple([usize])])"-->4 + end + 2-."0:0".->7 + 2-."1:0".->1 + 1["(1) ExitBlock"] + subgraph 7 ["(7) DataflowBlock"] + direction LR + 8["(8) Input"] + 8--"0:1
usize"-->9 + 9["(9) Output"] + 10["(10) const:sum:{tag:0, val:const:seq:{}}"] + 10--"0:0
Sum(UnitSum(1))"-->11 + 11["(11) LoadConstant"] + 11--"0:0
Sum(UnitSum(1))"-->9 + end + 7-."0:0".->1 + end diff --git a/src/hugr/views/snapshots/hugr__hugr__views__tests__mermaid_string.snap b/src/hugr/views/snapshots/hugr__hugr__views__tests__mmd_dfg.snap similarity index 95% rename from src/hugr/views/snapshots/hugr__hugr__views__tests__mermaid_string.snap rename to src/hugr/views/snapshots/hugr__hugr__views__tests__mmd_dfg.snap index c330dcf4d..5e7debf9e 100644 --- a/src/hugr/views/snapshots/hugr__hugr__views__tests__mermaid_string.snap +++ b/src/hugr/views/snapshots/hugr__hugr__views__tests__mmd_dfg.snap @@ -1,6 +1,5 @@ --- source: src/hugr/views/tests.rs -assertion_line: 77 expression: h.mermaid_string() --- graph LR diff --git a/src/hugr/views/snapshots/hugr__hugr__views__tests__mmd_empty_dfg.snap b/src/hugr/views/snapshots/hugr__hugr__views__tests__mmd_empty_dfg.snap new file mode 100644 index 000000000..d807ab294 --- /dev/null +++ b/src/hugr/views/snapshots/hugr__hugr__views__tests__mmd_empty_dfg.snap @@ -0,0 +1,11 @@ +--- +source: src/hugr/views/tests.rs +expression: h.mermaid_string() +--- +graph LR + subgraph 0 ["(0) DFG"] + direction LR + 1["(1) Input"] + 1--"0:0
Sum(UnitSum(2))"-->2 + 2["(2) Output"] + end diff --git a/src/hugr/views/tests.rs b/src/hugr/views/tests.rs index 39a4f3af8..a61c83da3 100644 --- a/src/hugr/views/tests.rs +++ b/src/hugr/views/tests.rs @@ -61,20 +61,30 @@ fn node_connections( Ok(()) } +/// Render some hugrs into dot format. +/// +/// The first parameter `test_name` is required due to insta and rstest limitations. +/// See https://github.com/la10736/rstest/issues/183 #[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri #[rstest] -fn dot_string(sample_hugr: (Hugr, BuildHandle, BuildHandle)) { - let (h, _, _) = sample_hugr; - - insta::assert_yaml_snapshot!(h.dot_string()); +#[case::dfg("dot_dfg", sample_hugr().0)] +#[case::cfg("dot_cfg", crate::builder::test::simple_cfg_hugr())] +#[case::empty_dfg("dot_empty_dfg", crate::builder::test::simple_dfg_hugr())] +fn dot_string(#[case] test_name: &str, #[case] h: Hugr) { + insta::assert_yaml_snapshot!(test_name, h.dot_string()); } +/// Render some hugrs into mermaid format. +/// +/// The first parameter `test_name` is required due to insta and rstest limitations. +/// See https://github.com/la10736/rstest/issues/183 #[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri #[rstest] -fn mermaid_string(sample_hugr: (Hugr, BuildHandle, BuildHandle)) { - let (h, _, _) = sample_hugr; - - insta::assert_display_snapshot!(h.mermaid_string()); +#[case::dfg("mmd_dfg", sample_hugr().0)] +#[case::cfg("mmd_cfg", crate::builder::test::simple_cfg_hugr())] +#[case::empty_dfg("mmd_empty_dfg", crate::builder::test::simple_dfg_hugr())] +fn mermaid_string(#[case] test_name: &str, #[case] h: Hugr) { + insta::assert_snapshot!(test_name, h.mermaid_string()); } #[rstest]