From d0cd1973cceb5118d950738e5c94638114fb1304 Mon Sep 17 00:00:00 2001 From: zhengchun Date: Fri, 29 Mar 2024 23:56:29 +0800 Subject: [PATCH] update test cases --- xpath_expression_test.go | 11 +----- xpath_function_test.go | 7 ++-- xpath_test.go | 72 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/xpath_expression_test.go b/xpath_expression_test.go index 6cf1a4d..cfd82f4 100644 --- a/xpath_expression_test.go +++ b/xpath_expression_test.go @@ -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 /* @@ -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) diff --git a/xpath_function_test.go b/xpath_function_test.go index 6df870a..9feff05 100644 --- a/xpath_function_test.go +++ b/xpath_function_test.go @@ -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) @@ -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) { @@ -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) { diff --git a/xpath_test.go b/xpath_test.go index 138a639..a017a8f 100644 --- a/xpath_test.go +++ b/xpath_test.go @@ -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) { @@ -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) @@ -814,3 +823,66 @@ func createHtmlExample() *TNode { lines++ return doc } + +func createMyBookExample() *TNode { + /* + + + + XML Developer's Guide + Gambardella, Matthew + 44.95 + 2000-10-01 + + + Midnight Rain + Ralls, Kim + 5.95 + 2000-12-16 + + + */ + 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 +}