def test_function_apply_args(self): def typed_expr(type_obj): class Expr(codegen.Expression): type = type_obj def constrain_type(self, type_obj, from_ftl_source=None): self.type = type_obj return Expr() function = types.Function.for_multiple_inputs( [dtypes.String, dtypes.Number], dtypes.Bool) zero_applied = function.apply_args([]) self.assertEqual(zero_applied.as_signature(codegen.Module()), "String -> number -> Bool") self.assertEqual(zero_applied, function) one_applied = function.apply_args([typed_expr(dtypes.String)]) self.assertEqual(one_applied.as_signature(codegen.Module()), "number -> Bool") self.assertEqual(one_applied, types.Function(dtypes.Number, dtypes.Bool)) two_applied = function.apply_args( [typed_expr(dtypes.String), typed_expr(dtypes.Number)]) self.assertEqual(two_applied.as_signature(codegen.Module()), "Bool") self.assertEqual(two_applied, dtypes.Bool)
def test_function_call_args(self): scope = codegen.Scope() scope.reserve_name("aFunction", type=types.Function(dtypes.Number, dtypes.String)) func_call = codegen.FunctionCall(scope.variables["aFunction"], [codegen.Number(123)]) self.assertCodeEqual(func_call.as_source_code(), "aFunction 123")
def test_function_signature(self): self.assertEqual( types.Function(dtypes.String, dtypes.Number).as_signature(codegen.Module()), "String -> number", ) self.assertEqual( types.Function.for_multiple_inputs([dtypes.String, dtypes.Number], dtypes.Bool).as_signature( codegen.Module()), "String -> number -> Bool", )
def test_empty_record_signature_in_function(self): function = types.Function(types.Record(), types.UnconstrainedType()) self.assertEqual(function.as_signature(codegen.Module()), "a -> b")
def test_function_call_using_apply(self): scope = codegen.Scope() scope.reserve_name("aFunction", type=types.Function(dtypes.Number, dtypes.String)) func_call = scope.variables["aFunction"].apply(codegen.Number(123)) self.assertCodeEqual(func_call.as_source_code(), "aFunction 123")