Example #1
0
def test_transformer_is_keyword_future(syntax_error, keyword):
    token = Token("any", keyword)
    with raises(StorySyntaxError):
        Transformer.is_keyword(token)
    name = "future_reserved_keyword"
    format = {"keyword": keyword}
    syntax_error.assert_called_with(name, token=token, format_args=format)
Example #2
0
def test_transformer_is_keyword_future(syntax_error, keyword):
    token = Token('any', keyword)
    with raises(StorySyntaxError):
        Transformer.is_keyword(token)
    name = 'future_reserved_keyword'
    format = {'keyword': keyword}
    syntax_error.assert_called_with(name, token=token, format=format)
Example #3
0
def test_transformer_implicit_output(tree):
    """
    Ensures Transformer.implicit_output adds an output tree when needed
    """
    tree.service_fragment.output = None
    tree.service_fragment.children = []
    Transformer.implicit_output(tree)
    expected = Tree('output', [tree.service_fragment.command.child()])
    assert tree.service_fragment.children[-1] == expected
Example #4
0
def test_transformer_function_block_empty(patch, tree, magic):
    """
    Ensures that indented arguments are added back to the their original node
    """
    assert Transformer.function_block([]) == Tree('function_block', [])
    assert Transformer.function_block([0]) == Tree('function_block', [0])
    m = magic()
    m.data = 'some_block'
    assert Transformer.function_block([m]) == Tree('function_block', [m])
    assert Transformer.function_block([m, m]) == Tree('function_block', [m, m])
Example #5
0
def test_transformer_absolute_expression(patch, tree):
    """
    Ensures absolute_expression are untouched when they don't contain
    just a path
    """
    patch.object(Tree, 'follow_node_chain')
    tree.follow_node_chain.return_value = None
    result = Transformer.absolute_expression([tree])
    assert result == Tree('absolute_expression', [tree])
    result = Transformer.absolute_expression([tree, tree])
    assert result == Tree('absolute_expression', [tree, tree])
Example #6
0
def test_transformer_when_block(patch, tree):
    """
    Ensures when_block nodes are transformed correctly
    """
    patch.object(Transformer, 'implicit_output')
    result = Transformer.when_block([tree])
    Transformer.implicit_output.assert_called_with(tree)
    assert result == Tree('when_block', [tree])
Example #7
0
def test_transformer_service_block_when(patch, tree):
    """
    Ensures service_block nodes with a when block are transformed correctly
    """
    tree.block.rules = None
    matches = ['service_block', tree]
    result = Transformer.service_block(matches)
    assert result == Tree('service_block', matches)
Example #8
0
def test_transformer_service_block_when(patch, tree):
    """
    Ensures service_block nodes with a when block are transformed correctly
    """
    patch.object(Transformer, 'implicit_output')
    tree.block.rules = None
    matches = ['service_block', tree]
    result = Transformer.service_block(matches)
    Transformer.implicit_output.assert_called_with('service_block')
    assert result == Tree('service_block', matches)
Example #9
0
def test_transformer_service_block_indented_args(tree, magic):
    """
    Ensures service_block with indented arguments are transformed correctly.
    """
    tree.find_data.return_value = ['argument']
    block = magic()
    matches = [block, tree]
    result = Transformer.service_block(matches)
    block.service_fragment.children.append.assert_called_with('argument')
    assert result == Tree('service_block', [block])
Example #10
0
def test_transformer_service_block_indented_args(tree, magic, patch):
    """
    Ensures service_block with indented arguments are transformed correctly.
    """
    patch.object(Transformer, "filter_nested_block", return_value=["argument"])
    block = magic()
    matches = [block, tree]
    result = Transformer.service_block(matches)
    block.service_fragment.children.append.assert_called_with("argument")
    assert result == Tree("service_block", [block])
Example #11
0
def test_transformer_absolute_expression_zero(patch, tree, magic):
    """
    Ensures absolute_expression are untouched when they don't contain
    just a path
    """
    patch.object(Tree, "follow_node_chain")
    m = magic()
    tree.follow_node_chain.return_value = m
    result = Transformer.absolute_expression([tree])
    expected = Tree("service_block",
                    [Tree("service", [m, Tree("service_fragment", [])])])
    assert result == expected
Example #12
0
def test_transformer_function_block(patch, tree, magic):
    """
    Ensures that indented arguments are added back to the their original node
    """
    function_block = magic()
    m = magic()
    block = magic()
    m.data = 'indented_typed_arguments'
    m.find_data.return_value = ['.indented.node.']
    r = Transformer.function_block([function_block, m, block])
    m.find_data.assert_called_with('typed_argument')
    function_block.children.append.assert_called_with('.indented.node.')
    assert r.data == 'function_block'
    assert r.children == [function_block, Tree('nested_block', [block])]
Example #13
0
def test_transformer_function_block(patch, tree, magic):
    """
    Ensures that indented arguments are added back to the their original node
    """
    function_block = magic()
    m = magic()
    block = magic()
    m.data = "indented_typed_arguments"
    m.find_data.return_value = [".indented.node."]
    r = Transformer.function_block([function_block, m, block])
    m.find_data.assert_called_with("typed_argument")
    function_block.children.append.assert_called_with(".indented.node.")
    assert r.data == "function_block"
    assert r.children == [function_block, Tree("nested_block", [block])]
