summaryrefslogtreecommitdiff
path: root/tests/colour_log_formatter.cpp
blob: baa8b9d400514ae6044a0e4208f6e699ccbe6f94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 *  Boost Software License - Version 1.0 - August 17th, 2003
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 *  SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 *  FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 */
#include "colour_log_formatter.h"

#include <iostream>
#include <string>

#include <boost/test/impl/execution_monitor.ipp>
#if BOOST_VERSION >= 105900
#include <boost/test/tree/test_unit.hpp>
#else
#include <boost/test/unit_test_suite_impl.hpp>
#endif
#include <boost/test/framework.hpp>
#include <boost/test/utils/basic_cstring/io.hpp>
#include <boost/test/utils/lazy_ostream.hpp>
#include <boost/version.hpp>

// ************************************************************************** //
// **************            colour_log_formatter            ************** //
// ************************************************************************** //

using namespace boost::unit_test;
namespace Wae {
namespace Test {

namespace {

const char *GREEN_BEGIN = "\033[0;32m";
const char *RED_BEGIN = "\033[0;31m";
const char *CYAN_BEGIN = "\033[0;36m";
const char *BOLD_YELLOW_BEGIN = "\033[1;33m";
const char *COLOR_END = "\033[m";

const_string
test_unit_type_name(const test_unit &tu)
{
#if BOOST_VERSION >= 105900
	return const_string(tu.p_type_name);
#else
	return tu.p_type_name.get();
#endif
}

const_string
test_unit_name(const test_unit &tu)
{
#if BOOST_VERSION >= 105900
	return const_string(tu.p_name);
#else
	return tu.p_name.get();
#endif
}

const_string
test_phase_identifier()
{
	return test_unit_name(framework::current_test_case());
}

const_string
get_basename(const const_string &file_name)
{
	return basename(file_name.begin());
}

std::string
get_basename(const std::string &file_name)
{
	return basename(file_name.c_str());
}

bool
test_unit_type_name_contains(const test_unit &tu, const std::string &substr)
{
	return test_unit_type_name(tu).find(const_string(substr)) == 0;
}

} // local namespace

//____________________________________________________________________________//

void
colour_log_formatter::log_start(
	std::ostream &output,
	counter_t test_cases_amount)
{
	if (test_cases_amount > 0)
		output  << "Running " << test_cases_amount << " test "
				<< (test_cases_amount > 1 ? "cases" : "case") << "..." << std::endl;
}

//____________________________________________________________________________//

void
colour_log_formatter::log_finish(std::ostream &ostr)
{
	ostr.flush();
}

//____________________________________________________________________________//

void
colour_log_formatter::log_build_info(std::ostream &output)
{
	output << "Platform: " << BOOST_PLATFORM << std::endl
		   << "Compiler: " << BOOST_COMPILER << std::endl
		   << "STL	 : " << BOOST_STDLIB << std::endl
		   << "Boost   : " << BOOST_VERSION / 100000 << "."
		   << BOOST_VERSION / 100 % 1000 << "."
		   << BOOST_VERSION % 100 << std::endl;
}



//____________________________________________________________________________//

void
colour_log_formatter::log_build_info(std::ostream &output, bool log_build_info)
{
	if (log_build_info)
		output  << "Platform: " << BOOST_PLATFORM            << '\n'
				<< "Compiler: " << BOOST_COMPILER            << '\n'
				<< "STL     : " << BOOST_STDLIB              << '\n';

	output << "Boost   : " << BOOST_VERSION / 100000      << '.'
			<< BOOST_VERSION / 100 % 1000  << '.'
			<< BOOST_VERSION % 100       << std::endl;
}

//____________________________________________________________________________//

void
colour_log_formatter::test_unit_start(
	std::ostream &output,
	test_unit const &tu)
{
	if (test_unit_type_name_contains(tu, "suite")) {
		output << "Starting test ";
	} else {
		output << "Running test ";
	}
	output << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
			   std::endl;
}

//____________________________________________________________________________//

void
colour_log_formatter::test_unit_finish(
	std::ostream &output,
	test_unit const &tu,
	unsigned long elapsed)
{
	if (test_unit_type_name_contains(tu, "suite")) {
		output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" <<
			   std::endl;
		return;
	}

	std::string color = GREEN_BEGIN;
	std::string status = "OK";

	if (m_isTestCaseFailed) {
		color = RED_BEGIN;
		status = "FAIL";
	}

	output << "\t" << "[   " << color << status << COLOR_END << "   ]";

	output << ", " << CYAN_BEGIN << "time: ";

	if (elapsed > 0) {
		if (elapsed % 1000 == 0)
			output << elapsed / 1000 << "ms";
		else
			output << elapsed << "mks";
	} else {
		output << "N/A";
	}

	output << COLOR_END << std::endl;
	m_isTestCaseFailed = false;
}

//____________________________________________________________________________//

void
colour_log_formatter::test_unit_skipped(
	std::ostream &output,
	test_unit const &tu)
{
	output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu)
		   << "\"" << "is skipped" << std::endl;
}

//____________________________________________________________________________//

void
colour_log_formatter::log_exception(
	std::ostream &output,
	log_checkpoint_data const &checkpoint_data,
	boost::execution_exception const &ex)
{
	boost::execution_exception::location const &loc = ex.where();
	output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
		   << '(' << loc.m_line_num << "), ";

	output << "fatal error in \""
		   << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
		   "\": ";

	output << COLOR_END << ex.what();

	if (!checkpoint_data.m_file_name.is_empty()) {
		output << '\n';
		output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
			   << '(' << checkpoint_data.m_line_num << ")";

		if (!checkpoint_data.m_message.empty())
			output << ": " << checkpoint_data.m_message;
	}

	output << std::endl;
	m_isTestCaseFailed = true;
}

void
colour_log_formatter::log_exception_start(
	std::ostream &output,
	log_checkpoint_data const &checkpoint_data,
	boost::execution_exception const &ex)
{
	boost::execution_exception::location const &loc = ex.where();
	output << '\t' << BOLD_YELLOW_BEGIN << get_basename(loc.m_file_name)
		   << '(' << loc.m_line_num << "), ";

	output << "fatal error in \""
		   << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) <<
		   "\": ";

