Ejemplo n.º 1
0
def test_xpath():
    document = etree.fromstring('<root><a/><b/></root>')
    transformation = Transformation(Rule('//a', lib.set_text('x')))
    result = transformation(document)
    assert result.text is None
    assert result.find('a').text == 'x'
    assert result.find('b').text is None
Ejemplo n.º 2
0
def test_attributes():
    document = etree.fromstring('<root><a b="x"/><a b="y"/></root>')
    transformation = Transformation(Rule({'b': 'x'}, lib.set_text('x')))
    result = transformation(document)
    assert result.text is None
    assert result[0].text == 'x'
    assert result[1].text is None
Ejemplo n.º 3
0
def test_attributes(constraint):
    document = Document('<root><a b="x"/><a b="y"/></root>')
    transformation = Transformation(Rule(constraint, lib.set_text("x")))
    result = transformation(document).root
    assert not result._data_node._exists
    assert result[0].full_text == "x", str(result)
    assert not len(result[1])
Ejemplo n.º 4
0
def test_OneOf():
    document = etree.fromstring('<root x="x"><a x="x"/><b x="x"/></root>')
    transformation = Transformation(
        Rule(OneOf('a', 'b', {'x': 'x'}), lib.set_text('x')))
    result = transformation(document)
    assert result.text == 'x'
    assert all(x.text is None for x in result)
Ejemplo n.º 5
0
def test_xpath(xpath):
    document = Document("<root><a/><b/></root>")
    transformation = Transformation(Rule(xpath, lib.set_text("x")))
    result = transformation(document).root
    assert not result._data_node._exists
    assert first(result.css_select("a")).full_text == "x"
    assert first(result.css_select("b")).full_text == ""
Ejemplo n.º 6
0
def test_OneOf():
    document = Document('<root x="x"><a x="x"/><b x="x"/></root>')
    transformation = Transformation(
        Rule(OneOf("a", "b", {"x": "x"}), lib.set_text("x")))
    result = transformation(document).root
    assert result[0] == "x"

    assert all(x.full_text == "" for x in result.child_nodes(is_tag_node))
Ejemplo n.º 7
0
def test_any_element():
    document = etree.fromstring('<root><a/><b/></root>')
    transformation = Transformation(Rule('*', lib.set_text('x')))
    for element in transformation(document):
        assert element.text == 'x'
Ejemplo n.º 8
0
def test_Any():
    document = etree.fromstring('<root><a/><b/></root>')
    transformation = Transformation(Rule(Any('a', 'b'), lib.set_text('x')))
    result = transformation(document)
    assert result.text is None
    assert all(x.text == 'x' for x in result)
Ejemplo n.º 9
0
def test_set_text():
    node = new_tag_node("pre")
    transformation = Transformation(lib.put_variable("x", "Hello world."),
                                    Rule("/", lib.set_text(Ref("x"))))
    assert str(transformation(node)) == "<pre>Hello world.</pre>"
Ejemplo n.º 10
0
def test_any_element():
    document = Document("<root><a/><b/></root>")
    transformation = Transformation(Rule("*", lib.set_text("x")))
    for node in transformation(document).root.child_nodes():
        assert node[0] == "x"
Ejemplo n.º 11
0
def test_Any():
    document = Document("<root><a/><b/></root>")
    transformation = Transformation(Rule(Any("a", "b"), lib.set_text("x")))
    result = transformation(document).root
    assert not result._data_node._exists
    assert all(x.content == "x" for x in result.child_nodes(is_text_node))