Exemplo n.º 1
0
 def test_function(self):
     module = codegen.Module()
     func = codegen.Function('myfunc', args=['myarg1', 'myarg2'],
                             parent_scope=module.scope)
     self.assertCodeEqual(as_source_code(func), """
         def myfunc(myarg1, myarg2):
             pass
     """)
Exemplo n.º 2
0
 def test_reserve_name_function_arg(self):
     scope = codegen.Scope()
     scope.reserve_function_arg_name('arg_name')
     scope.reserve_name('myfunc')
     func = codegen.Function('myfunc',
                             args=['arg_name'],
                             parent_scope=scope)
     self.assertNotIn('arg_name2', func.all_reserved_names())
Exemplo n.º 3
0
 def test_function_return(self):
     module = codegen.Module()
     func = codegen.Function('myfunc',
                             parent_scope=module)
     func.add_return(codegen.String("Hello"))
     self.assertCodeEqual(as_source_code(func), """
         def myfunc():
             return 'Hello'
     """)
Exemplo n.º 4
0
 def test_add_function(self):
     module = codegen.Module()
     func_name = module.scope.reserve_name('myfunc')
     func = codegen.Function(func_name,
                             parent_scope=module)
     module.add_function(func_name, func)
     self.assertCodeEqual(as_source_code(module), """
         def myfunc():
             pass
     """)
Exemplo n.º 5
0
 def test_function_args_name_reserved_check(self):
     module = codegen.Module()
     module.scope.reserve_function_arg_name('my_arg')
     func_name = module.scope.reserve_name('myfunc')
     func = codegen.Function(func_name, args=['my_arg'],
                             parent_scope=module.scope)
     func.add_return(func.variable('my_arg'))
     self.assertCodeEqual(as_source_code(func), """
         def myfunc(my_arg):
             return my_arg
     """)
Exemplo n.º 6
0
 def test_variable_reference_function_arg_check(self):
     scope = codegen.Scope()
     func_name = scope.reserve_name('myfunc')
     func = codegen.Function(func_name, args=['my_arg'],
                             parent_scope=scope)
     # Can't use undefined 'some_name'
     self.assertRaises(AssertionError,
                       codegen.VariableReference,
                       'some_name',
                       func)
     # But can use function argument 'my_arg'
     ref = codegen.VariableReference('my_arg', func)
     self.assertCodeEqual(as_source_code(ref), 'my_arg')
Exemplo n.º 7
0
    def test_multiple_add_assignment_in_inherited_scope(self):
        # try/if etc inherit their scope from function
        scope = codegen.Scope()
        scope.reserve_name('myfunc')
        func = codegen.Function('myfunc',
                                args=[],
                                parent_scope=scope)
        try_ = codegen.Try([], func)
        name = func.reserve_name('name')

        # We'd really like to ensure no multiple assignments ever,
        # but the way that if/try etc. work make that hard.
        # Instead, we add a keyword argument to allow the second assignment.
        try_.try_block.add_assignment(name, codegen.Number(1))
        self.assertRaises(AssertionError,
                          try_.try_block.add_assignment,
                          name, codegen.Number(2))
        self.assertRaises(AssertionError,
                          try_.except_block.add_assignment,
                          name, codegen.Number(2))
        try_.except_block.add_assignment(name, codegen.Number(2),
                                         allow_multiple=True)
Exemplo n.º 8
0
 def test_function_bad_arg(self):
     module = codegen.Module()
     func = codegen.Function('myfunc', args=['my arg'],
                             parent_scope=module.scope)
     self.assertRaises(AssertionError, as_source_code, func)