Skip to content

Commit

Permalink
#48: Supports array and function type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
theHolgi committed Mar 20, 2024
1 parent d79bb49 commit 5b50d47
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions hammocking/hammocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion hammocking/templates/gmock/mockup.cc.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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()}};
Expand Down
2 changes: 1 addition & 1 deletion hammocking/templates/gmock/mockup.h.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
2 changes: 1 addition & 1 deletion hammocking/templates/plain_c/mockup.c.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

{{function.get_signature()}}{
{% if function.has_return_value() %}
return 0;
return ({{function.return_type}})0;
{% endif %}
} /* {{function.name}} */
{% endfor %}
14 changes: 14 additions & 0 deletions tests/hammocking_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 5b50d47

Please sign in to comment.