コード例 #1
0
ファイル: Compiler.py プロジェクト: shaz13/storyscript
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()
コード例 #2
0
def test_compiler_compile(patch):
    patch.object(Preprocessor, 'process')
    patch.many(Compiler, ['parse_tree', 'compiler'])
    result = Compiler.compile('tree')
    Preprocessor.process.assert_called_with('tree')
    Compiler.compiler().parse_tree.assert_called_with(Preprocessor.process())
    lines = Compiler.compiler().lines
    expected = {'tree': lines.lines, 'version': version,
                'services': lines.get_services(), 'functions': lines.functions,
                'entrypoint': lines.first(), 'modules': lines.modules}
    assert result == expected
コード例 #3
0
ファイル: Compiler.py プロジェクト: nomorepanic/storyscript
def test_compiler_service_command(patch, compiler, lines, tree):
    patch.object(Objects, 'arguments')
    patch.object(Compiler, 'output')
    compiler.service(tree, None, 'parent')
    line = tree.line()
    service = tree.path.extract_path()
    command = tree.service_fragment.command.child()
    lines.set_scope.assert_called_with(line, 'parent', Compiler.output())
    lines.execute.assert_called_with(line, service,
                                     command, Objects.arguments(),
                                     Compiler.output(), None, 'parent')
コード例 #4
0
ファイル: compiler.py プロジェクト: riccardomc/storyscript
def test_compiler_service_command(patch, compiler, tree):
    patch.object(Objects, 'arguments')
    patch.many(Compiler, ['add_line', 'output'])
    compiler.service(tree)
    line = tree.line()
    service = tree.child().child().value
    assert compiler.outputs[tree.line()] == Compiler.output()
    compiler.add_line.assert_called_with('execute', line, service=service,
                                         command=tree.node().child(),
                                         parent=None, output=Compiler.output(),
                                         args=Objects.arguments())
コード例 #5
0
def test_assignments_int_positive(parser):
    """
    Ensures that assignments to positive integers are compiled correctly
    """
    tree = parser.parse('a = +3')
    result = Compiler.compile(tree)
    assert result['tree']['1']['args'] == [3]
コード例 #6
0
ファイル: Compiler.py プロジェクト: nomorepanic/storyscript
def test_compiler_chained_mutations(patch, magic, tree):
    patch.object(Objects, 'mutation')
    mutation = magic()
    tree.find_data.return_value = [mutation]
    result = Compiler.chained_mutations(tree)
    Objects.mutation.assert_called_with(mutation.mutation_fragment)
    assert result == [Objects.mutation()]
コード例 #7
0
def test_compiler_expression_sum_to_parenthesis(parser):
    tree = parser.parse('1 + (2 + 3)')
    result = Compiler.compile(tree)
    rhs = {'$OBJECT': 'expression', 'expression': 'sum', 'values': [2, 3]}
    args = [{'$OBJECT': 'expression', 'expression': 'sum', 'values': [1, rhs]}]
    assert result['tree']['1']['method'] == 'expression'
    assert result['tree']['1']['args'] == args
コード例 #8
0
ファイル: Compiler.py プロジェクト: nomorepanic/storyscript
def test_compiler_extract_values(patch, tree):
    patch.object(Objects, 'entity')
    tree.expression = None
    result = Compiler.extract_values(tree)
    tree.child.assert_called_with(1)
    Objects.entity.assert_called_with(tree.child())
    assert result == [Objects.entity()]
コード例 #9
0
ファイル: Compiler.py プロジェクト: nomorepanic/storyscript
def test_compiler_foreach_block(patch, compiler, lines, tree):
    patch.init(Tree)
    patch.object(Objects, 'entity')
    patch.many(Compiler, ['subtree', 'output'])
    compiler.foreach_block(tree, '1')
    assert Objects.entity.call_count == 1
    compiler.output.assert_called_with(tree.foreach_statement.output)
    args = [Objects.entity()]
    lines.set_scope.assert_called_with(tree.line(), '1', Compiler.output())
    lines.append.assert_called_with('for',
                                    tree.line(),
                                    args=args,
                                    enter=tree.nested_block.line(),
                                    output=Compiler.output(),
                                    parent='1')
    compiler.subtree.assert_called_with(tree.nested_block, parent=tree.line())
