C ++ mock framework capable of making fun of non-virtual C methods and functions

I know that the first part of this question was asked before , but it was a long time ago :). I was wondering if the time means when any of the mocking open source frameworks caught up with Typemock Isolator ++ when it comes to ridicule of non-virtual C methods and functions. I'm most interested in gcc on Linux. Until now, I was interested in mocking accessors (so that I can simulate the states in the mocked object - see below) and replacing C functions from other libraries (select, pcap_ *, etc.).

class Foo { public: ... bool IsCondition() { return condition; }; ... private: bool condition; } // I want a framework that allows me to do something like this: TEST(TestFoo) { MOCK_INTERFACE(Foo) mock_foo; EXPECT_CALL(mock_foo, IsCondition).returns(true); EXPECT(mock_foo.IsCondition()); } 
+4
source share
2 answers

GMock supports what they call hi-perf dependency injection for mocking non-virtual methods .

The bottom line is to use the templates:

 template <class PacketStream> void CreateConnection(PacketStream* stream) { ... } template <class PacketStream> class PacketReader { public: void ReadPackets(PacketStream* stream, size_t packet_num); }; 

Then you can use CreateConnection () and PacketReader in the production code and use the CreateConnection () and PacketReader tests in the tests.

For C functions, they recommend interfaces, so probably not what you want. However, if you have separate libraries, you can always refer to the test library, which contains functions with the same signatures as the deployment library. You could even do it dynamically with LD_PRELOAD if you felt especially brave. That sounds like a lot of links to me.

Cxxtest , if you look in section 8.1, advanced functions support some macros to simplify the use / creation of interfaces:

From this link:

 CXXTEST_MOCK_GLOBAL( time_t, /* Return type */ time, /* Name of the function */ ( time_t *t ), /* Prototype */ ( t ) /* Argument list */ ); 8.1.2. Mock Functions in Tested Code 

Validated code uses macrobial global functions, rather than directly using global functions. You get access to the layout of functions in the T namespace (for the test), so the tested code calls T :: time () instead of time (). This is the equivalent of using abstract interfaces instead of concrete classes.

 // rand_example.cpp #include <time_mock.h> int generateRandomNumber() { return T::time( NULL ) * 3; } 

I was very lucky with the previous Cxxtest method.

+5
source

With recent GCC (e.g. 4.6), you can write a C plugin or extension in MELT for this purpose.

However, in order to configure GCC (with a C plugin or extension in MELT), you need to partially understand its internal representations (Gimple and Tree-s), which takes time (probably more than a week of work). Thus, this approach makes sense if you have a sufficiently large code base to do your best.

0
source

Source: https://habr.com/ru/post/1391500/


All Articles