diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2022-05-29 15:55:53 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2022-05-29 15:55:53 +0900 |
commit | 1ebed922b8c642b6ff4cec71820f24ceeb331c18 (patch) | |
tree | 08b71e6330b418d3d263264193262f489bba29b5 /spec/test/parser.go | |
parent | Add vartan-test command (diff) | |
download | urubu-1ebed922b8c642b6ff4cec71820f24ceeb331c18.tar.gz urubu-1ebed922b8c642b6ff4cec71820f24ceeb331c18.tar.xz |
Support testable tree output in vartan-parse command
Diffstat (limited to 'spec/test/parser.go')
-rw-r--r-- | spec/test/parser.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/spec/test/parser.go b/spec/test/parser.go index 0513ee3..175c89e 100644 --- a/spec/test/parser.go +++ b/spec/test/parser.go @@ -57,6 +57,30 @@ func (t *Tree) path() string { return fmt.Sprintf("%v.[%v]%v", t.Parent.path(), t.Offset, t.Kind) } +func (t *Tree) Format() []byte { + var b bytes.Buffer + t.format(&b, 0) + return b.Bytes() +} + +func (t *Tree) format(buf *bytes.Buffer, depth int) { + for i := 0; i < depth; i++ { + buf.WriteString(" ") + } + buf.WriteString("(") + buf.WriteString(t.Kind) + if len(t.Children) > 0 { + buf.WriteString("\n") + for i, c := range t.Children { + c.format(buf, depth+1) + if i < len(t.Children)-1 { + buf.WriteString("\n") + } + } + } + buf.WriteString(")") +} + func DiffTree(expected, actual *Tree) []*TreeDiff { if expected == nil && actual == nil { return nil |