From b0bfbfea076df0bf703f20255a46b8c32d57cdf0 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 15:50:16 -0800 Subject: [PATCH 01/12] add random style lines --- d2chaos/d2chaos.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/d2chaos/d2chaos.go b/d2chaos/d2chaos.go index 910c999351..4a8c23a560 100644 --- a/d2chaos/d2chaos.go +++ b/d2chaos/d2chaos.go @@ -121,6 +121,15 @@ func (gs *dslGenState) node() error { gs.nodeShapes[nodeID] = randShape } + if gs.roll(25, 75) == 0 { + // 25% chance of adding a style + randStyle, randVal := gs.randStyle() + gs.g, err = d2oracle.Set(gs.g, nodeID+".style."+randStyle, nil, go2.Pointer(randVal)) + if err != nil { + return err + } + } + return nil } @@ -262,6 +271,44 @@ func (gs *dslGenState) randStr(n int, inKey bool) string { return d2format.Format(as) } +var universalStyles = []string{ + "opacity", + "stroke", + "fill", + "stroke-width", + "stroke-dash", + "border-radius", +} + +var floatStyles = map[string]struct{}{ + "opacity": {}, +} + +var intStyles = map[string]struct{}{ + "stroke-width": {}, + "stroke-dash": {}, + "border-radius": {}, +} + +var colorStyles = map[string]struct{}{ + "stroke": {}, + "fill": {}, +} + +func (gs *dslGenState) randStyle() (string, string) { + style := universalStyles[gs.rand.Intn(len(universalStyles))] + if _, ok := floatStyles[style]; ok { + return style, fmt.Sprint(gs.rand.Float64()) + } + if _, ok := intStyles[style]; ok { + return style, fmt.Sprint(gs.rand.Intn(6)) + } + if _, ok := colorStyles[style]; ok { + return style, "blue" + } + return "", "" +} + func (gs *dslGenState) randShape() string { for { s := shapes[gs.rand.Intn(len(shapes))] From cae4f4706550e0c68c7196509ff277b83ff0e75d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 15:58:15 -0800 Subject: [PATCH 02/12] add random arrowheads --- d2chaos/d2chaos.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/d2chaos/d2chaos.go b/d2chaos/d2chaos.go index 4a8c23a560..a9f87754a9 100644 --- a/d2chaos/d2chaos.go +++ b/d2chaos/d2chaos.go @@ -155,13 +155,21 @@ func (gs *dslGenState) edge() error { } } + srcArrowhead := "" srcArrow := "-" if gs.randBool() { srcArrow = "<" + if gs.roll(25, 75) == 0 { + srcArrowhead = gs.randArrowhead() + } } dstArrow := "-" + dstArrowhead := "" if gs.randBool() { dstArrow = ">" + if gs.roll(25, 75) == 0 { + dstArrowhead = gs.randArrowhead() + } if srcArrow == "<" { dstArrow = "->" } @@ -172,6 +180,30 @@ func (gs *dslGenState) edge() error { if err != nil { return err } + if srcArrowhead != "" { + gs.g, err = d2oracle.Set(gs.g, key+".source-arrowhead.shape", nil, go2.Pointer(srcArrowhead)) + if err != nil { + return err + } + if gs.randBool() { + gs.g, err = d2oracle.Set(gs.g, key+".source-arrowhead.label", nil, go2.Pointer("1")) + if err != nil { + return err + } + } + } + if dstArrowhead != "" { + gs.g, err = d2oracle.Set(gs.g, key+".target-arrowhead.shape", nil, go2.Pointer(dstArrowhead)) + if err != nil { + return err + } + if gs.randBool() { + gs.g, err = d2oracle.Set(gs.g, key+".target-arrowhead.label", nil, go2.Pointer("1")) + if err != nil { + return err + } + } + } if gs.randBool() { maxLen := 8 if complexIDs { @@ -309,6 +341,21 @@ func (gs *dslGenState) randStyle() (string, string) { return "", "" } +var arrowheads = []string{ + "arrow", + "diamond", + "circle", + "triangle", + "cf-one", + "cf-many", + "cf-one-required", + "cf-many-required", +} + +func (gs *dslGenState) randArrowhead() string { + return arrowheads[gs.rand.Intn(len(arrowheads))] +} + func (gs *dslGenState) randShape() string { for { s := shapes[gs.rand.Intn(len(shapes))] From 4c3e99378f49101d9a72174359cfd150424d473f Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 16:00:32 -0800 Subject: [PATCH 03/12] add edge style randomly --- d2chaos/d2chaos.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/d2chaos/d2chaos.go b/d2chaos/d2chaos.go index a9f87754a9..144fd9ddb6 100644 --- a/d2chaos/d2chaos.go +++ b/d2chaos/d2chaos.go @@ -204,6 +204,13 @@ func (gs *dslGenState) edge() error { } } } + if gs.roll(25, 75) == 0 { + // 25% chance of adding a style + gs.g, err = d2oracle.Set(gs.g, key+".style.stroke", nil, go2.Pointer("blue")) + if err != nil { + return err + } + } if gs.randBool() { maxLen := 8 if complexIDs { From 1f4af973b94165d9a7dc9ded918c014c681c5083 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 16:31:45 -0800 Subject: [PATCH 04/12] save --- d2chaos/d2chaos_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/d2chaos/d2chaos_test.go b/d2chaos/d2chaos_test.go index ad7343b5a8..03f4e805c0 100644 --- a/d2chaos/d2chaos_test.go +++ b/d2chaos/d2chaos_test.go @@ -17,6 +17,7 @@ import ( "oss.terrastruct.com/d2/d2compiler" "oss.terrastruct.com/d2/d2exporter" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout" + "oss.terrastruct.com/d2/d2oracle" "oss.terrastruct.com/d2/lib/log" "oss.terrastruct.com/d2/lib/textmeasure" ) @@ -133,6 +134,22 @@ func test(t *testing.T, textPath, text string) { t.Fatal(err) } }) + t.Run("d2oracle.Delete", func(t *testing.T) { + key := "" + defer func() { + r := recover() + if r != nil { + t.Errorf("recovered d2oracle panic deleting %s: %#v\n%s\n%s", key, r, debug.Stack(), text) + } + }() + for _, obj := range g.Objects { + key = obj.AbsID() + _, err := d2oracle.Delete(g, key) + if err != nil { + t.Fatal(err) + } + } + }) } func testPinned(t *testing.T, outDir string) { From 5cf0198e46e4e801b89d6909b9b7b9746262f4e6 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 16:46:50 -0800 Subject: [PATCH 05/12] save --- d2chaos/d2chaos_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/d2chaos/d2chaos_test.go b/d2chaos/d2chaos_test.go index 03f4e805c0..d91005202a 100644 --- a/d2chaos/d2chaos_test.go +++ b/d2chaos/d2chaos_test.go @@ -16,6 +16,7 @@ import ( "oss.terrastruct.com/d2/d2chaos" "oss.terrastruct.com/d2/d2compiler" "oss.terrastruct.com/d2/d2exporter" + "oss.terrastruct.com/d2/d2format" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout" "oss.terrastruct.com/d2/d2oracle" "oss.terrastruct.com/d2/lib/log" @@ -136,17 +137,19 @@ func test(t *testing.T, textPath, text string) { }) t.Run("d2oracle.Delete", func(t *testing.T) { key := "" + before := "" defer func() { r := recover() if r != nil { - t.Errorf("recovered d2oracle panic deleting %s: %#v\n%s\n%s", key, r, debug.Stack(), text) + t.Errorf("recovered d2oracle panic deleting %s: %#v\n%s\n%s", key, r, debug.Stack(), before) } }() for _, obj := range g.Objects { key = obj.AbsID() - _, err := d2oracle.Delete(g, key) + before = d2format.Format(g.AST) + g, err = d2oracle.Delete(g, key) if err != nil { - t.Fatal(err) + t.Fatal(fmt.Errorf("Failed to delete %s in\n%s\n: %v", key, before, err)) } } }) From 6e97befc0ccb308a61a0aa81e39694a6834ac693 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 17:01:45 -0800 Subject: [PATCH 06/12] fix delete --- d2chaos/d2chaos_test.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/d2chaos/d2chaos_test.go b/d2chaos/d2chaos_test.go index d91005202a..b5144e7264 100644 --- a/d2chaos/d2chaos_test.go +++ b/d2chaos/d2chaos_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io/ioutil" + "math/rand" "os" "path/filepath" "runtime/debug" @@ -13,6 +14,7 @@ import ( "github.com/stretchr/testify/assert" + "oss.terrastruct.com/d2/d2ast" "oss.terrastruct.com/d2/d2chaos" "oss.terrastruct.com/d2/d2compiler" "oss.terrastruct.com/d2/d2exporter" @@ -137,19 +139,23 @@ func test(t *testing.T, textPath, text string) { }) t.Run("d2oracle.Delete", func(t *testing.T) { key := "" - before := "" + var lastAST *d2ast.Map defer func() { r := recover() if r != nil { - t.Errorf("recovered d2oracle panic deleting %s: %#v\n%s\n%s", key, r, debug.Stack(), before) + t.Errorf("recovered d2oracle panic deleting %s: %#v\n%s\n%s", key, r, debug.Stack(), d2format.Format(lastAST)) } }() + // In a random order, delete every object one by one + rand.Shuffle(len(g.Objects), func(i, j int) { + g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] + }) for _, obj := range g.Objects { key = obj.AbsID() - before = d2format.Format(g.AST) + lastAST = g.AST g, err = d2oracle.Delete(g, key) if err != nil { - t.Fatal(fmt.Errorf("Failed to delete %s in\n%s\n: %v", key, before, err)) + t.Fatal(fmt.Errorf("Failed to delete %s in\n%s\n: %v", key, d2format.Format(lastAST), err)) } } }) From 56cb678efffaf3945fa53b310537fb77e43fff33 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 17:14:23 -0800 Subject: [PATCH 07/12] save --- d2chaos/d2chaos.go | 3 ++ d2chaos/d2chaos_test.go | 65 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/d2chaos/d2chaos.go b/d2chaos/d2chaos.go index 144fd9ddb6..5931e2da4b 100644 --- a/d2chaos/d2chaos.go +++ b/d2chaos/d2chaos.go @@ -305,6 +305,9 @@ func (gs *dslGenState) randStr(n int, inKey bool) string { rune('}'), rune('{'), rune('\\'), + rune('\''), + rune('"'), + rune(' '), }) as := d2ast.RawString(s, inKey) return d2format.Format(as) diff --git a/d2chaos/d2chaos_test.go b/d2chaos/d2chaos_test.go index b5144e7264..49c1d5780b 100644 --- a/d2chaos/d2chaos_test.go +++ b/d2chaos/d2chaos_test.go @@ -19,6 +19,7 @@ import ( "oss.terrastruct.com/d2/d2compiler" "oss.terrastruct.com/d2/d2exporter" "oss.terrastruct.com/d2/d2format" + "oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout" "oss.terrastruct.com/d2/d2oracle" "oss.terrastruct.com/d2/lib/log" @@ -137,6 +138,7 @@ func test(t *testing.T, textPath, text string) { t.Fatal(err) } }) + // In a random order, delete every object one by one t.Run("d2oracle.Delete", func(t *testing.T) { key := "" var lastAST *d2ast.Map @@ -146,7 +148,6 @@ func test(t *testing.T, textPath, text string) { t.Errorf("recovered d2oracle panic deleting %s: %#v\n%s\n%s", key, r, debug.Stack(), d2format.Format(lastAST)) } }() - // In a random order, delete every object one by one rand.Shuffle(len(g.Objects), func(i, j int) { g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] }) @@ -159,6 +160,68 @@ func test(t *testing.T, textPath, text string) { } } }) + // In a random order, move every nested object one level up + t.Run("d2oracle.MoveOut", func(t *testing.T) { + key := "" + var lastAST *d2ast.Map + defer func() { + r := recover() + if r != nil { + t.Errorf("recovered d2oracle panic moving out %s: %#v\n%s\n%s", key, r, debug.Stack(), d2format.Format(lastAST)) + } + }() + rand.Shuffle(len(g.Objects), func(i, j int) { + g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] + }) + for _, obj := range g.Objects { + if obj.Parent == obj.Graph.Root { + continue + } + key = obj.AbsID() + lastAST = g.AST + g, err = d2oracle.Move(g, key, obj.Parent.AbsID()+"."+obj.ID) + if err != nil { + t.Fatal(fmt.Errorf("Failed to move %s in\n%s\n: %v", key, d2format.Format(lastAST), err)) + } + } + }) + // In a random order, choose one container (if any), and move all objects into that + t.Run("d2oracle.MoveIn", func(t *testing.T) { + var container *d2graph.Object + key := "" + var lastAST *d2ast.Map + defer func() { + r := recover() + if r != nil { + t.Errorf("recovered d2oracle panic moving %s into %s: %#v\n%s\n%s", key, container.AbsID(), r, debug.Stack(), d2format.Format(lastAST)) + } + }() + rand.Shuffle(len(g.Objects), func(i, j int) { + g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] + }) + for _, obj := range g.Objects { + if len(obj.ChildrenArray) > 0 { + container = obj + } + if obj.Attributes.Shape.Value == "sequence_diagram" { + return + } + } + if container == nil { + return + } + for _, obj := range g.Objects { + if obj == container || obj.Parent == container { + continue + } + key = obj.AbsID() + lastAST = g.AST + g, err = d2oracle.Move(g, key, container.AbsID()+"."+obj.ID) + if err != nil { + t.Fatal(fmt.Errorf("Failed to move %s into %s in\n%s\n: %v", key, container.AbsID(), d2format.Format(lastAST), err)) + } + } + }) } func testPinned(t *testing.T, outDir string) { From 76790688ad7ba4f31e7e3f75d6f6cd7f4873ab7b Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 17:17:27 -0800 Subject: [PATCH 08/12] save --- d2chaos/d2chaos_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/d2chaos/d2chaos_test.go b/d2chaos/d2chaos_test.go index 49c1d5780b..44fbc2034a 100644 --- a/d2chaos/d2chaos_test.go +++ b/d2chaos/d2chaos_test.go @@ -196,9 +196,9 @@ func test(t *testing.T, textPath, text string) { t.Errorf("recovered d2oracle panic moving %s into %s: %#v\n%s\n%s", key, container.AbsID(), r, debug.Stack(), d2format.Format(lastAST)) } }() - rand.Shuffle(len(g.Objects), func(i, j int) { - g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] - }) + // rand.Shuffle(len(g.Objects), func(i, j int) { + // g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] + // }) for _, obj := range g.Objects { if len(obj.ChildrenArray) > 0 { container = obj From e1664ca24319406ac76c19aff9708f949546d044 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 17:48:08 -0800 Subject: [PATCH 09/12] update --- d2chaos/d2chaos.go | 3 -- d2chaos/d2chaos_test.go | 88 ++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/d2chaos/d2chaos.go b/d2chaos/d2chaos.go index 5931e2da4b..144fd9ddb6 100644 --- a/d2chaos/d2chaos.go +++ b/d2chaos/d2chaos.go @@ -305,9 +305,6 @@ func (gs *dslGenState) randStr(n int, inKey bool) string { rune('}'), rune('{'), rune('\\'), - rune('\''), - rune('"'), - rune(' '), }) as := d2ast.RawString(s, inKey) return d2format.Format(as) diff --git a/d2chaos/d2chaos_test.go b/d2chaos/d2chaos_test.go index 44fbc2034a..3ef8b972c2 100644 --- a/d2chaos/d2chaos_test.go +++ b/d2chaos/d2chaos_test.go @@ -19,7 +19,6 @@ import ( "oss.terrastruct.com/d2/d2compiler" "oss.terrastruct.com/d2/d2exporter" "oss.terrastruct.com/d2/d2format" - "oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout" "oss.terrastruct.com/d2/d2oracle" "oss.terrastruct.com/d2/lib/log" @@ -140,6 +139,10 @@ func test(t *testing.T, textPath, text string) { }) // In a random order, delete every object one by one t.Run("d2oracle.Delete", func(t *testing.T) { + g, err := d2compiler.Compile("", strings.NewReader(text), nil) + if err != nil { + t.Fatal(err) + } key := "" var lastAST *d2ast.Map defer func() { @@ -162,6 +165,10 @@ func test(t *testing.T, textPath, text string) { }) // In a random order, move every nested object one level up t.Run("d2oracle.MoveOut", func(t *testing.T) { + g, err := d2compiler.Compile("", strings.NewReader(text), nil) + if err != nil { + t.Fatal(err) + } key := "" var lastAST *d2ast.Map defer func() { @@ -186,42 +193,49 @@ func test(t *testing.T, textPath, text string) { } }) // In a random order, choose one container (if any), and move all objects into that - t.Run("d2oracle.MoveIn", func(t *testing.T) { - var container *d2graph.Object - key := "" - var lastAST *d2ast.Map - defer func() { - r := recover() - if r != nil { - t.Errorf("recovered d2oracle panic moving %s into %s: %#v\n%s\n%s", key, container.AbsID(), r, debug.Stack(), d2format.Format(lastAST)) - } - }() - // rand.Shuffle(len(g.Objects), func(i, j int) { - // g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] - // }) - for _, obj := range g.Objects { - if len(obj.ChildrenArray) > 0 { - container = obj - } - if obj.Attributes.Shape.Value == "sequence_diagram" { - return - } - } - if container == nil { - return - } - for _, obj := range g.Objects { - if obj == container || obj.Parent == container { - continue - } - key = obj.AbsID() - lastAST = g.AST - g, err = d2oracle.Move(g, key, container.AbsID()+"."+obj.ID) - if err != nil { - t.Fatal(fmt.Errorf("Failed to move %s into %s in\n%s\n: %v", key, container.AbsID(), d2format.Format(lastAST), err)) - } - } - }) + // TODO this doesn't work and I don't know why. Keeps trying to move a FROM that doesn't exist + // t.Run("d2oracle.MoveIn", func(t *testing.T) { + // g, err := d2compiler.Compile("", strings.NewReader(text), nil) + // if err != nil { + // t.Fatal(err) + // } + // for _, obj := range g.Objects { + // if obj.Attributes.Shape.Value == "sequence_diagram" { + // return + // } + // } + // var container *d2graph.Object + // key := "" + // var lastAST *d2ast.Map + // defer func() { + // r := recover() + // if r != nil { + // t.Errorf("recovered d2oracle panic moving %s into %s: %#v\n%s\n%s", key, container.AbsID(), r, debug.Stack(), d2format.Format(lastAST)) + // } + // }() + // rand.Shuffle(len(g.Objects), func(i, j int) { + // g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] + // }) + // container = g.Objects[0] + // OUTER: + // for _, obj := range g.Objects { + // if obj == container || obj.Parent == container { + // continue + // } + // // Skip ancestors of the container chosen + // for curr := container; curr != nil; curr = curr.Parent { + // if curr == obj { + // continue OUTER + // } + // } + // key = obj.AbsID() + // lastAST = g.AST + // g, err = d2oracle.Move(g, key, container.AbsID()+"."+obj.ID) + // if err != nil { + // t.Fatal(fmt.Errorf("Failed to move %s into %s in\n%s\n: %v", key, container.AbsID(), d2format.Format(lastAST), err)) + // } + // } + // }) } func testPinned(t *testing.T, outDir string) { From e1570ef6ecf95c51407eccc6bd573261783d92ca Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 18:02:59 -0800 Subject: [PATCH 10/12] save --- d2chaos/d2chaos_test.go | 96 ++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/d2chaos/d2chaos_test.go b/d2chaos/d2chaos_test.go index 3ef8b972c2..3ee5cdeed5 100644 --- a/d2chaos/d2chaos_test.go +++ b/d2chaos/d2chaos_test.go @@ -19,6 +19,7 @@ import ( "oss.terrastruct.com/d2/d2compiler" "oss.terrastruct.com/d2/d2exporter" "oss.terrastruct.com/d2/d2format" + "oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout" "oss.terrastruct.com/d2/d2oracle" "oss.terrastruct.com/d2/lib/log" @@ -106,11 +107,6 @@ func test(t *testing.T, textPath, text string) { t.Fatal(err) } - g, err := d2compiler.Compile("", strings.NewReader(text), nil) - if err != nil { - t.Fatal(err) - } - t.Run("layout", func(t *testing.T) { defer func() { r := recover() @@ -118,7 +114,10 @@ func test(t *testing.T, textPath, text string) { t.Errorf("recovered layout engine panic: %#v\n%s", r, debug.Stack()) } }() - + g, err := d2compiler.Compile("", strings.NewReader(text), nil) + if err != nil { + t.Fatal(err) + } ctx := log.WithTB(context.Background(), t, nil) ruler, err := textmeasure.NewRuler() @@ -194,48 +193,49 @@ func test(t *testing.T, textPath, text string) { }) // In a random order, choose one container (if any), and move all objects into that // TODO this doesn't work and I don't know why. Keeps trying to move a FROM that doesn't exist - // t.Run("d2oracle.MoveIn", func(t *testing.T) { - // g, err := d2compiler.Compile("", strings.NewReader(text), nil) - // if err != nil { - // t.Fatal(err) - // } - // for _, obj := range g.Objects { - // if obj.Attributes.Shape.Value == "sequence_diagram" { - // return - // } - // } - // var container *d2graph.Object - // key := "" - // var lastAST *d2ast.Map - // defer func() { - // r := recover() - // if r != nil { - // t.Errorf("recovered d2oracle panic moving %s into %s: %#v\n%s\n%s", key, container.AbsID(), r, debug.Stack(), d2format.Format(lastAST)) - // } - // }() - // rand.Shuffle(len(g.Objects), func(i, j int) { - // g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] - // }) - // container = g.Objects[0] - // OUTER: - // for _, obj := range g.Objects { - // if obj == container || obj.Parent == container { - // continue - // } - // // Skip ancestors of the container chosen - // for curr := container; curr != nil; curr = curr.Parent { - // if curr == obj { - // continue OUTER - // } - // } - // key = obj.AbsID() - // lastAST = g.AST - // g, err = d2oracle.Move(g, key, container.AbsID()+"."+obj.ID) - // if err != nil { - // t.Fatal(fmt.Errorf("Failed to move %s into %s in\n%s\n: %v", key, container.AbsID(), d2format.Format(lastAST), err)) - // } - // } - // }) + t.Run("d2oracle.MoveIn", func(t *testing.T) { + g, err := d2compiler.Compile("", strings.NewReader(text), nil) + if err != nil { + t.Fatal(err) + } + for _, obj := range g.Objects { + if obj.Attributes.Shape.Value == "sequence_diagram" { + return + } + } + var container *d2graph.Object + key := "" + var lastAST *d2ast.Map + defer func() { + r := recover() + if r != nil { + t.Errorf("recovered d2oracle panic moving %s into %s: %#v\n%s\n%s", key, container.AbsID(), r, debug.Stack(), d2format.Format(lastAST)) + } + }() + rand.Shuffle(len(g.Objects), func(i, j int) { + g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] + }) + OUTER: + for i := 1; i < len(g.Objects); i++ { + obj := g.Objects[i] + container = g.Objects[i-1] + if obj == container || obj.Parent == container { + continue + } + // Skip ancestors of the container chosen + for curr := container; curr != nil; curr = curr.Parent { + if curr == obj { + continue OUTER + } + } + key = obj.AbsID() + lastAST = g.AST + g, err = d2oracle.Move(g, key, container.AbsID()+"."+obj.ID) + if err != nil { + t.Fatal(fmt.Errorf("Failed to move %s into %s in\n%s\n: %v", key, container.AbsID(), d2format.Format(lastAST), err)) + } + } + }) } func testPinned(t *testing.T, outDir string) { From c585af4172bd7bcde1f8a1352b3152e81e968f95 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 18:14:31 -0800 Subject: [PATCH 11/12] save --- d2chaos/d2chaos_test.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/d2chaos/d2chaos_test.go b/d2chaos/d2chaos_test.go index 3ee5cdeed5..880568846f 100644 --- a/d2chaos/d2chaos_test.go +++ b/d2chaos/d2chaos_test.go @@ -19,7 +19,6 @@ import ( "oss.terrastruct.com/d2/d2compiler" "oss.terrastruct.com/d2/d2exporter" "oss.terrastruct.com/d2/d2format" - "oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout" "oss.terrastruct.com/d2/d2oracle" "oss.terrastruct.com/d2/lib/log" @@ -191,8 +190,7 @@ func test(t *testing.T, textPath, text string) { } } }) - // In a random order, choose one container (if any), and move all objects into that - // TODO this doesn't work and I don't know why. Keeps trying to move a FROM that doesn't exist + // In a random order, move an object into another object t.Run("d2oracle.MoveIn", func(t *testing.T) { g, err := d2compiler.Compile("", strings.NewReader(text), nil) if err != nil { @@ -203,22 +201,22 @@ func test(t *testing.T, textPath, text string) { return } } - var container *d2graph.Object + containerKey := "" key := "" var lastAST *d2ast.Map defer func() { r := recover() if r != nil { - t.Errorf("recovered d2oracle panic moving %s into %s: %#v\n%s\n%s", key, container.AbsID(), r, debug.Stack(), d2format.Format(lastAST)) + t.Errorf("recovered d2oracle panic moving %s into %s: %#v\n%s\n%s", key, containerKey, r, debug.Stack(), d2format.Format(lastAST)) } }() - rand.Shuffle(len(g.Objects), func(i, j int) { - g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] - }) OUTER: for i := 1; i < len(g.Objects); i++ { - obj := g.Objects[i] - container = g.Objects[i-1] + rand.Shuffle(len(g.Objects), func(i, j int) { + g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] + }) + obj := g.Objects[1] + container := g.Objects[0] if obj == container || obj.Parent == container { continue } @@ -229,10 +227,11 @@ func test(t *testing.T, textPath, text string) { } } key = obj.AbsID() + containerKey = container.AbsID() lastAST = g.AST - g, err = d2oracle.Move(g, key, container.AbsID()+"."+obj.ID) + g, err = d2oracle.Move(g, key, containerKey+"."+obj.ID) if err != nil { - t.Fatal(fmt.Errorf("Failed to move %s into %s in\n%s\n: %v", key, container.AbsID(), d2format.Format(lastAST), err)) + t.Fatal(fmt.Errorf("Failed to move %s into %s in\n%s\n: %v", key, containerKey, d2format.Format(lastAST), err)) } } }) From a556b309f88393a64bad992f2f0a4b5241bc15c1 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 22 Feb 2023 18:17:56 -0800 Subject: [PATCH 12/12] move out --- d2chaos/d2chaos_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/d2chaos/d2chaos_test.go b/d2chaos/d2chaos_test.go index 880568846f..d6d2f80d63 100644 --- a/d2chaos/d2chaos_test.go +++ b/d2chaos/d2chaos_test.go @@ -212,6 +212,10 @@ func test(t *testing.T, textPath, text string) { }() OUTER: for i := 1; i < len(g.Objects); i++ { + g, err := d2compiler.Compile("", strings.NewReader(text), nil) + if err != nil { + t.Fatal(err) + } rand.Shuffle(len(g.Objects), func(i, j int) { g.Objects[i], g.Objects[j] = g.Objects[j], g.Objects[i] }) @@ -229,7 +233,7 @@ func test(t *testing.T, textPath, text string) { key = obj.AbsID() containerKey = container.AbsID() lastAST = g.AST - g, err = d2oracle.Move(g, key, containerKey+"."+obj.ID) + _, err = d2oracle.Move(g, key, containerKey+"."+obj.ID) if err != nil { t.Fatal(fmt.Errorf("Failed to move %s into %s in\n%s\n: %v", key, containerKey, d2format.Format(lastAST), err)) }