Example #1
0
def test_compiler_mutation_block_nested(patch, compiler, lines, tree):
    patch.many(Objects, ['expression', 'mutation_fragment'])
    patch.object(JSONCompiler, 'chained_mutations', return_value=['chained'])
    tree.path = None
    compiler.mutation_block(tree, None)
    JSONCompiler.chained_mutations.assert_called_with(tree.nested_block)
    args = [Objects.expression(), Objects.mutation_fragment(),
            'chained', 'chained']
    kwargs = {'args': args, 'parent': None}
    lines.append.assert_called_with('mutation', tree.position(), **kwargs)
Example #2
0
def test_compiler_mutation_block_from_service(patch, compiler, lines, tree):
    patch.many(Objects, ["path", "mutation_fragment"])
    patch.object(JSONCompiler, "chained_mutations", return_value=["chained"])
    tree.nested_block = None
    tree.data = "mutation_block"
    compiler.mutation_block(tree, None)
    Objects.path.assert_called_with(tree.path)
    Objects.mutation_fragment.assert_called_with(tree.mutation_fragment)
    JSONCompiler.chained_mutations.assert_called_with(tree)
    args = [Objects.path(), Objects.mutation_fragment(), "chained"]
    kwargs = {"args": args, "parent": None}
    lines.append.assert_called_with("mutation", tree.position(), **kwargs)
Example #3
0
def test_compiler_mutation_block_from_service(patch, compiler, lines, tree):
    patch.many(Objects, ['path', 'mutation_fragment'])
    patch.object(JSONCompiler, 'chained_mutations', return_value=['chained'])
    tree.nested_block = None
    tree.data = 'mutation_block'
    compiler.mutation_block(tree, None)
    Objects.path.assert_called_with(tree.path)
    Objects.mutation_fragment.assert_called_with(tree.mutation_fragment)
    JSONCompiler.chained_mutations.assert_called_with(tree)
    args = [Objects.path(), Objects.mutation_fragment(), 'chained']
    kwargs = {'args': args, 'parent': None}
    lines.append.assert_called_with('mutation', tree.position(), **kwargs)
Example #4
0
def test_compiler_mutation_block_nested(patch, compiler, lines, tree):
    patch.many(Objects, ["expression", "mutation_fragment"])
    patch.object(JSONCompiler, "chained_mutations", return_value=["chained"])
    tree.path = None
    compiler.mutation_block(tree, None)
    JSONCompiler.chained_mutations.assert_called_with(tree.nested_block)
    args = [
        Objects.expression(),
        Objects.mutation_fragment(),
        "chained",
        "chained",
    ]
    kwargs = {"args": args, "parent": None}
    lines.append.assert_called_with("mutation", tree.position(), **kwargs)
Example #5
0
def test_compiler_assignment_unary(patch, compiler, lines, tree):
    """
    Ensures a line like "x = value" is compiled correctly
    """
    patch.many(Objects, ["names", "entity", "expression"])
    af = tree.assignment_fragment.base_expression
    af.service = None
    af.mutation = None
    lines.lines = {lines.last(): {"method": None}}
    compiler.assignment(tree, "1")
    Objects.names.assert_called_with(tree.path)
    Objects.expression.assert_called_with(af.expression)
    kwargs = {"args": [Objects.expression()], "parent": "1"}
    lines.append.assert_called_with("expression", tree.position(), **kwargs)
    lines.set_name.assert_called_with(Objects.names())
Example #6
0
def test_compiler_catch_block(patch, compiler, lines, tree):
    """
    Ensures that catch blocks are compiled correctly.
    """
    patch.object(Objects, 'names')
    patch.object(JSONCompiler, 'subtree')
    compiler.catch_block(tree, '1')
    lines.set_exit.assert_called_with(tree.line())
    Objects.names.assert_called_with(tree.catch_statement)
    lines.set_scope.assert_called_with(tree.line(), '1', Objects.names())
    lines.finish_scope.assert_called_with(tree.line())
    kwargs = {'enter': tree.nested_block.line(), 'output': Objects.names(),
              'parent': '1'}
    lines.append.assert_called_with('catch', tree.line(), **kwargs)
    compiler.subtree.assert_called_with(tree.nested_block, parent=tree.line())
