Debian Codegraph @

path_test.go

raw symbols SCIP
1 // Copyright 2015-2019 Brett Vickers.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package etreescip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/
6
7 import "testingscip-go gomod github.com/golang/go/src go1.23.0 testing/"
8
9 var testXMLscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/testXML. = `
10 <?xml version="1.0" encoding="UTF-8"?>
11 <bookstore xmlns:p="urn:books-com:prices">
12
13 <!Directive>
14
15 <book category="COOKING">
16 <title lang="en">Everyday Italian</title>
17 <author>Giada De Laurentiis</author>
18 <year>2005</year>
19 <p:price>30.00</p:price>
20 <editor>Clarkson Potter</editor>
21 </book>
22
23 <book category="CHILDREN">
24 <title lang="en" sku="150">Harry Potter</title>
25 <author>J K. Rowling</author>
26 <year>2005</year>
27 <p:price p:tax="1.99">29.99</p:price>
28 <editor></editor>
29 <editor/>
30 </book>
31
32 <book category="WEB">
33 <title lang="en">XQuery Kick Start</title>
34 <author>James McGovern</author>
35 <author>Per Bothner</author>
36 <author>Kurt Cagle</author>
37 <author>James Linn</author>
38 <author>Vaidyanathan Nagarajan</author>
39 <year>2003</year>
40 <price>49.99</price>
41 <editor>
42 </editor>
43 </book>
44
45 <!-- Final book -->
46 <book category="WEB" path="/books/xml">
47 <title lang="en">Learning XML</title>
48 <author>Erik T. Ray</author>
49 <year>2003</year>
50 <p:price>39.95</p:price>
51 </book>
52
53 </bookstore>
54 `
55
56 type testscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/test# struct {
57 pathscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/test#path. string
58 resultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/test#result. interface{}
59 }
60
61 type errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult# string
62
63 var testsscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/tests. = []testscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/test#{
64 // basic queries
65 {"./bookstore/book/title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}},
66 {"./bookstore/book/author", []string{"Giada De Laurentiis", "J K. Rowling", "James McGovern", "Per Bothner", "Kurt Cagle", "James Linn", "Vaidyanathan Nagarajan", "Erik T. Ray"}},
67 {"./bookstore/book/year", []string{"2005", "2005", "2003", "2003"}},
68 {"./bookstore/book/p:price", []string{"30.00", "29.99", "39.95"}},
69 {"./bookstore/book/isbn", nil},
70
71 // descendant queries
72 {"//title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}},
73 {"//book/title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}},
74 {".//title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}},
75 {".//bookstore//title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}},
76 {".//book/title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}},
77 {".//p:price/.", []string{"30.00", "29.99", "39.95"}},
78 {".//price", []string{"30.00", "29.99", "49.99", "39.95"}},
79
80 // positional queries
81 {"./bookstore/book[1]/title", "Everyday Italian"},
82 {"./bookstore/book[4]/title", "Learning XML"},
83 {"./bookstore/book[5]/title", nil},
84 {"./bookstore/book[3]/author[0]", "James McGovern"},
85 {"./bookstore/book[3]/author[1]", "James McGovern"},
86 {"./bookstore/book[3]/author[3]/./.", "Kurt Cagle"},
87 {"./bookstore/book[3]/author[6]", nil},
88 {"./bookstore/book[-1]/title", "Learning XML"},
89 {"./bookstore/book[-4]/title", "Everyday Italian"},
90 {"./bookstore/book[-5]/title", nil},
91
92 // text function queries
93 {"./bookstore/book[author='James McGovern']/title", "XQuery Kick Start"},
94 {"./bookstore/book[author='Per Bothner']/title", "XQuery Kick Start"},
95 {"./bookstore/book[author='Kurt Cagle']/title", "XQuery Kick Start"},
96 {"./bookstore/book[author='James Linn']/title", "XQuery Kick Start"},
97 {"./bookstore/book[author='Vaidyanathan Nagarajan']/title", "XQuery Kick Start"},
98 {"//book[p:price='29.99']/title", "Harry Potter"},
99 {"//book[price='29.99']/title", "Harry Potter"},
100 {"//book/price[text()='29.99']", "29.99"},
101 {"//book/author[text()='Kurt Cagle']", "Kurt Cagle"},
102 {"//book/editor[text()]", []string{"Clarkson Potter", "\n\t\t"}},
103
104 // namespace function queries
105 {"//*[namespace-uri()]", []string{"30.00", "29.99", "39.95"}},
106 {"//*[namespace-uri()='urn:books-com:prices']", []string{"30.00", "29.99", "39.95"}},
107 {"//*[namespace-uri()='foo']", nil},
108 {"//*[namespace-prefix()]", []string{"30.00", "29.99", "39.95"}},
109 {"//*[namespace-prefix()='p']", []string{"30.00", "29.99", "39.95"}},
110 {"//*[name()='p:price']", []string{"30.00", "29.99", "39.95"}},
111 {"//*[local-name()='price']", []string{"30.00", "29.99", "49.99", "39.95"}},
112 {"//price[namespace-uri()='']", []string{"49.99"}},
113 {"//price[namespace-prefix()='']", []string{"49.99"}},
114 {"//price[name()='price']", []string{"49.99"}},
115 {"//price[local-name()='price']", []string{"30.00", "29.99", "49.99", "39.95"}},
116
117 // attribute queries
118 {"./bookstore/book[@category='WEB']/title", []string{"XQuery Kick Start", "Learning XML"}},
119 {"./bookstore/book[@path='/books/xml']/title", []string{"Learning XML"}},
120 {"./bookstore/book[@category='COOKING']/title[@lang='en']", "Everyday Italian"},
121 {`./bookstore/book[@category="COOKING"]/title[@lang="en"]`, "Everyday Italian"},
122 {"./bookstore/book/title[@lang='en'][@sku='150']", "Harry Potter"},
123 {"./bookstore/book/title[@lang='fr']", nil},
124 {"//p:price[@p:tax='1.99']", []string{"29.99"}},
125 {"//p:price[@tax='1.99']", []string{"29.99"}},
126 {"//p:price[@p:tax]", []string{"29.99"}},
127 {"//p:price[@tax]", []string{"29.99"}},
128
129 // parent queries
130 {"./bookstore/book[@category='COOKING']/title/../../book[4]/title", "Learning XML"},
131
132 // root queries
133 {"/bookstore/book[1]/title", "Everyday Italian"},
134 {"/bookstore/book[4]/title", "Learning XML"},
135 {"/bookstore/book[5]/title", nil},
136 {"/bookstore/book[3]/author[0]", "James McGovern"},
137 {"/bookstore/book[3]/author[1]", "James McGovern"},
138 {"/bookstore/book[3]/author[3]/./.", "Kurt Cagle"},
139 {"/bookstore/book[3]/author[6]", nil},
140 {"/bookstore/book[-1]/title", "Learning XML"},
141 {"/bookstore/book[-4]/title", "Everyday Italian"},
142 {"/bookstore/book[-5]/title", nil},
143
144 // bad paths
145 {"./bookstore/book[]", errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#("etree: path contains an empty filter expression.")},
146 {"./bookstore/book[='WEB']", errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#("etree: path contains a filter expression with no key.")},
147 {"./bookstore/book[@='WEB']", errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#("etree: path contains a filter expression with no key.")},
148 {"./bookstore/book[@category='WEB'", errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#("etree: path has invalid filter [brackets].")},
149 {"./bookstore/book[@category='WEB]", errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#("etree: path has mismatched filter quotes.")},
150 {`./bookstore/book[@category='WEB"]`, errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#("etree: path has mismatched filter quotes.")},
151 {`./bookstore/book[@category="WEB']`, errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#("etree: path has mismatched filter quotes.")},
152 {"./bookstore/book[author]a", errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#("etree: path has invalid filter [brackets].")},
153 {"/][", errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#("etree: path has invalid filter [brackets].")},
154 }
155
156 func TestPathscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/TestPath().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) {
157 doc := NewDocumentscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/NewDocument().()
158 err := doc.ReadFromStringscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Document#ReadFromString().(testXMLscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/testXML.)
159 if err != nil {
160 t.Errorscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Error().(err)
161 }
162
163 for _, test := range testsscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/tests. {
164 path, err := CompilePathscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/CompilePath().(test.pathscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/test#path.)
165 if err != nil {
166 if r, ok := test.resultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/test#result..(errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#); !ok || err.Error() != string(r) {
167 failscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/fail().(t, test)
168 }
169 continue
170 }
171
172 // Test both FindElementsPath and FindElementPath
173 element := doc.FindElementPathscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#FindElementPath().(path)
174 elements := doc.FindElementsPathscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#FindElementsPath().(path)
175
176 switch s := test.resultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/test#result..(type) {
177 case errorResultscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/errorResult#:
178 failscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/fail().(t, test)
179 case nil:
180 if element != nil || len(elements) != 0 {
181 failscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/fail().(t, test)
182 }
183 case string:
184 if element == nil || element.Textscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#Text().() != s ||
185 len(elements) != 1 || elements[0].Textscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#Text().() != s {
186 failscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/fail().(t, test)
187 }
188 case []string:
189 if element == nil || element.Textscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#Text().() != s[0] || len(elements) != len(s) {
190 failscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/fail().(t, test)
191 continue
192 }
193 for i := 0; i < len(elements); i++ {
194 if elements[i].Textscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#Text().() != s[i] {
195 failscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/fail().(t, test)
196 break
197 }
198 }
199 }
200
201 }
202 }
203
204 func failscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/fail().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#, test testscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/test#) {
205 t.Helperscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Helper().()
206 t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("etree: failed test '%s'\n", test.pathscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/test#path.)
207 }
208
209 func TestAbsolutePathscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/TestAbsolutePath().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) {
210 doc := NewDocumentscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/NewDocument().()
211 err := doc.ReadFromStringscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Document#ReadFromString().(testXMLscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/testXML.)
212 if err != nil {
213 t.Errorscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Error().(err)
214 }
215
216 elements := doc.FindElementsscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#FindElements().("//book/author")
217 for _, e := range elements {
218 title := e.FindElementscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#FindElement().("/bookstore/book[1]/title")
219 if title == nil || title.Textscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#Text().() != "Everyday Italian" {
220 t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("etree: absolute path test failed")
221 }
222
223 title = e.FindElementscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#FindElement().("//book[p:price='29.99']/title")
224 if title == nil || title.Textscip-go gomod github.com/beevik/etree . `github.com/beevik/etree`/Element#Text().() != "Harry Potter" {
225 t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("etree: absolute path test failed")
226 }
227 }
228 }