Using wxString with Google Mock

Has anyone out there had any luck using Google Mock in conjunction with wxWidgets? I have a class Foo with setters that take a const reference to wxString in the signature like so:

class Foo {
public:
    Foo();
    virtual ~Foo();
    void setName(const wxString& name);
};

I then proceed to mock Foo like this:

class MockFoo : public Foo {
    MOCK_METHOD1(setName, void(const wxString& name));
};

My other mocks works just fine but there is something about the wxString parameter that it doesn't like. When I compile I see the following:

C:gmock-1.6.0gtestincludegtestinternalgtest-internal.h:890: error: conversion from `const wxUniChar' to `long long int' is ambiguous
C:wxWidgets-2.9.0includewxunichar.h:74: note: candidates are: wxUniChar::operator char() const
C:wxWidgets-2.9.0includewxunichar.h:75: note:                 wxUniChar::operator unsigned char() const
//more potential candidates from wxUniChar follow after that

The jist is that Google Mock cannot determine which operator() function to call since the operator() functions provided by wxUniChar do not map to what Google Mock expects. I am seeing this error for the 'long long int' and the 'testing::internal::BiggestInt' conversions.


This must be a consequence of using a proxy class, wxUniCharRef , as the result type of wxString::operator[]() (see the "Traps for the unwary" section of wxString documentation for more details), but I'm not sure about where exactly does it come from as there doesn't seem to be any code accessing wxString characters here. What exactly is at the line 890 of gtest-internal.h ?

Also, you say that you're using a const reference to wxString, but your code doesn't. I don't think it's really relevant for your question but it's confusing to have this discrepancy between the description and the code snippets...


The following additions to the wxUniChar header file seemed to work:

wxUniChar(long long int c) { m_value = c; }

operator long long int() const { return (long long int)m_value; }

wxUniChar& operator=(long long int c) { m_value = c; return *this; }

bool operator op(long long int c) const { return m_value op (value_type)c; }

wxUniCharRef& operator=(long long int c) { return *this = wxUniChar(c); }

operator long long int() const { return UniChar(); }

bool operator op(long long int c) const { return UniChar() op c; }

I plugged these into the appropriate sections of the header file and the compilation error went away. If I have time later I will work on a patch for wxWidgets with some unit tests if this sounds like a reasonable solution.

链接地址: http://www.djcxy.com/p/27922.html

上一篇: 如何在Visual Studio中处理noexcept

下一篇: 在Google Mock中使用wxString