コード例 #10
0
def test_compiler_set_regular_expression_flags(parser):
    """
    Ensures regular expressions with flags are compiled correctly
    """
    tree = parser.parse('a = /^foo/g')
    result = Compiler.compile(tree)
    assert result['tree']['1']['args'][0]['flags'] == 'g'
コード例 #11
0
def test_compiler_if_else(parser):
    source = 'if colour == "red"\n\tx = 0\nelse\n\tx = 1'
    tree = parser.parse(source)
    result = Compiler.compile(tree)
    assert result['tree']['1']['exit'] == '3'
    assert result['tree']['3']['method'] == 'else'
    assert result['tree']['4']['parent'] == '3'
コード例 #12
0
def test_compiler_try(parser):
    source = 'try\n\tx=0'
    tree = parser.parse(source)
    result = Compiler.compile(tree)
    assert result['tree']['1']['method'] == 'try'
    assert result['tree']['1']['enter'] == '2'
    assert result['tree']['2']['parent'] == '1'
コード例 #13
0
ファイル: Compiler.py プロジェクト: nomorepanic/storyscript
def test_compiler_indented_chain(patch, compiler, lines, tree):
    patch.object(Compiler, 'chained_mutations')
    lines.last.return_value = '1'
    lines.lines = {'1': {'method': 'mutation', 'args': ['args']}}
    compiler.indented_chain(tree, '0')
    Compiler.chained_mutations.assert_called_with(tree)
    assert lines.lines['1']['args'] == ['args'] + Compiler.chained_mutations()
コード例 #14
0
def test_compiler_mutation_chained(parser, source):
    """
    Ensures that chained mutations are compiled correctly
    """
    tree = parser.parse(source)
    result = Compiler.compile(tree)
    args = [
        1, {
            '$OBJECT': 'mutation',
            'mutation': 'increment',
            'arguments': []
        }, {
            '$OBJECT':
            'mutation',
            'mutation':
            'format',
            'arguments': [{
                '$OBJECT': 'argument',
                'name': 'to',
                'argument': {
                    '$OBJECT': 'string',
                    'string': 'string'
                }
            }]
        }
    ]
    assert result['tree']['1']['args'] == args
コード例 #15
0
def test_compiler_if_inline_expression(parser):
    tree = parser.parse('if (random numbers)\n\tx = 0')
    result = Compiler.compile(tree)
    entry = result['entrypoint']
    name = result['tree'][entry]['name']
    assert result['tree']['1']['method'] == 'if'
    assert result['tree']['1']['args'] == [{'$OBJECT': 'path', 'paths': name}]
コード例 #16
0
def test_compiler_try_finally(parser):
    source = 'try\n\tx=0\nfinally\n\tx=1'
    tree = parser.parse(source)
    result = Compiler.compile(tree)
    assert result['tree']['3']['method'] == 'finally'
    assert result['tree']['3']['enter'] == '4'
    assert result['tree']['4']['parent'] == '3'
コード例 #17
0
def test_compiler_try_raise(parser):
    source = 'try\n\tx=0\ncatch as error\n\traise'
    tree = parser.parse(source)
    result = Compiler.compile(tree)
    assert result['tree']['1']['exit'] == '3'
    assert result['tree']['3']['method'] == 'catch'
    assert result['tree']['4']['method'] == 'raise'
    assert result['tree']['4']['parent'] == '3'
コード例 #18
0
ファイル: compiler.py プロジェクト: riccardomc/storyscript
def test_compiler_foreach_block_parent(patch, compiler, tree):
    patch.object(Objects, 'path')
    patch.many(Compiler, ['add_line', 'subtree', 'output'])
    compiler.foreach_block(tree, parent='1')
    args = [Objects.path()]
    compiler.add_line.assert_called_with('for', tree.line(), args=args,
                                         enter=tree.node().line(),
                                         output=Compiler.output(), parent='1')
コード例 #19
0
def test_compiler_expression_path(parser):
    """
    Ensures that expressions with paths are compiled correctly.
    """
    tree = parser.parse('x = 3\nx + 2')
    result = Compiler.compile(tree)
    path = {'$OBJECT': 'path', 'paths': ['x']}
    assert result['tree']['2']['args'][0]['values'][0] == path
コード例 #20
0
def test_compiler_function_call(parser):
    source = 'function sum a:int returns int\n\treturn 0\nsum a:1'
    tree = parser.parse(source)
    result = Compiler.compile(tree)
    args = [{'$OBJECT': 'argument', 'name': 'a', 'argument': 1}]
    assert result['tree']['3']['method'] == 'call'
    assert result['tree']['3']['service'] == 'sum'
    assert result['tree']['3']['args'] == args
