diff --git a/LICENSE.txt b/LICENSE.txt index 0a8a7f3..c5bfeba 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Valentin Fritz (aka. VFRZ) +Copyright (c) 2020 Valentin Fritz (aka. VFRZ) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Sources/DotNetGraph.Tests/Edge/BasicEdgeTests.cs b/Sources/DotNetGraph.Tests/Edge/BasicEdgeTests.cs index 9b06d43..c898a4e 100644 --- a/Sources/DotNetGraph.Tests/Edge/BasicEdgeTests.cs +++ b/Sources/DotNetGraph.Tests/Edge/BasicEdgeTests.cs @@ -13,12 +13,7 @@ public class BasicEdgeTests public void EdgeWithIdentifierToIdentifier() { var graph = new DotGraph("TestGraph") - { - Elements = - { - new DotEdge("hello", "world") - } - }; + .Add(new DotEdge("hello", "world")); var compiled = graph.Compile(); @@ -30,12 +25,8 @@ public void EdgeWithIdentifierToIdentifierDirectedGraph() { var graph = new DotGraph("TestGraph") { - Directed = true, - Elements = - { - new DotEdge("hello", "world") - } - }; + Directed = true + }.Add(new DotEdge("hello", "world")); var compiled = graph.Compile(); @@ -50,14 +41,9 @@ public void EdgeWithNodeToNode() var worldNode = new DotNode("world"); var graph = new DotGraph("TestGraph") - { - Elements = - { - helloNode, - worldNode, - new DotEdge(helloNode, worldNode) - } - }; + .Add(helloNode) + .Add(worldNode) + .Add(new DotEdge(helloNode, worldNode)); var compiled = graph.Compile(); @@ -68,17 +54,12 @@ public void EdgeWithNodeToNode() public void EdgeWithMultipleAttributes() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotEdge("hello", "world") { - new DotEdge("hello", "world") - { - Color = Color.Red, - ArrowHead = DotEdgeArrowType.Box, - ArrowTail = DotEdgeArrowType.Diamond - } - } - }; + Color = Color.Red, + ArrowHead = DotEdgeArrowType.Box, + ArrowTail = DotEdgeArrowType.Diamond + }); var compiled = graph.Compile(); @@ -89,15 +70,10 @@ public void EdgeWithMultipleAttributes() public void EdgeWithColor() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotEdge("hello", "world") { - new DotEdge("hello", "world") - { - Color = Color.Red - } - } - }; + Color = Color.Red + }); var compiled = graph.Compile(); @@ -108,15 +84,10 @@ public void EdgeWithColor() public void EdgeWithFontColor() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotEdge("hello", "world") { - new DotEdge("hello", "world") - { - FontColor = Color.Blue - } - } - }; + FontColor = Color.Blue + }); var compiled = graph.Compile(); @@ -127,15 +98,10 @@ public void EdgeWithFontColor() public void EdgeWithLabel() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotEdge("hello", "world") { - new DotEdge("hello", "world") - { - Label = "Hello, \"world\"!" - } - } - }; + Label = "Hello, \"world\"!" + }); var compiled = graph.Compile(); @@ -146,15 +112,10 @@ public void EdgeWithLabel() public void EdgeWithArrowHead() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotEdge("hello", "world") { - new DotEdge("hello", "world") - { - ArrowHead = DotEdgeArrowType.Box - } - } - }; + ArrowHead = DotEdgeArrowType.Box + }); var compiled = graph.Compile(); @@ -165,15 +126,10 @@ public void EdgeWithArrowHead() public void EdgeWithArrowTail() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotEdge("hello", "world") { - new DotEdge("hello", "world") - { - ArrowTail = DotEdgeArrowType.Diamond - } - } - }; + ArrowTail = DotEdgeArrowType.Diamond + }); var compiled = graph.Compile(); diff --git a/Sources/DotNetGraph.Tests/Node/BasicNodeTests.cs b/Sources/DotNetGraph.Tests/Node/BasicNodeTests.cs index 1ce6346..c48c878 100644 --- a/Sources/DotNetGraph.Tests/Node/BasicNodeTests.cs +++ b/Sources/DotNetGraph.Tests/Node/BasicNodeTests.cs @@ -14,12 +14,7 @@ public class BasicNodeTests public void EmptyNode() { var graph = new DotGraph("TestGraph") - { - Elements = - { - new DotNode("TestNode") - } - }; + .Add(new DotNode("TestNode")); var compiled = graph.Compile(); @@ -30,18 +25,13 @@ public void EmptyNode() public void NodeWithMultipleAttributes() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - Color = Color.Blue, - Label = "Test", - Shape = DotNodeShape.Box - } - } - }; - + Color = Color.Blue, + Label = "Test", + Shape = DotNodeShape.Box + }); + var compiled = graph.Compile(); Check.That(compiled).HasSameValueAs("graph TestGraph { TestNode [color=\"#0000FF\",label=\"Test\",shape=box]; }"); @@ -51,12 +41,7 @@ public void NodeWithMultipleAttributes() public void NodeWithColor() { var graph = new DotGraph("TestGraph") - { - Elements = - { - new DotNode("TestNode", Color.Red) - } - }; + .Add(new DotNode("TestNode", Color.Red)); var compiled = graph.Compile(); @@ -67,15 +52,10 @@ public void NodeWithColor() public void NodeWithShape() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - Shape = DotNodeShape.Square - } - } - }; + Shape = DotNodeShape.Square + }); var compiled = graph.Compile(); @@ -86,15 +66,10 @@ public void NodeWithShape() public void NodeWithStyle() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - Style = DotNodeStyle.Dashed - } - } - }; + Style = DotNodeStyle.Dashed + }); var compiled = graph.Compile(); @@ -105,15 +80,10 @@ public void NodeWithStyle() public void NodeWithFontColor() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - FontColor = Color.Red - } - } - }; + FontColor = Color.Red + }); var compiled = graph.Compile(); @@ -124,15 +94,10 @@ public void NodeWithFontColor() public void NodeWithFillColor() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - FillColor = Color.Red - } - } - }; + FillColor = Color.Red + }); var compiled = graph.Compile(); @@ -143,114 +108,89 @@ public void NodeWithFillColor() public void NodeWithLabel() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - Label = "Hello, \"world\"!" - } - } - }; + Label = "Hello, \"world\"!" + }); var compiled = graph.Compile(); Check.That(compiled).HasSameValueAs("graph TestGraph { TestNode [label=\"Hello, \\\"world\\\"!\"]; }"); } - + [Fact] public void NodeWithWidth() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - Width = 0.64f - } - } - }; + Width = 0.64f + }); var compiled = graph.Compile(); Check.That(compiled).HasSameValueAs("graph TestGraph { TestNode [width=0.64]; }"); } - + [Fact] public void NodeWithHeight() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - Height = 0.64f - } - } - }; + Height = 0.64f + }); var compiled = graph.Compile(); Check.That(compiled).HasSameValueAs("graph TestGraph { TestNode [height=0.64]; }"); } - + [Fact] public void NodeWithWidthAndHeightUsesCorrectCulture() { var currentCulture = Thread.CurrentThread.CurrentCulture; var currentUiCulture = Thread.CurrentThread.CurrentUICulture; - + var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - Width = 0.46f, - Height = 0.64f - } - } - }; + Width = 0.46f, + Height = 0.64f + }); var cultureInfo = new CultureInfo("de-DE"); Thread.CurrentThread.CurrentCulture = cultureInfo; Thread.CurrentThread.CurrentUICulture = cultureInfo; - + var compiled = graph.Compile(); Check.That(compiled).HasSameValueAs("graph TestGraph { TestNode [width=0.46,height=0.64]; }"); - + Thread.CurrentThread.CurrentCulture = currentCulture; Thread.CurrentThread.CurrentUICulture = currentUiCulture; } - + [Fact] public void NodeWithLargeHeightUsesCorrectCulture() { var currentCulture = Thread.CurrentThread.CurrentCulture; var currentUiCulture = Thread.CurrentThread.CurrentUICulture; - + var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotNode("TestNode") { - new DotNode("TestNode") - { - Height = 12345.67f - } - } - }; + Height = 12345.67f + }); var cultureInfo = new CultureInfo("fr-FR"); Thread.CurrentThread.CurrentCulture = cultureInfo; Thread.CurrentThread.CurrentUICulture = cultureInfo; - + var compiled = graph.Compile(); Check.That(compiled).HasSameValueAs("graph TestGraph { TestNode [height=12345.67]; }"); - + Thread.CurrentThread.CurrentCulture = currentCulture; Thread.CurrentThread.CurrentUICulture = currentUiCulture; } diff --git a/Sources/DotNetGraph.Tests/SubGraph/BasicSubGraphTests.cs b/Sources/DotNetGraph.Tests/SubGraph/BasicSubGraphTests.cs index 0952345..8eb7755 100644 --- a/Sources/DotNetGraph.Tests/SubGraph/BasicSubGraphTests.cs +++ b/Sources/DotNetGraph.Tests/SubGraph/BasicSubGraphTests.cs @@ -14,12 +14,7 @@ public class BasicSubGraphTests public void EmptySubGraph() { var graph = new DotGraph("TestGraph") - { - Elements = - { - new DotSubGraph("TestSubGraph") - } - }; + .Add(new DotSubGraph("TestSubGraph")); var compiled = graph.Compile(); @@ -30,17 +25,12 @@ public void EmptySubGraph() public void SubGraphWithMultipleAttributes() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotSubGraph("TestSubGraph") { - new DotSubGraph("TestSubGraph") - { - Style = DotSubGraphStyle.Dashed, - Color = Color.Red, - Label = "Hello, world!" - } - } - }; + Style = DotSubGraphStyle.Dashed, + Color = Color.Red, + Label = "Hello, world!" + }); var compiled = graph.Compile(); @@ -51,15 +41,10 @@ public void SubGraphWithMultipleAttributes() public void SubGraphWithStyle() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotSubGraph("TestSubGraph") { - new DotSubGraph("TestSubGraph") - { - Style = DotSubGraphStyle.Dashed - } - } - }; + Style = DotSubGraphStyle.Dashed + }); var compiled = graph.Compile(); @@ -70,15 +55,10 @@ public void SubGraphWithStyle() public void SubGraphWithLabel() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotSubGraph("TestSubGraph") { - new DotSubGraph("TestSubGraph") - { - Label = "Hello, \"world\"!" - } - } - }; + Label = "Hello, \"world\"!" + }); var compiled = graph.Compile(); @@ -89,15 +69,10 @@ public void SubGraphWithLabel() public void SubGraphWithColor() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotSubGraph("TestSubGraph") { - new DotSubGraph("TestSubGraph") - { - Color = Color.Red - } - } - }; + Color = Color.Red + }); var compiled = graph.Compile(); @@ -108,41 +83,32 @@ public void SubGraphWithColor() public void SubGraphWithEdge() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotSubGraph("TestSubGraph") { - new DotSubGraph("TestSubGraph") + Elements = { - Elements = - { - new DotEdge("hello", "world") - } + new DotEdge("hello", "world") } - } - }; + }); var compiled = graph.Compile(); Check.That(compiled).HasSameValueAs("graph TestGraph { subgraph TestSubGraph { hello -- world; } }"); } - + [Fact] public void SubGraphWithEdgeDirected() { var graph = new DotGraph("TestGraph") { Directed = true, + }.Add(new DotSubGraph("TestSubGraph") + { Elements = { - new DotSubGraph("TestSubGraph") - { - Elements = - { - new DotEdge("hello", "world") - } - } + new DotEdge("hello", "world") } - }; + }); var compiled = graph.Compile(); @@ -153,18 +119,13 @@ public void SubGraphWithEdgeDirected() public void SubGraphWithNode() { var graph = new DotGraph("TestGraph") - { - Elements = + .Add(new DotSubGraph("TestSubGraph") { - new DotSubGraph("TestSubGraph") + Elements = { - Elements = - { - new DotNode("TestNode") - } + new DotNode("TestNode") } - } - }; + }); var compiled = graph.Compile(); diff --git a/Sources/DotNetGraph/Compiler/DotCompiler.cs b/Sources/DotNetGraph/Compiler/DotCompiler.cs index 14cb757..f200ed3 100644 --- a/Sources/DotNetGraph/Compiler/DotCompiler.cs +++ b/Sources/DotNetGraph/Compiler/DotCompiler.cs @@ -14,7 +14,7 @@ namespace DotNetGraph.Compiler public class DotCompiler { private readonly DotGraph _graph; - + public DotCompiler(DotGraph graph) { _graph = graph; diff --git a/Sources/DotNetGraph/Core/DotGuidNodeIdentifierGenerator.cs b/Sources/DotNetGraph/Core/DotGuidNodeIdentifierGenerator.cs new file mode 100644 index 0000000..34890c8 --- /dev/null +++ b/Sources/DotNetGraph/Core/DotGuidNodeIdentifierGenerator.cs @@ -0,0 +1,12 @@ +using System; + +namespace DotNetGraph.Core +{ + public class DotGuidNodeIdentifierGenerator : IDotNodeIdentifierGenerator + { + public string GenerateIdentifier() + { + return Guid.NewGuid().ToString("D"); + } + } +} \ No newline at end of file diff --git a/Sources/DotNetGraph/Core/IDotNodeIdentifierGenerator.cs b/Sources/DotNetGraph/Core/IDotNodeIdentifierGenerator.cs new file mode 100644 index 0000000..0ca9514 --- /dev/null +++ b/Sources/DotNetGraph/Core/IDotNodeIdentifierGenerator.cs @@ -0,0 +1,7 @@ +namespace DotNetGraph.Core +{ + public interface IDotNodeIdentifierGenerator + { + string GenerateIdentifier(); + } +} \ No newline at end of file diff --git a/Sources/DotNetGraph/DotGraph.cs b/Sources/DotNetGraph/DotGraph.cs index 35c0315..85cc831 100644 --- a/Sources/DotNetGraph/DotGraph.cs +++ b/Sources/DotNetGraph/DotGraph.cs @@ -1,21 +1,26 @@ using System.Collections.Generic; using DotNetGraph.Core; +using DotNetGraph.Node; namespace DotNetGraph { public class DotGraph : IDotElement { + public IEnumerable Elements => _elements; + + private readonly List _elements; + public bool Directed { get; set; } public string Identifier { get; set; } public bool Strict { get; set; } - public List Elements { get; } + public IDotNodeIdentifierGenerator NodeIdentifierGenerator { get; set; } = new DotGuidNodeIdentifierGenerator(); public DotGraph() { - Elements = new List(); + _elements = new List(); } public DotGraph(string identifier, bool directed = false) : this() @@ -23,5 +28,20 @@ public DotGraph(string identifier, bool directed = false) : this() Identifier = identifier; Directed = directed; } + + public DotGraph Add(IDotElement element) + { + if (element is DotNode nodeElement) + { + if (string.IsNullOrWhiteSpace(nodeElement.Identifier) && NodeIdentifierGenerator != null) + { + nodeElement.Identifier = NodeIdentifierGenerator.GenerateIdentifier(); + } + } + + _elements.Add(element); + + return this; + } } } \ No newline at end of file diff --git a/Sources/DotNetGraph/DotNetGraph.nuspec b/Sources/DotNetGraph/DotNetGraph.nuspec index 598268f..6abdc84 100644 --- a/Sources/DotNetGraph/DotNetGraph.nuspec +++ b/Sources/DotNetGraph/DotNetGraph.nuspec @@ -3,15 +3,15 @@ xmlns="https://raw.githubusercontent.com/NuGet/NuGet.Client/dev/src/NuGet.Core/NuGet.Packaging/compiler/resources/nuspec.xsd"> DotNetGraph - 2.0.0 + 2.0.1 DotNetGraph VFRZ VFRZ https://github.com/vfrz/DotNetGraph Create GraphViz DOT diagram / graph using C#/.Net - New major release (rewrite from scratch and subgraph support added) - Copyright 2019 + Fix height formatting to culture invariant (by eXpl0it3r) + Copyright 2020 DOT Graphviz true LICENSE.txt @@ -25,4 +25,4 @@ - \ No newline at end of file +