我将一个指向数组的指针传递给一个函数,我正在使用googlemock模拟它。我想验证参数的内容,它适用于标量,但我无法获取数组的元素:
#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class InterfaceTest : public ::testing::Test {};
class MockFortranFuncInterfacePointerArgs {
public:
MOCK_METHOD(void, fortran_interface_test_func_pointerargs, (int*, int*));
};
void testfunc_pointer(MockFortranFuncInterfacePointerArgs* func_interface) {
int testscalar = 123;
int testarray[3] = {1, 2, 3};
func_interface->fortran_interface_test_func_pointerargs(&testscalar, &testarray[0]);
}
TEST_F(InterfaceTest, TestFuncInterfacePointerArgs) {
MockFortranFuncInterfacePointerArgs mock_func_interface;
int passed_scalar = 0;
int passed_array[3] = {0, 0, 0};
// int passed_array = 0;
EXPECT_CALL(mock_func_interface, fortran_interface_test_func_pointerargs(testing::_, testing::_))
.WillOnce(testing::DoAll(
testing::SaveArgPointee<0>(&passed_scalar),
testing::SetArrayArgument<1>(passed_array, passed_array + 3)
));
testfunc_pointer(&mock_func_interface);
std::cout << passed_scalar << std::endl; // prints 123
std::cout << passed_array[0] << " " << passed_array[1] << " " << passed_array[2] << std::endl; // prints 0 0 0
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
我如何修改这个测试,使我能够验证传递给fortran_interface_test_func_pointerargs
的数组的所有三个元素?我不能直接比较它们,因为我需要先存储在testfunc_pointer
中传递的数组,这是不可能的,目前使用SetArrayArgument
我在google工具箱中找不到任何可以解决这个问题的东西
但是这可以通过提供自己的匹配器来解决:
MATCHER_P2(ArrayPointee, size, subMatcher, "")
{
return ExplainMatchResult(subMatcher, std::make_tuple(arg, size), result_listener);
}
这个匹配器允许将指针参数转换为指针和大小的元组,其他匹配器将其视为std::span
(自c++ 20起可用)。
所以测试可以这样调整(修改我的MCVE从评论):
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::ElementsAre;
MATCHER_P2(ArrayPointee, size, subMatcher, "")
{
return ExplainMatchResult(subMatcher, std::make_tuple(arg, size), result_listener);
}
class IFoo {
public:
virtual void foo(int*) = 0;
};
class MockFoo : public IFoo {
public:
MOCK_METHOD(void, foo, (int*), ());
};
void UseFoo(IFoo& foo)
{
int arr[] { 3, 5, 7 };
foo.foo(arr);
}
TEST(TestFoo, fooIsCalledWithProperArray)
{
MockFoo mock;
EXPECT_CALL(mock, foo(ArrayPointee(3, ElementsAre(3, 5, 7))));
UseFoo(mock);
}
- 通过测试演示
- 测试演示失败