#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(test1)
{
std::optional<int> opt1(10);
BOOST_TEST(t == 11);
std::optional<long> opt2(11);
BOOST_CHECK_EQUAL(opt1, opt2);
}
有没有什么办法,使升压测试打印(代码:BOOST_TEST)std
类型?超载operator<<
必须是命名空间中的std
由ADL被发现和延伸std
是被禁止的。在boost's documentation提到的唯一的事情是关于UDT和解决方案还依赖于日常生活,因为它强调在相同的命名空间UDT添加自定义功能boost_test_print_type
。
关于建议重复
我不确定。如何将一个瘦包装,在重复的建议,将工作?这是否意味着我将不得不转换为每个测试用例包装每个断言之前,而不是直接使用标准型(可选)?如果是这样,那是不是我要找的和不想要的!
下面是基于包装模板的解决方案。我认为这是很不理想,但预期它应该工作。
template <class T>
struct PrintableOpt {
std::optional<T> value;
};
必要的运算符重载会
template <class T>
std::ostream& operator << (std::ostream& os, const PrintableOpt<T>& opt)
{
if (opt.value)
return os << *opt.value;
else
return os << "std::nullopt";
}
template <class T, class U>
bool operator == (const PrintableOpt<T>& lhs, const PrintableOpt<U>& rhs)
{
return lhs.value && rhs.value && *lhs.value == *rhs.value;
}
template <class T, class U>
bool operator != (const PrintableOpt<T>& lhs, const PrintableOpt<U>& rhs)
{
return !(lhs == rhs);
}
为了方便起见,这里有两个辅助功能,构建包装实例:
template <class T>
auto printable(T&& opt)
{
return PrintableOpt<T>{std::forward<T>(opt)};
}
template <class T>
auto printable(std::optional<T>& opt)
{
return PrintableOpt<T>{opt};
}
测试现在看起来是这样的:
BOOST_AUTO_TEST_CASE(test1)
{
std::optional<int> opt1(10);
BOOST_TEST(printable(opt1) == printable(11));
std::optional<long> opt2(11);
BOOST_CHECK_EQUAL(printable(opt1), printable(opt2));
}