| 1 |
package xpath |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
) |
| 6 |
|
| 7 |
func TestLogicals(t *testing.T) { |
| 8 |
test_xpath_elements(t, book_example, `//book[1 + 1]`, 9) |
| 9 |
test_xpath_elements(t, book_example, `//book[1 * 2]`, 9) |
| 10 |
test_xpath_elements(t, book_example, `//book[5 div 2]`, 9) // equal to `//book[2]` |
| 11 |
test_xpath_elements(t, book_example, `//book[3 div 2]`, 3) |
| 12 |
test_xpath_elements(t, book_example, `//book[3 - 2]`, 3) |
| 13 |
test_xpath_elements(t, book_example, `//book[price > 35]`, 15, 25) |
| 14 |
test_xpath_elements(t, book_example, `//book[price >= 30]`, 3, 15, 25) |
| 15 |
test_xpath_elements(t, book_example, `//book[price < 30]`, 9) |
| 16 |
test_xpath_elements(t, book_example, `//book[price <= 30]`, 3, 9) |
| 17 |
test_xpath_elements(t, book_example, `//book[count(author) > 1]`, 15) |
| 18 |
test_xpath_elements(t, book_example, `//book[position() mod 2 = 0]`, 9, 25) |
| 19 |
} |
| 20 |
|
| 21 |
func TestPositions(t *testing.T) { |
| 22 |
test_xpath_elements(t, employee_example, `/empinfo/employee[2]`, 8) |
| 23 |
test_xpath_elements(t, employee_example, `//employee[position() = 2]`, 8) |
| 24 |
test_xpath_elements(t, employee_example, `/empinfo/employee[2]/name`, 9) |
| 25 |
test_xpath_elements(t, employee_example, `//employee[position() > 1]`, 8, 13) |
| 26 |
test_xpath_elements(t, employee_example, `//employee[position() <= 2]`, 3, 8) |
| 27 |
test_xpath_elements(t, employee_example, `//employee[last()]`, 13) |
| 28 |
test_xpath_elements(t, employee_example, `//employee[position() = last()]`, 13) |
| 29 |
test_xpath_elements(t, book_example, `//book[@category = "web"][2]`, 25) |
| 30 |
test_xpath_elements(t, book_example, `//book[@category = "web"][last()]`, 25) |
| 31 |
test_xpath_elements(t, book_example, `(//book[@category = "web"])[2]`, 25) |
| 32 |
test_xpath_elements(t, book_example, `(//book[@category = "web"])[last()]`, 25) |
| 33 |
test_xpath_elements(t, employee_example, `(//employee)[last()]`, 13) |
| 34 |
test_xpath_elements(t, book_example, `(//author)[last()]`, 27) |
| 35 |
} |
| 36 |
|
| 37 |
func TestPredicates(t *testing.T) { |
| 38 |
test_xpath_elements(t, employee_example, `//employee[name]`, 3, 8, 13) |
| 39 |
test_xpath_elements(t, employee_example, `/empinfo/employee[@id]`, 3, 8, 13) |
| 40 |
test_xpath_elements(t, book_example, `//book[@category = "web"]`, 15, 25) |
| 41 |
test_xpath_elements(t, book_example, `//book[author = "J K. Rowling"]`, 9) |
| 42 |
test_xpath_elements(t, book_example, `//book[./author/text() = "J K. Rowling"]`, 9) |
| 43 |
test_xpath_elements(t, book_example, `//book[year = 2005]`, 3, 9) |
| 44 |
test_xpath_elements(t, book_example, `//year[text() = 2005]`, 6, 12) |
| 45 |
test_xpath_elements(t, employee_example, `/empinfo/employee[1][@id=1]`, 3) |
| 46 |
test_xpath_elements(t, employee_example, `/empinfo/employee[@id][2]`, 8) |
| 47 |
} |
| 48 |
|
| 49 |
func TestOperators(t *testing.T) { |
| 50 |
test_xpath_elements(t, employee_example, `//designation[@discipline and @experience]`, 5, 10) |
| 51 |
test_xpath_elements(t, employee_example, `//designation[@discipline or @experience]`, 5, 10, 15) |
| 52 |
test_xpath_elements(t, employee_example, `//designation[@discipline | @experience]`, 5, 10, 15) |
| 53 |
test_xpath_elements(t, employee_example, `/empinfo/employee[@id != "2" ]`, 3, 13) |
| 54 |
test_xpath_elements(t, employee_example, `/empinfo/employee[@id and @id = "2"]`, 8) |
| 55 |
test_xpath_elements(t, employee_example, `/empinfo/employee[@id = "1" or @id = "2"]`, 3, 8) |
| 56 |
} |
| 57 |
|
| 58 |
func TestNestedPredicates(t *testing.T) { |
| 59 |
test_xpath_elements(t, employee_example, `//employee[./name[@from]]`, 8) |
| 60 |
test_xpath_elements(t, employee_example, `//employee[.//name[@from = "CA"]]`, 8) |
| 61 |
} |