def _generate_token_list():
    return [
        Connector.build(TerminalSymbol.OPEN),
        Variable.build('x'),
        Connector.build(TerminalSymbol.PLUS),
        Variable.build('y'),
        Connector.build(TerminalSymbol.CLOSE)
    ]
Beispiel #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
Beispiel #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()
Beispiel #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()
Beispiel #5
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
Beispiel #6
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
Beispiel #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
Beispiel #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
Beispiel #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
Beispiel #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
Beispiel #11
0
def test_variable_caching():
    var = Variable.build('x')
    assert Variable.build('x') is var
Beispiel #12
0
def test_variable_str():
    assert str(Variable.build('x')) == 'x'
Beispiel #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]
Beispiel #14
0
def test_variable_type_matches():
    assert Variable.build('x').matches(TerminalSymbol.VARIABLE)
Beispiel #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]
Beispiel #16
0
def test_variable_representation():
    assert Variable.build('x').representation() == 'x'
Beispiel #17
0
def test_error_on_no_representation():
    with pytest.raises(ValueError):
        Variable(None)
Beispiel #18
0
def test_variable_not_equal():
    assert Variable.build('x') != Variable.build('y')