google test PrintTo for std::set<std::string>
google test PrintTo for std::set<std::string>
在谷歌测试高级文档中,他们说在定义要打印的值的同一命名空间中编写PrintTo
,如果它是您自己的命名空间中的您自己的类,那就太好了,但如果它是std::set
,您不应该向命名空间添加新成员std
。
那么如何自定义std::set<std::string>
的PrintTo
行为呢? google测试中的默认打印机在一定数量的值之后停止打印,当不同的值在默认打印机发出的值之后时,这没有用。
#include <set>
#include <string>
#include <gtest/gtest.h>
void PrintTo(const std::set<std::string> &value, std::ostream *str)
{
*str << "Got here!n";
}
TEST(MapPrint, custom_printer)
{
std::set<std::string> one{"foo"};
std::set<std::string> two{"bar"};
ASSERT_EQ(one, two); // doesn't print 'Got here!'
}
有效的方法是在命名空间testing::internal
中定义PrintTo
。 这对我来说仍然有点肮脏。 据推测,internal
命名空间是为了让谷歌测试可以更改他们喜欢的任何实现细节,而不考虑向后兼容性。 至少它不会导致未定义的行为,就像在命名空间std
中定义PrintTo
一样。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- std::vector::reserve(未知m),我知道m<<;N(通常)并且知道N
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- C++运算符<<调用::ostream而不是std::osttream
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- C++重载<<具有typedef'd std::vector
- 以x的倍数填充前导零,使用std::cout<<std::十六进制
- 错误:没有匹配'运算符<<"在'std::cout
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- std::ostream&operator< & lt; (std:: ostream&压力,压力& &;val)
- 将std::endl传递给std::operator<<
- std::映射<>或std::vector<>在处理大型标志集时
- 重载& lt; & lt;使用命名空间std