且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

控制 Boost.Test 源位置格式的输出

更新时间:2023-12-01 09:30:16

我在这篇历史帖子中找到了解决方案:https://richarddingwall.name/2008/06/01/using-the-boost-unit-test-framework-with-xcode-3

I found a solution in this historical post: https://richarddingwall.name/2008/06/01/using-the-boost-unit-test-framework-with-xcode-3

使用(丢失的)虚函数覆盖和 Boost.Test 装置的艺术:

Using the (lost of) art of virtual function override and Boost.Test fixtures:

只需添加此代码(对原始帖子的一些更新,带有次要格式和 C++11 更新):

Simply add this code (some updates to the original post, with minor formats and C++11 updates):

#include<boost/test/output/compiler_log_formatter.hpp>

struct xcode_log_formatter: boost::unit_test::output::compiler_log_formatter{
    // Produces an Xcode-friendly message prefix.
    void print_prefix(std::ostream& output, boost::unit_test::const_string file_name, std::size_t line) override{
        output << file_name << ':' << line << ": error: ";
    }
};

// Set up the unit test framework to use an xcode-friendly log formatter.
struct xcode_config{
    xcode_config(){boost::unit_test::unit_test_log.set_formatter(new xcode_log_formatter);}
};

// Call our fixture.
BOOST_GLOBAL_FIXTURE(xcode_config);

有了这个改变,输出看起来像(注意文件:lineno 格式).

with this change the output looks like (note file:lineno format).

Running 7 test cases...
./layout.hpp:781: error: error: in "layout_to_offset_1d_nontrivial": check L[3] == &B[3] - base(B) has failed [3 != 6]
Running 7 test cases...
./.././detail/layout.hpp:781: error: error: in "layout_to_offset_1d_nontrivial": check L[3] == &B[3] - base(B) has failed [3 != 6]

我仍然对更简单的解决方案感兴趣.

I am still interested in simpler solutions.

这是这段代码的更紧凑版本,根据我自己的情况重命名(xcode->gedit):

Here it is a more compact version of this code, renamed for my own case (xcode->gedit):

#include<boost/test/output/compiler_log_formatter.hpp>
struct gedit_config{
    struct formatter : boost::unit_test::output::compiler_log_formatter{
        void print_prefix(std::ostream& out, boost::unit_test::const_string file, std::size_t line){
            out<< file <<':'<< line <<": ";
        }
    };
    gedit_config(){boost::unit_test::unit_test_log.set_formatter(new formatter);}
};
BOOST_GLOBAL_FIXTURE(gedit_config);