Beispiel #1
0
def test_compiler_compile(patch, magic):
    patch.many(JSONCompiler, ['parse_tree'])
    patch.object(Lines, 'entrypoint')
    tree = magic()
    result = JSONCompiler(story=None).compile(tree)
    JSONCompiler.parse_tree.assert_called_with(tree)
    lines = JSONCompiler(story=None).lines
    expected = {'tree': lines.lines, 'version': version,
                'services': lines.get_services(), 'functions': lines.functions,
                'entrypoint': lines.entrypoint()}
    assert result == expected
Beispiel #2
0
def test_compiler_foreach_block(patch, compiler, lines, tree):
    patch.init(Tree)
    patch.many(JSONCompiler, ['subtree', 'output', 'fake_base_expression'])
    compiler.foreach_block(tree, '1')
    compiler.output.assert_called_with(tree.foreach_statement.output)
    args = [compiler.fake_base_expression()]
    lines.set_scope.assert_called_with(tree.line(), '1', JSONCompiler.output())
    lines.finish_scope.assert_called_with(tree.line())
    lines.append.assert_called_with('for', tree.line(), args=args,
                                    enter=tree.nested_block.line(),
                                    output=JSONCompiler.output(), parent='1')
    compiler.subtree.assert_called_with(tree.nested_block, parent=tree.line())
Beispiel #3
0
def test_compiler_compile(patch, magic):
    patch.many(JSONCompiler, ["parse_tree"])
    patch.object(Lines, "entrypoint")
    tree = magic()
    result = JSONCompiler(story=None).compile(tree)
    JSONCompiler.parse_tree.assert_called_with(tree)
    lines = JSONCompiler(story=None).lines
    expected = {
        "tree": lines.lines,
        "version": version,
        "services": lines.get_services(),
        "functions": lines.functions,
        "entrypoint": lines.entrypoint(),
    }
    assert result == expected
Beispiel #4
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()]
Beispiel #5
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()]
Beispiel #6
0
def test_compiler_compile(patch, magic):
    patch.object(Compiler, 'generate')
    patch.object(JSONCompiler, 'compile')
    tree = magic()
    result = Compiler.compile(tree, story=None, features=None)
    Compiler.generate.assert_called_with(tree, None)
    JSONCompiler.compile.assert_called_with(Compiler.generate())
    assert result == JSONCompiler.compile()
Beispiel #7
0
def test_compiler_compile(patch, magic):
    patch.object(Compiler, "generate", return_value=("tree", "sem"))
    patch.object(JSONCompiler, "compile")
    tree = magic()
    story = magic()
    result = Compiler.compile(tree, story=story)
    Compiler.generate.assert_called_with(tree, story.context, scope=None)
    JSONCompiler.compile.assert_called_with("tree")
    assert result.output() == JSONCompiler.compile()
    assert result.module() == "sem"
    assert result.backend == "json"
Beispiel #8
0
def test_compiler_return_statement_entity(patch, compiler, lines, tree):
    """
    Ensures Compiler.return_statement can compile return statements that return
    entities.
    """
    patch.object(JSONCompiler, 'fake_base_expression')
    compiler.return_statement(tree, '1')
    position = tree.position()
    JSONCompiler.fake_base_expression.assert_called_with(
        tree.base_expression, '1')
    kwargs = {'args': [JSONCompiler.fake_base_expression()], 'parent': '1'}
    lines.append.assert_called_with('return', position, **kwargs)
Beispiel #9
0
def test_compiler_elseif_block(patch, compiler, lines, tree):
    patch.many(JSONCompiler, ['subtree', 'fake_base_expression',
                              'create_scope'])
    compiler.elseif_block(tree, '1')
    exp = tree.elseif_statement.base_expression
    JSONCompiler.fake_base_expression.assert_called_with(exp, '1')
    args = [JSONCompiler.fake_base_expression()]
    compiler.create_scope.assert_called_with(tree.position(), '1')
    lines.append.assert_called_with('elif', tree.position(), args=args,
                                    enter=tree.nested_block.line(),
                                    parent='1')
    compiler.subtree.assert_called_with(tree.nested_block,
                                        parent=tree.position().line)
