| 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 |
type test struct { |
| 10 |
path string |
| 11 |
result interface{} |
| 12 |
} |
| 13 |
|
| 14 |
type errorResult string |
| 15 |
|
| 16 |
var tests = []test{ |
| 17 |
// basic queries |
| 18 |
{"./bookstore/book/title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}}, |
| 19 |
{"./bookstore/book/author", []string{"Giada De Laurentiis", "J K. Rowling", "James McGovern", "Per Bothner", "Kurt Cagle", "James Linn", "Vaidyanathan Nagarajan", "Erik T. Ray"}}, |
| 20 |
{"./bookstore/book/year", []string{"2005", "2005", "2003", "2003"}}, |
| 21 |
{"./bookstore/book/p:price", []string{"30.00", "29.99", "39.95"}}, |
| 22 |
{"./bookstore/book/isbn", nil}, |
| 23 |
|
| 24 |
// descendant queries |
| 25 |
{"//title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}}, |
| 26 |
{"//book/title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}}, |
| 27 |
{".//title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}}, |
| 28 |
{".//bookstore//title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}}, |
| 29 |
{".//book/title", []string{"Everyday Italian", "Harry Potter", "XQuery Kick Start", "Learning XML"}}, |
| 30 |
{".//p:price/.", []string{"30.00", "29.99", "39.95"}}, |
| 31 |
{".//price", []string{"30.00", "29.99", "49.99", "39.95"}}, |
| 32 |
|
| 33 |
// positional queries |
| 34 |
{"./bookstore/book[1]/title", "Everyday Italian"}, |
| 35 |
{"./bookstore/book[4]/title", "Learning XML"}, |
| 36 |
{"./bookstore/book[5]/title", nil}, |
| 37 |
{"./bookstore/book[3]/author[0]", "James McGovern"}, |
| 38 |
{"./bookstore/book[3]/author[1]", "James McGovern"}, |
| 39 |
{"./bookstore/book[3]/author[3]/./.", "Kurt Cagle"}, |
| 40 |
{"./bookstore/book[3]/author[6]", nil}, |
| 41 |
{"./bookstore/book[-1]/title", "Learning XML"}, |
| 42 |
{"./bookstore/book[-4]/title", "Everyday Italian"}, |
| 43 |
{"./bookstore/book[-5]/title", nil}, |
| 44 |
|
| 45 |
// text function queries |
| 46 |
{"./bookstore/book[author='James McGovern']/title", "XQuery Kick Start"}, |
| 47 |
{"./bookstore/book[author='Per Bothner']/title", "XQuery Kick Start"}, |
| 48 |
{"./bookstore/book[author='Kurt Cagle']/title", "XQuery Kick Start"}, |
| 49 |
{"./bookstore/book[author='James Linn']/title", "XQuery Kick Start"}, |
| 50 |
{"./bookstore/book[author='Vaidyanathan Nagarajan']/title", "XQuery Kick Start"}, |
| 51 |
{"//book[p:price='29.99']/title", "Harry Potter"}, |
| 52 |
{"//book[price='29.99']/title", "Harry Potter"}, |
| 53 |
{"//book/price[text()='29.99']", "29.99"}, |
| 54 |
{"//book/author[text()='Kurt Cagle']", "Kurt Cagle"}, |
| 55 |
{"//book/editor[text()]", []string{"Clarkson Potter", "\n\t\t"}}, |
| 56 |
|
| 57 |
// namespace function queries |
| 58 |
{"//*[namespace-uri()]", []string{"30.00", "29.99", "39.95"}}, |
| 59 |
{"//*[namespace-uri()='urn:books-com:prices']", []string{"30.00", "29.99", "39.95"}}, |
| 60 |
{"//*[namespace-uri()='foo']", nil}, |
| 61 |
{"//*[namespace-prefix()]", []string{"30.00", "29.99", "39.95"}}, |
| 62 |
{"//*[namespace-prefix()='p']", []string{"30.00", "29.99", "39.95"}}, |
| 63 |
{"//*[name()='p:price']", []string{"30.00", "29.99", "39.95"}}, |
| 64 |
{"//*[local-name()='price']", []string{"30.00", "29.99", "49.99", "39.95"}}, |
| 65 |
{"//price[namespace-uri()='']", []string{"49.99"}}, |
| 66 |
{"//price[namespace-prefix()='']", []string{"49.99"}}, |
| 67 |
{"//price[name()='price']", []string{"49.99"}}, |
| 68 |
{"//price[local-name()='price']", []string{"30.00", "29.99", "49.99", "39.95"}}, |
| 69 |
|
| 70 |
// attribute queries |
| 71 |
{"./bookstore/book[@category='WEB']/title", []string{"XQuery Kick Start", "Learning XML"}}, |
| 72 |
{"./bookstore/book[@path='/books/xml']/title", []string{"Learning XML"}}, |
| 73 |
{"./bookstore/book[@category='COOKING']/title[@lang='en']", "Everyday Italian"}, |
| 74 |
{`./bookstore/book[@category="COOKING"]/title[@lang="en"]`, "Everyday Italian"}, |
| 75 |
{"./bookstore/book/title[@lang='en'][@sku='150']", "Harry Potter"}, |
| 76 |
{"./bookstore/book/title[@lang='fr']", nil}, |
| 77 |
{"//p:price[@p:tax='1.99']", []string{"29.99"}}, |
| 78 |
{"//p:price[@tax='1.99']", []string{"29.99"}}, |
| 79 |
{"//p:price[@p:tax]", []string{"29.99"}}, |
| 80 |
{"//p:price[@tax]", []string{"29.99"}}, |
| 81 |
|
| 82 |
// parent queries |
| 83 |
{"./bookstore/book[@category='COOKING']/title/../../book[4]/title", "Learning XML"}, |
| 84 |
|
| 85 |
// root queries |
| 86 |
{"/bookstore/book[1]/title", "Everyday Italian"}, |
| 87 |
{"/bookstore/book[4]/title", "Learning XML"}, |
| 88 |
{"/bookstore/book[5]/title", nil}, |
| 89 |
{"/bookstore/book[3]/author[0]", "James McGovern"}, |
| 90 |
{"/bookstore/book[3]/author[1]", "James McGovern"}, |
| 91 |
{"/bookstore/book[3]/author[3]/./.", "Kurt Cagle"}, |
| 92 |
{"/bookstore/book[3]/author[6]", nil}, |
| 93 |
{"/bookstore/book[-1]/title", "Learning XML"}, |
| 94 |
{"/bookstore/book[-4]/title", "Everyday Italian"}, |
| 95 |
{"/bookstore/book[-5]/title", nil}, |
| 96 |
|
| 97 |
// bad paths |
| 98 |
{"./bookstore/book[]", errorResult("etree: path contains an empty filter expression.")}, |
| 99 |
{"./bookstore/book[='WEB']", errorResult("etree: path contains a filter expression with no key.")}, |
| 100 |
{"./bookstore/book[@='WEB']", errorResult("etree: path contains a filter expression with no key.")}, |
| 101 |
{"./bookstore/book[@category='WEB'", errorResult("etree: path has invalid filter [brackets].")}, |
| 102 |
{"./bookstore/book[@category='WEB]", errorResult("etree: path has mismatched filter quotes.")}, |
| 103 |
{`./bookstore/book[@category='WEB"]`, errorResult("etree: path has mismatched filter quotes.")}, |
| 104 |
{`./bookstore/book[@category="WEB']`, errorResult("etree: path has mismatched filter quotes.")}, |
| 105 |
{"./bookstore/book[author]a", errorResult("etree: path has invalid filter [brackets].")}, |
| 106 |
{"/][", errorResult("etree: path has invalid filter [brackets].")}, |
| 107 |
} |
| 108 |
|
| 109 |
func TestPath(t *testing.T) { |
| 110 |
doc := NewDocument() |
| 111 |
err := doc.ReadFromString(bookstoreXML) |
| 112 |
if err != nil { |
| 113 |
t.Error(err) |
| 114 |
} |
| 115 |
|
| 116 |
for _, test := range tests { |
| 117 |
path, err := CompilePath(test.path) |
| 118 |
if err != nil { |
| 119 |
if r, ok := test.result.(errorResult); !ok || err.Error() != string(r) { |
| 120 |
fail(t, test) |
| 121 |
} |
| 122 |
continue |
| 123 |
} |
| 124 |
|
| 125 |
// Test both FindElementsPath and FindElementPath |
| 126 |
element := doc.FindElementPath(path) |
| 127 |
elements := doc.FindElementsPath(path) |
| 128 |
|
| 129 |
switch s := test.result.(type) { |
| 130 |
case errorResult: |
| 131 |
fail(t, test) |
| 132 |
case nil: |
| 133 |
if element != nil || len(elements) != 0 { |
| 134 |
fail(t, test) |
| 135 |
} |
| 136 |
case string: |
| 137 |
if element == nil || element.Text() != s || |
| 138 |
len(elements) != 1 || elements[0].Text() != s { |
| 139 |
fail(t, test) |
| 140 |
} |
| 141 |
case []string: |
| 142 |
if element == nil || element.Text() != s[0] || len(elements) != len(s) { |
| 143 |
fail(t, test) |
| 144 |
continue |
| 145 |
} |
| 146 |
for i := 0; i < len(elements); i++ { |
| 147 |
if elements[i].Text() != s[i] { |
| 148 |
fail(t, test) |
| 149 |
break |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func fail(t *testing.T, test test) { |
| 158 |
t.Helper() |
| 159 |
t.Errorf("etree: failed test '%s'\n", test.path) |
| 160 |
} |
| 161 |
|
| 162 |
func TestAbsolutePath(t *testing.T) { |
| 163 |
doc := NewDocument() |
| 164 |
err := doc.ReadFromString(bookstoreXML) |
| 165 |
if err != nil { |
| 166 |
t.Error(err) |
| 167 |
} |
| 168 |
|
| 169 |
elements := doc.FindElements("//book/author") |
| 170 |
for _, e := range elements { |
| 171 |
title := e.FindElement("/bookstore/book[1]/title") |
| 172 |
if title == nil || title.Text() != "Everyday Italian" { |
| 173 |
t.Errorf("etree: absolute path test failed") |
| 174 |
} |
| 175 |
|
| 176 |
title = e.FindElement("//book[p:price='29.99']/title") |
| 177 |
if title == nil || title.Text() != "Harry Potter" { |
| 178 |
t.Errorf("etree: absolute path test failed") |
| 179 |
} |
| 180 |
} |
| 181 |
} |