diff options
Diffstat (limited to 'rust/dot-dsl/tests/dot-dsl.rs')
| -rw-r--r-- | rust/dot-dsl/tests/dot-dsl.rs | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/rust/dot-dsl/tests/dot-dsl.rs b/rust/dot-dsl/tests/dot-dsl.rs new file mode 100644 index 0000000..ae469b7 --- /dev/null +++ b/rust/dot-dsl/tests/dot-dsl.rs | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | use dot_dsl::graph::graph_items::edge::Edge; | ||
| 2 | use dot_dsl::graph::graph_items::node::Node; | ||
| 3 | use dot_dsl::graph::Graph; | ||
| 4 | use maplit::hashmap; | ||
| 5 | |||
| 6 | #[test] | ||
| 7 | fn test_empty_graph() { | ||
| 8 | let graph = Graph::new(); | ||
| 9 | |||
| 10 | assert!(graph.nodes.is_empty()); | ||
| 11 | |||
| 12 | assert!(graph.edges.is_empty()); | ||
| 13 | |||
| 14 | assert!(graph.attrs.is_empty()); | ||
| 15 | } | ||
| 16 | |||
| 17 | #[test] | ||
| 18 | fn test_graph_with_one_node() { | ||
| 19 | let nodes = vec![Node::new("a")]; | ||
| 20 | |||
| 21 | let graph = Graph::new().with_nodes(&nodes); | ||
| 22 | |||
| 23 | assert!(graph.edges.is_empty()); | ||
| 24 | |||
| 25 | assert!(graph.attrs.is_empty()); | ||
| 26 | |||
| 27 | assert_eq!(graph.nodes, vec![Node::new("a")]); | ||
| 28 | } | ||
| 29 | |||
| 30 | #[test] | ||
| 31 | fn test_graph_with_one_node_with_keywords() { | ||
| 32 | let nodes = vec![Node::new("a").with_attrs(&[("color", "green")])]; | ||
| 33 | |||
| 34 | let graph = Graph::new().with_nodes(&nodes); | ||
| 35 | |||
| 36 | assert!(graph.edges.is_empty()); | ||
| 37 | |||
| 38 | assert!(graph.attrs.is_empty()); | ||
| 39 | |||
| 40 | assert_eq!( | ||
| 41 | graph.nodes, | ||
| 42 | vec![Node::new("a").with_attrs(&[("color", "green")])] | ||
| 43 | ); | ||
| 44 | } | ||
| 45 | |||
| 46 | #[test] | ||
| 47 | fn test_graph_with_one_edge() { | ||
| 48 | let edges = vec![Edge::new("a", "b")]; | ||
| 49 | |||
| 50 | let graph = Graph::new().with_edges(&edges); | ||
| 51 | |||
| 52 | assert!(graph.nodes.is_empty()); | ||
| 53 | |||
| 54 | assert!(graph.attrs.is_empty()); | ||
| 55 | |||
| 56 | assert_eq!(graph.edges, vec![Edge::new("a", "b")]); | ||
| 57 | } | ||
| 58 | |||
| 59 | #[test] | ||
| 60 | fn test_graph_with_one_attribute() { | ||
| 61 | let graph = Graph::new().with_attrs(&[("foo", "1")]); | ||
| 62 | |||
| 63 | let expected_attrs = hashmap! { | ||
| 64 | "foo".to_string() => "1".to_string(), | ||
| 65 | }; | ||
| 66 | |||
| 67 | assert!(graph.nodes.is_empty()); | ||
| 68 | |||
| 69 | assert!(graph.edges.is_empty()); | ||
| 70 | |||
| 71 | assert_eq!(graph.attrs, expected_attrs); | ||
| 72 | } | ||
| 73 | |||
| 74 | #[test] | ||
| 75 | fn test_graph_with_attributes() { | ||
| 76 | let nodes = vec![ | ||
| 77 | Node::new("a").with_attrs(&[("color", "green")]), | ||
| 78 | Node::new("c"), | ||
| 79 | Node::new("b").with_attrs(&[("label", "Beta!")]), | ||
| 80 | ]; | ||
| 81 | |||
| 82 | let edges = vec![ | ||
| 83 | Edge::new("b", "c"), | ||
| 84 | Edge::new("a", "b").with_attrs(&[("color", "blue")]), | ||
| 85 | ]; | ||
| 86 | |||
| 87 | let attrs = vec![("foo", "1"), ("title", "Testing Attrs"), ("bar", "true")]; | ||
| 88 | |||
| 89 | let expected_attrs = hashmap! { | ||
| 90 | "foo".to_string() => "1".to_string(), | ||
| 91 | "title".to_string() => "Testing Attrs".to_string(), | ||
| 92 | "bar".to_string() => "true".to_string(), | ||
| 93 | }; | ||
| 94 | |||
| 95 | let graph = Graph::new() | ||
| 96 | .with_nodes(&nodes) | ||
| 97 | .with_edges(&edges) | ||
| 98 | .with_attrs(&attrs); | ||
| 99 | |||
| 100 | assert_eq!( | ||
| 101 | graph.nodes, | ||
| 102 | vec![ | ||
| 103 | Node::new("a").with_attrs(&[("color", "green")]), | ||
| 104 | Node::new("c"), | ||
| 105 | Node::new("b").with_attrs(&[("label", "Beta!")]), | ||
| 106 | ] | ||
| 107 | ); | ||
| 108 | |||
| 109 | assert_eq!( | ||
| 110 | graph.edges, | ||
| 111 | vec![ | ||
| 112 | Edge::new("b", "c"), | ||
| 113 | Edge::new("a", "b").with_attrs(&[("color", "blue")]), | ||
| 114 | ] | ||
| 115 | ); | ||
| 116 | |||
| 117 | assert_eq!(graph.attrs, expected_attrs); | ||
| 118 | } | ||
| 119 | |||
| 120 | #[test] | ||
| 121 | fn test_graph_stores_attributes() { | ||
| 122 | let attributes = [("foo", "bar"), ("bat", "baz"), ("bim", "bef")]; | ||
| 123 | let graph = Graph::new().with_nodes( | ||
| 124 | &["a", "b", "c"] | ||
| 125 | .iter() | ||
| 126 | .zip(attributes.iter()) | ||
| 127 | .map(|(name, &attr)| Node::new(&name).with_attrs(&[attr])) | ||
| 128 | .collect::<Vec<_>>(), | ||
| 129 | ); | ||
| 130 | |||
| 131 | assert_eq!( | ||
| 132 | graph | ||
| 133 | .get_node("c") | ||
| 134 | .expect("node must be stored") | ||
| 135 | .get_attr("bim"), | ||
| 136 | Some("bef") | ||
| 137 | ); | ||
| 138 | } | ||