Beispiel #10
0
def test_compiler_return_statement_entity(patch, compiler, lines, tree):
    """
    Ensures Compiler.return_statement can compile return statements that return
    entities.
    """
    patch.object(JSONCompiler, "fake_base_expression")
    compiler.return_statement(tree, "1")
    position = tree.position()
    JSONCompiler.fake_base_expression.assert_called_with(
        tree.base_expression, "1"
    )
    kwargs = {"args": [JSONCompiler.fake_base_expression()], "parent": "1"}
    lines.append.assert_called_with("return", position, **kwargs)
Beispiel #11
0
def test_compiler_if_block(patch, compiler, lines, tree):
    patch.many(JSONCompiler, ['subtree', 'fake_base_expression'])
    tree.elseif_block = None
    tree.else_block = None
    tree.extract.return_value = []
    compiler.if_block(tree, '1')
    exp = tree.if_statement.base_expression
    JSONCompiler.fake_base_expression.assert_called_with(exp, '1')
    nested_block = tree.nested_block
    args = [JSONCompiler.fake_base_expression()]
    lines.set_scope.assert_called_with(tree.line(), '1')
    lines.finish_scope.assert_called_with(tree.line())
    lines.append.assert_called_with('if', tree.line(), args=args,
                                    enter=nested_block.line(), parent='1')
    compiler.subtree.assert_called_with(nested_block, parent=tree.line())
Beispiel #12
0
def test_compiler_if_block(patch, compiler, lines, tree):
    patch.many(
        JSONCompiler, ["subtree", "fake_base_expression", "create_scope"]
    )
    tree.elseif_block = None
    tree.else_block = None
    tree.extract.return_value = []
    compiler.if_block(tree, "1")
    exp = tree.if_statement.base_expression
    JSONCompiler.fake_base_expression.assert_called_with(exp, "1")
    nested_block = tree.nested_block
    args = [JSONCompiler.fake_base_expression()]
    compiler.create_scope.assert_called_with(tree.position(), "1")
    lines.append.assert_called_with(
        "if", tree.position(), args=args, enter=nested_block.line(), parent="1"
    )
    compiler.subtree.assert_called_with(
        nested_block, parent=tree.position().line
    )
Beispiel #13
0
def test_compiler_elseif_block(patch, compiler, lines, tree):
    patch.many(
        JSONCompiler, ["subtree", "fake_base_expression", "create_scope"]
    )
    compiler.elseif_block(tree, "1")
    exp = tree.elseif_statement.base_expression
    JSONCompiler.fake_base_expression.assert_called_with(exp, "1")
    args = [JSONCompiler.fake_base_expression()]
    compiler.create_scope.assert_called_with(tree.position(), "1")
    lines.append.assert_called_with(
        "elif",
        tree.position(),
        args=args,
        enter=tree.nested_block.line(),
        parent="1",
    )
    compiler.subtree.assert_called_with(
        tree.nested_block, parent=tree.position().line
    )
Beispiel #14
0
def test_compiler_foreach_block(patch, compiler, lines, tree):
    patch.init(Tree)
    patch.many(
        JSONCompiler,
        ["subtree", "output", "fake_base_expression", "create_scope"],
    )
    compiler.foreach_block(tree, "1")
    compiler.output.assert_called_with(tree.foreach_statement.output)
    args = [compiler.fake_base_expression()]
    compiler.create_scope.assert_called_with(
        tree.position(), "1", compiler.output()
    )
    lines.append.assert_called_with(
        "for",
        tree.position(),
        args=args,
        enter=tree.nested_block.line(),
        output=JSONCompiler.output(),
        parent="1",
    )
    compiler.subtree.assert_called_with(
        tree.nested_block, parent=tree.position().line
    )
Beispiel #15
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()]
Beispiel #16
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()]
Beispiel #17
0
def test_compiler_output(tree):
    tree.children = [Token("token", "output")]
    result = JSONCompiler.output(tree)
    assert result == ["output"]
Beispiel #18
0
def test_compiler_output_none():
    assert JSONCompiler.output(None) == []
Beispiel #19
0
def test_compiler_output(tree):
    tree.children = [Token('token', 'output')]
    result = JSONCompiler.output(tree)
    assert result == ['output']
Beispiel #20
0
def test_compiler_init(patch):
    patch.init(Lines)
    compiler = JSONCompiler(story=None)
    assert isinstance(compiler.lines, Lines)
Beispiel #21
0
def compiler(patch, lines):
    patch.init(Lines)
    compiler = JSONCompiler(story=None)
    compiler.lines = lines
    return compiler