From a3fd9c4da77ceef1d35bc530bc4bce42ab4337aa Mon Sep 17 00:00:00 2001 From: seancarroll Date: Mon, 14 Jun 2021 22:13:04 -0500 Subject: Bugfix: Allow ID Strings A user, Martin, reached out about a bug with IDs specifically with quoting. Dotavious initially just used the string provided by caller however this is problematic for a few different scenarios amongst others 1. Strings with spaces 2. Double-quoted strings 3. Strings with non-alphanumerical content Details about IDs can be found at https://graphviz.org/doc/info/lang.html This commit conditional formats IDs based on the following 1. Wrap string in quotes if containing non alpha-numerical characters 2. Escape quotes found in string My rationale for conditionally formatting is mostly around keeping previous behavior intact. Quote from the link above An ID is just a string; the lack of quote characters in the first two forms is just for simplicity. There is no semantic difference between abc_2 and "abc_2", or between 2.34 and "2.34" Given these IDs are semantically the same I wanted to keep the same formatting which is unquoted. NOTE: This does not address HTML strings as IDs which needs to be addressed at a later date. --- tests/dot.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'tests/dot.rs') diff --git a/tests/dot.rs b/tests/dot.rs index bf960a9..9d944a2 100644 --- a/tests/dot.rs +++ b/tests/dot.rs @@ -62,6 +62,36 @@ digraph { ); } +#[test] +fn quotted_id() { + let quotted_id = r#"Earvin "Magic" Johnson"#; + let g = GraphBuilder::new_named_directed(quotted_id) + .build() + .unwrap(); + let r = test_input(g); + assert_eq!( + r.unwrap(), + r#"digraph "Earvin \"Magic\" Johnson" { +} +"# + ); +} + +#[test] +fn id_with_space() { + let id = "A Graph"; + let g = GraphBuilder::new_named_directed(id) + .build() + .unwrap(); + let r = test_input(g); + assert_eq!( + r.unwrap(), + r#"digraph "A Graph" { +} +"# + ); +} + #[test] fn empty_digraph() { let g = GraphBuilder::new_named_directed("empty_graph") @@ -197,6 +227,28 @@ fn single_edge() { ); } +#[test] +fn format_edges() { + let g = GraphBuilder::new_named_directed("format_edges") + .add_node(Node::new(r#"Earvin "Magic" Johnson"#)) + .add_node(Node::new("A Graph")) + .add_edge(Edge::new(r#"Earvin "Magic" Johnson"#, "A Graph")) + .build() + .unwrap(); + + let r = test_input(g); + + assert_eq!( + r.unwrap(), + r#"digraph format_edges { + "Earvin \"Magic\" Johnson"; + "A Graph"; + "Earvin \"Magic\" Johnson" -> "A Graph"; +} +"# + ); +} + #[test] fn single_edge_with_style() { let edge = EdgeBuilder::new("N0", "N1") -- cgit v1.2.3