From fdb2a8621dbb08f8cb5a28ee6aaafe07bdbb5d77 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Wed, 23 Dec 2020 09:00:04 +0000 Subject: [rust] DOT DSL --- dot-dsl/tests/dot-dsl.rs | 138 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 dot-dsl/tests/dot-dsl.rs (limited to 'dot-dsl/tests/dot-dsl.rs') diff --git a/dot-dsl/tests/dot-dsl.rs b/dot-dsl/tests/dot-dsl.rs new file mode 100644 index 0000000..ae469b7 --- /dev/null +++ b/dot-dsl/tests/dot-dsl.rs @@ -0,0 +1,138 @@ +use dot_dsl::graph::graph_items::edge::Edge; +use dot_dsl::graph::graph_items::node::Node; +use dot_dsl::graph::Graph; +use maplit::hashmap; + +#[test] +fn test_empty_graph() { + let graph = Graph::new(); + + assert!(graph.nodes.is_empty()); + + assert!(graph.edges.is_empty()); + + assert!(graph.attrs.is_empty()); +} + +#[test] +fn test_graph_with_one_node() { + let nodes = vec![Node::new("a")]; + + let graph = Graph::new().with_nodes(&nodes); + + assert!(graph.edges.is_empty()); + + assert!(graph.attrs.is_empty()); + + assert_eq!(graph.nodes, vec![Node::new("a")]); +} + +#[test] +fn test_graph_with_one_node_with_keywords() { + let nodes = vec![Node::new("a").with_attrs(&[("color", "green")])]; + + let graph = Graph::new().with_nodes(&nodes); + + assert!(graph.edges.is_empty()); + + assert!(graph.attrs.is_empty()); + + assert_eq!( + graph.nodes, + vec![Node::new("a").with_attrs(&[("color", "green")])] + ); +} + +#[test] +fn test_graph_with_one_edge() { + let edges = vec![Edge::new("a", "b")]; + + let graph = Graph::new().with_edges(&edges); + + assert!(graph.nodes.is_empty()); + + assert!(graph.attrs.is_empty()); + + assert_eq!(graph.edges, vec![Edge::new("a", "b")]); +} + +#[test] +fn test_graph_with_one_attribute() { + let graph = Graph::new().with_attrs(&[("foo", "1")]); + + let expected_attrs = hashmap! { + "foo".to_string() => "1".to_string(), + }; + + assert!(graph.nodes.is_empty()); + + assert!(graph.edges.is_empty()); + + assert_eq!(graph.attrs, expected_attrs); +} + +#[test] +fn test_graph_with_attributes() { + let nodes = vec![ + Node::new("a").with_attrs(&[("color", "green")]), + Node::new("c"), + Node::new("b").with_attrs(&[("label", "Beta!")]), + ]; + + let edges = vec![ + Edge::new("b", "c"), + Edge::new("a", "b").with_attrs(&[("color", "blue")]), + ]; + + let attrs = vec![("foo", "1"), ("title", "Testing Attrs"), ("bar", "true")]; + + let expected_attrs = hashmap! { + "foo".to_string() => "1".to_string(), + "title".to_string() => "Testing Attrs".to_string(), + "bar".to_string() => "true".to_string(), + }; + + let graph = Graph::new() + .with_nodes(&nodes) + .with_edges(&edges) + .with_attrs(&attrs); + + assert_eq!( + graph.nodes, + vec![ + Node::new("a").with_attrs(&[("color", "green")]), + Node::new("c"), + Node::new("b").with_attrs(&[("label", "Beta!")]), + ] + ); + + assert_eq!( + graph.edges, + vec![ + Edge::new("b", "c"), + Edge::new("a", "b").with_attrs(&[("color", "blue")]), + ] + ); + + assert_eq!(graph.attrs, expected_attrs); +} + +#[test] +fn test_graph_stores_attributes() { + let attributes = [("foo", "bar"), ("bat", "baz"), ("bim", "bef")]; + let graph = Graph::new().with_nodes( + &["a", "b", "c"] + .iter() + .zip(attributes.iter()) + .map(|(name, &attr)| Node::new(&name).with_attrs(&[attr])) + .collect::>(), + ); + + assert_eq!( + graph + .get_node("c") + .expect("node must be stored") + .get_attr("bim"), + Some("bef") + ); +} -- cgit v1.2.3