Skip to content

Commit

Permalink
Fix nullable setting and add RankDir for graph
Browse files Browse the repository at this point in the history
  • Loading branch information
vfrz committed Aug 7, 2023
1 parent 29b989c commit d8fbe9c
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 16 deletions.
16 changes: 16 additions & 0 deletions Sources/DotNetGraph.Tests/Core/DotGraphTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ public async Task CompileEmptyDirectedGraph()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("digraph \"Test\" {\n}\n");
}

[TestMethod]
public async Task CompileEmptyDirectedGraphWithRankDir()
{
var graph = new DotGraph()
.WithIdentifier("Test")
.WithRankDir(DotRankDir.RL)
.Directed();

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

var result = writer.GetStringBuilder().ToString();
result.Should().Be("digraph \"Test\" {\n\trankdir=\"RL\"\n}\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public void WithIdentifierHtml()
graph.Identifier.IsHtml.Should().Be(true);
}

[TestMethod]
public void WithRankDir()
{
var graph = new DotGraph()
.WithRankDir(DotRankDir.TB);

graph.RankDir.Should().Be(DotRankDir.TB);
}

[TestMethod]
public void Add()
{
Expand Down
18 changes: 18 additions & 0 deletions Sources/DotNetGraph/Core/DotBaseGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,27 @@ namespace DotNetGraph.Core
public abstract class DotBaseGraph : DotElement
{
public DotIdentifier Identifier { get; set; }

public DotRankDir? RankDir { get; set; }

public List<IDotElement> Elements { get; } = new List<IDotElement>();

public abstract override Task CompileAsync(CompilationContext context);

protected async Task CompileBodyAsync(CompilationContext context)
{
await Identifier.CompileAsync(context);
await context.WriteLineAsync(" {");
context.IndentationLevel++;
if (RankDir != null)
{
await context.WriteIndentationAsync();
await context.WriteLineAsync($"rankdir=\"{RankDir.ToString()}\"");
}
await CompileAttributesAsync(context);
context.IndentationLevel--;
await context.WriteIndentationAsync();
await context.WriteLineAsync("}");
}
}
}
10 changes: 2 additions & 8 deletions Sources/DotNetGraph/Core/DotGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@ public class DotGraph : DotBaseGraph
public bool Strict { get; set; }

public bool Directed { get; set; }

public override async Task CompileAsync(CompilationContext context)
{
context.DirectedGraph = Directed;
await context.WriteIndentationAsync();
if (Strict)
await context.WriteAsync("strict ");
await context.WriteAsync(Directed ? "digraph " : "graph ");
await Identifier.CompileAsync(context);
await context.WriteLineAsync(" {");
context.IndentationLevel++;
await CompileAttributesAsync(context);
context.IndentationLevel--;
await context.WriteIndentationAsync();
await context.WriteLineAsync("}");
await CompileBodyAsync(context);
}
}
}
11 changes: 11 additions & 0 deletions Sources/DotNetGraph/Core/DotRankDir.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// ReSharper disable InconsistentNaming
namespace DotNetGraph.Core
{
public enum DotRankDir
{
TB = 0,
BT = 1,
LR = 2,
RL = 3
}
}
8 changes: 1 addition & 7 deletions Sources/DotNetGraph/Core/DotSubgraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ public override async Task CompileAsync(CompilationContext context)
{
await context.WriteIndentationAsync();
await context.WriteAsync("subgraph ");
await Identifier.CompileAsync(context);
await context.WriteLineAsync(" {");
context.IndentationLevel++;
await CompileAttributesAsync(context);
context.IndentationLevel--;
await context.WriteIndentationAsync();
await context.WriteLineAsync("}");
await CompileBodyAsync(context);
}
}
}
1 change: 0 additions & 1 deletion Sources/DotNetGraph/DotNetGraph.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Sources/DotNetGraph/Extensions/DotBaseGraphExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public static T WithIdentifier<T>(this T graph, string identifier, bool isHtml =
return graph;
}

public static T WithRankDir<T>(this T graph, DotRankDir? rankDir) where T : DotBaseGraph
{
graph.RankDir = rankDir;
return graph;
}

public static T Add<T>(this T graph, IDotElement element) where T : DotBaseGraph
{
graph.Elements.Add(element);
Expand Down

0 comments on commit d8fbe9c

Please sign in to comment.