Example #14
0
def test_transformer_when_block(patch, tree):
    """
    Ensures when_block nodes are transformed correctly
    """
    tree.data = 'when_service'
    tree.output = None
    tree.children = [Token('NAME', '.name.')]
    tree.child_token.return_value = Token('NAME', '.child.', line=42)
    tree.path.child_token.return_value = Token('NAME', '.path.')
    result = Transformer.when_block([tree, 'block'])
    assert result == Tree('concise_when_block', [
        Token('NAME', '.child.'),
        Token('NAME', '.path.'),
        Tree('when_block', [tree, 'block'])
    ])
Example #15
0
def test_transformer_when_block_no_command(patch, tree):
    """
    Ensures when_block nodes without command are transformed correctly
    """
    patch.object(Transformer, 'argument_shorthand')
    tree.data = 'when_service'
    tree.output = None
    tree.children = [Token('NAME', '.name.')]
    tree.service_fragment.command = None
    result = Transformer.when_block([tree, 'block'])
    assert result == Tree('when_block', [
        Tree('service', [
            Tree('path', [tree.child_token()]),
            tree.service_fragment,
        ]),
        'block',
    ])
Example #16
0
def test_transformer_when_block(patch, tree):
    """
    Ensures when_block nodes are transformed correctly
    """
    tree.data = "when_service"
    tree.output = None
    tree.children = [Token("NAME", ".name.")]
    tree.child_token.return_value = Token("NAME", ".child.", line=42)
    tree.path.child_token.return_value = Token("NAME", ".path.")
    result = Transformer.when_block([tree, "block"])
    assert result == Tree(
        "concise_when_block",
        [
            Token("NAME", ".child."),
            Token("NAME", ".path."),
            Tree("when_block", [tree, "block"]),
        ],
    )
Example #17
0
def test_transformer_when_block_no_command(patch, tree):
    """
    Ensures when_block nodes without command are transformed correctly
    """
    patch.object(Transformer, "argument_shorthand")
    tree.data = "when_service"
    tree.output = None
    tree.children = [Token("NAME", ".name.")]
    tree.service_fragment.command = None
    result = Transformer.when_block([tree, "block"])
    assert result == Tree(
        "when_block",
        [
            Tree(
                "service",
                [
                    Tree("path", [tree.child_token()]),
                    tree.service_fragment,
                ],
            ),
            "block",
        ],
    )
Example #18
0
def test_transformer_command(patch, magic):
    patch.object(Transformer, 'is_keyword')
    result = Transformer.command(['matches'])
    Transformer.is_keyword.assert_called_with('matches')
    assert result == Tree('command', ['matches'])
Example #19
0
def test_transformer_assignment_error_dash(syntax_error, magic):
    token = magic(value='-')
    matches = [magic(children=[token])]
    with raises(StorySyntaxError):
        Transformer.assignment(matches)
    syntax_error.assert_called_with('variables_dash', token=token)
Example #20
0
def test_transformer_assignment(magic):
    matches = [magic()]
    assert Transformer.assignment(matches) == Tree('assignment', matches)
Example #21
0
def test_transformer_arguments_short():
    matches = [Tree('path', ['token'])]
    expected = ['token', Tree('path', ['token'])]
    assert Transformer.arguments(matches) == Tree('arguments', expected)
Example #22
0
def test_transformer_arguments():
    assert Transformer.arguments('matches') == Tree('arguments', 'matches')
Example #23
0
def test_transformer_implicit_output_none(tree):
    assert Transformer.implicit_output(tree) is None
Example #24
0
def test_transformer_is_keyword(syntax_error, keyword):
    token = Token('any', keyword)
    with raises(StorySyntaxError):
        Transformer.is_keyword(token)
    name = 'reserved_keyword_{}'.format(keyword)
    syntax_error.assert_called_with(name, token=token)
Example #25
0
def test_transformer_rules(rule):
    result = getattr(Transformer(), rule)(['matches'])
    assert isinstance(result, Tree)
    assert result.data == rule
    assert result.children == ['matches']
Example #26
0
def test_multi_line_string_multi_line_indented():
    assert Transformer.multi_line_string('aa\n\tbb\n  cc') == 'aa bb cc'
Example #27
0
def test_transformer_service_block():
    """
    Ensures service_block nodes are transformed correctly
    """
    assert Transformer.service_block([]) == Tree('service_block', [])
Example #28
0
def test_transformer_path(patch):
    patch.object(Transformer, 'is_keyword')
    result = Transformer.path(['matches'])
    Transformer.is_keyword.assert_called_with('matches')
    assert result == Tree('path', ['matches'])
Example #29
0
def test_multi_line_string_multi_line_backslash():
    text = 'aa\\\n\tbb\\\n  cc'
    assert Transformer.multi_line_string(text) == 'aabbcc'
Example #30
0
def test_multi_line_string_multi_line_start_multiple_backslashs():
    assert Transformer.multi_line_string('\\\n\\\n  b') == 'b'