Skip to content

Commit

Permalink
update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Mar 29, 2024
1 parent 5a15460 commit d0cd197
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
11 changes: 1 addition & 10 deletions xpath_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ import (
"testing"
)

/*
The below list are not supported yet
*/
// ================================
// */employee
// (4,2)
// ===============================

func Test_descendant_issue(t *testing.T) {
// Issue #93 https://github.com/antchfx/xpath/issues/93
/*
Expand Down Expand Up @@ -48,8 +40,7 @@ func TestRelativePaths(t *testing.T) {
test_xpath_tags(t, book_example, `//book/..`, "bookstore")
test_xpath_elements(t, book_example, `//book[@category="cooking"]/..`, 2)
test_xpath_elements(t, book_example, `//book/year[text() = 2005]/../..`, 2) // bookstore
// Warning. duplicate elements.
//test_xpath_elements(t, book_example, `//book/year/../following-sibling::*`, 9, 15, 25)
test_xpath_elements(t, book_example, `//book/year/../following-sibling::*`, 9, 15, 25)
test_xpath_count(t, book_example, `//bookstore/book/*`, 20)
test_xpath_tags(t, html_example, "//title/../..", "html")
test_xpath_elements(t, html_example, "//ul/../p", 19)
Expand Down
7 changes: 5 additions & 2 deletions xpath_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func Test_func_boolean(t *testing.T) {
test_xpath_eval(t, empty_example, `true()`, true)
test_xpath_eval(t, empty_example, `false()`, false)
test_xpath_eval(t, empty_example, `boolean(0)`, false)
test_xpath_eval(t, empty_example, `boolean(null)`, false)
test_xpath_eval(t, empty_example, `boolean(1)`, true)
test_xpath_eval(t, empty_example, `boolean(2)`, true)
test_xpath_eval(t, empty_example, `boolean(true)`, false)
Expand Down Expand Up @@ -80,7 +81,8 @@ func Test_func_last(t *testing.T) {
}

func Test_func_local_name(t *testing.T) {
// TODO
test_xpath_eval(t, book_example, `local-name(bookstore)`, "bookstore")
test_xpath_eval(t, mybook_example, `local-name(//mybook:book)`, "book")
}

func Test_func_starts_with(t *testing.T) {
Expand Down Expand Up @@ -210,7 +212,8 @@ func Test_func_round(t *testing.T) {
}

func Test_func_namespace_uri(t *testing.T) {
// TODO
//test_xpath_eval(t, mybook_example, `namespace-uri(//mybook:book)`, "http://www.contoso.com/books")
//test_xpath_elements(t, mybook_example, `//*[namespace-uri()='http://www.contoso.com/books']`, 3, 9)
}

func Test_func_normalize_space(t *testing.T) {
Expand Down
72 changes: 72 additions & 0 deletions xpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
book_example = createBookExample()
html_example = createHtmlExample()
empty_example = createNode("", RootNode)
mybook_example = createMyBookExample()
)

func test_xpath_elements(t *testing.T, root *TNode, expr string, expected ...int) {
Expand Down Expand Up @@ -128,6 +129,14 @@ func TestCompile(t *testing.T) {
assertNil(t, err)
}

func TestInvalidXPath(t *testing.T) {
var err error
_, err = Compile("()")
assertErr(t, err)
_, err = Compile("(1,2,3)")
assertErr(t, err)
}

func TestCompileWithNS(t *testing.T) {
_, err := CompileWithNS("/foo", nil)
assertNil(t, err)
Expand Down Expand Up @@ -814,3 +823,66 @@ func createHtmlExample() *TNode {
lines++
return doc
}

func createMyBookExample() *TNode {
/*
<?xml version="1.0" encoding="utf-8"?>
<books xmlns:mybook="http://www.contoso.com/books">
<mybook:book id="bk101">
<title>XML Developer's Guide</title>
<author>Gambardella, Matthew</author>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</mybook:book>
<mybook:book id="bk102">
<title>Midnight Rain</title>
<author>Ralls, Kim</author>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
</mybook:book>
</books>
*/
lines := 0
doc := createNode("", RootNode)
lines++
books := doc.createChildNode("books", ElementNode)
books.addAttribute("xmlns:mybook", "http://www.contoso.com/books")
books.lines = lines
lines++
data := []struct {
id string
title string
author string
price float64
publish string
}{
{id: "bk101", title: "XML Developer's Guide", author: "Gambardella, Matthew", price: 44.95, publish: "2000-10-01"},
{id: "bk102", title: "Midnight Rain", author: "Ralls, Kim", price: 5.95, publish: "2000-12-16"},
}
for i := 0; i < len(data); i++ {
v := data[i]
book := books.createChildNode("mybook:book", ElementNode)
book.addAttribute("id", v.id)
book.lines = lines
lines++
title := book.createChildNode("title", ElementNode)
title.createChildNode(v.title, TextNode)
title.lines = lines
lines++
author := book.createChildNode("author", ElementNode)
author.createChildNode(v.author, TextNode)
author.lines = lines
lines++
price := book.createChildNode("price", ElementNode)
price.createChildNode(fmt.Sprintf("%.2f", v.price), TextNode)
price.lines = lines
lines++
publish_date := book.createChildNode("publish_date", ElementNode)
publish_date.createChildNode(v.publish, TextNode)
publish_date.lines = lines
lines++
// skip the last of book element
lines++
}
return doc
}

0 comments on commit d0cd197

Please sign in to comment.