Exemplo n.º 1
0
def _generate_token_list():
    return [
        Connector.build(TerminalSymbol.OPEN),
        Variable.build('x'),
        Connector.build(TerminalSymbol.PLUS),
        Variable.build('y'),
        Connector.build(TerminalSymbol.CLOSE)
    ]
Exemplo n.º 2
0
def test_post_process_node_many_children():
    b = Builder()
    node = InternalNode([
        LeafNode.build(Variable.build('a')),
        LeafNode.build(Variable.build('b')),
        LeafNode.build(Variable.build('c'))
    ])
    as_list = node.to_list()

    result = b._post_process_node(node, False)
    assert result == [node]
    assert result[0].to_list() == as_list
Exemplo n.º 3
0
def test_build_many_leaf_node_children():
    b = Builder()
    children = [
        LeafNode.build(Variable.build('a')),
        LeafNode.build(Variable.build('b')),
        LeafNode.build(Variable.build('c'))
    ]
    b.add_child(InternalNode.build(children))
    result = b.build()

    assert type(result) is InternalNode
    assert len(result.get_children()) == len(children)
    for child in children:
        assert child in result.get_children()
Exemplo n.º 4
0
def test_match_large_seq_large_prod_fails():
    _, _, seq = _generate_test_symbol_sequence()
    _, prod, _ = _generate_test_symbol_sequence()
    prod.insert(0, Variable.build('e'))
    state = seq.match(prod)

    assert state is FAILURE
Exemplo n.º 5
0
def test_build_one_leaf_node_child():
    b = Builder()
    child = LeafNode.build(Variable.build('a'))
    b.add_child(child)
    result = b.build()

    assert result is child
Exemplo n.º 6
0
def test_simplify_node_with_many_children():
    b = Builder()
    grandchildren = [
        LeafNode.build(Variable.build('a')),
        LeafNode.build(Variable.build('b')),
        LeafNode.build(Variable.build('c'))
    ]
    for grandchild in grandchildren:
        b.add_child(InternalNode.build([grandchild]))

    result = b.build()

    assert type(result) is InternalNode
    assert len(result.get_children()) == len(grandchildren)
    for grandchild in grandchildren:
        assert grandchild in result.get_children()
Exemplo n.º 7
0
def test_build_one_child_with_leaf_node_grandchild():
    b = Builder()
    grandchild = LeafNode.build(Variable.build('a'))
    child = InternalNode.build([grandchild])
    b.add_child(child)
    result = b.build()

    assert result is grandchild
Exemplo n.º 8
0
def _str_to_token_list(expr):
    token_list = []
    for char in expr:
        if char in TERMINAL_SYMBOL_TRANSLATIONS:
            token_list.append(
                Connector.build(TERMINAL_SYMBOL_TRANSLATIONS[char]))
        else:
            token_list.append(Variable.build(char))
    return token_list
Exemplo n.º 9
0
def test_simplify_acts_on_all_children():
    b = Builder()
    grandchildren = [
        LeafNode.build(Variable.build('a')),
        LeafNode.build(Variable.build('b')),
        LeafNode.build(Variable.build('c'))
    ]
    children = []
    for grandchild in grandchildren:
        node = InternalNode.build([grandchild])
        b.add_child(node)
        children.append(node)

    b.simplify()

    for grandchild in grandchildren:
        assert grandchild in b._children

    for child in children:
        assert child not in b._children
Exemplo n.º 10
0
def _generate_production_from_terminal_symbols(terminal_symbols):
    # List of variable names to generate test variables from
    variable_names = ['d', 'c', 'b', 'a']

    # Build Token from each supplied TerminalSymbol & add to production
    production = []
    for ts in terminal_symbols:
        if ts is not TerminalSymbol.VARIABLE:
            production.append(Connector.build(ts))
        else:
            production.append(Variable.build(variable_names.pop()))

    return production
Exemplo n.º 11
0
def test_post_process_node_one_child():
    b = Builder()
    deep_leaf_node = LeafNode.build(Variable.build('a'))
    node = InternalNode.build([InternalNode.build([deep_leaf_node])])

    assert b._post_process_node(node, False) == [deep_leaf_node]
Exemplo n.º 12
0
def test_simplify_node_with_leaf_node():
    b = Builder()
    child = LeafNode.build(Variable.build('a'))
    result = b._simplify_node(child)

    assert result == [child]
Exemplo n.º 13
0
def test_variable_representation():
    assert Variable.build('x').representation() == 'x'
Exemplo n.º 14
0
def test_variable_str():
    assert str(Variable.build('x')) == 'x'
Exemplo n.º 15
0
def test_variable_type_matches():
    assert Variable.build('x').matches(TerminalSymbol.VARIABLE)
Exemplo n.º 16
0
def test_variable_caching():
    var = Variable.build('x')
    assert Variable.build('x') is var