Skip to content

Commit

Permalink
Merge pull request exercism#1553 from dvermd/1377_dotdsl
Browse files Browse the repository at this point in the history
dot-dsl: add test cases for attributes on Nodes and Edges (exercism#1377)
  • Loading branch information
petertseng authored Oct 4, 2022
2 parents c970696 + bfb25ac commit 18573d0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
5 changes: 5 additions & 0 deletions exercises/practice/binary-search/tests/binary-search.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// 1.65.0 says these &[] borrows are needless borrows,
// but 1.64.0 requires them.
// The Rust track will reevaluate this after 1.65.0 is released.
#![allow(clippy::needless_borrow)]

use binary_search::find;

#[test]
Expand Down
8 changes: 6 additions & 2 deletions exercises/practice/dot-dsl/.meta/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub mod graph {
self
}

pub fn get_node(&self, name: &str) -> Option<&Node> {
pub fn node(&self, name: &str) -> Option<&Node> {
self.nodes.iter().find(|n| n.name == name)
}
}
Expand Down Expand Up @@ -71,6 +71,10 @@ pub mod graph {

self
}

pub fn attr(&self, name: &str) -> Option<&str> {
self.attrs.get(name).map(|v| v.as_ref())
}
}
}

Expand Down Expand Up @@ -100,7 +104,7 @@ pub mod graph {
self
}

pub fn get_attr(&self, name: &str) -> Option<&str> {
pub fn attr(&self, name: &str) -> Option<&str> {
self.attrs.get(name).map(|v| v.as_ref())
}
}
Expand Down
77 changes: 69 additions & 8 deletions exercises/practice/dot-dsl/tests/dot-dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ fn test_graph_with_one_edge() {
assert_eq!(graph.edges, vec![Edge::new("a", "b")]);
}

#[test]
#[ignore]
fn test_graph_with_one_edge_with_keywords() {
let edges = vec![Edge::new("a", "b").with_attrs(&[("color", "blue")])];

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").with_attrs(&[("color", "blue")])]
);
}

#[test]
#[ignore]
fn test_graph_with_one_attribute() {
Expand Down Expand Up @@ -124,7 +141,44 @@ fn test_graph_with_attributes() {

#[test]
#[ignore]
fn test_graph_stores_attributes() {
fn test_edges_store_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"), ("fill", "darkblue")]),
];

let attrs = vec![("foo", "1"), ("title", "Testing Attrs"), ("bar", "true")];

let graph = Graph::new()
.with_nodes(&nodes)
.with_edges(&edges)
.with_attrs(&attrs);

assert_eq!(
graph.edges,
vec![
Edge::new("b", "c"),
Edge::new("a", "b").with_attrs(&[("color", "blue"), ("fill", "darkblue")]),
]
);

assert_eq!(graph.edges[1].attr("color"), Some("blue"));
assert_eq!(graph.edges[1].attr("fill"), Some("darkblue"));
assert_eq!(graph.edges[1].attr("foo"), None);
assert_eq!(graph.edges[0].attr("color"), None);
assert_eq!(graph.edges[0].attr("fill"), None);
assert_eq!(graph.edges[0].attr("foo"), None);
}

#[test]
#[ignore]
fn test_graph_nodes_store_attributes() {
let attributes = [("foo", "bar"), ("bat", "baz"), ("bim", "bef")];
let graph = Graph::new().with_nodes(
&["a", "b", "c"]
Expand All @@ -134,11 +188,18 @@ fn test_graph_stores_attributes() {
.collect::<Vec<_>>(),
);

assert_eq!(
graph
.get_node("c")
.expect("node must be stored")
.get_attr("bim"),
Some("bef")
);
let a = graph.node("a").expect("node a must be stored");
assert_eq!(a.attr("foo"), Some("bar"));
assert_eq!(a.attr("bat"), None);
assert_eq!(a.attr("bim"), None);

let b = graph.node("b").expect("node b must be stored");
assert_eq!(b.attr("foo"), None);
assert_eq!(b.attr("bat"), Some("baz"));
assert_eq!(b.attr("bim"), None);

let c = graph.node("c").expect("node c must be stored");
assert_eq!(c.attr("foo"), None);
assert_eq!(c.attr("bat"), None);
assert_eq!(c.attr("bim"), Some("bef"));
}

0 comments on commit 18573d0

Please sign in to comment.