def test_function_call_bad_kwarg_names(self): module = codegen.Module() module.scope.reserve_name('a_function') allowed_args = [ # (name, allowed) pairs. # We allow reserved names etc. because we can # call these using **{} syntax ('hyphen-ated', True), ('class', True), ('True', True), (' pre_space', False), ('post_space ', False), ('mid space', False), ('valid_arg', True), ] for arg_name, allowed in allowed_args: func_call = codegen.FunctionCall('a_function', [], {arg_name: codegen.String("a")}, module.scope) if allowed: output = as_source_code(func_call) self.assertNotEqual(output, '') if not allowable_name(arg_name): self.assertIn('**{', output) else: self.assertRaises(AssertionError, as_source_code, func_call)
def test_function_call_sensitive(self): module = codegen.Module() module.scope.reserve_name('a_function') func_call = codegen.FunctionCall('a_function', [], {}, module.scope) # codegen should refuse to create a call to 'exec', there is no reason # for us to generate code like that. func_call.function_name = 'exec' self.assertRaises(AssertionError, as_source_code, func_call)
def test_function_call_kwarg_star_syntax(self): module = codegen.Module() module.scope.reserve_name('a_function') func_call = codegen.FunctionCall('a_function', [], {"hyphen-ated": codegen.Number(1)}, module.scope) self.assertCodeEqual(as_source_code(func_call), """ a_function(**{'hyphen-ated': 1}) """)
def test_function_call_bad_name(self): module = codegen.Module() module.scope.reserve_name('a_function') func_call = codegen.FunctionCall('a_function', [], {}, module.scope) func_call.function_name = 'bad function name' self.assertRaises(AssertionError, as_source_code, func_call)
def test_function_call_args_and_kwargs(self): module = codegen.Module() module.scope.reserve_name('a_function') func_call = codegen.FunctionCall('a_function', [codegen.Number(123)], {'x': codegen.String("hello")}, module.scope) self.assertCodeEqual(as_source_code(func_call), "a_function(123, x='hello')")
def test_function_call_known(self): module = codegen.Module() module.scope.reserve_name('a_function') func_call = codegen.FunctionCall('a_function', [], {}, module.scope) self.assertCodeEqual(as_source_code(func_call), "a_function()")