Teg*_*gan 5 c++ googletest googlemock
背景:
\n我正在模拟硬件 i2c 传输函数,并尝试匹配传递给它的数组。这是在嵌入式环境中,因此需要 C 风格的数组并且缺少 STL 容器。
\n尝试匹配我的第二个参数,即 c 样式数组(下面的缓冲区),在编译时失败。
\n设置:
\n接口定义为:
\nvirtual I2C_Status_e i2c1Transmit(uint8_t address, \n const uint8_t buffer[], \n uint8_t length) = 0;\nRun Code Online (Sandbox Code Playgroud)\n我将我的测试模拟设置为:
\nMOCK_METHOD(I2C_Status_e,\n i2c1Transmit,\n (uint8_t address, const uint8_t buffer[], uint8_t length),\n (override));\nRun Code Online (Sandbox Code Playgroud)\n二进制文件中的调用点(此传输发送单个 8 位命令):
\nstatus = hal->i2c1Transmit(address, readStatusCommand, 1);\nRun Code Online (Sandbox Code Playgroud)\n其中 readStatusCommand 默认为:
\nconst uint8_t readStatusCommand[1] = {0x00};\nRun Code Online (Sandbox Code Playgroud)\n测试代码:
\n这里 readStatusCommand 具有与上面相同的签名。
\nEXPECT_CALL(hal, i2c1Transmit(address0, ElementsAreArray(readStatusCommand, 1), txLength))\n .WillOnce(Return(HAL_Ok));\nRun Code Online (Sandbox Code Playgroud)\n编译器输出:
\n/home/.../vendor/gtest/googletest-release-1.10.x/googlemock/include/gmock/gmock-matchers.h: In instantiation of \xe2\x80\x98class testing::internal::ElementsAreMatcherImpl<const unsigned char* const&>\xe2\x80\x99:\n/home/.../vendor/gtest/googletest-release-1.10.x/googlemock/include/gmock/gmock-matchers.h:3518:31: required from \xe2\x80\x98testing::internal::ElementsAreArrayMatcher<T>::operator testing::Matcher<T>() const [with Container = const unsigned char*; T = unsigned char]\xe2\x80\x99\n/home/.../..._tests.cpp:88:5: required from here \n ^^^* Note: Expect Call above is line 88 here. *^^^\n/home/.../vendor/gtest/googletest-release-1.10.x/googlemock/include/gmock/gmock-matchers.h:3084:45: error: \xe2\x80\x98testing::internal::ElementsAreMatcherImpl<const unsigned char* const&>::StlContainer\xe2\x80\x99 {aka \xe2\x80\x98const unsigned char*\xe2\x80\x99} is not a class, struct, or union type\n 3084 | typedef typename StlContainer::value_type Element;\n | ^~~~~~~\n/home/.../vendor/gtest/googletest-release-1.10.x/googlemock/include/gmock/gmock-matchers.h:3218:43: error: \xe2\x80\x98testing::internal::StlContainerView<const unsigned char*>::type\xe2\x80\x99 {aka \xe2\x80\x98const unsigned char*\xe2\x80\x99} is not a class, struct, or union type\n 3218 | ::std::vector<Matcher<const Element&> > matchers_;\n | ^~~~~~~~~\nRun Code Online (Sandbox Code Playgroud)\n预期的行为/我尝试过的:
\n根据此处的匹配器文档,它应该有效吗?ElementsAreArray 被定义为在传入长度的情况下处理 c 样式数组。我尝试使用 ElementsAre 达到相同的效果。
\n它也绝对是匹配器,将匹配器切换为 ::_ 可以使其编译良好。
\n不,它不应该工作 -匹配器文档指的是匹配的参数是 STL 容器,而const uint8_t buffer[]是指针的情况。
这ElementsAreArray(readStatusCommand, 1)是长度为 1 的容器的匹配器。但是,与之比较的第二个参数没有关联的长度。
这会起作用,参见ii。这些匹配器还可以匹配注释,即指Multi-argument Matchers。
EXPECT_CALL(hal, i2c1Transmit(address0, _, _))
.With(Args<1, 2>(ElementsAreArray(readStatusCommand)))
.WillOnce(Return(HAL_Ok));
Run Code Online (Sandbox Code Playgroud)
ElementsAreArray(readStatusCommand)不需要设置显式数组长度,因为具有具有关联长度的readStatusCommand类型。const uint8_t[1]
| 归档时间: |
|
| 查看次数: |
605 次 |
| 最近记录: |