1

CppUTest documentations says

MockSupportPlugin makes the work with mocks easier. It does the following work for you automatically:

  • checkExpectations at the end of every test (on global scope, which goes recursive over all scopes)
  • clear all expectations at the end of every test
  • install all comparators that were configured in the plugin at the beginning of every test
  • remove all comparators at the end of every test

ref: https://cpputest.github.io/plugin_manual.html

I tried the following example:

#include "CppUTest/TestRegistry.h"
#include "CppUTestExt/MockSupportPlugin.h"

MyDummyComparator dummyComparator;
MockSupportPlugin mockPlugin;

mockPlugin.installComparator("MyDummyType", dummyComparator);
TestRegistry::getCurrentRegistry()->installPlugin(&mockPlugin);

with my added MYDummyComparator:

class MyDummyComparator : public MockNamedValueComparator
{
   bool isEqual( const void *object1, const void *object2 )
   {
      return object1 == object2;
   }

   SimpleString valueToString( const void *object )
   {
      return SimpleString();
   }
} dummyComparator;

But when I remove expectOneCall() or expectNCalls() from my tests, it shows the tests failed. How do I use MockSupportPlugin from CPPUTest to achieve doing "checkExpectations at the end of every test (on global scope, which goes recursive over all scopes)" automatically?

andca
  • 33
  • 5

1 Answers1

0

The mock type comparators would be used in your mock comparisons.

For example, you need to compare a struct of type Point, which looks like this:

struct Point {
    int    x;
    int    y;
};

You would define your comparator like this:

class PointTypeComparator : public MockNamedValueComparator
{
public:
    bool isEqual(const void* object1, const void* object2) override
    {
        // Casting here the void pointers to the type to compare
        const auto *pointObject1 = (const Point *) object1; 
        const auto *pointObject2 = (const Point *) object2;

        // Perform comparison, in this case, comparing x and y
        return ((pointObject1->x == pointObject2->x)
                && (pointObject1->y == pointObject2->y);
    }
    virtual SimpleString valueToString(const void* object)
    {
        return (char *) "string";
    }
};

Next, within you test group, you need to install these comparators in the setup and also in the teardown clear them:

TEST_GROUP(MyTest)
{
    void setup()
    {
        PointTypeComparator pointComparator;
        mock().installComparator("Point *", pointComparator);  // Note, its a pointer to a Point type
    }

    void teardown()
    {
        // Call check expectations here, and also clear all comparators after that
        mock().checkExpectations();
        mock().clear();
        mock().removeAllComparatorsAndCopiers();
    }
};

Next, you can use this Comparator, using the withParameterOfType function as:

mock().expectOneCall("foo")
    .withParameterOfType("Point *", "name", &address); // Here name is the name of variable, and &address is the address of the Point type variable.
mmcblk1
  • 158
  • 1
  • 3
  • 10
  • do I need a comparitor for each mock? – andca Mar 30 '20 at 20:07
  • You need a comparator for a specific `type`. Once you have it installed, you can use it every time you need to compare the `type`. – mmcblk1 Mar 30 '20 at 20:40
  • I was expecting using the comparator can avoid repeatedly calling expectOneCall and expectNCalls, but seemed like that's not the case. And I don't really get what the comparator is for actually, like what are really being compared? – andca Apr 03 '20 at 15:55
  • A comparator is used when you need to compare a `ParameterOfType` within your mock. – mmcblk1 Apr 03 '20 at 16:22
  • You probably need to make `pointComparitor` static as it will be destroyed when `setup()` returns ... From the CppUTest manual... "Warning 1: Pay attention to the scope of your comparator variable! Comparators are not copied, instead it uses the exact instance as passed to the installComparator function. So make sure it is still in-scope when the framework tries to use it! For example, if you installComparator inside the TEST, but do the checkExpectations in the teardown, then it is likely to cause a crash since the comparator has been destroyed." – Anonymouse Jun 30 '22 at 07:52