Skip to content

Commit

Permalink
added tests for nextEnd method
Browse files Browse the repository at this point in the history
  • Loading branch information
gebv committed Jul 6, 2021
1 parent a5b0860 commit c2741d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 8 additions & 8 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func lookupNextToken(in string, offset int, parent *node, res *[]Token, numParam
// -- -- {END}

if offset+child.Token.Len <= len(in) {
if nextEnd(in, offset, child) && offset+child.Token.Len != len(in) {
if child.nextEnd() && offset+child.Token.Len != len(in) {
continue
}
if in[offset:offset+child.Token.Len] == child.Token.Raw {
Expand Down Expand Up @@ -223,13 +223,6 @@ func rightPath(in string, offset int, node *node) (*node, int) {
return nil, 0
}

func nextEnd(in string, offset int, node *node) bool {
if len(node.Childs) != 1 {
return false
}
return node.Childs[0].Token.Mode == END
}

// TODO: cover with tests as the tree is filled
func appendChild(parent *node, i int, tokens []Token) {
if i >= len(tokens) {
Expand Down Expand Up @@ -299,6 +292,13 @@ func (n *node) lengthConstOrZero() int {
return len(n.Token.Raw)
}

func (n *node) nextEnd() bool {
if len(n.Childs) != 1 {
return false
}
return n.Childs[0].Token.Mode == END
}

// Less returns true if
// - left token type is CONST
// - more length of value of token (type is CONST) on the left than right
Expand Down
13 changes: 13 additions & 0 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,16 @@ func Test_PatternWithSeparator(t *testing.T) {
t.Log(params)
})
}

func Test_nextEnd(t *testing.T) {
n := &node{}
assert.False(t, n.nextEnd())
n = &node{Childs: []*node{{Token: ConstToken("!")}}}
assert.False(t, n.nextEnd())
n = &node{Childs: []*node{{Token: SeparatorToken("a")}}}
assert.False(t, n.nextEnd())
n = &node{Childs: []*node{{Token: EndToken}}}
assert.True(t, n.nextEnd())
n = &node{Childs: []*node{{Token: NamedEndToken("abc")}}}
assert.True(t, n.nextEnd())
}

0 comments on commit c2741d7

Please # to comment.