c++11 'noexcept' keyword and mocking those functions.

2,704 views
Skip to first unread message

Alex Shaver

unread,
Jul 24, 2015, 12:58:48 PM7/24/15
to Google C++ Mocking Framework
If a function has 'noexcept' keyword, I don't know if I can mock it. The initial compiling (gcc or clang) complains:

Mock_Foo.h:10:24: error: exception specification of overriding function is more lax than base version

MOCK_CONST_METHOD0(value, int());

                   ^

/gmock-svn/include/gmock/gmock-generated-function-mockers.h:687:62: note: expanded from macro 'MOCK_CONST_METHOD0'

#define MOCK_CONST_METHOD0(m, ...) GMOCK_METHOD0_(, const, , m, __VA_ARGS__)

                                                             ^

/gmock-svn/include/gmock/gmock-generated-function-mockers.h:357:37: note: expanded from macro 'GMOCK_METHOD0_'

GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \

                                  ^

Foo.h:10:17: note: overridden virtual function is here

virtual int value() const noexcept{ return _value; }

            ^

Here's the sample code used to generate:

Foo.h:

class Foo{
public:
 
Foo();
 
~Foo();


 
virtual int value() const noexcept{ return _value; }
 
virtual void setValue(const int value){ _value = value; }


private:
 
int _value;
};


class Bar{
public:
 
Bar(Foo* foo) : _foo{foo}{}
 
void doStuff(){
    _foo
->value();
   
}
private:
 
Foo* _foo;
 
};

Mock_Foo.h
#include "Foo.h"
#include "gmock/gmock.h"
class Mock_Foo : public Foo{
public:
    MOCK_CONST_METHOD0(value, int());
    MOCK_METHOD1(setValue, void(const int));
};

Foo_Test.h
#include "gtest/gtest.h"
#include "Mock_Foo.h"
TEST
(FooTest, foo){
 
Mock_Foo foo;
 
Bar bar{&foo};

  EXPECT_CALL
(foo, value());

  bar
.doStuff();
}



Corey Kosak

unread,
Aug 7, 2015, 6:51:27 PM8/7/15
to Alex Shaver, Google C++ Mocking Framework
It would be straightforward to introduce a new set of macros to support this. These new macros might look like

MOCK_NOEXCEPT_METHOD_{0 through 10}
MOCK_CONST_NOEXCEPT_METHOD_{0 through 10}

I'm not sure whether the GMock team wants to do this. They may be concerned about the doubling (and in the future, exponential growth) of the number of macros gMock defines. If you wanted to do this "for now" in your own copy of gMock I could show you how to get started.


If you're stuck and want a workaround, you could do something like this

changes to Mock_Foo.h:
    virtual int value() const noexcept{ return value_hack(); }  // forward all calls to value_hack
    MOCK_CONST_METHOD0(value_hack, int());  // mock value_hack instead of mocking value


changes to "Foo_Test.h" (I think you meant Foo_Test.cc):
  EXPECT_CALL(foo, value_hack());   // set expectations on value_hack instead of value


The above isn't pretty, and it's probably officially frowned upon, but at the very least it will get you unstuck.


--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/5994389b-1e43-4d97-b231-5d1bee609d00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Edgar

unread,
Aug 19, 2015, 7:17:17 PM8/19/15
to Google C++ Mocking Framework
On Friday, July 24, 2015 at 1:58:48 PM UTC-3, Alex Shaver wrote:
If a function has 'noexcept' keyword, I don't know if I can mock it. The initial compiling (gcc or clang) complains:

Mock_Foo.h:10:24: error: exception specification of overriding function is more lax than base version

MOCK_CONST_METHOD0(value, int());

                   ^

/gmock-svn/include/gmock/gmock-generated-function-mockers.h:687:62: note: expanded from macro 'MOCK_CONST_METHOD0'

#define MOCK_CONST_METHOD0(m, ...) GMOCK_METHOD0_(, const, , m, __VA_ARGS__)

                                                             ^

/gmock-svn/include/gmock/gmock-generated-function-mockers.h:357:37: note: expanded from macro 'GMOCK_METHOD0_'

GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \

                                  ^

Foo.h:10:17: note: overridden virtual function is here

virtual int value() const noexcept{ return _value; }

            ^

Here's the sample code used to generate:

Foo.h:

class Foo{
public:
 
Foo();
 
~Foo();


 
virtual int value() const noexcept{ return _value; }
 
virtual void setValue(const int value){ _value = value; }


private:
 
int _value;
};

 

Hi Alex:

I submitted a patch a while back which would allow:

  MOCK_QUALIFIED_METHOD0(value, const noexcept, int());

https://codereview.appspot.com/103030043/
See the test for a full example.

- Edgar

Alex Shaver

unread,
Aug 20, 2015, 10:43:46 AM8/20/15
to Google C++ Mocking Framework
Oh that's cool. If it gets into the main branch I'll have to take a look at it.
Reply all
Reply to author
Forward
0 new messages