Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chtenb committed Aug 11, 2023
1 parent 8b1bfe9 commit 419bd31
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.]+}";

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions Rubjerg.Graphviz.Test/OldTutorial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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)]
Expand Down
3 changes: 2 additions & 1 deletion Rubjerg.Graphviz.Test/Reproductions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
10 changes: 5 additions & 5 deletions Rubjerg.Graphviz.Test/TestDotLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down
13 changes: 12 additions & 1 deletion Rubjerg.Graphviz.Test/TestXDotLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
}
}
8 changes: 5 additions & 3 deletions Rubjerg.Graphviz.Test/Tutorial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.]+}";

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions Rubjerg.Graphviz/XDot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}";
}

/// <param name="Origin">The origin of the rectangle, which is the point closest to the origin of the coordinate system.</param>
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 419bd31

Please sign in to comment.