Example #7
0
def test_compiler_assignment_unary(patch, compiler, lines, tree):
    """
    Ensures a line like "x = value" is compiled correctly
    """
    patch.many(Objects, ['names', 'entity', 'expression'])
    af = tree.assignment_fragment.base_expression
    af.service = None
    af.mutation = None
    lines.lines = {lines.last(): {'method': None}}
    compiler.assignment(tree, '1')
    Objects.names.assert_called_with(tree.path)
    Objects.expression.assert_called_with(af.expression)
    kwargs = {'args': [Objects.expression()], 'parent': '1'}
    lines.append.assert_called_with('expression', tree.position(), **kwargs)
    lines.set_name.assert_called_with(Objects.names())
Example #8
0
def test_compiler_arguments(patch, compiler, lines, tree):
    patch.object(Objects, 'arguments')
    lines.lines = {'1': {'method': 'execute', 'args': ['args']}}
    lines.last.return_value = lines.lines['1']
    compiler.arguments(tree, '0')
    Objects.arguments.assert_called_with(tree)
    assert lines.lines['1']['args'] == ['args'] + Objects.arguments()
Example #9
0
def test_compiler_arguments(patch, compiler, lines, tree):
    patch.object(Objects, "arguments")
    lines.lines = {"1": {"method": "execute", "args": ["args"]}}
    lines.last.return_value = lines.lines["1"]
    compiler.arguments(tree, "0")
    Objects.arguments.assert_called_with(tree)
    assert lines.lines["1"]["args"] == ["args"] + Objects.arguments()
Example #10
0
def test_compiler_extract_values(patch, tree):
    patch.object(Objects, 'entity')
    tree.expression = None
    result = JSONCompiler(story=None).extract_values(tree)
    tree.child.assert_called_with(1)
    Objects.entity.assert_called_with(tree.child())
    assert result == [Objects.entity()]
Example #11
0
def test_compiler_throw_name_statement(patch, compiler, lines, tree):
    patch.object(Objects, 'entity')
    tree.children = [Token('RAISE', 'throw'), Token('NAME', 'error')]
    compiler.throw_statement(tree, '1')
    args = [Objects.entity()]
    lines.append.assert_called_with('throw', tree.position(), args=args,
                                    parent='1')
Example #12
0
def test_compiler_chained_mutations(patch, magic, tree):
    patch.object(Objects, 'mutation_fragment')
    mutation = magic()
    tree.find_data.return_value = [mutation]
    result = JSONCompiler(story=None).chained_mutations(tree)
    Objects.mutation_fragment.assert_called_with(mutation.mutation_fragment)
    assert result == [Objects.mutation_fragment()]
Example #13
0
def test_compiler_throw_name_statement(patch, compiler, lines, tree):
    patch.object(Objects, "entity")
    tree.children = [Token("RAISE", "throw"), Token("NAME", "error")]
    compiler.throw_statement(tree, "1")
    args = [Objects.entity()]
    lines.append.assert_called_with(
        "throw", tree.position(), args=args, parent="1"
    )
Example #14
0
def test_compiler_mutation_block(patch, compiler, lines, tree):
    patch.many(Objects, ['primary_expression', 'mutation_fragment'])
    patch.object(JSONCompiler, 'chained_mutations', return_value=['chained'])
    tree.path = None
    tree.nested_block = None
    compiler.mutation_block(tree, None)
    expr = tree.mutation.primary_expression
    Objects.primary_expression.assert_called_with(expr)
    Objects.mutation_fragment.assert_called_with(
        tree.mutation.mutation_fragment)
    JSONCompiler.chained_mutations.assert_called_with(tree.mutation)
    args = [
        Objects.primary_expression(),
        Objects.mutation_fragment(), 'chained'
    ]
    kwargs = {'args': args, 'parent': None}
    lines.append.assert_called_with('mutation', tree.line(), **kwargs)
Example #15
0
def test_compiler_assignment_service(patch, compiler, lines, tree):
    patch.object(Objects, 'names')
    patch.object(JSONCompiler, 'service')
    lines.is_variable_defined.return_value = False
    compiler.assignment(tree, '1')
    service = tree.assignment_fragment.base_expression.service
    JSONCompiler.service.assert_called_with(service, None, '1')
    lines.set_name.assert_called_with(Objects.names())
