From 419bd31fd283d1b7aec5514226c3640eb7600926 Mon Sep 17 00:00:00 2001 From: chtenb Date: Fri, 11 Aug 2023 12:19:25 +0200 Subject: [PATCH] Fix tests --- README.md | 8 +++++--- Rubjerg.Graphviz.Test/OldTutorial.cs | 6 +++--- Rubjerg.Graphviz.Test/Reproductions.cs | 3 ++- Rubjerg.Graphviz.Test/TestDotLayout.cs | 10 +++++----- Rubjerg.Graphviz.Test/TestXDotLayout.cs | 13 ++++++++++++- Rubjerg.Graphviz.Test/Tutorial.cs | 8 +++++--- Rubjerg.Graphviz/XDot.cs | 4 ++++ 7 files changed, 36 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e98b404..8607e7d 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ namespace Rubjerg.Graphviz.Test; public class Tutorial { public const string PointPattern = @"{X=[\d.]+, Y=[\d.]+}"; - public const string RectPattern = @"{X=[\d.]+,Y=[\d.]+,Width=[\d.]+,Height=[\d.]+}"; + public const string RectPattern = @"{X=[\d.]+, Y=[\d.]+, Width=[\d.]+, Height=[\d.]+}"; public const string SplinePattern = @"{X=[\d.]+, Y=[\d.]+}, {X=[\d.]+, Y=[\d.]+}, {X=[\d.]+, Y=[\d.]+}, {X=[\d.]+, Y=[\d.]+}"; @@ -130,8 +130,10 @@ public class Tutorial // Or we can ask Graphviz to compute the layout and programatically read out the layout attributes // This will create a copy of our original graph with layout information attached to it in the form - // of attributes. - RootGraph layout = root.CreateLayout(); + // of attributes. Graphviz outputs coordinates in a bottom-left originated coordinate system. + // But since many applications require rendering in a top-left originated coordinate system, + // we provide a way to translate the coordinates. + RootGraph layout = root.CreateLayout(coordinateSystem: CoordinateSystem.TopLeft); // There are convenience methods available that parse these attributes for us and give // back the layout information in an accessible form. diff --git a/Rubjerg.Graphviz.Test/OldTutorial.cs b/Rubjerg.Graphviz.Test/OldTutorial.cs index 288e01c..314daf0 100644 --- a/Rubjerg.Graphviz.Test/OldTutorial.cs +++ b/Rubjerg.Graphviz.Test/OldTutorial.cs @@ -73,7 +73,7 @@ public void Layouting() // Like a bounding box of an object RectangleD nodeboundingbox = nodeA.GetBoundingBox(); - Utils.AssertPattern(@"{X=[\d.]+,Y=[\d.]+,Width=[\d.]+,Height=[\d.]+}", nodeboundingbox.ToString()); + Utils.AssertPattern(@"{X=[\d.]+, Y=[\d.]+, Width=[\d.]+, Height=[\d.]+}", nodeboundingbox.ToString()); // Or splines between nodes Node nodeB = root.GetNode("B"); @@ -133,8 +133,8 @@ public void Clusters() SubGraph cluster = root.GetSubgraph("cluster_1"); RectangleD clusterbox = cluster.GetBoundingBox(); RectangleD rootgraphbox = root.GetBoundingBox(); - Utils.AssertPattern(@"{X=[\d.]+,Y=[\d.]+,Width=[\d.]+,Height=[\d.]+}", clusterbox.ToString()); - Utils.AssertPattern(@"{X=[\d.]+,Y=[\d.]+,Width=[\d.]+,Height=[\d.]+}", rootgraphbox.ToString()); + Utils.AssertPattern(@"{X=[\d.]+, Y=[\d.]+, Width=[\d.]+, Height=[\d.]+}", clusterbox.ToString()); + Utils.AssertPattern(@"{X=[\d.]+, Y=[\d.]+, Width=[\d.]+, Height=[\d.]+}", rootgraphbox.ToString()); } [Test, Order(4)] diff --git a/Rubjerg.Graphviz.Test/Reproductions.cs b/Rubjerg.Graphviz.Test/Reproductions.cs index cc5bb14..c755eb5 100644 --- a/Rubjerg.Graphviz.Test/Reproductions.cs +++ b/Rubjerg.Graphviz.Test/Reproductions.cs @@ -40,7 +40,8 @@ public void TestRecordShapeAlignment(string fontname, double fontsize, double ma root.ComputeLayout(); //TestContext.Write(root.ToDotString()); - var rects = nodeA.GetRecordRectangles().ToList(); + // This test is fixed by passing snapOntoDrawingCoordinates: true + var rects = nodeA.GetRecordRectangles(snapOntoDrawingCoordinates: true).ToList(); Assert.That(rects[0].FarPoint().X, Is.EqualTo(rects[2].FarPoint().X)); } diff --git a/Rubjerg.Graphviz.Test/TestDotLayout.cs b/Rubjerg.Graphviz.Test/TestDotLayout.cs index 055a589..3f89955 100644 --- a/Rubjerg.Graphviz.Test/TestDotLayout.cs +++ b/Rubjerg.Graphviz.Test/TestDotLayout.cs @@ -34,13 +34,13 @@ public void TestLayoutMethodsWithoutLayout() { CreateSimpleTestGraph(out RootGraph root, out Node nodeA, out Edge edge); - Assert.AreEqual(root.GetBoundingBox(), default(RectangleF)); + Assert.AreEqual(root.GetBoundingBox(), default(RectangleD)); Assert.AreEqual(root.GetDrawing().Count, 0); Assert.AreEqual(root.GetLabelDrawing().Count, 0); - Assert.AreEqual(nodeA.GetPosition(), default(PointF)); - Assert.AreEqual(nodeA.GetBoundingBox(), default(RectangleF)); - Assert.AreEqual(nodeA.GetSize(), default(SizeF)); + Assert.AreEqual(nodeA.GetPosition(), default(PointD)); + Assert.AreEqual(nodeA.GetBoundingBox(), default(RectangleD)); + Assert.AreEqual(nodeA.GetSize(), default(SizeD)); Assert.AreEqual(nodeA.GetRecordRectangles().Count(), 0); Assert.AreEqual(nodeA.GetDrawing().Count, 0); Assert.AreEqual(nodeA.GetLabelDrawing().Count, 0); @@ -181,7 +181,7 @@ public void TestRecordShapeOrder() var rects = nodeA.GetRecordRectangles().ToList(); - Utils.AssertOrder(rects, r => (r.Origin.X, r.Origin.Y)); + Utils.AssertOrder(rects, r => (r.Origin.X, -r.Origin.Y)); Assert.That(rects.Count, Is.EqualTo(9)); } diff --git a/Rubjerg.Graphviz.Test/TestXDotLayout.cs b/Rubjerg.Graphviz.Test/TestXDotLayout.cs index 5a909c7..6b98160 100644 --- a/Rubjerg.Graphviz.Test/TestXDotLayout.cs +++ b/Rubjerg.Graphviz.Test/TestXDotLayout.cs @@ -82,7 +82,7 @@ public void TestRecordShapeOrder() nodeA.SetAttribute("label", "1|2|3|{4|5}|6|{7|8|9}"); - var xdotGraph = root.CreateLayout(); + var xdotGraph = root.CreateLayout(coordinateSystem: CoordinateSystem.TopLeft); var xNodeA = xdotGraph.GetNode("A"); var rects = xNodeA.GetRecordRectangles().ToList(); @@ -108,4 +108,15 @@ public void TestEmptyRecordShapes() var rects = xNodeA.GetRecordRectangles().ToList(); Assert.That(rects.Count, Is.EqualTo(5)); } + + [Test()] + public void TestCoordinateTransformation() + { + RootGraph root = Utils.CreateUniqueTestGraph(); + Node nodeA = root.GetOrAddNode("A"); + var xdotGraph = root.CreateLayout(coordinateSystem: CoordinateSystem.TopLeft); + // Check that translating back gets us the old bounding box + var translatedBack = xdotGraph.GetBoundingBox().ForCoordSystem(CoordinateSystem.BottomLeft, xdotGraph.RawMaxY()); + Assert.AreEqual(translatedBack, xdotGraph.RawBoundingBox()); + } } diff --git a/Rubjerg.Graphviz.Test/Tutorial.cs b/Rubjerg.Graphviz.Test/Tutorial.cs index 34b5221..c3b6779 100644 --- a/Rubjerg.Graphviz.Test/Tutorial.cs +++ b/Rubjerg.Graphviz.Test/Tutorial.cs @@ -7,7 +7,7 @@ namespace Rubjerg.Graphviz.Test; public class Tutorial { public const string PointPattern = @"{X=[\d.]+, Y=[\d.]+}"; - public const string RectPattern = @"{X=[\d.]+,Y=[\d.]+,Width=[\d.]+,Height=[\d.]+}"; + public const string RectPattern = @"{X=[\d.]+, Y=[\d.]+, Width=[\d.]+, Height=[\d.]+}"; public const string SplinePattern = @"{X=[\d.]+, Y=[\d.]+}, {X=[\d.]+, Y=[\d.]+}, {X=[\d.]+, Y=[\d.]+}, {X=[\d.]+, Y=[\d.]+}"; @@ -79,8 +79,10 @@ public void Layouting() // Or we can ask Graphviz to compute the layout and programatically read out the layout attributes // This will create a copy of our original graph with layout information attached to it in the form - // of attributes. - RootGraph layout = root.CreateLayout(); + // of attributes. Graphviz outputs coordinates in a bottom-left originated coordinate system. + // But since many applications require rendering in a top-left originated coordinate system, + // we provide a way to translate the coordinates. + RootGraph layout = root.CreateLayout(coordinateSystem: CoordinateSystem.TopLeft); // There are convenience methods available that parse these attributes for us and give // back the layout information in an accessible form. diff --git a/Rubjerg.Graphviz/XDot.cs b/Rubjerg.Graphviz/XDot.cs index cb2a21b..de9abef 100644 --- a/Rubjerg.Graphviz/XDot.cs +++ b/Rubjerg.Graphviz/XDot.cs @@ -72,6 +72,8 @@ internal PointD ForCoordSystem(CoordinateSystem coordSystem, double maxY) return this; return new PointD(X, maxY - Y); } + + public override string ToString() => $"{{X={X}, Y={Y}}}"; } /// The origin of the rectangle, which is the point closest to the origin of the coordinate system. @@ -104,6 +106,8 @@ internal RectangleD ForCoordSystem(CoordinateSystem coordSystem, double maxY) Origin = new PointD(translated.X, translated.Y - Height), }; } + + public override string ToString() => $"{{X={X}, Y={Y}, Width={Width}, Height={Height}}}"; } public record struct ColorStop(float Frac, string HtmlColor);