|
| 1 | +#[macro_use] |
| 2 | +extern crate maplit; |
| 3 | +extern crate dot_dsl; |
| 4 | + |
| 5 | +use dot_dsl::graph::graph_items::edge::Edge; |
| 6 | +use dot_dsl::graph::graph_items::node::Node; |
| 7 | +use dot_dsl::graph::Graph; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn test_empty_graph() { |
| 11 | + let graph = Graph::new(); |
| 12 | + |
| 13 | + assert!(graph.nodes.is_empty()); |
| 14 | + |
| 15 | + assert!(graph.edges.is_empty()); |
| 16 | + |
| 17 | + assert!(graph.attrs.is_empty()); |
| 18 | +} |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn test_graph_with_one_node() { |
| 22 | + let nodes = vec![Node::new("a")]; |
| 23 | + |
| 24 | + let graph = Graph::new().with_nodes(&nodes); |
| 25 | + |
| 26 | + assert!(graph.edges.is_empty()); |
| 27 | + |
| 28 | + assert!(graph.attrs.is_empty()); |
| 29 | + |
| 30 | + assert_eq!(graph.nodes, vec![Node::new("a")]); |
| 31 | +} |
| 32 | + |
| 33 | +#[test] |
| 34 | +fn test_graph_with_one_node_with_keywords() { |
| 35 | + let nodes = vec![Node::new("a").with_attrs(&[("color", "green")])]; |
| 36 | + |
| 37 | + let graph = Graph::new().with_nodes(&nodes); |
| 38 | + |
| 39 | + assert!(graph.edges.is_empty()); |
| 40 | + |
| 41 | + assert!(graph.attrs.is_empty()); |
| 42 | + |
| 43 | + assert_eq!( |
| 44 | + graph.nodes, |
| 45 | + vec![Node::new("a").with_attrs(&[("color", "green")])] |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn test_graph_with_one_edge() { |
| 51 | + let edges = vec![Edge::new("a", "b")]; |
| 52 | + |
| 53 | + let graph = Graph::new().with_edges(&edges); |
| 54 | + |
| 55 | + assert!(graph.nodes.is_empty()); |
| 56 | + |
| 57 | + assert!(graph.attrs.is_empty()); |
| 58 | + |
| 59 | + assert_eq!(graph.edges, vec![Edge::new("a", "b")]); |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn test_graph_with_one_attribute() { |
| 64 | + let graph = Graph::new().with_attrs(&[("foo", "1")]); |
| 65 | + |
| 66 | + let expected_attrs = hashmap!{ |
| 67 | + "foo".to_string() => "1".to_string(), |
| 68 | + }; |
| 69 | + |
| 70 | + assert!(graph.nodes.is_empty()); |
| 71 | + |
| 72 | + assert!(graph.edges.is_empty()); |
| 73 | + |
| 74 | + assert_eq!(graph.attrs, expected_attrs); |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn test_graph_with_attributes() { |
| 79 | + let nodes = vec![ |
| 80 | + Node::new("a").with_attrs(&[("color", "green")]), |
| 81 | + Node::new("c"), |
| 82 | + Node::new("b").with_attrs(&[("label", "Beta!")]), |
| 83 | + ]; |
| 84 | + |
| 85 | + let edges = vec![ |
| 86 | + Edge::new("b", "c"), |
| 87 | + Edge::new("a", "b").with_attrs(&[("color", "blue")]), |
| 88 | + ]; |
| 89 | + |
| 90 | + let attrs = vec![("foo", "1"), ("title", "Testing Attrs"), ("bar", "true")]; |
| 91 | + |
| 92 | + let expected_attrs = hashmap! { |
| 93 | + "foo".to_string() => "1".to_string(), |
| 94 | + "title".to_string() => "Testing Attrs".to_string(), |
| 95 | + "bar".to_string() => "true".to_string(), |
| 96 | + }; |
| 97 | + |
| 98 | + let graph = Graph::new() |
| 99 | + .with_nodes(&nodes) |
| 100 | + .with_edges(&edges) |
| 101 | + .with_attrs(&attrs); |
| 102 | + |
| 103 | + assert_eq!( |
| 104 | + graph.nodes, |
| 105 | + vec![ |
| 106 | + Node::new("a").with_attrs(&[("color", "green")]), |
| 107 | + Node::new("c"), |
| 108 | + Node::new("b").with_attrs(&[("label", "Beta!")]), |
| 109 | + ] |
| 110 | + ); |
| 111 | + |
| 112 | + assert_eq!( |
| 113 | + graph.edges, |
| 114 | + vec![ |
| 115 | + Edge::new("b", "c"), |
| 116 | + Edge::new("a", "b").with_attrs(&[("color", "blue")]), |
| 117 | + ] |
| 118 | + ); |
| 119 | + |
| 120 | + assert_eq!(graph.attrs, expected_attrs); |
| 121 | +} |
0 commit comments