Fix tests

This commit is contained in:
Júlio César Batista 2022-12-02 10:23:11 -08:00
parent 11d29facb8
commit f918991550
No known key found for this signature in database
GPG key ID: 10C4B861BF314878
6 changed files with 6771 additions and 6778 deletions

View file

@ -58,7 +58,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte
layoutObjects, objectOrder := getLayoutObjects(g, objectsToRemove) layoutObjects, objectOrder := getLayoutObjects(g, objectsToRemove)
g.Objects = layoutObjects g.Objects = layoutObjects
if g.Root.Attributes.Shape.Value == d2target.ShapeSequenceDiagram { if isRootSequenceDiagram(g) {
// don't need to run the layout engine if the root is a sequence diagram // don't need to run the layout engine if the root is a sequence diagram
g.Root.TopLeft = geo.NewPoint(0, 0) g.Root.TopLeft = geo.NewPoint(0, 0)
} else if err := layout(ctx, g); err != nil { } else if err := layout(ctx, g); err != nil {
@ -66,10 +66,13 @@ func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Conte
} }
cleanup(g, sequenceDiagrams, objectOrder, edgeOrder) cleanup(g, sequenceDiagrams, objectOrder, edgeOrder)
return nil return nil
} }
func isRootSequenceDiagram(g *d2graph.Graph) bool {
return g.Root.Attributes.Shape.Value == d2target.ShapeSequenceDiagram
}
// layoutSequenceDiagram finds the edges inside the sequence diagram and performs the layout on the object descendants // layoutSequenceDiagram finds the edges inside the sequence diagram and performs the layout on the object descendants
func layoutSequenceDiagram(g *d2graph.Graph, obj *d2graph.Object) *sequenceDiagram { func layoutSequenceDiagram(g *d2graph.Graph, obj *d2graph.Object) *sequenceDiagram {
// find the edges that belong to this sequence diagram // find the edges that belong to this sequence diagram
@ -119,7 +122,13 @@ func getLayoutObjects(g *d2graph.Graph, toRemove map[*d2graph.Object]struct{}) (
// - adds the sequence diagram descendants back to the graph objects // - adds the sequence diagram descendants back to the graph objects
// - sorts edges and objects to their original graph order // - sorts edges and objects to their original graph order
func cleanup(g *d2graph.Graph, sequenceDiagrams map[string]*sequenceDiagram, objectsOrder, edgesOrder map[string]int) { func cleanup(g *d2graph.Graph, sequenceDiagrams map[string]*sequenceDiagram, objectsOrder, edgesOrder map[string]int) {
for _, obj := range g.Objects { var objects []*d2graph.Object
if isRootSequenceDiagram(g) {
objects = []*d2graph.Object{g.Root}
} else {
objects = g.Objects
}
for _, obj := range objects {
if _, exists := sequenceDiagrams[obj.AbsID()]; !exists { if _, exists := sequenceDiagrams[obj.AbsID()]; !exists {
continue continue
} }

View file

@ -34,26 +34,30 @@ func TestBasicSequenceDiagram(t *testing.T) {
g.Edges = []*d2graph.Edge{ g.Edges = []*d2graph.Edge{
{ {
Src: n1, Src: n1,
Dst: n2, Dst: n2,
Index: 0,
Attributes: d2graph.Attributes{ Attributes: d2graph.Attributes{
Label: d2graph.Scalar{Value: "left to right"}, Label: d2graph.Scalar{Value: "left to right"},
}, },
}, },
{ {
Src: n2, Src: n2,
Dst: n1, Dst: n1,
Index: 0,
Attributes: d2graph.Attributes{ Attributes: d2graph.Attributes{
Label: d2graph.Scalar{Value: "right to left"}, Label: d2graph.Scalar{Value: "right to left"},
}, },
}, },
{ {
Src: n1, Src: n1,
Dst: n2, Dst: n2,
Index: 1,
}, },
{ {
Src: n2, Src: n2,
Dst: n1, Dst: n1,
Index: 1,
}, },
} }
nEdges := len(g.Edges) nEdges := len(g.Edges)
@ -187,17 +191,21 @@ func TestSpansSequenceDiagram(t *testing.T) {
g.Edges = []*d2graph.Edge{ g.Edges = []*d2graph.Edge{
{ {
Src: a_t1, Src: a_t1,
Dst: b_t1, Dst: b_t1,
Index: 0,
}, { }, {
Src: b_t1, Src: b_t1,
Dst: a_t1, Dst: a_t1,
Index: 0,
}, { }, {
Src: a_t2, Src: a_t2,
Dst: b, Dst: b,
Index: 0,
}, { }, {
Src: b, Src: b,
Dst: a_t2, Dst: a_t2,
Index: 0,
}, },
} }

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 498 KiB

After

Width:  |  Height:  |  Size: 498 KiB

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 498 KiB

After

Width:  |  Height:  |  Size: 498 KiB