示例#1
0
def test_namespaces():
    element = Element("test", {
        "xmlns": "default ns",
        "xmlns:junk-ns0": "junk-ns value 0",
        "xmlns:junk-ns1": "junk-ns value 1",
        "py:pyattr": "py value",
        "attr": "a new attr",
    })

    ns = namespaces(element)
    expected = {
        "": "default ns",
        "junk-ns0": "junk-ns value 0",
        "junk-ns1": "junk-ns value 1",
    }
    assert ns == expected
    assert element.get("xmlns") == "default ns"
    assert element.get("xmlns:junk-ns0") == "junk-ns value 0"
    assert element.get("xmlns:junk-ns1") == "junk-ns value 1"
    assert element.get("py:pyattr") == "py value"
    assert element.get("attr") == "a new attr"

    ns = namespaces(element, remove=True)
    assert element.get("xmlns") is None
    assert element.get("xmlns:junk-ns0") is None
    assert element.get("xmlns:junk-ns1") is None
    assert element.get("py:pyattr") == "py value"
    assert element.get("attr") == "a new attr"
示例#2
0
def test_element_attributes():
    element = Element("test", dict(a=0, b=1), c=2)
    assert element.get("d") is None
    assert element.get("d", "") == ""
    assert element.get("c") == 2
    assert element.get("c", "") == 2

    element.set("c", "c")
    assert element.get("c") == "c"

    keys = element.keys()
    assert len(keys) == 3
    for k in ["a", "b", "c"]:
        assert k in keys

    items = element.items()
    assert len(items) == 3
    for i in [("a", 0), ("b", 1), ("c", "c")]:
        assert i in items