Exemplo n.º 1
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.º 2
0
def test_build_one_empty_child():
    b = Builder()
    b.add_child(InternalNode.build([]))
    result = b.build()

    assert type(result) is InternalNode
    assert result.get_children() == []
Exemplo n.º 3
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.º 4
0
def test_builder_simplify_times_divide():
    SIMPLEST_STRING = '[a,*,b,/,c]'
    node = NonTerminalSymbol.parse_input(_str_to_token_list('a*b/c'))
    b = InternalNode.Builder()
    b.add_child(node)
    b = b.build()

    assert str(b) == SIMPLEST_STRING
Exemplo n.º 5
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.º 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_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.º 8
0
    def match(self, token_list):
        # Guard against None token_list
        if token_list is None:
            raise ValueError('SymbolSequence cannot match to None token_list')

        # Track remainder and children
        remainder = token_list
        builder = InternalNode.Builder()

        # Attempt to parse each Token in the list
        for prod_symbol in self._production:
            state = prod_symbol.parse(remainder)

            if state.success():
                builder.add_child(state.node())
                remainder = state.remainder()
            else:
                return FAILURE

        # Return a ParseState containing a new InternalNode for the root and the remainder
        b = builder.build()

        return ParseState.build(b, remainder)
def test_empty_children_str():
    node = InternalNode.build([])
    assert str(node) == '[]'
Exemplo n.º 10
0
def test_post_process_node_no_children():
    b = Builder()
    node = InternalNode.build([])

    assert b._post_process_node(node, False) == []
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_removes_none_children():
    b = Builder()
    b.add_child(InternalNode.build([]))
    b.simplify()

    assert b._children == []
Exemplo n.º 13
0
def test_recurse_on_1_child_str():
    node = InternalNode.build([1])
    assert str(node) == '[1]'
Exemplo n.º 14
0
def test_recurse_on_many_child_str():
    node = InternalNode.build([1, 2, 3])
    assert str(node) == '[1,2,3]'
Exemplo n.º 15
0
def test_add_child_returns_true():
    b = Builder()
    node = InternalNode.build([])
    result = b.add_child(node)

    assert result
Exemplo n.º 16
0
def test_recurse_on_many_child_list():
    node = InternalNode.build([MockNode(), MockNode(), MockNode()])
    assert node.to_list() == [[], [], []]
Exemplo n.º 17
0
def test_empty_children_list():
    node = InternalNode.build([])
    assert node.to_list() == []
Exemplo n.º 18
0
def test_error_on_none_children():
    with pytest.raises(ValueError):
        InternalNode.build(None)
Exemplo n.º 19
0
def test_build_returns_internal_node():
    node = InternalNode.build([])
    assert type(node) == InternalNode
Exemplo n.º 20
0
def test_add_child_adds_child():
    b = Builder()
    node = InternalNode.build([])
    b.add_child(node)

    assert b._children[0] is node