def _generate_token_list():
    return [
        Connector.build(TerminalSymbol.OPEN),
        Variable.build('x'),
        Connector.build(TerminalSymbol.PLUS),
        Variable.build('y'),
        Connector.build(TerminalSymbol.CLOSE)
    ]
Esempio 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
Esempio 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()
Esempio n. 4
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()
Esempio 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
Esempio n. 6
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
Esempio 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
Esempio 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
Esempio 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
Esempio 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
Esempio n. 11
0
def test_variable_caching():
    var = Variable.build('x')
    assert Variable.build('x') is var
Esempio n. 12
0
def test_variable_str():
    assert str(Variable.build('x')) == 'x'
Esempio n. 13
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]
Esempio n. 14
0
def test_variable_type_matches():
    assert Variable.build('x').matches(TerminalSymbol.VARIABLE)
Esempio n. 15
0
def test_simplify_node_with_leaf_node():
    b = Builder()
    child = LeafNode.build(Variable.build('a'))
    result = b._simplify_node(child)

    assert result == [child]
Esempio n. 16
0
def test_variable_representation():
    assert Variable.build('x').representation() == 'x'
Esempio n. 17
0
def test_variable_not_equal():
    assert Variable.build('x') != Variable.build('y')