eranpeer / FakeIt

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

‘operator()’ is not a member of ‘std::remove_reference<OperatorBoolTests::SomeInterface&>::type #134

Closed vvvlc closed 2 years ago

vvvlc commented 6 years ago

Hi I was trying to use FakeIt in my project and I stumbled on compilation error of this sample (I made a test case for you)

#include <string>

#include "tpunit++.hpp"
#include "fakeit.hpp"

using namespace fakeit;

struct OperatorBoolTests : tpunit::TestFixture {

    OperatorBoolTests() :
    tpunit::TestFixture(
    TEST(OperatorBoolTests::enum_class_arg)
    )
    {
    }

    struct SomeInterface {
        virtual operator bool() = 0;
    };

    void enum_class_arg() {
        Mock<SomeInterface> mock;
        //When(Method(mock, operator())).AlwaysReturn(true);
        When(OverloadedMethod(mock, operator(), bool(void))).AlwaysReturn(true);

        mock.get().operator bool();
        //Verify(OverloadedMethodMethod(mock, operator(), bool(void))).Exactly(1);
    }

} __OperatorBoolTests;

I get compilation error

In member function ‘void OperatorBoolTests::enum_class_arg()’:
cc1plus: error: ‘operator()’ is not a member of ‘std::remove_reference<OperatorBoolTests::SomeInterface&>::type {aka OperatorBoolTests::SomeInterface}’

for line

When(OverloadedMethod(mock, operator(), bool(void))).AlwaysReturn(true);

When I comment out

//When(OverloadedMethod(mock, operator(), bool(void))).AlwaysReturn(true);

compilation succeed, but test fails on mock.get().operator bool(); with this stack trace

(gdb) backtrace
#0  0x00007ffff76feedd in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x000055555580e0d3 in fakeit::StandaloneAdapter::handle(fakeit::UnexpectedMethodCallEvent const&) (
    this=0x555555bbb1e0 <fakeit::StandaloneFakeit::getInstance()::instance+64>, evt=...) at ../config/standalone/StandaloneFakeit.hpp:77
#2  0x000055555580c59e in fakeit::FakeitContext::handle(fakeit::UnexpectedMethodCallEvent const&) (
    this=0x555555bbb1a0 <fakeit::StandaloneFakeit::getInstance()::instance>, e=...) at ../include/fakeit/FakeitContext.hpp:27
#3  0x00005555555f98ec in fakeit::MockImpl::unmocked (this=0x555555bdc190) at ../include/fakeit/MockImpl.hpp:233
#4  0x00005555555f9265 in OperatorBoolTests::enum_class_arg (this=0x555555bbb843 <__OperatorBoolTests>) at ../tests/operatorbool_tests.cpp:36
#5  0x000055555565b3d1 in tpunit::TestFixture::__do_tests (f=0x555555bce110) at ../tests/tpunit++.hpp:500
#6  0x000055555565b166 in tpunit::TestFixture::__do_run () at ../tests/tpunit++.hpp:360
#7  0x000055555565b503 in tpunit::Tests::Run () at ../tests/tpunit++.hpp:549
#8  0x000055555565b50e in main () at ../tests/tpunit++main.cpp:30

Is this kind of operator supported by FakeIt? Thx Vitek

FranckRJ commented 2 years ago

It seems to work if you use "operator bool" as the method name.

https://gcc.godbolt.org/z/T7hEn1xTT