Skip to content

Commit

Permalink
Decrement Depth in DecodeElement()
Browse files Browse the repository at this point in the history
A test case and fix for Issue #3
  • Loading branch information
cristoper authored and mmcdole committed Oct 3, 2018
1 parent dd19db0 commit e18b6e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions xpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func (p *XMLPullParser) DecodeElement(v interface{}) error {
// to the previous StartTag event's name
p.resetTokenState()
p.Event = EndTag
p.Depth--
p.Name = name
p.token = nil
return nil
Expand Down
27 changes: 27 additions & 0 deletions xpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ func TestSpaceStackNestedTag(t *testing.T) {
assert.EqualValues(t, map[string]string{}, p.Spaces)
}

func TestDecodeElementDepth(t *testing.T) {
crReader := func(charset string, input io.Reader) (io.Reader, error) {
return input, nil
}
r := bytes.NewBufferString(`<root><d2>foo</d2><d2>bar</d2></root>`)
p := xpp.NewXMLPullParser(r, false, crReader)

type v struct{}

// move to root
p.NextTag()
assert.Equal(t, "root", p.Name)
assert.Equal(t, 1, p.Depth)

// decode first <d2>
p.NextTag()
assert.Equal(t, "d2", p.Name)
assert.Equal(t, 2, p.Depth)
p.DecodeElement(&v{})

// decode second <d2>
p.NextTag()
assert.Equal(t, "d2", p.Name)
assert.Equal(t, 2, p.Depth) // should still be 2, not 3
p.DecodeElement(&v{})
}

func toNextStart(t *testing.T, p *xpp.XMLPullParser) {
for {
tok, err := p.NextToken()
Expand Down

0 comments on commit e18b6e9

Please sign in to comment.