0

I have a function that returns a gsl::span of a struct object. I have written a mock for this as bellow.

class AppleMock:public Apple
{
public:
    constexpr AppleMock(gsl::span<const tree> _trees) noexcept
        : Apple(_trees)
    {
    }

    gsl::span<const trees> getTrees() const noexcept
    {
        mock("Apple").actualCall("getTrees")
                                 .withOutputParameterOfType("gsl::span<const tree>", "trees", trees);
    }

 };

A tree object looks like this

struct tree
{
    const char*      name;
    water_func       start;
    int              temperature;
};

Since gsl::span<const tree> is a custom return type, I wrote a custom copier as bellow.

class TreeSpanTypeCopier : public MockNamedValueCopier
{
public:
    virtual void copy(void* out, const void* in)
    {
        *(gsl::span<const tree>*)out = *(const gsl::span<const tree>*)in;
    }
};

I installed my custom copier in the test file as below

    TEST_GROUP (appleGarden)
{
    void setup()
    {
        mock().disable();

        initAppleGarden();

        mock().enable();

        TreeSpanTypeComparator treeSpanComparator;
        mock().installComparator("gsl::span<const tree>", treeSpanComparator);

        TreeSpanTypeCopier treeSpanCopier;
        mock().installCopier("gsl::span<const tree>", treeSpanCopier);
    }

    void teardown()
    {
        mock().removeAllComparatorsAndCopiers();
    }
};

However, I get an error as below,

ut/AppleMock.hpp:24:97: fatal error: no viable conversion from 'const gsl::span' to 'void *' .withOutputParameterOfType("gsl::span", "trees", trees);

                                            ^~~~~~

../../ext/lfs/cpputest/clang64/include/CppUTestExt/MockActualCall.h:62:117: note: passing argument to parameter 'output' here virtual MockActualCall& withOutputParameterOfType(const SimpleString& typeName, const SimpleString& name, void* output)=0;

  1. Is this method correct?
  2. Or should I update the custom copier so that it will deep copy the members of each object for each element of the span?
  3. Is there a simpler way to Output a span of objects?

0 Answers0