Source

et-2009-provolone / elementtree / unittests / test_xpath.py

Full commit
# $Id: test_xpath.py 3340 2007-12-19 00:44:05Z fredrik $

from support import *
from elementtree import ElementPath

def xpath_tokenizer(p, ns=None):
    """
    Test the XPath tokenizer.

    >>> # tests from the xml specification
    >>> xpath_tokenizer("*")
    ['*']
    >>> xpath_tokenizer("text()")
    ['text', '()']
    >>> xpath_tokenizer("@name")
    ['@', 'name']
    >>> xpath_tokenizer("@*")
    ['@', '*']
    >>> xpath_tokenizer("para[1]")
    ['para', '[', '1', ']']
    >>> xpath_tokenizer("para[last()]")
    ['para', '[', 'last', '()', ']']
    >>> xpath_tokenizer("*/para")
    ['*', '/', 'para']
    >>> xpath_tokenizer("/doc/chapter[5]/section[2]")
    ['/', 'doc', '/', 'chapter', '[', '5', ']', '/', 'section', '[', '2', ']']
    >>> xpath_tokenizer("chapter//para")
    ['chapter', '//', 'para']
    >>> xpath_tokenizer("//para")
    ['//', 'para']
    >>> xpath_tokenizer("//olist/item")
    ['//', 'olist', '/', 'item']
    >>> xpath_tokenizer(".")
    ['.']
    >>> xpath_tokenizer(".//para")
    ['.', '//', 'para']
    >>> xpath_tokenizer("..")
    ['..']
    >>> xpath_tokenizer("../@lang")
    ['..', '/', '@', 'lang']
    >>> xpath_tokenizer("chapter[title]")
    ['chapter', '[', 'title', ']']
    >>> xpath_tokenizer("employee[@secretary and @assistant]")
    ['employee', '[', '@', 'secretary', '', 'and', '', '@', 'assistant', ']']

    prefixes
    >>> xpath_tokenizer("spam:egg", dict(spam="http://spam"))
    ['{http://spam}egg']
    >>> xpath_tokenizer("./spam:egg", dict(spam="http://spam"))
    ['.', '/', '{http://spam}egg']
    >>> xpath_tokenizer(".//spam:egg", dict(spam="http://spam"))
    ['.', '//', '{http://spam}egg']

    clark notation
    >>> xpath_tokenizer("{http://spam}egg")
    ['{http://spam}egg']
    >>> xpath_tokenizer("./spam.egg")
    ['.', '/', 'spam.egg']
    >>> xpath_tokenizer(".//{http://spam}egg")
    ['.', '//', '{http://spam}egg']
    """
    out = []
    for op, tag in ElementPath.xpath_tokenizer(p, ns):
        out.append(op or tag)
    return out

# --------------------------------------------------------------------
# jquery path tests

target = ET.parse("unittests/jquery.xml").getroot()

def t(description, path):
    return [
        elem.get("id") for elem in target.findall(path) if elem.get("id")
        ]

def jquery_xpath():
    """

    >>> t("All P Elements", ".//p")
    ['firstp', 'ap', 'sndp', 'en', 'sap', 'first']
    >>> t("Absolute Path", "./body")
    ['body']
    >>> t("Absolute Path w/ *", "./*")
    ['body']
    >>> t("Long Absolute Path", "./body/dl/div/div/p")
    ['sndp', 'en', 'sap']
    >>> t("Absolute and Relative Paths", ".//p")
    ['firstp', 'ap', 'sndp', 'en', 'sap', 'first']
    >>> t("All Children, Explicit", ".//code/*")
    ['anchor1', 'anchor2']
    >>> t("All Children, Implicit", ".//code/")
    ['anchor1', 'anchor2']
    >>> t("Attribute Exists", ".//a[@title]")
    ['google']
    >>> t("Attribute Equals", ".//a[@rel='bookmark']")
    ['simon1']
    >>> t("Parent Axis", ".//p/..")
    ['main', 'foo']
    >>> t("Sibling Axis", ".//p/../")
    ['firstp', 'ap', 'foo', 'first', 'firstUL', 'empty', 'form', 'floatTest', 'iframe', 'lengthtest', 'table', 'fx-queue', 'fx-tests', 'sndp', 'en', 'sap']
    >>> t("Sibling Axis", ".//p/../*")
    ['firstp', 'ap', 'foo', 'first', 'firstUL', 'empty', 'form', 'floatTest', 'iframe', 'lengthtest', 'table', 'fx-queue', 'fx-tests', 'sndp', 'en', 'sap']
    >>> t("Has Children", ".//p[a]")
    ['firstp', 'ap', 'en', 'sap']

    """