Exemple #1
0
def test_exp_1(fa, fb, fc):
    g = ast_to_graph(((fa + fb) | fc) * slice(0, None))

    assert {*g.successors(_Initial())} == {fa, fc, _Terminal()}
    assert {*g.successors(fa)} == {fb}
    assert {*g.successors(fb)} == {fa, fc, _Terminal()}
    assert {*g.successors(fc)} == {fa, fc, _Terminal()}
Exemple #2
0
def test_any_number_capture_2(fa, fb):
    cap = fa["foo"]
    g = ast_to_graph(AnyNumber(cap))
    assert g.edges[_Initial(), fa].get("start_captures", []) == [cap]
    assert g.edges[_Initial(), fa].get("stop_captures", []) == []
    assert g.edges[fa, fa].get("start_captures", []) == [cap]
    assert g.edges[fa, fa].get("stop_captures", []) == [cap]
    assert g.edges[fa, _Terminal()].get("start_captures", []) == []
    assert g.edges[fa, _Terminal()].get("stop_captures", []) == [cap]
    assert g.edges[_Initial(), _Terminal()].get("start_captures", []) == []
    assert g.edges[_Initial(), _Terminal()].get("stop_captures", []) == []
Exemple #3
0
def test_initial_terminal():
    i1 = _Initial()
    i2 = _Initial()
    t1 = _Terminal()
    t2 = _Terminal()

    assert i1 == i1
    assert i1 == i2
    assert t1 == t1
    assert t1 == t2
    assert i1 != t1
    assert t1 != i1
Exemple #4
0
def test_capture_on_capture(fa, fb):
    cap1 = fa["foo"]
    cap2 = fb["bar"]
    exp = cap1 + cap2

    g = ast_to_graph(exp)
    assert g.edges[_Initial(), fa].get("start_captures", []) == [cap1]
    assert g.edges[_Initial(), fa].get("stop_captures", []) == []
    assert g.edges[fa, fb].get("start_captures", []) == [cap2]
    assert g.edges[fa, fb].get("stop_captures", []) == [cap1]
    assert g.edges[fb, _Terminal()].get("start_captures", []) == []
    assert g.edges[fb, _Terminal()].get("stop_captures", []) == [cap2]
Exemple #5
0
def unscrew(x):
    if x == _Terminal():
        return "T"
    elif x == _Initial():
        return "I"
    else:
        assert isinstance(x, Final)
        assert isinstance(x.statement, Eq)
        return x.statement.ref
Exemple #6
0
def test_any_number_capture_3(fa, fb):
    cap1 = AnyNumber(fa)["foo"]
    cap2 = fb["bar"]
    g = ast_to_graph(cap1 + cap2)
    edges = list(
        sorted([
            ("I", "a"),
            ("I", "b"),
            ("a", "a"),
            ("a", "b"),
            ("b", "T"),
        ]))
    assert graph_edges(g) == edges
    assert g.edges[_Initial(), fa].get("start_captures", []) == [cap1]
    assert g.edges[_Initial(), fa].get("stop_captures", []) == []
    assert g.edges[_Initial(), fb].get("start_captures", []) == [cap2]
    assert g.edges[_Initial(), fb].get("stop_captures", []) == []
    assert g.edges[fa, fa].get("start_captures", []) == []
    assert g.edges[fa, fa].get("stop_captures", []) == []
    assert g.edges[fa, fb].get("start_captures", []) == [cap2]
    assert g.edges[fa, fb].get("stop_captures", []) == [cap1]
    assert g.edges[fb, _Terminal()].get("start_captures", []) == []
    assert g.edges[fb, _Terminal()].get("stop_captures", []) == [cap2]
Exemple #7
0
def test_exp_2(fa, fb):
    g = ast_to_graph(AnyNumber(AnyNumber(fa) | AnyNumber(fb)))

    assert {*g.successors(_Initial())} == {fa, fb, _Terminal()}
    assert {*g.successors(fa)} == {fa, fb, _Terminal()}
    assert {*g.successors(fb)} == {fa, fb, _Terminal()}
Exemple #8
0
def test_capture(fa):
    capture = Capture(statement=fa, name="foo")
    g = ast_to_graph(capture)
    assert g.get_edge_data(_Initial(), fa) == {"start_captures": [capture]}
    assert g.get_edge_data(fa, _Terminal()) == {"stop_captures": [capture]}
Exemple #9
0
def test_any_number(fa):
    g = ast_to_graph(AnyNumber(fa))
    assert {*g.successors(_Initial())} == {fa, _Terminal()}
    assert {*g.successors(fa)} == {fa, _Terminal()}
Exemple #10
0
def test_maybe(fa):
    g = ast_to_graph(Maybe(fa))
    assert {*g.successors(_Initial())} == {fa, _Terminal()}
    assert {*g.successors(fa)} == {_Terminal()}
Exemple #11
0
def test_alternate(fa, fb):
    g = ast_to_graph(fa | fb)
    assert {*g.successors(_Initial())} == {fa, fb}
    assert {*g.successors(fa)} == {_Terminal()}
    assert {*g.successors(fb)} == {_Terminal()}
Exemple #12
0
def test_concatenate(fa, fb):
    g = ast_to_graph(fa + fb)
    assert [*g.successors(_Initial())] == [fa]
    assert [*g.successors(fa)] == [fb]
    assert [*g.successors(fb)] == [_Terminal()]
Exemple #13
0
def test_final(fa):
    g = ast_to_graph(fa)
    assert [*g.successors(_Initial())] == [fa]
    assert [*g.successors(fa)] == [_Terminal()]