【问题标题】:Using wxString with Google Mock将 wxString 与 Google Mock 一起使用
【发布时间】:2013-05-14 17:39:23
【问题描述】:

有没有人在使用 Google Mock 和 wxWidgets 时遇到过运气?我有一个带有 setter 的类 Foo,它在签名中对 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.0\gtest\include\gtest\internal\gtest-internal.h:890: error: conversion from `const wxUniChar' to `long long int' is ambiguous
C:\wxWidgets-2.9.0\include\wx\unichar.h:74: note: candidates are: wxUniChar::operator char() const
C:\wxWidgets-2.9.0\include\wx\unichar.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”和“testing::internal::BiggestInt”转换中看到了这个错误。

【问题讨论】:

    标签: c++ unit-testing wxwidgets googlemock


    【解决方案1】:

    这一定是使用代理类wxUniCharRef 作为wxString::operator[]() 的结果类型的结果(有关更多详细信息,请参阅wxString documentation 的“粗心大意的陷阱”部分),但我不是确定它究竟来自哪里,因为这里似乎没有任何代码访问 wxString 字符。 gtest-internal.h 的第 890 行究竟是什么?

    另外,您说您正在使用对 wxString 的 const 引用,但您的代码没有。我认为这与您的问题并不真正相关,但描述和代码 sn-ps 之间存在这种差异令人困惑......

    【讨论】:

    • 抱歉不一致。我更新了这个例子。我能够通过向 wxUniChar 添加一个构造函数和各种 operator= 和 operator() 函数来解决我的问题,以用于“long long int”类型并构建 wxWidgets。
    【解决方案2】:

    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 开发一个补丁,并进行一些单元测试,如果这听起来是一个合理的解决方案。

    【讨论】:

    • 如果我们真的需要允许在同一个表达式中混合wxUniCharlong long,那么我们真的需要这个(unsigned long long 也一样)。但我仍然想知道为什么首先需要这样做......
    • 我相信这是因为 Google Test 在其一些内部代码中使用了它所谓的“BiggestInt”类型,该类型可能对 Google Mock 对象执行验证。它看起来像是 long long int 或 __int64 取决于平台。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多