在Google测试Advanced documentation中,他们说要在定义要打印的值的同一名称空间中写入PrintTo
,如果它是您自己的名称空间中的您自己的类,那么很棒,但如果它是std::set
您不是' t应该将新成员添加到名称空间std
。
那么您如何自定义PrintTo
的std::set<std::string>
行为? 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!'
}
有效的方法是在名称空间PrintTo
中定义testing::internal
。这对我来说仍然有点脏。大概internal
命名空间是为了让Google测试可以在其中更改其所需的任何实现细节,而无需考虑向后兼容性。至少它不会像在名称空间PrintTo
中定义std
那样导致未定义的行为。