Skip to content

Commit 291bf30

Browse files
committed
Add TEST_CONTEXT
Avoid to require manual output in anticipation of failures.
1 parent cc20f63 commit 291bf30

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

test/boostLocale/test/unit_test.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,26 @@ namespace boost { namespace locale { namespace test {
4848
}
4949
int error_counter;
5050
int test_counter;
51+
std::string context;
5152
};
5253
inline test_result& results()
5354
{
5455
static test_result instance;
5556
return instance;
5657
}
5758

59+
struct context {
60+
const std::string oldCtx;
61+
context(std::string ctx) : oldCtx(std::move(results().context)) { results().context = std::move(ctx); }
62+
~context() { results().context = std::move(oldCtx); }
63+
};
64+
5865
inline void report_error(const char* expr, const char* file, int line)
5966
{
6067
std::cerr << "Error at " << file << '#' << line << ": " << expr << std::endl;
68+
std::string& ctx = results().context;
69+
if(!ctx.empty())
70+
std::cerr << " context: " << ctx << std::endl;
6171
if(++boost::locale::test::results().error_counter > BOOST_LOCALE_ERROR_LIMIT)
6272
throw std::runtime_error("Error limits reached, stopping unit test");
6373
}
@@ -97,6 +107,10 @@ namespace boost { namespace locale { namespace test {
97107
BOOST_LOCALE_START_CONST_CONDITION \
98108
} while(0) BOOST_LOCALE_END_CONST_CONDITION
99109

110+
#define TEST_CONTEXT(expr) \
111+
boost::locale::test::context _##__COUNTER__( \
112+
static_cast<const std::stringstream&>(std::stringstream{} << expr).str())
113+
100114
void test_main(int argc, char** argv);
101115

102116
int main(int argc, char** argv)

test/test_catalog.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ void test_plural_expr_rand(const T& ref, const char* expr)
3131
const auto n = getRandValue(minVal, maxVal);
3232
const auto result = ptr(n);
3333
const auto refResult = ref(n);
34-
if(result != refResult) {
35-
std::cerr << "Expression: " << expr << "; n=" << n << '\n'; // LCOV_EXCL_LINE
36-
TEST_EQ(result, refResult); // LCOV_EXCL_LINE
37-
}
34+
TEST_CONTEXT("Expression: " << expr << "; n=" << n);
35+
TEST_EQ(result, refResult);
3836
}
3937
}
4038

test/test_codecvt.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ void test_codecvt_in_n_m(const cvt_type& cvt, int n, int m)
5353
std::mbstate_t mb2 = mb;
5454
std::codecvt_base::result r = cvt.in(mb, from, end, from_next, to, to_end, to_next);
5555

56-
int count = cvt.length(mb2, from, end, to_end - to);
56+
const int count = cvt.length(mb2, from, end, to_end - to);
5757
TEST_EQ(memcmp(&mb, &mb2, sizeof(mb)), 0);
58-
if(count != from_next - from)
59-
std::cout << count << " " << from_next - from << std::endl; // LCOV_EXCL_LINE
6058
TEST_EQ(count, from_next - from);
6159

6260
if(r == cvt_type::partial) {

test/test_encoding.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -832,10 +832,9 @@ void test_simple_encodings()
832832
const auto encodings = get_simple_encodings();
833833
for(auto it = encodings.begin(), end = encodings.end(); it != end; ++it) {
834834
TEST_EQ(normalize_encoding(*it), *it); // Must be normalized
835-
const auto it2 = std::find(it + 1, end, *it);
836-
TEST(it2 == end);
837-
if(it2 != end)
838-
std::cerr << "Duplicate entry: " << *it << '\n'; // LCOV_EXCL_LINE
835+
TEST_CONTEXT("Entry: " << *it);
836+
// Must be unique
837+
TEST(std::find(it + 1, end, *it) == end);
839838
}
840839
const auto it = std::is_sorted_until(encodings.begin(), encodings.end());
841840
TEST(it == encodings.end());
@@ -852,10 +851,9 @@ void test_win_codepages()
852851
auto is_same_win_codepage = [&it](const windows_encoding& rhs) -> bool {
853852
return it->codepage == rhs.codepage && std::strcmp(it->name, rhs.name) == 0;
854853
};
855-
const auto* it2 = std::find_if(it + 1, end, is_same_win_codepage);
856-
TEST(it2 == end);
857-
if(it2 != end)
858-
std::cerr << "Duplicate entry: " << it->name << ':' << it->codepage << '\n'; // LCOV_EXCL_LINE
854+
TEST_CONTEXT("Entry: " << it->name << ':' << it->codepage);
855+
// Must be unique
856+
TEST(std::find_if(it + 1, end, is_same_win_codepage) == end);
859857
}
860858
const auto cmp = [](const windows_encoding& rhs, const windows_encoding& lhs) -> bool { return rhs < lhs.name; };
861859
const auto* it = std::is_sorted_until(all_windows_encodings, std::end(all_windows_encodings), cmp);

0 commit comments

Comments
 (0)