def generate_function(self, name: Text, level: int = 0, param_count: int = None) -> ast.Function: if param_count is None: param_count = int(math.ceil(self.rng.weibullvariate(1, 1.15) * 4)) params = self._generate_params(param_count) body, body_type = self._generate_body(level, params) return ast.Function(self.m, self.fake_span, ast.NameDef(self.m, self.fake_span, name), parametric_bindings=(), params=params, return_type=body_type, body=body, public=False)
def test_identity_function(self): m = cpp_ast.Module('test') name_def_x = cpp_ast.NameDef(m, self.fake_span, 'x') name_ref_x = cpp_ast.NameRef(m, self.fake_span, 'x', name_def_x) type_u32 = cpp_ast.BuiltinTypeAnnotation(m, self.fake_span, cpp_ast.BuiltinType.U32) param_x = cpp_ast.Param(m, name_def_x, type_u32) name_def_f = cpp_ast.NameDef(m, self.fake_span, 'f') params = (param_x, ) f = cpp_ast.Function(m, self.fake_span, name_def_f, (), params, type_u32, name_ref_x, public=False) self.assertEqual(str(f), 'fn f(x: u32) -> u32 {\n x\n}')