Skip to content

Commit

Permalink
Proxy ErrAttributeNotFound from clib to allow consumers to test again…
Browse files Browse the repository at this point in the history
…st the error
  • Loading branch information
Fiveside committed Aug 14, 2017
1 parent d65fd00 commit 9764d75
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clib/clib.go
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ func XMLElementGetAttributeNode(n PtrSource, name string) (uintptr, error) {
}

if prop == nil || XMLNodeType(prop._type) != AttributeNode {
return 0, errors.New("attribute not found")
return 0, ErrAttributeNotFound
}

return uintptr(unsafe.Pointer(prop)), nil
Expand Down
1 change: 1 addition & 0 deletions clib/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
)

var (
ErrAttributeNotFound = errors.New("attribute not found")
ErrAttributeNameTooLong = errors.New("attribute name too long")
ErrElementNameTooLong = errors.New("element name too long")
ErrNamespaceURITooLong = errors.New("namespace uri too long")
Expand Down
2 changes: 1 addition & 1 deletion dom/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
ErrAttributeNotFound = errors.New("attribute not found")
ErrAttributeNotFound = clib.ErrAttributeNotFound
ErrInvalidNodeType = errors.New("invalid node type")
)

Expand Down
27 changes: 27 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"regexp"
"testing"

"github.com/lestrrat/go-libxml2/dom"
"github.com/lestrrat/go-libxml2/types"

"github.com/lestrrat/go-libxml2/clib"
"github.com/lestrrat/go-libxml2/parser"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -330,3 +333,27 @@ func TestPiWrapNodeIssue(t *testing.T) {
t.Fatalf("XML did not convert back correctly, expected: %v, got: %v", textXML, str)
}
}

func TestGetNonexistentAttributeReturnsRecoverableError(t *testing.T) {
const src = `<?xml version="1.0"?><rootnode/>`
doc, err := ParseString(src)
if !assert.NoError(t, err, "Should parse") {
return
}
defer doc.Free()

rootNode, err := doc.DocumentElement()
if !assert.NoError(t, err, "Should find root element") {
return
}

el, ok := rootNode.(types.Element)
if !ok {
t.Fatalf("Root node was not an element")
}

_, err = el.GetAttribute("non-existant")
if err != dom.ErrAttributeNotFound {
t.Fatalf("GetAttribute() error not comparable to existing library")
}
}

0 comments on commit 9764d75

Please sign in to comment.