Re: [googlemock: 1749] How to MOCK functions returning reference to a STL container

510 views
Skip to first unread message

liam mail

unread,
Feb 18, 2013, 12:25:13 PM2/18/13
to Partha Biswas, googl...@googlegroups.com
On 18 February 2013 09:38, Partha Biswas <pbisw...@gmail.com> wrote:
> Hi,
>
> Kindly consider the following piece of code :
>
> class A
> {
> public:
> virtual std::map<int, int>& getthedata() = 0;
> };
>
> class AMock : public A
> {
> public:
> MOCK_METHOD0(getthedata, std::map<int,int>&());
> };
>
> This gives a compilation error
> error: macro "MOCK_METHOD0" passed 3 arguments, but takes just 2
>
> I realized that if i mock as shown below the compilation goes through:
>
> class AMock : public A
> {
> public:
> typedef std::map<int, map> mapdata;
> MOCK_METHOD0(getthedata, mapdata&());
> };
>
> My question : Are there any other way to solve this?
>
> --
>
> ---
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

There are only two methods worth noting to ignore the commas in the
template type in a macro. One is as you have listed (using a typedef)
and the other being that you enclose the template type inside
parentheses, although for this method to work in most situations then
the macro had to have been written with this in mind as the
parentheses are not removed when the macro is expanded.

--Liam

Billy Donahue

unread,
Feb 18, 2013, 1:44:17 PM2/18/13
to liam mail, Partha Biswas, googl...@googlegroups.com


On Monday, February 18, 2013, liam mail wrote:
On 18 February 2013 09:38, Partha Biswas <pbisw...@gmail.com> wrote:
> Hi,
>
> Kindly consider the following piece of code :
>
> class A
> {
>         public:
>                  virtual std::map<int, int>& getthedata() = 0;
> };
>
> class AMock : public A
> {
>         public:
>                    MOCK_METHOD0(getthedata, std::map<int,int>&());
> };
>
> This gives a compilation error
> error: macro "MOCK_METHOD0" passed 3 arguments, but takes just 2
>
> I realized that if i mock as shown below the compilation goes through:
>
> class AMock : public A
> {
>         public:
>                  typedef std::map<int, map> mapdata;
>                  MOCK_METHOD0(getthedata, mapdata&());
> };
>
> My question : Are there any other way to solve this?

That way looks good, here's another. A macro that just holds the comma:

#define comma ,
MOCK_METHOD0(getthedata, std::map<int comma int>&());

Whether that's better depends on your judgement and how many of these you are going to need.
I haven't tried it, but my guess is that it will probably work.


 
 
> --
>
> ---
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

There are only two methods worth noting to ignore the commas in the
template type in a macro. One is as you have listed (using a typedef)
and the other being that you enclose the template type inside
parentheses, although for this method to work in most situations then
the macro had to have been written with this in mind as the
parentheses are not removed when the macro is expanded.

--Liam

--

---
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.
For more options, visit https://groups.google.com/groups/opt_out.


--



liam mail

unread,
Feb 18, 2013, 2:06:46 PM2/18/13
to Billy Donahue, googl...@googlegroups.com
On 18 February 2013 18:44, Billy Donahue <billyd...@google.com> wrote:
> #define comma ,
> MOCK_METHOD0(getthedata, std::map<int comma int>&());


Unfortunately this will just pass the problem onto the next macro that
is used in the chain.

--Liam
Reply all
Reply to author
Forward
0 new messages