Looking for syntax to return a copy of const std::string as std::string from mock

422 views
Skip to first unread message

David Wallace

unread,
Apr 27, 2015, 4:10:09 PM4/27/15
to googl...@googlegroups.com
Using Gmock 1.6 with Visual Studio 2013 on Windows:

The method I am trying to mock has signature:
virtual std::string translate_sdc_name_interface(const std::string& name, const std::string &  hierDelimiter, drModuleInst * topModule) const = 0;
The corresponding mock declaration is:
  MOCK_CONST_METHOD3(translate_sdc_name_interface,
      std::string(const std::string& name, const std::string & hierDelimiter, drModuleInst * topModule));

When the mocked method is invoked, I want the default action to be to return a copy of the first argument.  None of the following syntaxes work:
class StructVendorOptionsTest : public VendorOptionsTest {
public:
StructVendorOptionsTest() {
//ON_CALL(vendorExterns, translate_sdc_name_interface(_, _, _)).WillByDefault(ReturnArg<0>);
//ON_CALL(vendorExterns, translate_sdc_name_interface(_, _, _)).WillByDefault(WithArg<0>(ReturnNew<std::string>));
//ON_CALL(vendorExterns, translate_sdc_name_interface(_, _, _)).WillByDefault(WithArg<0>(ReturnNew<std::string>()));
//ON_CALL(vendorExterns, translate_sdc_name_interface(_, _, _)).WillByDefault(ReturnNew<std::string>(ReturnArg<0>()));
}
protected:
MockVendorExterns vendorExterns;
};
The first of these complains about being unable to convert a const std::string & to a std::string; the others result in various syntax errors with the template expansions. I haven't found a good example of this in the documentation. What's the right syntax to use here (I assume it is possible to do this)?

Dave W.

David Wallace

unread,
Apr 27, 2015, 4:39:47 PM4/27/15
to googl...@googlegroups.com
I've now found that I can use an explicit helper function to get the result I want.  But is there a cleaner way to do this that doesn't require the helper function?

static std::string CopyString(const std::string &arg) { return arg; }
class StructVendorOptionsTest : public VendorOptionsTest {
public:
StructVendorOptionsTest() {
//ON_CALL(vendorExterns, translate_sdc_name_interface(_, _, _)).WillByDefault(ReturnArg<0>);
//ON_CALL(vendorExterns, translate_sdc_name_interface(_, _, _)).WillByDefault(WithArg<0>(ReturnNew<std::string>));
//ON_CALL(vendorExterns, translate_sdc_name_interface(_, _, _)).WillByDefault(WithArg<0>(ReturnNew<std::string>()));
//ON_CALL(vendorExterns, translate_sdc_name_interface(_, _, _)).WillByDefault(ReturnNew<std::string>(ReturnArg<0>()));
ON_CALL(vendorExterns, translate_sdc_name_interface(_, _, _)).WillByDefault(WithArg<0>(Invoke(CopyString)));
}
protected:
MockVendorExterns vendorExterns;
};

Samuel Benzaquen

unread,
Apr 27, 2015, 4:55:57 PM4/27/15
to David Wallace, googl...@googlegroups.com
Can you post the full compiler output of the 'ReturnArg<0>' version?
That seems like it should work.
_Sam

--

---
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/4b3f5c4f-8acc-4b90-a005-6ff4a3c0c69a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Wallace

unread,
Apr 27, 2015, 5:35:17 PM4/27/15
to Samuel Benzaquen, googl...@googlegroups.com
Hi, Samuel.  When I uncomment the first line (with ReturnArg<0>), the compiler output I get is:
2>------ Build started: Project: Test_Shared_SDCVendorOptions_SDCVendorOptions, Configuration: Debug Win32 ------
2>  TestSDCVendorOptions.cpp
2>c:\users\dave\documents\visual studio 2008\projects\clocks\shared\sdcvendoroptions\unittests\testsdcvendoroptions.cpp(305): error C2664: 'testing::internal::OnCallSpec<F> &testing::internal::OnCallSpec<F>::WillByDefault(const testing::Action<F> &)' : cannot convert argument 1 from 'testing::ReturnArgAction<k> (__cdecl *)(void)' to 'const testing::Action<F> &'
2>          with
2>          [
2>              F=std::string (const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,drModuleInst *)
2>          ]
2>          Reason: cannot convert from 'overloaded-function' to 'const testing::Action<F>'
2>          with
2>          [
2>              F=std::string (const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,drModuleInst *)
2>          ]
2>          No constructor could take the source type, or constructor overload resolution was ambiguous

(Note: the path to my source files goes through a visual studio 2008 directory, but the current compiler being used is visual studio 2013.)  I hope this is helpful.

Dave W.

Samuel Benzaquen

unread,
Apr 27, 2015, 5:52:56 PM4/27/15
to David Wallace, googl...@googlegroups.com
I see the problem now. I missed it when I first read your code.
ReturnArg<> is a function that returns an action. You must call it. It should be: WillByDefault(ReturnArg<0>())
Note that ReturnNew<> is also a function that returns an action.

_Sam

David Wallace

unread,
Apr 29, 2015, 11:23:23 AM4/29/15
to Samuel Benzaquen, googl...@googlegroups.com
Thanks, Samuel.  That was indeed the problem.  It can be challenging to interpret template error messages involving code you didn't write - in this case, I saw the error involving inability to convert something involving the const string reference, and drew the wrong conclusion from it.

Dave W.
Reply all
Reply to author
Forward
0 new messages