| 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 etree |
| 6 | |
| 7 | import "testing" |
| 8 | |
| 9 | var 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 test struct { |
| 57 | path string |
| 58 | result interface{} |
| 59 | } |
| 60 | |
| 61 | type errorResult string |
| 62 | |
| 63 | var tests = []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[]", errorResult("etree: path contains an empty filter expression.")}, |
| 146 | {"./bookstore/book[='WEB']", errorResult("etree: path contains a filter expression with no key.")}, |
| 147 | {"./bookstore/book[@='WEB']", errorResult("etree: path contains a filter expression with no key.")}, |
| 148 | {"./bookstore/book[@category='WEB'", errorResult("etree: path has invalid filter [brackets].")}, |
| 149 | {"./bookstore/book[@category='WEB]", errorResult("etree: path has mismatched filter quotes.")}, |
| 150 | {`./bookstore/book[@category='WEB"]`, errorResult("etree: path has mismatched filter quotes.")}, |
| 151 | {`./bookstore/book[@category="WEB']`, errorResult("etree: path has mismatched filter quotes.")}, |
| 152 | {"./bookstore/book[author]a", errorResult("etree: path has invalid filter [brackets].")}, |
| 153 | {"/][", errorResult("etree: path has invalid filter [brackets].")}, |
| 154 | } |
| 155 | |
| 156 | func TestPath(t *testing.T) { |
| 157 | doc := NewDocument() |
| 158 | err := doc.ReadFromString(testXML) |
| 159 | if err != nil { |
| 160 | t.Error(err) |
| 161 | } |
| 162 | |
| 163 | for _, test := range tests { |
| 164 | path, err := CompilePath(test.path) |
| 165 | if err != nil { |
| 166 | if r, ok := test.result.(errorResult); !ok || err.Error() != string(r) { |
| 167 | fail(t, test) |
| 168 | } |
| 169 | continue |
| 170 | } |
| 171 | |
| 172 | // Test both FindElementsPath and FindElementPath |
| 173 | element := doc.FindElementPath(path) |
| 174 | elements := doc.FindElementsPath(path) |
| 175 | |
| 176 | switch s := test.result.(type) { |
| 177 | case errorResult: |
| 178 | fail(t, test) |
| 179 | case nil: |
| 180 | if element != nil || len(elements) != 0 { |
| 181 | fail(t, test) |
| 182 | } |
| 183 | case string: |
| 184 | if element == nil || element.Text() != s || |
| 185 | len(elements) != 1 || elements[0].Text() != s { |
| 186 | fail(t, test) |
| 187 | } |
| 188 | case []string: |
| 189 | if element == nil || element.Text() != s[0] || len(elements) != len(s) { |
| 190 | fail(t, test) |
| 191 | continue |
| 192 | } |
| 193 | for i := 0; i < len(elements); i++ { |
| 194 | if elements[i].Text() != s[i] { |
| 195 | fail(t, test) |
| 196 | break |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | func fail(t *testing.T, test test) { |
| 205 | t.Helper() |
| 206 | t.Errorf("etree: failed test '%s'\n", test.path) |
| 207 | } |
| 208 | |
| 209 | func TestAbsolutePath(t *testing.T) { |
| 210 | doc := NewDocument() |
| 211 | err := doc.ReadFromString(testXML) |
| 212 | if err != nil { |
| 213 | t.Error(err) |
| 214 | } |
| 215 | |
| 216 | elements := doc.FindElements("//book/author") |
| 217 | for _, e := range elements { |
| 218 | title := e.FindElement("/bookstore/book[1]/title") |
| 219 | if title == nil || title.Text() != "Everyday Italian" { |
| 220 | t.Errorf("etree: absolute path test failed") |
| 221 | } |
| 222 | |
| 223 | title = e.FindElement("//book[p:price='29.99']/title") |
| 224 | if title == nil || title.Text() != "Harry Potter" { |
| 225 | t.Errorf("etree: absolute path test failed") |
| 226 | } |
| 227 | } |
| 228 | } |