Example #16
0
def test_compiler_catch_block(patch, compiler, lines, tree):
    """
    Ensures that catch blocks are compiled correctly.
    """
    patch.object(Objects, 'names')
    patch.many(JSONCompiler, ['subtree', 'create_scope'])
    tree.catch_statement.children = ['catch', 'output']
    compiler.catch_block(tree, '1')

    Objects.names.assert_called_with(
        Tree('catch_statement', tree.catch_statement.children[1:])
    )
    compiler.create_scope.assert_called_with(tree.position(), '1',
                                             Objects.names())
    kwargs = {'enter': tree.nested_block.line(), 'output': Objects.names(),
              'parent': '1'}
    lines.append.assert_called_with('catch', tree.position(), **kwargs)
    compiler.subtree.assert_called_with(tree.nested_block,
                                        parent=tree.position().line)
Example #17
0
def test_compiler_service_command(patch, compiler, lines, tree):
    patch.object(Objects, 'arguments')
    patch.object(JSONCompiler, 'output')
    tree.data = 'service'
    compiler.service(tree, None, 'parent')
    position = tree.position()
    service = tree.path.extract_path()
    command = tree.service_fragment.command.child()
    lines.execute.assert_called_with(position, service, command,
                                     Objects.arguments(), compiler.output(),
                                     None, 'parent')
Example #18
0
def test_compiler_call_expression(patch, compiler, lines, tree):
    """
    Ensures that function call expression can be compiled
    """
    patch.many(Objects, ['arguments', 'names'])
    Objects.names.return_values = ['.path.']
    compiler.call_expression(tree, 'parent')
    Objects.arguments.assert_called_with(tree)
    lines.append.assert_called_with('call', tree.position(),
                                    function=tree.path.extract_path(),
                                    args=Objects.arguments(), parent='parent',
                                    output=None)
Example #19
0
def test_compiler_service_nested_block(patch, magic, compiler, lines, tree):
    patch.object(Objects, 'arguments')
    patch.object(JSONCompiler, 'output')
    tree.node.return_value = None
    nested_block = magic()
    tree.data = 'service'
    compiler.service(tree, nested_block, 'parent')
    position = tree.position()
    service = tree.path.extract_path()
    command = tree.service_fragment.command.child()
    lines.execute.assert_called_with(position, service, command,
                                     Objects.arguments(), compiler.output(),
                                     nested_block.line(), 'parent')
Example #20
0
def test_compiler_function_block(patch, compiler, lines, tree):
    patch.object(Objects, 'function_arguments')
    patch.many(JSONCompiler, ['subtree', 'function_output'])
    compiler.function_block(tree, '1')
    statement = tree.function_statement
    Objects.function_arguments.assert_called_with(statement)
    compiler.function_output.assert_called_with(statement)
    lines.append.assert_called_with('function', tree.line(),
                                    function=statement.child().value,
                                    args=Objects.function_arguments(),
                                    output=compiler.function_output(),
                                    enter=tree.nested_block.line(),
                                    parent='1')
    compiler.subtree.assert_called_with(tree.nested_block, parent=tree.line())
Example #21
0
def test_compiler_catch_block(patch, compiler, lines, tree):
    """
    Ensures that catch blocks are compiled correctly.
    """
    patch.object(Objects, "names")
    patch.many(JSONCompiler, ["subtree", "create_scope"])
    tree.catch_statement.children = ["catch", "output"]
    compiler.catch_block(tree, "1")

    Objects.names.assert_called_with(
        Tree("catch_statement", tree.catch_statement.children[1:])
    )
    compiler.create_scope.assert_called_with(
        tree.position(), "1", Objects.names()
    )
    kwargs = {
        "enter": tree.nested_block.line(),
        "output": Objects.names(),
        "parent": "1",
    }
    lines.append.assert_called_with("catch", tree.position(), **kwargs)
    compiler.subtree.assert_called_with(
        tree.nested_block, parent=tree.position().line
    )
Example #22
0
def test_compiler_call_expression(patch, compiler, lines, tree):
    """
    Ensures that function call expression can be compiled
    """
    patch.many(Objects, ["arguments", "names"])
    Objects.names.return_values = [".path."]
    compiler.call_expression(tree, "parent")
    Objects.arguments.assert_called_with(tree)
    lines.append.assert_called_with(
        "call",
        tree.position(),
        function=tree.path.extract_path(),
        args=Objects.arguments(),
        parent="parent",
        output=None,
    )
