【问题标题】:Gtest std::array matcher for custom type自定义类型的 Gtest std::array 匹配器
【发布时间】:2021-02-05 04:30:33
【问题描述】:

我有工会

using my_union = union my_union {
    struct {
        int var1;
        int var2;
    };
    long
    var;
    bool operator == (const my_union & oth) {
        return var == oth.var;
    }
};
using my_union_1d = std::array < my_union, 2 > ;
using my_union_2d = std::array < my_union_1d, 3 > ;

我有一些函数可以接受这个数组

class test {
    public:
        void Foo(my_union_2d & arg1);
};

现在我需要检查是否使用我预期的参数调用了 Mocked-Foo

class TestMock: Public Test {
    public: MOCK_METHOD(void, Foo, (my_union_2d & ));
};
my_union_2d expected_args;
EXPECT_CALL( * (TestMock * ) obj, Foo(expected_args)).Times(1):

但是代码无法编译,因为没有找到数组 == 运算符的数组。我尝试更改 AllOfArray,但我似乎迷失在 GTest 的文档中。有谁知道需要做什么或参考点?

【问题讨论】:

    标签: arrays c++11 googletest matcher


    【解决方案1】:

    作为成员函数的正确operator==应该是const方法:

    bool operator==(const my_union &oth) const { return var == oth.var;}
    

    【讨论】:

    • 我收回我之前的回复。我将 == 运算符重载为非 const 方法,一旦更改为“const”,它就按照@Quarra 的建议运行良好
    【解决方案2】:

    经过一番调查,我能够解决编译问题。

    MATCHER_P(MyMatch, oth, "Arg is not the same")
    {
        for (uint8_t outer=0; outer <3; outer++)
        {
            for (uint8_t inner = 0; inner < 2; inner++)
            {
                if ((arg[inner].var1 != oth[inner].var1) ||
                    (arg[inner].var2 != oth[inner].var2))
                    return 0;
            }
        }
        return 1;
    }
    

    并调用为

    EXPECT_CALL( * (TestMock * ) obj, Foo(MyMatch(expected_args))).Times(1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多