Skip to content

Commit 7b014c3

Browse files
authored
Add explit name parameter (#44)
1 parent 38f81a6 commit 7b014c3

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Sources/DotNetGraph.Tests/Core/DotNodeTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ public async Task CompileNodeWithColor()
3939
var result = writer.GetStringBuilder().ToString();
4040
result.Should().Be("Test [\n\t\"color\"=\"#FF0000\"\n]\n");
4141
}
42+
43+
[TestMethod]
44+
public async Task CompileNodesProperties()
45+
{
46+
var node = new DotNode()
47+
.WithIdentifier("node", quoteReservedWords: false).WithStyle(DotNodeStyle.Filled);
48+
await using var writer = new StringWriter();
49+
var context = new CompilationContext(writer, new CompilationOptions());
50+
await node.CompileAsync(context);
51+
52+
var result = writer.GetStringBuilder().ToString();
53+
result.Should().Be("node [\n\t\"style\"=\"filled\"\n]\n");
54+
}
4255
}

Sources/DotNetGraph/Core/DotIdentifier.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace DotNetGraph.Core
99
{
1010
public class DotIdentifier : IDotElement, IEquatable<DotIdentifier>
1111
{
12+
private readonly bool _quoteReservedWords;
13+
1214
private static readonly Regex NoQuotesRequiredRegex
1315
= new Regex("^([a-zA-Z\\200-\\377_][a-zA-Z\\200-\\3770-9_]*|[-]?(.[0-9]+|[0-9]+(.[0-9]+)?))$");
1416

@@ -26,8 +28,9 @@ private static readonly Regex NoQuotesRequiredRegex
2628

2729
public bool IsHtml { get; set; } = false;
2830

29-
public DotIdentifier(string value, bool isHtml = false)
31+
public DotIdentifier(string value, bool isHtml = false, bool quoteReservedWords = true)
3032
{
33+
_quoteReservedWords = quoteReservedWords;
3134
Value = value;
3235
IsHtml = isHtml;
3336
}
@@ -41,7 +44,7 @@ public async Task CompileAsync(CompilationContext context)
4144
}
4245

4346
var value = context.Options.AutomaticEscapedCharactersFormat ? Value.FormatGraphvizEscapedCharacters() : Value;
44-
if (RequiresDoubleQuotes(value))
47+
if (RequiresDoubleQuotes(value) && _quoteReservedWords)
4548
await context.TextWriter.WriteAsync($"\"{value}\"");
4649
else
4750
await context.TextWriter.WriteAsync($"{value}");

Sources/DotNetGraph/Extensions/DotNodeExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ namespace DotNetGraph.Extensions
55
{
66
public static class DotNodeExtensions
77
{
8-
public static DotNode WithIdentifier(this DotNode node, string identifier, bool isHtml = false)
8+
public static DotNode WithIdentifier(this DotNode node, string identifier, bool isHtml = false,
9+
bool quoteReservedWords = true)
910
{
10-
node.Identifier = new DotIdentifier(identifier, isHtml);
11+
node.Identifier = new DotIdentifier(identifier, isHtml, quoteReservedWords);
1112
return node;
1213
}
1314

0 commit comments

Comments
 (0)