e0a347a05e862607dad7086cee6cb99f27b07105
[m6w6/libmemcached] / libtest / formatter.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "mem_config.h"
38
39 #include <libtest/common.h>
40
41 #include <iostream>
42 #include <fstream>
43
44 namespace libtest {
45
46 class TestCase {
47 public:
48 TestCase(const std::string& arg):
49 _name(arg),
50 _result(TEST_FAILURE)
51 {
52 }
53
54 const std::string& name() const
55 {
56 return _name;
57 }
58
59 test_return_t result() const
60 {
61 return _result;
62 }
63
64 void result(test_return_t arg)
65 {
66 _result= arg;
67 }
68
69 void result(test_return_t arg, const libtest::Timer& timer_)
70 {
71 _result= arg;
72 _timer= timer_;
73 }
74
75 const libtest::Timer& timer() const
76 {
77 return _timer;
78 }
79
80 void timer(libtest::Timer& arg)
81 {
82 _timer= arg;
83 }
84
85 private:
86 std::string _name;
87 test_return_t _result;
88 libtest::Timer _timer;
89 };
90
91 Formatter::Formatter(const std::string& frame_name, const std::string& arg)
92 {
93 _suite_name= frame_name;
94 _suite_name+= ".";
95 _suite_name+= arg;
96 }
97
98 Formatter::~Formatter()
99 {
100 for (TestCases::iterator iter= _testcases.begin(); iter != _testcases.end(); ++iter)
101 {
102 delete *iter;
103 }
104 }
105
106 TestCase* Formatter::current()
107 {
108 return _testcases.back();
109 }
110
111 void Formatter::skipped()
112 {
113 current()->result(TEST_SKIPPED);
114 Out << name() << "." << current()->name() << "\t\t\t\t\t" << "[ " << test_strerror(current()->result()) << " ]";
115
116 reset();
117 }
118
119 void Formatter::failed()
120 {
121 assert(current());
122 current()->result(TEST_FAILURE);
123
124 Out << name() << "." << current()->name() << "\t\t\t\t\t" << "[ " << test_strerror(current()->result()) << " ]";
125
126 reset();
127 }
128
129 void Formatter::success(const libtest::Timer& timer_)
130 {
131 assert(current());
132 current()->result(TEST_SUCCESS, timer_);
133
134 Out << name() << "."
135 << current()->name()
136 << "\t\t\t\t\t"
137 << current()->timer()
138 << " [ " << test_strerror(current()->result()) << " ]";
139
140 reset();
141 }
142
143 void Formatter::xml(libtest::Framework& framework_, std::ofstream& output)
144 {
145 output << "<testsuites name=\"" << framework_.name() << "\">" << std::endl;
146 for (Suites::iterator framework_iter= framework_.suites().begin();
147 framework_iter != framework_.suites().end();
148 ++framework_iter)
149 {
150 output << "\t<testsuite name=\"" << (*framework_iter)->name() << "\" classname=\"\" package=\"\">" << std::endl;
151
152 for (TestCases::iterator case_iter= (*framework_iter)->formatter()->testcases().begin();
153 case_iter != (*framework_iter)->formatter()->testcases().end();
154 ++case_iter)
155 {
156 output << "\t\t<testcase name=\""
157 << (*case_iter)->name()
158 << "\" time=\""
159 << (*case_iter)->timer().elapsed_milliseconds()
160 << "\">"
161 << std::endl;
162
163 switch ((*case_iter)->result())
164 {
165 case TEST_SKIPPED:
166 output << "\t\t <skipped/>" << std::endl;
167 break;
168
169 case TEST_FAILURE:
170 output << "\t\t <failure message=\"\" type=\"\"/>"<< std::endl;
171 break;
172
173 case TEST_SUCCESS:
174 break;
175 }
176 output << "\t\t</testcase>" << std::endl;
177 }
178 output << "\t</testsuite>" << std::endl;
179 }
180 output << "</testsuites>" << std::endl;
181 }
182
183 void Formatter::push_testcase(const std::string& arg)
184 {
185 assert(_suite_name.empty() == false);
186 TestCase* _current_testcase= new TestCase(arg);
187 _testcases.push_back(_current_testcase);
188 }
189
190 void Formatter::reset()
191 {
192 }
193 } // namespace libtest