Skip to content

Commit 4ad12c3

Browse files
committed
test: easy,error,if,loop
1 parent 07d7308 commit 4ad12c3

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

src/flow/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use petgraph::stable_graph::{NodeIndex, StableDiGraph};
55
use petgraph::visit::{EdgeRef, IntoNodeReferences};
66
use petgraph::EdgeDirection;
77
pub mod display;
8-
8+
pub mod test;
99
// ANCHOR: nodeandedge
1010
#[derive(Debug, Clone, PartialEq)]
1111
pub enum GraphNodeType {

src/flow/test.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#[cfg(test)]
2+
mod test {
3+
use crate::{
4+
ast::node::NodeEnum,
5+
flow::{display::Dot, from_ast},
6+
nomparser::{statement::statement_block, Span},
7+
};
8+
9+
#[test]
10+
fn test_easy_flow_chart() {
11+
let s = "{
12+
return;
13+
}
14+
";
15+
let (_, node) = statement_block(Span::from(s)).unwrap();
16+
let graph = from_ast(Box::new(NodeEnum::STS(node)));
17+
let exp = "digraph test {
18+
D0 [shape=box, style=rounded, label=\"begin\", fontname=\"\"];
19+
{rank = sink; D1 [shape=box, style=rounded, label=\"end\", fontname=\"\"];}
20+
D4 [shape=box, label=\"return\\l\", fontname=\"\"];
21+
D4 -> D1;
22+
D0 -> D4;
23+
}"
24+
.replace(|c: char| c.is_whitespace(), "");
25+
let out = Dot::new(true)
26+
.generate_from_graph(&graph, &"test".to_string())
27+
.replace(|c: char| c.is_whitespace(), "");
28+
assert_eq!(3, graph.node_count());
29+
assert_eq!(2, graph.edge_count());
30+
assert_eq!(exp, out);
31+
}
32+
#[test]
33+
fn test_error_flow_chart() {
34+
let s = "{
35+
let a = 1
36+
return;
37+
}
38+
";
39+
let (_, node) = statement_block(Span::from(s)).unwrap();
40+
let graph = from_ast(Box::new(NodeEnum::STS(node)));
41+
let exp = "digraph test{
42+
D0[shape=box,style=rounded,label=\"begin\",fontname=\"\"];
43+
{rank=sink;D1[shape=box,style=rounded,label=\"end\",fontname=\"\"];}
44+
D4[shape=box,label=<<FONT>leta=1</FONT><BR/><FONTCOLOR=\"red\">missingsemi</FONT>>,fontname=\"\"];
45+
D6[shape=box,label=\"return\\l\",fontname=\"\"];
46+
D4->D6;
47+
D6->D1;
48+
D0->D4;
49+
}"
50+
.replace(|c: char| c.is_whitespace(), "");
51+
let out = Dot::new(true)
52+
.generate_from_graph(&graph, &"test".to_string())
53+
.replace(|c: char| c.is_whitespace(), "");
54+
assert_eq!(4, graph.node_count());
55+
assert_eq!(3, graph.edge_count());
56+
assert_eq!(exp, out);
57+
}
58+
#[test]
59+
fn test_if_flow_chart() {
60+
let s = "{
61+
if a == 1{
62+
a = 2;
63+
}
64+
return;
65+
}
66+
";
67+
let (_, node) = statement_block(Span::from(s)).unwrap();
68+
let graph = from_ast(Box::new(NodeEnum::STS(node)));
69+
let exp = "digraph test{
70+
D0[shape=box,style=rounded,label=\"begin\",fontname=\"\"];
71+
{rank=sink;D1[shape=box,style=rounded,label=\"end\",fontname=\"\"];}
72+
D4[shape=diamond,label=\"a==1?\\l\",fontname=\"\"];
73+
D9[shape=box,label=\"a=2\\l\",fontname=\"\"];
74+
D11[shape=box,label=\"return\\l\",fontname=\"\"];
75+
D4:e->D11:n[xlabel=N];
76+
D9->D11;
77+
D4:s->D9:n[xlabel=Y];
78+
D11->D1;D0->D4;
79+
}"
80+
.replace(|c: char| c.is_whitespace(), "");
81+
let out = Dot::new(true)
82+
.generate_from_graph(&graph, &"test".to_string())
83+
.replace(|c: char| c.is_whitespace(), "");
84+
assert_eq!(5, graph.node_count());
85+
assert_eq!(5, graph.edge_count());
86+
assert_eq!(exp, out);
87+
}
88+
#[test]
89+
fn test_loop_flow_chart() {
90+
let s = "{
91+
for let a = 1; a<3;a=a+1{
92+
a = a+2;
93+
}
94+
return;
95+
}
96+
";
97+
let (_, node) = statement_block(Span::from(s)).unwrap();
98+
let graph = from_ast(Box::new(NodeEnum::STS(node)));
99+
let exp = "digraph test{
100+
D0[shape=box,style=rounded,label=\"begin\",fontname=\"\"];
101+
{rank=sink;D1[shape=box,style=rounded,label=\"end\",fontname=\"\"];}
102+
D6[shape=diamond,label=\"a<3?\\l\",fontname=\"\"];
103+
D7[shape=box,label=\"leta=1\\l\",fontname=\"\"];
104+
D8[shape=box,label=\"a=a+1\\l\",fontname=\"\"];
105+
D11[shape=box,label=\"a=a+2\\l\",fontname=\"\"];
106+
D13[shape=box,label=\"return\\l\",fontname=\"\"];
107+
D6:e->D13:n[xlabel=N];
108+
D7->D6;
109+
D11->D8;
110+
D8->D6;
111+
D6:s->D11:n[xlabel=Y];
112+
D13->D1;
113+
D0->D7;
114+
}"
115+
.replace(|c: char| c.is_whitespace(), "");
116+
let out = Dot::new(true)
117+
.generate_from_graph(&graph, &"test".to_string())
118+
.replace(|c: char| c.is_whitespace(), "");
119+
assert_eq!(7, graph.node_count());
120+
assert_eq!(7, graph.edge_count());
121+
assert_eq!(exp, out);
122+
}
123+
}

0 commit comments

Comments
 (0)