From 5b50d47e8cd5584f22666d332d05440c5745f663 Mon Sep 17 00:00:00 2001 From: Holger Lamm Date: Wed, 20 Mar 2024 18:35:48 +0100 Subject: [PATCH] #48: Supports array and function type parameter --- hammocking/hammocking.py | 4 ++++ hammocking/templates/gmock/mockup.cc.j2 | 2 +- hammocking/templates/gmock/mockup.h.j2 | 2 +- hammocking/templates/plain_c/mockup.c.j2 | 2 +- tests/hammocking_test.py | 14 ++++++++++++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/hammocking/hammocking.py b/hammocking/hammocking.py index fe1da8d..e0431de 100755 --- a/hammocking/hammocking.py +++ b/hammocking/hammocking.py @@ -39,6 +39,10 @@ def render(self, name) -> str: res = f"{name}[{self.t.get_array_size()}]" element_type = RenderableType(self.t.get_array_element_type()) return element_type.render(res) + elif self.t.kind == TypeKind.INCOMPLETEARRAY: + res = f"{name}[]" + element_type = RenderableType(self.t.get_array_element_type()) + return element_type.render(res) elif self.t.kind == TypeKind.POINTER and self.t.get_pointee().kind == TypeKind.FUNCTIONPROTO: # param is of type function pointer pt = self.t.get_pointee() diff --git a/hammocking/templates/gmock/mockup.cc.j2 b/hammocking/templates/gmock/mockup.cc.j2 index 59975c4..e1f09e4 100644 --- a/hammocking/templates/gmock/mockup.cc.j2 +++ b/hammocking/templates/gmock/mockup.cc.j2 @@ -14,7 +14,7 @@ extern "C" { if(0 != mockup_global_ptr) return mockup_global_ptr->{{function.get_call()}}; else - return ({{function.type}})0; + return ({{function.return_type}})0; {% else %} if(0 != mockup_global_ptr) mockup_global_ptr->{{function.get_call()}}; diff --git a/hammocking/templates/gmock/mockup.h.j2 b/hammocking/templates/gmock/mockup.h.j2 index 0ce2855..2ad3516 100644 --- a/hammocking/templates/gmock/mockup.h.j2 +++ b/hammocking/templates/gmock/mockup.h.j2 @@ -13,7 +13,7 @@ class class_mockup { public: {% for function in functions %} - MOCK_METHOD(({{function.type}}), {{function.name}}, ({{function.get_param_types()}})); + MOCK_METHOD(({{function.return_type}}), {{function.name}}, ({{function.get_param_types()}})); {% endfor %} }; /* class_mockup */ diff --git a/hammocking/templates/plain_c/mockup.c.j2 b/hammocking/templates/plain_c/mockup.c.j2 index a6bd404..d76b609 100644 --- a/hammocking/templates/plain_c/mockup.c.j2 +++ b/hammocking/templates/plain_c/mockup.c.j2 @@ -9,7 +9,7 @@ {{function.get_signature()}}{ {% if function.has_return_value() %} - return 0; + return ({{function.return_type}})0; {% endif %} } /* {{function.name}} */ {% endfor %} diff --git a/tests/hammocking_test.py b/tests/hammocking_test.py index 3f17e20..cb3661a 100644 --- a/tests/hammocking_test.py +++ b/tests/hammocking_test.py @@ -76,6 +76,20 @@ def test_variadic_function(self): assert f.get_call() == "printf_func(fmt)" # TODO assert f.get_param_types() == "const char *" # ? + def test_array_param(self): + f = Function(clang_parse("void x(int arg[]);")) + assert f.name == "x" + assert f.get_signature() == "void x(int arg[])" + assert f.get_call() == "x(arg)" + assert f.get_param_types() == "int[]" + + def test_funcptr_param(self): + f = Function(clang_parse("void x(int (*cb)(void));")) + assert f.name == "x" + assert f.get_signature() == "void x(int (*cb)())" + assert f.get_call() == "x(cb)" + assert f.get_param_types() == "int (*)(void)" + class TestMockupWriter: def test_empty_templates(self):