1010#define BOOST_LOCALE_UNIT_TEST_HPP
1111
1212#include < boost/locale/config.hpp>
13+ #include < boost/config/helper_macros.hpp>
1314#include < cstdlib>
1415#include < iomanip>
1516#include < iostream>
@@ -33,8 +34,10 @@ namespace boost { namespace locale { namespace test {
3334 // / Name/path of current executable
3435 std::string exe_name;
3536
37+ class test_context ;
38+
3639 struct test_result {
37- test_result () : error_counter( 0 ), test_counter( 0 )
40+ test_result ()
3841 {
3942#if defined(_MSC_VER) && (_MSC_VER > 1310)
4043 // disable message boxes on assert(), abort()
@@ -46,18 +49,42 @@ namespace boost { namespace locale { namespace test {
4649 _CrtSetReportFile (_CRT_ASSERT, _CRTDBG_FILE_STDERR);
4750#endif
4851 }
49- int error_counter;
50- int test_counter;
52+ int error_counter = 0 ;
53+ int test_counter = 0 ;
54+ const test_context* context = nullptr ;
5155 };
5256 inline test_result& results ()
5357 {
5458 static test_result instance;
5559 return instance;
5660 }
5761
62+ class test_context {
63+ const test_context* oldCtx_;
64+ const std::string msg_;
65+
66+ public:
67+ test_context (std::string ctx) : oldCtx_(results().context), msg_(std::move(ctx)) { results ().context = this ; }
68+ ~test_context () { results ().context = oldCtx_; }
69+ friend std::ostream& operator <<(std::ostream& os, const test_context& c)
70+ {
71+ const test_context* current = &c;
72+ os << " CONTEXT: " ;
73+ std::string indent = " \n\t " ;
74+ do {
75+ os << indent << current->msg_ ;
76+ indent += ' \t ' ;
77+ } while ((current = current->oldCtx_ ) != nullptr );
78+ return os;
79+ }
80+ };
81+
5882 inline void report_error (const char * expr, const char * file, int line)
5983 {
6084 std::cerr << " Error at " << file << ' #' << line << " : " << expr << std::endl;
85+ const auto * context = results ().context ;
86+ if (context)
87+ std::cerr << ' ' << *context << std::endl;
6188 if (++boost::locale::test::results ().error_counter > BOOST_LOCALE_ERROR_LIMIT)
6289 throw std::runtime_error (" Error limits reached, stopping unit test" );
6390 }
@@ -97,6 +124,10 @@ namespace boost { namespace locale { namespace test {
97124 BOOST_LOCALE_START_CONST_CONDITION \
98125 } while (0 ) BOOST_LOCALE_END_CONST_CONDITION
99126
127+ #define TEST_CONTEXT (expr ) \
128+ boost::locale::test::test_context BOOST_JOIN (test_context_, __COUNTER__)( \
129+ static_cast <const std::stringstream&>(std::stringstream{} << expr).str())
130+
100131void test_main(int argc, char ** argv);
101132
102133int main (int argc, char ** argv)
0 commit comments