コード例 #21
0
def test_compiler_set_service(parser):
    """
    Ensures that service assignments are compiled correctly
    """
    tree = parser.parse('a = alpine echo')
    result = Compiler.compile(tree)
    assert result['tree']['1']['method'] == 'execute'
    assert result['tree']['1']['name'] == ['a']
コード例 #22
0
def test_compiler_set_object_empty(parser):
    """
    Ensures that assignments to empty objects are compiled correctly
    """
    tree = parser.parse('a = {}')
    result = Compiler.compile(tree)
    assert result['tree']['1']['method'] == 'set'
    assert result['tree']['1']['args'] == [{'$OBJECT': 'dict', 'items': []}]
コード例 #23
0
def test_compiler_while(parser):
    tree = parser.parse('while cond\n\tx = 0')
    result = Compiler.compile(tree)
    args = [{'$OBJECT': 'path', 'paths': ['cond']}]
    assert result['tree']['1']['method'] == 'while'
    assert result['tree']['1']['args'] == args
    assert result['tree']['1']['enter'] == '2'
    assert result['tree']['2']['parent'] == '1'
コード例 #24
0
def test_compiler_foreach(parser):
    tree = parser.parse('foreach items as item\n\tx = 0')
    result = Compiler.compile(tree)
    args = [{'$OBJECT': 'path', 'paths': ['items']}]
    assert result['tree']['1']['method'] == 'for'
    assert result['tree']['1']['output'] == ['item']
    assert result['tree']['1']['args'] == args
    assert result['tree']['1']['enter'] == '2'
    assert result['tree']['2']['parent'] == '1'
コード例 #25
0
def test_compiler_set_regular_expression(parser):
    """
    Ensures regular expressions are compiled correctly
    """
    tree = parser.parse('a = /^foo/')
    result = Compiler.compile(tree)
    assert result['tree']['1']['method'] == 'set'
    assert result['tree']['1']['args'][0]['$OBJECT'] == 'regexp'
    assert result['tree']['1']['args'][0]['regexp'] == '/^foo/'
コード例 #26
0
def test_compiler_try_catch(parser):
    source = 'try\n\tx=0\ncatch as error\n\tx=1'
    tree = parser.parse(source)
    result = Compiler.compile(tree)
    assert result['tree']['1']['exit'] == '3'
    assert result['tree']['3']['method'] == 'catch'
    assert result['tree']['3']['output'] == ['error']
    assert result['tree']['3']['enter'] == '4'
    assert result['tree']['4']['parent'] == '3'
コード例 #27
0
def test_compiler_expression_sum(parser):
    """
    Ensures that sums are compiled correctly
    """
    tree = parser.parse('3 + 2')
    result = Compiler.compile(tree)
    args = [{'$OBJECT': 'expression', 'expression': 'sum', 'values': [3, 2]}]
    assert result['tree']['1']['method'] == 'expression'
    assert result['tree']['1']['args'] == args
コード例 #28
0
def test_compiler_set(parser):
    """
    Ensures that assignments to integers are compiled correctly
    """
    tree = parser.parse('a = 0')
    result = Compiler.compile(tree)
    assert result['tree']['1']['method'] == 'set'
    assert result['tree']['1']['name'] == ['a']
    assert result['tree']['1']['args'] == [0]
コード例 #29
0
def test_compiler_set_list_multiline(parser):
    """
    Ensures that assignments to multiline lists are compiled correctly
    """
    tree = parser.parse('a = [\n\t1,\n\t2\n]')
    result = Compiler.compile(tree)
    args = [{'$OBJECT': 'list', 'items': [1, 2]}]
    assert result['tree']['1']['method'] == 'set'
    assert result['tree']['1']['args'] == args
コード例 #30
0
def test_compiler_when_path(patch, compiler, lines, tree):
    patch.object(Objects, 'path')
    patch.object(Compiler, 'output')
    tree.service = None
    compiler.when(tree, 'nested_block', '1')
    Objects.path.assert_called_with(tree.path)
    Compiler.output.assert_called_with(tree.output)
    lines.append.assert_called_with('when', tree.line(), args=[Objects.path()],
                                    output=Compiler.output(), parent='1')