eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.23k stars 169 forks source link

Usage with CppUTest: placement-new issues #177

Open sktpin opened 5 years ago

sktpin commented 5 years ago

Hey,

I am trying to use FakeIt with CppUTest. For now, using config/standalone. Compiling gets me this error: include/mockutils/DynamicProxy.hpp:175:21: error: 'dataMember' does not name a type new(dataMember) DATA_TYPE{initargs ...};

More context:

        template<typename DATA_TYPE, typename ... arglist>
        class DataMemeberWrapper : public Destructible {
        private:
            DATA_TYPE *dataMember;
        public:
            DataMemeberWrapper(DATA_TYPE *dataMem, const arglist &... initargs) :
                    dataMember(dataMem) {
                new(dataMember) DATA_TYPE{initargs ...}; // ### offending line ###
            }

            ~DataMemeberWrapper() override
            {
                dataMember->~DATA_TYPE();
            }
        };

I only did rudimentary template programming, and am especially not too familiar with C++11 stuff in that regard. I don't get the syntax: what is _new(pointerVariable) DATATYPE {initargs ...}; supposed to do, esp. the (pointerVariable) part? And why does GCC (6.3) think this should be a type? Edit: Nevermind that, I had all but forgotten about placement new, which I used once, ~ 10 years ago ;)

I suspect that the error still hs something to do with CppUTest's redefinitions of operator new for memory leak detection. Although I had a bazillion more errors before following the hint on the CppUTest page about that, and made a header file which contains #include's for all C++ headers before including their "new" overload. And that file in turn, gets included before I include "fakeit.h", reducing the errors down to this one. And all my file which includes fakeit does is, well, including it. Everything else related to fakeit is commented out.

Any ideas what needs to be done?

sktpin commented 5 years ago

I found this: cpputest issues

I followed the hack they mentioned of putting "#undef new" at the top of the file, in this case the single- header version I'm now using. That compiles so far. Haven't used it further, but will try soon. I inteded to make a config for CppUTest which I could share if anyone cares, then again, all this probably doesn't sound too enticing. CppUTest does have mocking facilities, but unlike FakeIt, they tried to avoid "too new C++" and it involves comparisons of strings to address things, and much more typing, which is why FakeIt looked attractive in the first place.