Skip to content

Commit

Permalink
parse: cleanup test utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaq committed Mar 16, 2018
1 parent 7812b3c commit bb6d4b3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
File renamed without changes.
43 changes: 43 additions & 0 deletions parse/check_parse_tree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package parse

import "fmt"

// checkParseTree checks whether the parse tree part of a Node is well-formed.
func checkParseTree(n Node) error {
children := n.Children()
if len(children) == 0 {
return nil
}

// Parent pointers of all children should point to me.
for i, ch := range children {
if ch.Parent() != n {
return fmt.Errorf("parent of child %d (%s) is wrong: %s", i, summary(ch), summary(n))
}
}

// The Begin of the first child should be equal to mine.
if children[0].Begin() != n.Begin() {
return fmt.Errorf("gap between node and first child: %s", summary(n))
}
// The End of the last child should be equal to mine.
nch := len(children)
if children[nch-1].End() != n.End() {
return fmt.Errorf("gap between node and last child: %s", summary(n))
}
// Consecutive children have consecutive position ranges.
for i := 0; i < nch-1; i++ {
if children[i].End() != children[i+1].Begin() {
return fmt.Errorf("gap between child %d and %d of: %s", i, i+1, summary(n))
}
}

// Check children recursively.
for _, ch := range n.Children() {
err := checkParseTree(ch)
if err != nil {
return err
}
}
return nil
}
40 changes: 0 additions & 40 deletions parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,46 +256,6 @@ func TestParse(t *testing.T) {
}
}

// checkParseTree checks whether the parse tree part of a Node is well-formed.
func checkParseTree(n Node) error {
children := n.Children()
if len(children) == 0 {
return nil
}

// Parent pointers of all children should point to me.
for i, ch := range children {
if ch.Parent() != n {
return fmt.Errorf("parent of child %d (%s) is wrong: %s", i, summary(ch), summary(n))
}
}

// The Begin of the first child should be equal to mine.
if children[0].Begin() != n.Begin() {
return fmt.Errorf("gap between node and first child: %s", summary(n))
}
// The End of the last child should be equal to mine.
nch := len(children)
if children[nch-1].End() != n.End() {
return fmt.Errorf("gap between node and last child: %s", summary(n))
}
// Consecutive children have consecutive position ranges.
for i := 0; i < nch-1; i++ {
if children[i].End() != children[i+1].Begin() {
return fmt.Errorf("gap between child %d and %d of: %s", i, i+1, summary(n))
}
}

// Check children recursively.
for _, ch := range n.Children() {
err := checkParseTree(ch)
if err != nil {
return err
}
}
return nil
}

var badCases = []struct {
src string
pos int // expected Begin position of first error
Expand Down

0 comments on commit bb6d4b3

Please sign in to comment.