Skip to content

Commit

Permalink
Merge pull request #394 from ejulio-ts/gh-376-d2chaos
Browse files Browse the repository at this point in the history
d2chaos: fixes chaos tests
  • Loading branch information
ejulio-ts authored Dec 6, 2022
2 parents b6612d3 + 862c03c commit d541e44
Show file tree
Hide file tree
Showing 9 changed files with 885 additions and 268 deletions.
30 changes: 20 additions & 10 deletions d2chaos/d2chaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (

func GenDSL(maxi int) (_ string, err error) {
gs := &dslGenState{
rand: mathrand.New(mathrand.NewSource(time.Now().UnixNano())),
g: d2graph.NewGraph(&d2ast.Map{}),
nodeShapes: make(map[string]string),
rand: mathrand.New(mathrand.NewSource(time.Now().UnixNano())),
g: d2graph.NewGraph(&d2ast.Map{}),
nodeShapes: make(map[string]string),
nodeContainer: make(map[string]string),
}
err = gs.gen(maxi)
if err != nil {
Expand All @@ -32,8 +33,9 @@ type dslGenState struct {
rand *mathrand.Rand
g *d2graph.Graph

nodesArr []string
nodeShapes map[string]string
nodesArr []string
nodeShapes map[string]string
nodeContainer map[string]string
}

func (gs *dslGenState) gen(maxi int) error {
Expand Down Expand Up @@ -70,6 +72,7 @@ func (gs *dslGenState) genNode(containerID string) (string, error) {
}
gs.nodesArr = append(gs.nodesArr, nodeID)
gs.nodeShapes[nodeID] = "square"
gs.nodeContainer[nodeID] = containerID
return nodeID, nil
}

Expand Down Expand Up @@ -123,7 +126,7 @@ func (gs *dslGenState) edge() error {
if err != nil {
return err
}
if src != dst {
if gs.findOuterSequenceDiagram(src) == gs.findOuterSequenceDiagram(dst) {
break
}
err = gs.node()
Expand Down Expand Up @@ -194,6 +197,16 @@ func randRune() rune {
return mathrand.Int31n(128) + 1
}

func (gs *dslGenState) findOuterSequenceDiagram(nodeID string) string {
for {
containerID := gs.nodeContainer[nodeID]
if containerID == "" || gs.nodeShapes[containerID] == d2target.ShapeSequenceDiagram {
return containerID
}
nodeID = containerID
}
}

func String(n int, exclude []rune) string {
var b strings.Builder
for i := 0; i < n; i++ {
Expand Down Expand Up @@ -233,10 +246,7 @@ func (gs *dslGenState) randStr(n int, inKey bool) string {
func (gs *dslGenState) randShape() string {
for {
s := shapes[gs.rand.Intn(len(shapes))]
if s != "image" {
return s
}
if s != "sequence_diagram" {
if s != d2target.ShapeImage {
return s
}
}
Expand Down
7 changes: 6 additions & 1 deletion d2layouts/d2dagrelayout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"math"
"regexp"
"strings"

"cdr.dev/slog"
Expand Down Expand Up @@ -257,7 +258,11 @@ func setGraphAttrs(attrs dagreGraphAttrs) string {
}

func escapeID(id string) string {
id = strings.ReplaceAll(id, `\n`, `\\n`)
// fixes \\
id = strings.ReplaceAll(id, "\\", `\\`)
// replaces \n with \\n whenever \n is not preceded by \ (does not replace \\n)
re := regexp.MustCompile(`[^\\](\n)`)
id = re.ReplaceAllString(id, `\\n`)
// avoid an unescaped \r becoming a \n in the layout result
id = strings.ReplaceAll(id, "\r", `\r`)
return id
Expand Down
5 changes: 4 additions & 1 deletion e2etests/regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
func testRegression(t *testing.T) {
tcs := []testCase{
{
name: "dagre_id_with_newline",
name: "dagre_special_ids",
script: `
ninety\nnine
eighty\reight
seventy\r\nseven
a\\yode -> there
a\\"ode -> there
a\\node -> there
`,
},
{
Expand Down

This file was deleted.

126 changes: 0 additions & 126 deletions e2etests/testdata/regression/dagre_id_with_newline/elk/board.exp.json

This file was deleted.

Loading

0 comments on commit d541e44

Please sign in to comment.