Example #23
0
def test_compiler_service(patch, compiler, lines, tree):
    """
    Ensures that service trees can be compiled
    """
    patch.object(Objects, 'arguments')
    patch.object(JSONCompiler, 'output')
    tree.node.return_value = None
    tree.data = 'service'
    compiler.service(tree, None, 'parent')
    position = tree.position()
    service = tree.path.extract_path()
    command = tree.service_fragment.command.child()
    Objects.arguments.assert_called_with(tree.service_fragment)
    JSONCompiler.output.assert_called_with(tree.service_fragment.output)
    lines.execute.assert_called_with(position, service, command,
                                     Objects.arguments(), compiler.output(),
                                     None, 'parent')
Example #24
0
def test_compiler_function_block(patch, compiler, lines, tree):
    patch.object(Objects, "function_arguments")
    patch.many(JSONCompiler, ["subtree", "function_output"])
    compiler.function_block(tree, "1")
    statement = tree.function_statement
    Objects.function_arguments.assert_called_with(statement)
    compiler.function_output.assert_called_with(statement)
    lines.append.assert_called_with(
        "function",
        tree.position(),
        function=statement.child().value,
        args=Objects.function_arguments(),
        output=compiler.function_output(),
        enter=tree.nested_block.line(),
        parent="1",
    )
    compiler.subtree.assert_called_with(
        tree.nested_block, parent=tree.position().line
    )
Example #25
0
def test_compiler_when_condensed(patch, compiler, lines, tree, magic):
    patch.object(JSONCompiler, 'service')
    patch.object(JSONCompiler, 'find_parent_with_output')
    # manual patching for staticmethod
    orig_method = Objects.name_to_path
    Objects.name_to_path = magic()
    sf = tree.service.service_fragment
    tree.service.path = '.path.'
    sf.command = None
    lines.lines = {'1': {}}
    lines.last.return_value = lines.lines['1']
    compiler.when(tree, 'nested_block', '1')
    JSONCompiler.service.assert_called_with(tree.service, 'nested_block', '1')
    assert lines.lines['1']['method'] == 'when'
    assert sf.command == '.path.'
    JSONCompiler.find_parent_with_output.assert_called_with(tree, '1')
    output_name = compiler.find_parent_with_output()[0]
    Objects.name_to_path.assert_called_with(output_name)
    assert tree.service.path == Objects.name_to_path()
    Objects.name_to_path = orig_method
Example #26
0
def test_compiler_when_condensed(patch, compiler, lines, tree, magic):
    patch.object(JSONCompiler, "service")
    patch.object(JSONCompiler, "find_parent_with_output")
    # manual patching for staticmethod
    orig_method = Objects.name_to_path
    Objects.name_to_path = magic()
    sf = tree.service.service_fragment
    tree.service.path = ".path."
    sf.command = None
    lines.lines = {"1": {}}
    lines.last.return_value = lines.lines["1"]
    compiler.when(tree, "nested_block", "1")
    JSONCompiler.service.assert_called_with(tree.service, "nested_block", "1")
    assert lines.lines["1"]["method"] == "when"
    assert sf.command == ".path."
    JSONCompiler.find_parent_with_output.assert_called_with(tree, "1")
    output_name = compiler.find_parent_with_output()[0]
    Objects.name_to_path.assert_called_with(output_name)
    assert tree.service.path == Objects.name_to_path()
    Objects.name_to_path = orig_method
Example #27
0
def test_compiler_function_output(patch, compiler, tree):
    patch.object(JSONCompiler, "output")
    patch.object(Objects, "types")
    result = compiler.function_output(tree)
    Objects.types.assert_called_with(tree.function_output.types)
    assert result == [Objects.types()["types"]]
Example #28
0
def test_compiler_function_output(patch, compiler, tree):
    patch.object(JSONCompiler, 'output')
    patch.object(Objects, 'types')
    result = compiler.function_output(tree)
    Objects.types.assert_called_with(tree.function_output.types)
    assert result == [Objects.types()['types']]
Example #29
0
def test_compiler_extract_values_mutation(patch, tree):
    patch.many(Objects, ['values', 'mutation_fragment'])
    result = JSONCompiler(story=None).extract_values(tree)
    Objects.values.assert_called_with(tree.expression.values)
    Objects.mutation_fragment.assert_called_with(tree.expression.mutation)
    assert result == [Objects.values(), Objects.mutation_fragment()]
Example #30
0
def test_compiler_extract_values_expression(patch, tree):
    patch.object(Objects, 'expression')
    tree.expression.mutation = None
    result = JSONCompiler(story=None).extract_values(tree)
    Objects.expression.assert_called_with(tree.expression)
    assert result == [Objects.expression()]