def test_build_function_ignore_arguments(self): from pysd.translation.builder import build_function_call args = ['a', 'b', 'c'] my_func_conf = { "name": "my_func", "parameters": [ {"name": "A"}, {"name": "B", "type": "ignore"}, {"name": "C"} ] } self.assertEqual(build_function_call(my_func_conf, args), "my_func(a, c)") my_func_conf = { "name": "my_func", "parameters": [ {"name": "A", "type": "ignore"}, {"name": "B"}, {"name": "C", "type": "ignore"} ] } self.assertEqual(build_function_call(my_func_conf, args), "my_func(b)")
def test_build_function_predef_arguments(self): from pysd.translation.builder import build_function_call args = ['a', 'c'] my_func_conf = { "name": "my_func", "parameters": [ {"name": "A"}, {"name": "0", "type": "predef"}, {"name": "C"} ] } self.assertEqual(build_function_call(my_func_conf, args), "my_func(a, 0, c)") my_func_conf = { "name": "my_func", "parameters": [ {"name": "time_step()", "type": "predef"}, {"name": "B"}, {"name": "1", "type": "predef"} ] } self.assertEqual(build_function_call(my_func_conf, ["b"]), "my_func(time_step(), b, 1)")
def test_build_function_optional_arguments(self): from pysd.translation.builder import build_function_call my_func_conf = { "name": "my_func", "parameters": [ {"name": "A"}, {"name": "B"}, {"name": "C", "optional": True} ] } self.assertEqual(build_function_call(my_func_conf, ['a', 'b', 'c']), "my_func(a, b, c)") self.assertEqual(build_function_call(my_func_conf, ['a', 'b']), "my_func(a, b)") # optional lambda argument, check optional + type my_func_conf = { "name": "my_func", "parameters": [ {"name": "A"}, {"name": "B"}, {"name": "C", "type": "lambda", "optional": True} ] } self.assertEqual(build_function_call(my_func_conf, ['a', 'b', 'c']), "my_func(a, b, lambda: c)") self.assertEqual(build_function_call(my_func_conf, ['a', 'b']), "my_func(a, b)")
def test_build_function_not_implemented(self): from warnings import catch_warnings from pysd.translation.builder import build_function_call args = ['a', 'b'] nif = {"name": "not_implemented_function", "module": "functions", "original_name": "NIF"} with catch_warnings(record=True) as ws: self.assertEqual(build_function_call(nif, args), "not_implemented_function('NIF',a,b)") self.assertEqual(len(ws), 1) self.assertTrue("Trying to translate NIF which it is " + "not implemented on PySD." in str(ws[0].message))
def test_build_function_with_time_dependency(self): from pysd.translation.builder import build_function_call args = ['a', 'b'] pulse = { "name": "pulse", "parameters": [ {"name": "time", "type": "time"}, {"name": "start"}, {"name": "duration"}, ], "module": "functions", } dependencies = {'a': 1, 'b': 2} self.assertNotIn('time', dependencies) self.assertEqual(build_function_call(pulse, args, dependencies), "pulse(__data['time'], a, b)") self.assertIn('time', dependencies)