Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

c++ - Is there a way to ensure lazy evaluation of BOOST_TESTs message?

Is it possible to ensure that the message macro parameter of BOOST_TEST is only evaluated, if the check actually fails. Lazy evaluation seems to happen for human readable output format, but not for JUnit output format. Can I somehow make lazy evaluation work reliably for all output types?

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);
}

This code works fine if I use human readable output format (--log_format=HRF command line option for executable). Using JUnit output (--log_format=JUNIT command line option) results in a access error, since the program attempts to get the size of a string at address 0.

This is a bit unexpected, since I'd assume the BOOST_TEST macro would work similar to this

// 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; 
}

Tested configurations:

  • boost 1.75.0
  • Visual Studio 2017/2019 or g++ 8.x (Win 10 / Ubuntu 18)
  • 64 bit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...