aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/test/parser.go24
-rw-r--r--spec/test/parser_test.go19
2 files changed, 43 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
diff --git a/spec/test/parser_test.go b/spec/test/parser_test.go
index 6e77f6d..41b7189 100644
--- a/spec/test/parser_test.go
+++ b/spec/test/parser_test.go
@@ -7,6 +7,25 @@ import (
"testing"
)
+func TestTree_Format(t *testing.T) {
+ expected := `(a
+ (b
+ (c))
+ (d)
+ (e))`
+ tree := NewTree("a",
+ NewTree("b",
+ NewTree("c"),
+ ),
+ NewTree("d"),
+ NewTree("e"),
+ )
+ actual := string(tree.Format())
+ if actual != expected {
+ t.Fatalf("unexpected format:\n%v", actual)
+ }
+}
+
func TestDiffTree(t *testing.T) {
tests := []struct {
t1 *Tree