	output << COLOR_END << ex.what();

	if (!checkpoint_data.m_file_name.is_empty()) {
		output << '\n';
		output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name)
			   << '(' << checkpoint_data.m_line_num << ")";

		if (!checkpoint_data.m_message.empty())
			output << ": " << checkpoint_data.m_message;
	}

	output << std::endl;
	m_isTestCaseFailed = true;
}

void
colour_log_formatter::log_exception_finish(std::ostream &os)
{
	os.flush();
}

//____________________________________________________________________________//

void
colour_log_formatter::log_entry_start(
	std::ostream &output,
	log_entry_data const &entry_data,
	log_entry_types let)
{
	switch (let) {
	case BOOST_UTL_ET_INFO:
		output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num <<
			   "), ";
		output << "info: ";
		break;

	case BOOST_UTL_ET_MESSAGE:
		break;

	case BOOST_UTL_ET_WARNING:
		output << '\t' << get_basename(entry_data.m_file_name) << '(' <<
			   entry_data.m_line_num << "), ";
		output << "warning in \"" << test_phase_identifier() << "\": ";
		break;

	case BOOST_UTL_ET_ERROR:
		output << '\t' << BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
			   << '(' << entry_data.m_line_num << "), ";
		output << "error in \"" << test_phase_identifier() << "\": ";
		m_isTestCaseFailed = true;
		break;

	case BOOST_UTL_ET_FATAL_ERROR:
		output << '\t' << BOLD_YELLOW_BEGIN <<  get_basename(entry_data.m_file_name)
			   << '(' << entry_data.m_line_num << "),  ";
		output <<  " fatal error in \"" << test_phase_identifier() << "\": ";
		m_isTestCaseFailed = true;
		break;
	}

	output << COLOR_END;
}

//____________________________________________________________________________//

void
colour_log_formatter::log_entry_value(
	std::ostream &output,
	const_string value)
{
	output << value;
}

//____________________________________________________________________________//

void
colour_log_formatter::log_entry_value(
	std::ostream &output,
	lazy_ostream const &value)
{
	output << value;
}

//____________________________________________________________________________//

void
colour_log_formatter::log_entry_finish(
	std::ostream &output)
{
	output << std::endl;
}

//____________________________________________________________________________//

void
colour_log_formatter::entry_context_start(
		std::ostream& output,
		boost::unit_test::log_level l)
{
	output << (l == log_successful_tests ? "\nAssertion" : "\nFailure")
		   << " occurred in a following context:";
}

void
colour_log_formatter::log_entry_context(
		std::ostream& output,
		boost::unit_test::const_string value)
{
	output << "\n    " << value;
}

void
colour_log_formatter::entry_context_finish(std::ostream& output)
{
	output.flush();
}

#if BOOST_VERSION >= 106500
void
colour_log_formatter::log_entry_context(std::ostream& os, log_level l, const_string value)
{
	(void)l;
	os << "\n    " << value;
}
void
colour_log_formatter::entry_context_finish(std::ostream& os, log_level l)
{
	(void)l;
	os.flush();
}
#endif

//____________________________________________________________________________//
} // namespace Test
} // namespace Wae

//____________________________________________________________________________//