Abstract classes as parameters

17 views
Skip to first unread message

amswain

unread,
Oct 30, 2009, 10:18:38 AM10/30/09
to mockitopp-dev
You cannot mock an interface that takes abstract classes as
parameters. Here's an example

include <mockitopp/MockObject.hpp>

struct ITradeable
{
virtual int GetId() = 0;
bool operator<(const ITradeable& rhs) const { return GetId() <
rhs.GetId(); }
bool operator==(const ITradeable& rhs) const { return GetId() ==
rhs.GetId(); }
};

struct MyTradeable : public ITradeable
{
int GetId() { return 1; }
};

struct ICalculator
{
//virtual int Calculate(const ITradeable&) = 0; // doesn’t
compile
virtual int Calculate(const MyTradeable&) = 0;
};

int main(int argc, char** argv)
{
MyTradeable instrument;
mockitopp::MockObject<ICalculator> mock;
mock(&ICalculator::Calculate);
int value = mock.getInstance().Calculate(instrument);
}


I had a quick hack and removed the const refs from
dynamic_vfunction.hpp

sed -i 's/typename tr1::remove_const<typename tr1::remove_reference<\(A
[0-9]\) >::type>::type/\1/g'

I also changed the code to use boost::tuple instead of the tuple that
ships with the code.

The above code will then compile but the problem then shifts onto the
matchers as these store arguments as values too. You also get
problems with reference to reference, which can be sorted out by using
boost/call_traits.

What plans, if any, do you have to sort this out?

Trevor

unread,
Nov 4, 2009, 11:30:52 AM11/4/09
to mockitopp-dev
I believe I have resolved your issues with fixes I have checked-in to
the trunk. I've made a few modifications to your code to make sure it
works within the parameters of the C++ language and mockitopp
framework. Let me know if you have further issue.

#include <iostream>
#include <mockitopp/mockitopp.hpp>

struct ITradeable
{
virtual ~ITradeable() {}

virtual int GetId() const = 0;

bool operator<(const ITradeable& rhs) const
{ return GetId() < rhs.GetId(); }

bool operator==(const ITradeable& rhs) const
{ return GetId() == rhs.GetId(); }
};

struct MyTradeable : public ITradeable
{
int GetId() const
{ return 1; }
};

struct ICalculator
{
virtual ~ICalculator() {}

virtual int Calculate(const ITradeable&) = 0;
virtual int Calculate(const MyTradeable&) = 0;
};

int main(int argc, char** argv)
{
MyTradeable instrument;
mockitopp::mock_object<ICalculator> mock;
// mock ICalculator function with const ITradeable& argument
mock(static_cast<int (ICalculator::*)(const ITradeable&)>
(&ICalculator::Calculate)).when(mockitopp::matcher::any<const
ITradeable&>()).thenReturn(0);
// mock ICalculator function with const MyTradeable& argument
mock(static_cast<int (ICalculator::*)(const MyTradeable&)>
(&ICalculator::Calculate)).when(mockitopp::matcher::any<const
MyTradeable&>()).thenReturn(1);
// invoke ICalculator function with const MyTradeable& argument
std::cout << mock.getInstance().Calculate((const ITradeable&)
instrument) << std::endl;
// invoke ICalculator function with const MyTradeable& argument
std::cout << mock.getInstance().Calculate(instrument) << std::endl;
}

Thanks,
Trevor

Adrian Swain

unread,
Nov 5, 2009, 9:18:11 AM11/5/09
to mockit...@googlegroups.com
Hi Trevor,

You still need to fix the equal class

This doesn't work

mock(&ICalculator::Calculate).when(mockitopp::matcher::equal<const ITradeable&>(instrument)).thenReturn(instrument.GetId());

Ideally it would be nice if you could just call

mock(&ICalculator::Calculate).when(mockitopp::matcher::equal(instrument)).thenReturn(instrument.GetId());

and have type deduction work correctly

You'll probably need to add template parameters to dynamic_vfunction::when(Matcher<U0>,...) or something like that?

Thanks
The contents of this email including any attachments are confidential. If you have received this email in error, please advise the sender by return email and delete this email. Any unauthorised use of the contents of the email is prohibited and you must not disseminate, copy or distribute the message or use the information contained in the email or its attachments in any way.

The views or opinions expressed are the author's own and may not reflect the views or opinions of Tibra. Tibra does not guarantee the integrity of any emails or attached files. E-mails may be interfered with, may contain computer viruses or other defects. Under no circumstances do we accept liability for any loss or damage which may result from your receipt of this message or any attachments.

Trevor

unread,
Nov 5, 2009, 4:58:16 PM11/5/09
to mockitopp-dev
Hi Adrian,

Thanks for reporting this. I missed updating the equal matcher to
support const reference arguments when I fixed the other problems. I
have checked in a fix for this SVN r123. I'm still in the process of
robustifying several of the test cases to cover missing possible logic
errors in the future. Let me know if you run into further issues.

Also, what compile are you testing this out with?

Thanks,
Trevor

Adrian Swain

unread,
Nov 6, 2009, 4:48:42 AM11/6/09
to mockit...@googlegroups.com
gcc4.3 on Linux and
Microsoft Visual Studio 2008 v9
Reply all
Reply to author
Forward
0 new messages