예제 #1
0
def test_specialize():
    assert sel.parse("co >> co[$n] >> nut").specialize(
        {"n": sel.Element(name=None, value=sel.VSymbol("x"))}
    ) == sel.parse("co >> co[x as n] >> nut")

    assert sel.parse("co >> co >> $nut").specialize(
        {"nut": sel.Element(name=None, category=sel.VSymbol("Fruit"))}
    ) == sel.parse("co >> co >> $nut:Fruit")

    assert sel.parse("co >> co >> $nut").specialize(
        {"nut": sel.Element(name="coconut", category=sel.VSymbol("Fruit"))}
    ) == sel.parse("co >> co >> (coconut as nut):Fruit")
예제 #2
0
def test_select():

    assert sel.select("apple > banana:tag.Sublime") == sel.Call(
        element=sel.Element(name=apple, capture="/0"),
        captures=(
            sel.Element(
                name="banana",
                capture="banana",
                category=tag.Sublime,
                tags=frozenset({1}),
            ),
        ),
    )

    assert sel.select("apple") == sel.select(">> *(!apple)")
    assert sel.select("pie:tag.Fruit") == sel.select(">> *(!pie:tag.Fruit)")
예제 #3
0
def test_parser():
    assert sel.parse("apple") == sel.Element(name="apple",
                                             capture="apple",
                                             tags=frozenset({1}))

    assert sel.parse("apple > banana") == sel.Call(
        sel.Element("apple"),
        captures=(sel.Element("banana", capture="banana",
                              tags=frozenset({1})), ),
        immediate=False,
    )

    assert sel.parse("apple >> banana") == sel.Call(
        element=sel.Element("apple"),
        captures=(),
        children=(sel.Call(
            element=sel.Element(None),
            captures=(sel.Element("banana",
                                  capture="banana",
                                  tags=frozenset({1})), ),
            immediate=False,
            collapse=True,
        ), ),
        immediate=False,
    )

    assert sel.parse("apple > banana > cherry") == sel.Call(
        element=sel.Element("apple"),
        children=(sel.Call(
            element=sel.Element("banana"),
            captures=(sel.Element("cherry",
                                  capture="cherry",
                                  tags=frozenset({1})), ),
            immediate=True,
        ), ),
        immediate=False,
    )

    assert sel.parse("*:Fruit") == sel.Element(
        name=None,
        category="Fruit",
        capture=None,
    )

    assert sel.parse("apple > :Fruit") == sel.Call(
        element=sel.Element("apple"),
        captures=(sel.Element(name=None,
                              category="Fruit",
                              capture=None,
                              tags=frozenset({1})), ),
        immediate=False,
    )

    assert sel.parse("apple{a}") == sel.Call(
        element=sel.Element("apple"),
        captures=(sel.Element(name="a", capture="a"), ),
    )

    assert sel.parse("apple{!a}") == sel.Call(
        element=sel.Element("apple"),
        captures=(sel.Element(name="a", capture="a", tags=frozenset({1})), ),
    )

    assert sel.parse("apple{a, b, c, d as e}") == sel.Call(
        element=sel.Element("apple"),
        captures=(
            sel.Element(name="a", capture="a"),
            sel.Element(name="b", capture="b"),
            sel.Element(name="c", capture="c"),
            sel.Element(name="d", capture="e"),
        ),
    )

    assert sel.parse("apple[pie]") == sel.Call(
        element=sel.Element("apple"),
        captures=(sel.Element("#key", value="pie"), ),
    )

    assert sel.parse("apple[0]") == sel.Call(element=sel.Element("apple"),
                                             captures=(sel.Element("#key",
                                                                   value=0), ))

    assert sel.parse("apple[* as filling]") == sel.Call(
        element=sel.Element("apple"),
        captures=(sel.Element("#key", capture="filling", key_field="value"), ),
    )

    assert sel.parse("axe > bow:Weapon > crowbar[* as length]") == sel.Call(
        element=sel.Element("axe"),
        children=(sel.Call(
            element=sel.Element("bow", category="Weapon"),
            children=(sel.Call(
                element=sel.Element("crowbar"),
                captures=(sel.Element(name="#key",
                                      capture="length",
                                      key_field="value"), ),
                immediate=True,
            ), ),
            immediate=True,
        ), ),
        immediate=False,
    )

    assert sel.parse("$f:Fruit") == sel.Element(name=None,
                                                category="Fruit",
                                                capture="f",
                                                key_field="name")

    assert sel.parse("!!x") == sel.Element(name="x",
                                           category=None,
                                           capture="x",
                                           tags=frozenset({1, 2}))