Skip to content

Commit

Permalink
Better identifiers compilation
Browse files Browse the repository at this point in the history
Adding double quotes only when required
  • Loading branch information
vfrz committed Aug 8, 2023
1 parent e1605f1 commit c10222e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Sources/DotNetGraph.Tests/Core/DotEdgeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task CompileEmptyEdge()
await edge.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"A\" -- \"B\"\n");
result.Should().Be("A -- B\n");
}

[TestMethod]
Expand All @@ -42,7 +42,7 @@ public async Task CompileEmptyDirectedEdge()
await edge.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"A\" -> \"B\"\n");
result.Should().Be("A -> B\n");
}

[TestMethod]
Expand Down Expand Up @@ -96,6 +96,6 @@ public async Task CompileWithAttributes()
await edge.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"A\" -> \"B\" [\n\t\"label\"=\"Test\"\n\t\"style\"=\"bold\"\n]\n");
result.Should().Be("A -> B [\n\t\"label\"=\"Test\"\n\t\"style\"=\"bold\"\n]\n");
}
}
10 changes: 5 additions & 5 deletions Sources/DotNetGraph.Tests/Core/DotGraphTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task CompileEmptyGraph()
await graph.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("graph \"Test\" {\n}\n");
result.Should().Be("graph Test {\n}\n");
}

[TestMethod]
Expand All @@ -37,7 +37,7 @@ public async Task CompileEmptyStrictGraph()
await graph.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("strict graph \"Test\" {\n}\n");
result.Should().Be("strict graph Test {\n}\n");
}

[TestMethod]
Expand All @@ -52,7 +52,7 @@ public async Task CompileEmptyDirectedGraph()
await graph.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("digraph \"Test\" {\n}\n");
result.Should().Be("digraph Test {\n}\n");
}

[TestMethod]
Expand All @@ -68,7 +68,7 @@ public async Task CompileEmptyDirectedGraphWithRankDir()
await graph.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("digraph \"Test\" {\n\t\"rankdir\"=\"RL\"\n}\n");
result.Should().Be("digraph Test {\n\t\"rankdir\"=\"RL\"\n}\n");
}

[TestMethod]
Expand All @@ -87,6 +87,6 @@ public async Task CompileDirectedGraphWithOneNode()
await graph.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("digraph \"Test\" {\n\t\"TestNode\"\n}\n");
result.Should().Be("digraph Test {\n\tTestNode\n}\n");
}
}
34 changes: 33 additions & 1 deletion Sources/DotNetGraph.Tests/Core/DotIdentifierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,39 @@ public async Task Compile()
await identifier.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"Test\"");
result.Should().Be("Test");
}

[TestMethod]
public async Task CompileWithQuotes()
{
var identifier = new DotIdentifier("Hello, world!");

await using var writer = new StringWriter();
var context = new CompilationContext(writer, new CompilationOptions());
await identifier.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"Hello, world!\"");
}

[DataTestMethod]
[DataRow("graph")]
[DataRow("digraph")]
[DataRow("subgraph")]
[DataRow("strict")]
[DataRow("node")]
[DataRow("edge")]
public async Task CompileWithQuotesReservedWords(string identifierValue)
{
var identifier = new DotIdentifier(identifierValue);

await using var writer = new StringWriter();
var context = new CompilationContext(writer, new CompilationOptions());
await identifier.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be($"\"{identifierValue}\"");
}

[TestMethod]
Expand Down
4 changes: 2 additions & 2 deletions Sources/DotNetGraph.Tests/Core/DotNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task CompileEmptyNode()
await node.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"Test\"\n");
result.Should().Be("Test\n");
}

[TestMethod]
Expand All @@ -38,6 +38,6 @@ public async Task CompileNodeWithColor()
await node.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("\"Test\" [\n\t\"color\"=\"#FF0000\"\n]\n");
result.Should().Be("Test [\n\t\"color\"=\"#FF0000\"\n]\n");
}
}
2 changes: 1 addition & 1 deletion Sources/DotNetGraph.Tests/Core/DotSubgraphTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public async Task CompileEmptySubgraph()
await subgraph.CompileAsync(context);

var result = writer.GetStringBuilder().ToString();
result.Should().Be("subgraph \"Test\" {\n}\n");
result.Should().Be("subgraph Test {\n}\n");
}
}
27 changes: 25 additions & 2 deletions Sources/DotNetGraph/Core/DotIdentifier.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DotNetGraph.Compilation;
using DotNetGraph.Extensions;
Expand All @@ -6,6 +8,19 @@ namespace DotNetGraph.Core
{
public class DotIdentifier : IDotElement
{
private static readonly Regex NoQuotesRequiredRegex
= new Regex("^([a-zA-Z\\200-\\377_][a-zA-Z\\200-\\3770-9_]*|[-]?(.[0-9]+|[0-9]+(.[0-9]+)?))$");

private static readonly string[] ReservedWords =
{
"graph",
"digraph",
"subgraph",
"strict",
"node",
"edge"
};

public string Value { get; set; }

public bool IsHtml { get; set; } = false;
Expand All @@ -23,9 +38,17 @@ public async Task CompileAsync(CompilationContext context)
await context.TextWriter.WriteAsync($"<{Value}>");
return;
}

var value = context.Options.AutomaticEscapedCharactersFormat ? Value.FormatGraphvizEscapedCharacters() : Value;
await context.TextWriter.WriteAsync($"\"{value}\"");
if (RequiresDoubleQuotes(value))
await context.TextWriter.WriteAsync($"\"{value}\"");
else
await context.TextWriter.WriteAsync($"{value}");
}

private static bool RequiresDoubleQuotes(string value)
{
return ReservedWords.Contains(value) || !NoQuotesRequiredRegex.IsMatch(value);
}
}
}

0 comments on commit c10222e

Please sign in to comment.