Using wxString with Google Mock

Using wxString with Google Mock

本文关键字:Mock Google with wxString Using      更新时间:2023-10-16

有没有人在与wxWidgets一起使用Google Mock时运气不错?我有一个类Foo,其setter在签名中接受对wxString的const引用,如下所示:

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

然后我继续像这样模拟Foo:

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

我的其他模拟工作得很好,但有一些关于wxString参数,它不喜欢。当我编译时,我看到以下内容:

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

问题在于Google Mock不能确定调用哪个operator()函数,因为wxUniChar提供的operator()函数不映射到Google Mock所期望的。我看到"long long int"answers"testing::internal::BiggestInt"转换出现此错误。

这一定是使用代理类wxUniCharRef作为wxString::operator[]()的结果类型的结果(请参阅wxString文档的"粗心的陷阱"部分了解更多细节),但我不确定它究竟来自何处,因为这里似乎没有任何访问wxString字符的代码。gtest-internal.h的第890行到底是什么?

同样,你说你正在使用对wxString的const引用,但你的代码没有。我认为这与你的问题无关但是描述和代码片段之间的差异让人很困惑。

在wxUniChar头文件中添加以下内容似乎可以工作:

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; }

我将这些插入到头文件的适当部分中,编译错误就消失了。如果这听起来像是一个合理的解决方案,那么如果我有时间的话,我会用一些单元测试来为wxWidgets做一个补丁。