Giter Club home page Giter Club logo

c-mock's People

Contributors

cjmay avatar dprotti avatar hjagodzinski avatar krzbe avatar mnisblee avatar muffo avatar ugoren avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

c-mock's Issues

Code and tests does not comile

Hi!
First of all I've to say that I like your extension very much.
It seems that not the latest version does not compile (even your tests):
C-Mock/test$ make

cc -c -fPIC math.c -o math.o
cc -shared -Wl,-soname,/home/roman/Documents/trainings/c/c-modern/libs/C-Mock/test/libmath.so -o libmath.so math.o
g++ -c -I'/home/roman/Documents/trainings/c/c-modern/libs/C-Mock/include' -I../../googletest/googlemock/include -I../../googletest/googletest/include add_function_mock.cc -o add_function_mock.o
In file included from /home/roman/Documents/trainings/c/c-modern/libs/C-Mock/include/cmock/cmock.h:38,
                 from add_function_mock.h:4,
                 from add_function_mock.cc:1:
/home/roman/Documents/trainings/c/c-modern/libs/C-Mock/include/cmock/cmock-function-mockers.h:193:27: error: expected identifier before ‘,’ token
  193 |     typedef GMOCK_RESULT_(, F) (*func_type)(GMOCK_ARG_(, 1, F) cmock_a1, \
      |                           ^
add_function_mock.h:8:1: note: in expansion of macro ‘DECLARE_FUNCTION_MOCK2’
    8 | DECLARE_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
      | ^~~~~~~~~~~~~~~~~~~~~~
/home/roman/Documents/trainings/c/c-modern/libs/C-Mock/include/cmock/cmock-function-mockers.h:193:33: error: expected identifier before ‘*’ token
  193 |     typedef GMOCK_RESULT_(, F) (*func_type)(GMOCK_ARG_(, 1, F) cmock_a1, \
      |                                 ^
add_function_mock.h:8:1: note: in expansion of macro ‘DECLARE_FUNCTION_MOCK2’
    8 | DECLARE_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));

Version of my compiler:
$ g++ --version

g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Version of googletest: actual master for day of 29.03.2021.

Allow mocking a function that isn't linked into the exe

When linking a test executable, we sometimes don't want to link it with all the libraries that are used in production. Mocking is very useful for this - if you mock all functions in a library, you don't need to link with the library itself. But C-mock searches for the real function with dlsym, and fails if it doesn't exist. So you have to link with it.

In my company linkage is a mess, so we have a local change that checks if the function exists only if we actually try to call it. This is very useful for us.
You can see the code in a branch in my clone.

But this changes behavior unconditionally, so I don't think it's appropriate for merge.

