c++ - 有没有办法确保对 BOOST_TESTs 消息进行惰性评估?
问题描述
如果检查实际上失败,是否可以确保BOOST_TEST
仅评估消息宏参数。延迟评估似乎发生在人类可读的输出格式上,但不适用于 JUnit 输出格式。我可以以某种方式使惰性评估对所有输出类型都可靠地工作吗?
MCVE
#define BOOST_TEST_MODULE my_module
#include <boost/test/included/unit_test.hpp>
#include <string>
struct S
{
std::string m_value;
};
S* f(void)
{
return nullptr;
}
BOOST_AUTO_TEST_CASE(foo_test)
{
S* result = f();
BOOST_TEST(result == nullptr, "f() should return nullptr but returned a object with m_value = " << result->m_value);
}
如果我使用人类可读的输出格式(--log_format=HRF
可执行文件的命令行选项),此代码可以正常工作。使用 JUnit 输出(--log_format=JUNIT
命令行选项)会导致访问错误,因为程序会尝试获取 address 处字符串的大小0
。
这有点出乎意料,因为我假设BOOST_TEST
宏的工作方式与此类似
// not the real macro, but one that works in a way I'd expect it to work
#define BOOST_TEST(condition, message) if (!(condition)) { \
some_output_stream << "The following test failed: " << #condition << std::endl \
<< "Message: " << message; \
}
测试配置:
- 提升 1.75.0
- Visual Studio 2017/2019 或 g++ 8.x (Win 10 / Ubuntu 18)
- 64 位
解决方案
推荐阅读
- ruby - 使用 capybara + selenium 验证特定 css 值的文本
- python - 如何将输入、变量和文本从一个 python 脚本发送到另一个已经运行的 python 脚本?
- ponylang - 小马的静态类方法?
- list - Prolog - 计算两个列表中的匹配项
- javascript - 为什么`@change` 触发器不适用于 v-data-picker?
- php - 按另一个预定义的、非详尽的数字数组对数字数组进行排序,然后升序排序
- mysql - 如何从父表的两个子表中获取值
- assembly - 尝试执行 /bin/sh 但生成 NULL
- html - 如何制作具有最小宽度的表格,而无需数据进入新行?
- php - 插入选择选项表单 Laravel - Vue