Skip to content

Commit

Permalink
Add dot group element (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
kokorins authored Jun 16, 2024
1 parent 7ae03f4 commit 38f81a6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Sources/DotNetGraph.Tests/Core/DotGroupTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.IO;
using System.Threading.Tasks;
using DotNetGraph.Compilation;
using DotNetGraph.Core;
using DotNetGraph.Extensions;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DotNetGraph.Tests.Core;

[TestClass]
public class DotGroupTests
{
[TestMethod]
public async Task EmptyGroupElements()
{
var group = new DotGroup();

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

var result = writer.GetStringBuilder().ToString();
result.Should().Be("{\n}\n");
}
[TestMethod]
public async Task GroupElementsAndAttributes()
{
var group = new DotGroup().WithAttribute("rank", "same");
group.Add(new DotNode().WithIdentifier("node-1"));
group.Add(new DotNode().WithIdentifier("node-2"));


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

var result = writer.GetStringBuilder().ToString();
result.Should().Be("{\n\t\"rank\"=same\n\t\"node-1\"\n\t\"node-2\"\n}\n");
}
}
21 changes: 21 additions & 0 deletions Sources/DotNetGraph/Core/DotGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading.Tasks;
using DotNetGraph.Compilation;

namespace DotNetGraph.Core
{
public class DotGroup : DotBaseGraph
{
public override async Task CompileAsync(CompilationContext context)
{
await context.WriteIndentationAsync();
await context.WriteLineAsync("{");
++context.IndentationLevel;
await CompileAttributesAsync(context);
foreach (var element in Elements)
await element.CompileAsync(context);
--context.IndentationLevel;
await context.WriteIndentationAsync();
await context.WriteLineAsync("}");
}
}
}

0 comments on commit 38f81a6

Please sign in to comment.