The only difficulty is how can we make it conditional. There are may alternatives, such as:

  • Compile time switch (e.g. make ALLOW_MISSING=1).
  • A separate set of macros (e.g. CMOCK_MOCK_FUNCTIONn_ALLOW_MISSING).
  • When using a mock class, a variation of CMockMocker (e.g. CMockMockerAllowMissing).
  • A macro defined by used before defining mocks (e.g. #define CMOCK_ALLOW_MISSING 1).

Can we add this functionality?

Thanks.

[BUG] An error occurred when the constructor of a global instance called the mock method

Hello,

I am testing C-Mock extension. Everything seems good. I can mock C type functions.

But, one of this mocked functions is called from a class's constructor. When I define a global instance, I get error from cmock-function-mockers.h, line=100. (error msg= Neither mock nor real function is present)

I made a change as below. It fixed my problem. Is there any better solution that you recommend?

image

Cygwin build environment Compatability

Hello
Is it possible to make this adapted to Cygwin environment? As The RTLD_NEXT is not supported in Cygwin it is not possible to use it in cygwin environment. Any plan to port it to windows also will be helpful.

Here is the build log
image

C-Mock is not working with googlemock 1.7

Hi,
I'm very interested in C-Mock to use it for an embedded system. The only problem is it doesn't work with googlemock 1.7. I had a look to try to adapt your current code with no success.
Could you try to fix it or advise me about it please ?
Manuel Teillet.

Compilation Errors when using Google test version 1.10

I am using google test as a framework for my unit testing. And I am also studying ways on how unit tests can be made as simple as possible and that's where I encountered this extension.
However when I used it for testing C functions there are build errors I encountered when compiling my test program
compile_error.log

It failed to compile my source file wlan_lib_mocker.cpp:
`#include "wlan_lib_mocker.h"

CMOCK_MOCK_FUNCTION1(wlanlibmocker, getResponse, int(int));`

wlan_lib_mocker.h contains:
`#ifndef WLAN_LIB_MOCKER_H
#define WLAN_LIB_MOCKER_H

#include <cmock/cmock.h>

#include "wlan_lib.h"

class wlanlibmocker : public CMockMocker<wlanlibmocker>
{
public:
MOCK_METHOD1(getResponse, int (int));
};

#endif /* WLAN_LIB_MOCKER_H */`

The wlan_lib.h header file contains just the function prototype for getResponse()

I am not sure if I am doing something wrong in here or there is something I've missed based on the instruction given on how to use C Mock. Other than this the main.cpp file just contains the main function which will run googletest's macro for running all tests and a test_driver.cpp for creating test cases.

Can't find the real function

I raised my google test version from 1.7.0 to 1.10.0.
and I also raised CMock file version from 0.1.0 to 0.3.1

Previously my test was running well. But after update the version, I got the error like below.
terminate called after throwing an instance of 'std::logic_error'
what(): Error: Function fopen not found. Neither mock nor real function is present.

How can I solve this problem?

It was worked before with
DECLARE_FUNCTION_MOCK2(fopen_Mock, fopen, FILE *(const char *, const char *));
IMPLEMENT_FUNCTION_MOCK2(fopen_Mock, fopen, FILE *(const char *, const char *));

and define the ACTION as below
ACTION(CHECK_PATH)
{
char str[500] = {0, };
if(strncmp(arg0, "/proc", 4) == 0 || strncmp(arg0, "/dev", 4) == 0 || strncmp(arg0, "/", 1) != 0)
{
return fopen_Mock::real(arg0, arg1);
}
memcpy(str, ".", 1);
memcpy(str+1, arg0, strlen(arg0));
return fopen_Mock::real(str, arg1);
}

Compilation errors when using latest Google Test (post 1.8.0)

Starting with google/googletest@fe402c2, C-Mock fails to build when using IMPLEMENT_FUNCTION_MOCKn.

Example from self tests:

In file included from /C-Mock/include/cmock/cmock.h:38:0,
                 from add_function_mock.h:4,
                 from add_function_mock.cc:1:
add_function_mock.cc: In member function 'testing::internal::MockSpec<int(int, int)>& AddFunctionMock::cmock_func(const testing::Matcher<int>&, const testing::Matcher<int>&)':
/C-Mock/include/cmock/cmock-function-mockers.h:241:42: error: invalid initialization of non-const reference of type 'testing::internal::MockSpec<int(int, int)>&' from an rvalue of type 'testing::internal::MockSpec<int(int, int)>'
     return mocker.With(cmock_a1, cmock_a2); \
                                          ^
add_function_mock.cc:3:1: note: in expansion of macro 'IMPLEMENT_FUNCTION_MOCK2'
 IMPLEMENT_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
 ^
Makefile:21: recipe for target 'add_function_mock.o' failed
make[1]: *** [add_function_mock.o] Error 1
make[1]: Leaving directory '/C-Mock/test'
Makefile:16: recipe for target 'test/cmock_test' failed
make: *** [test/cmock_test] Error 2

error: ‘std::tr1’ has not been declared

I am getting this error when I am doing a make within the C-Mock folder.

231 | GTEST_COMPILE_ASSERT_(::std::tr1::tuple_size<
| ^~~
add_function_mock.cc:3:1: note: in expansion of macro ‘IMPLEMENT_FUNCTION_MOCK2’
3 | IMPLEMENT_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
| ^~~~~~~~~~~~~~~~~~~~~~~~
/home/nmohan/C-Mock/include/cmock/cmock-function-mockers.h:232:52: error: expected primary-expression before ‘>’ token
232 | ::testing::internal::Function::ArgumentTuple>::value == 2,
| ^
add_function_mock.cc:3:1: note: in expansion of macro ‘IMPLEMENT_FUNCTION_MOCK2’
3 | IMPLEMENT_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
| ^~~~~~~~~~~~~~~~~~~~~~~~
/home/nmohan/C-Mock/include/cmock/cmock-function-mockers.h:232:55: error: ‘::value’ has not been declared
232 | ::testing::internal::Function::ArgumentTuple>::value == 2,
| ^~~~~
add_function_mock.cc:3:1: note: in expansion of macro ‘IMPLEMENT_FUNCTION_MOCK2’
3 | IMPLEMENT_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
| ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/nmohan/C-Mock/include/cmock/cmock.h:38,
from add_function_mock.h:4,
from add_function_mock.cc:1:
add_function_mock.cc: In member function ‘testing::internal::MockSpec<int(int, int)>& AddFunctionMock::cmock_func(const testing::Matcher&, const testing::Matcher&)’:
/home/nmohan/C-Mock/include/cmock/cmock-function-mockers.h:241:23: error: cannot bind non-const lvalue reference of type ‘testing::internal::MockSpec<int(int, int)>&’ to an rvalue of type ‘testing::internal::MockSpec<int(int, int)>’
241 | return mocker.With(cmock_a1, cmock_a2);
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
add_function_mock.cc:3:1: note: in expansion of macro ‘IMPLEMENT_FUNCTION_MOCK2’
3 | IMPLEMENT_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
| ^~~~~~~~~~~~~~~~~~~~~~~~
make[1]: *** [Makefile:19: add_function_mock.o] Error 1
make[1]: Leaving directory '/home/nmohan/C-Mock/test'
make: *** [Makefile:16: test/cmock_test] Error 2

$gcc --version
gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Suggested alternative usage, more similar to Google Mock

Hello,
I was shown C-Mock and find it a very useful extension to Google Mock.
When using it, I was a little annoyed by the need to define a separate mock class for each mocked function.
I believe, based on reading the code and my experience in messing with Google Mock under the hood, that better usage is possible, and I can implement it.

Here's my suggestion:

In the header file, declare a mock class, similarly to Google Mock, where each mocked function is a method:

class FooFunctionMock : FunctionMocker<FooFunctionMock>
{
public:
     MOCK_FUNCTION2(foo, int, (int, int));
     MOCK_FUNCTION1(bar, int, (int));
};

The FunctionMocker class will allow access to the FooFunctionMock instance (similarly to the instance pointer).
MOCK_FUNCTIONn will be similar to MOCK_METHODn, but will declare a real_fname member and call_fname method.

In the mock source file, use IMPLEMENT_FUNCTION_MOCK similarly to today:

IMPLEMENT_FUNCTION_MOCK2(FooFunctionMock, foo, int(int, int)); 
IMPLEMENT_FUNCTION_MOCK1(FooFunctionMock, bar, int(int));

Unlike the current macro, it won't implement a constructor and destructor, just real_fname and call_fname. So there's no problem in using it multiple times with the same class name.
I guess we'll need to rename it, to avoid conflict with the existing macro.

Expectations will be set using EXPECT_CALL, just like any Google Mock class.

Add release for current code

Please add a tagged release for the current version on master, so one can use it as CMake dependency easily along with GTest 1.10.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.