MCPcopy Create free account
hub / github.com/antchfx/xmlquery / TestXPath

Function TestXPath

query_test.go:47–83  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

45var doc = loadXML(xmlDoc)
46
47func TestXPath(t *testing.T) {
48 if list := Find(doc, "//book"); len(list) != 3 {
49 t.Fatal("count(//book) != 3")
50 }
51 if node := FindOne(doc, "//book[@id='bk101']"); node == nil {
52 t.Fatal("//book[@id='bk101] is not found")
53 }
54 if node := FindOne(doc, "//book[price>=44.95]"); node == nil {
55 t.Fatal("//book/price>=44.95 is not found")
56 }
57 if list := Find(doc, "//book[genre='Fantasy']"); len(list) != 2 {
58 t.Fatal("//book[genre='Fantasy'] items count is not equal 2")
59 }
60 var c int
61 FindEach(doc, "//book", func(i int, n *Node) {
62 c++
63 })
64 l := len(Find(doc, "//book"))
65 if c != l {
66 t.Fatal("count(//book) != 3")
67 }
68 c = 0
69 FindEachWithBreak(doc, "//book", func(i int, n *Node) bool {
70 if c == l-1 {
71 return false
72 }
73 c++
74 return true
75 })
76 if c != l-1 {
77 t.Fatal("FindEachWithBreak failed to stop.")
78 }
79 node := FindOne(doc, "//book[1]")
80 if node.SelectAttr("id") != "bk101" {
81 t.Fatal("//book[1]/@id != bk101")
82 }
83}
84
85func TestXPathCdUp(t *testing.T) {
86 doc := loadXML(`<a><b attr="1"/></a>`)

Callers

nothing calls this directly

Calls 5

FindFunction · 0.85
FindOneFunction · 0.85
FindEachFunction · 0.85
FindEachWithBreakFunction · 0.85
SelectAttrMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…