1 // ---
2 //
3 // $Id: textoutput.cpp,v 1.4 2008/07/15 20:33:31 hartwork Exp $
4 //
5 // CppTest - A C++ Unit Testing Framework
6 // Copyright (c) 2003 Niklas Lundell
7 //
8 // ---
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA 02111-1307, USA.
24 //
25 // ---
26 
27 #include <algorithm>
28 
29 #if (defined(__WIN32__) || defined(WIN32))
30 # include "winconfig.h"
31 #else
32 # include "config.h"
33 #endif
34 
35 #include "cpptest-textoutput.h"
36 #include "cpptest-time.h"
37 #include "utils.h"
38 
39 using namespace std;
40 
41 namespace Test
42 {
43 	namespace
44 	{
45 		// Outputs detailed assert source information. Used in verbose mode.
46 		//
47 		struct ShowSource
48 		{
49 			ostream& _stream;
ShowSourceTest::__anon1524dfa60111::ShowSource50 			ShowSource(ostream& stream) : _stream(stream) {}
operator ()Test::__anon1524dfa60111::ShowSource51 			void operator()(const Source& s)
52 			{
53 				_stream << "\tTest:    " << s.test()    << endl
54 						<< "\tSuite:   " << s.suite()   << endl
55 						<< "\tFile:    " << s.file()    << endl
56 						<< "\tLine:    " << s.line()    << endl
57 						<< "\tMessage: " << s.message() << endl << endl;
58 
59 			}
60 		};
61 
62 	} // anonymous namespace
63 
64 	/// Constructs a text output handler.
65 	///
66 	/// \param mode   Output mode.
67 	/// \param stream Stream to output to.
68 	///
TextOutput(Mode mode,ostream & stream)69 	TextOutput::TextOutput(Mode mode, ostream& stream)
70 	:   _mode(mode),
71 		_stream(stream),
72 		_total_errors(0)
73 	{}
74 
75 	void
finished(int tests,const Time & time)76 	TextOutput::finished(int tests, const Time& time)
77 	{
78 		_stream	<< "Total: " << tests << " tests, "
79 				<< correct(tests, _total_errors) << "% correct"
80 				<< " in " << time << " seconds" << endl;
81 	}
82 
83 	void
suite_start(int tests,const string & name)84 	TextOutput::suite_start(int tests, const string& name)
85 	{
86 		if (tests > 0)
87 		{
88 			_suite_name  = name;
89 			_suite_tests = _suite_errors = 0;
90 			_suite_total_tests = tests;
91 			_suite_error_list.clear();
92 
93 			_stream	<< _suite_name << ": "
94 					<< "0/" << _suite_total_tests
95 					<< "\r" << flush;
96 		}
97 	}
98 
99 	void
suite_end(int tests,const string & name,const Time & time)100 	TextOutput::suite_end(int tests, const string& name, const Time& time)
101 	{
102 		if (tests > 0)
103 		{
104 			_stream	<< name << ": " << tests << "/" << tests << ", "
105 					<< correct(tests, _suite_errors) << "% correct"
106 					<< " in " << time << " seconds" << endl;
107 
108 			if (_mode == Verbose && _suite_errors)
109 				for_each(_suite_error_list.begin(), _suite_error_list.end(),
110 						 ShowSource(_stream));
111 
112 			_total_errors += _suite_errors;
113 		}
114 	}
115 
116 	void
test_end(const string &,bool ok,const Time &)117 	TextOutput::test_end(const string&, bool ok, const Time&)
118 	{
119 		_stream	<< _suite_name << ": "
120 				<< ++_suite_tests << "/" << _suite_total_tests
121 				<< "\r" << flush;
122 		if (!ok)
123 			++_suite_errors;
124 	}
125 
126 	void
assertment(const Source & s)127 	TextOutput::assertment(const Source& s)
128 	{
129 		_suite_error_list.push_back(s);
130 	}
131 
132 } // namespace Test
133