aboutsummaryrefslogtreecommitdiff
path: root/rust/dot-dsl/tests/dot-dsl.rs
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2020-12-26 17:48:38 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:55:08 +0000
commit02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch)
tree8e39798fcaf27931d91c2088423fd4e97adcfc2d /rust/dot-dsl/tests/dot-dsl.rs
parent4e2052c4d792540c2f742b2c2a081b11117ed41d (diff)
downloadexercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz
exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip
Move Rust exercises in a subdirectory
Diffstat (limited to 'rust/dot-dsl/tests/dot-dsl.rs')
-rw-r--r--rust/dot-dsl/tests/dot-dsl.rs138
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 @@
1use dot_dsl::graph::graph_items::edge::Edge;
2use dot_dsl::graph::graph_items::node::Node;
3use dot_dsl::graph::Graph;
4use maplit::hashmap;
5
6#[test]
7fn 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]
18fn 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]
31fn 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]
47fn 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]
60fn 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]
75fn 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]
121fn 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}