1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
package goxpath
import (
"bytes"
"runtime/debug"
"testing"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
)
func execVal(xp, x string, exp string, ns map[string]string, t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Error("Panicked: from XPath expr: '" + xp)
t.Error(r)
t.Error(string(debug.Stack()))
}
}()
res := MustParse(xp).MustExec(xmltree.MustParseXML(bytes.NewBufferString(x)), func(o *Opts) { o.NS = ns })
if res.String() != exp {
t.Error("Incorrect result:'" + res.String() + "' from XPath expr: '" + xp + "'. Expecting: '" + exp + "'")
return
}
}
func TestNodeVal(t *testing.T) {
p := `/test`
x := `<?xml version="1.0" encoding="UTF-8"?><test>test<path>path</path>test2</test>`
exp := "testpathtest2"
execVal(p, x, exp, nil, t)
}
func TestAttrVal(t *testing.T) {
p := `/p1/@test`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo" foo="test"><p2/></p1>`
exp := "foo"
execVal(p, x, exp, nil, t)
}
func TestCommentVal(t *testing.T) {
p := `//comment()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><!-- comment --></p1>`
exp := ` comment `
execVal(p, x, exp, nil, t)
}
func TestProcInstVal(t *testing.T) {
p := `//processing-instruction()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><?proc test?></p1>`
exp := `test`
execVal(p, x, exp, nil, t)
}
func TestNodeNamespaceVal(t *testing.T) {
p := `/test:p1/namespace::test`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns:test="http://test"/>`
exp := `http://test`
execVal(p, x, exp, nil, t)
}
|