Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced generic macros & means to call a real function #22

Merged
merged 2 commits into from
Apr 17, 2021

Conversation

hjagodzinski
Copy link
Owner

@hjagodzinski hjagodzinski commented Apr 8, 2021

This PR introduces two generic macros CMOCK_MOCK_METHOD and CMOCK_MOCK_FUNCTION. The former wraps around MOCK_METHOD extending the CMockMocker class with a static member holding a pointer to a real function. This pointer can be accessed with the CMOCK_REAL_FUNCTION macro. Example:

#include <cmock/cmock.h>

#include "math.h"

class MathMocker : public CMockMocker<MathMocker>
{
public:
    CMOCK_MOCK_METHOD(int, add, (int, int));
    CMOCK_MOCK_METHOD(int, substract, (int, int));
};

CMOCK_MOCK_FUNCTION(MathMocker, int, add, (int, int));
CMOCK_MOCK_FUNCTION(MathMocker, int, substract, (int, int));

{
    MathMocker mock;

    EXPECT_CALL(mock, add(1, 1)).WillOnce(Return(11));
    ASSERT_EQ(11, add(1, 1)); // calling the mock

    EXPECT_CALL(mock, substract(1, 2)).WillOnce(Return(12));
    ASSERT_EQ(12, substract(1, 2)); // calling the mock

  ASSERT_EQ(2, CMOCK_REAL_FUNCTION(MathMocker, add)(1, 2)); // calling the real function
}

ASSERT_EQ(2, add(1, 1)); // calling the real function
ASSERT_EQ(-1, substract(1, 2)); // calling the real function

Resolves #17.

@hjagodzinski hjagodzinski merged commit d062610 into master Apr 17, 2021
@hjagodzinski hjagodzinski deleted the generic-macro branch April 17, 2021 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to call real function with CMOCK_MOCK_FUNCTIONn macros
1 participant