Fix bad if path
[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 "libtest/yatlcon.h"
38
39 #include <libtest/common.h>
40
41 #include <algorithm>
42 #include <fstream>
43 #include <iostream>
44
45 namespace libtest {
46
47 class TestCase {
48 public:
49 TestCase(const std::string& arg):
50 _name(arg),
51 _result(TEST_FAILURE)
52 {
53 }
54
55 const std::string& name() const
56 {
57 return _name;
58 }
59
60 test_return_t result() const
61 {
62 return _result;
63 }
64
65 void result(test_return_t arg)
66 {
67 _result= arg;
68 }
69
70 void result(test_return_t arg, const libtest::Timer& timer_)
71 {
72 _result= arg;
73 _timer= timer_;
74 }
75
76 const libtest::Timer& timer() const
77 {
78 return _timer;
79 }
80
81 void timer(libtest::Timer& arg)
82 {
83 _timer= arg;
84 }
85
86 private:
87 std::string _name;
88 test_return_t _result;
89 libtest::Timer _timer;
90 };
91
92 Formatter::Formatter(const std::string& frame_name, const std::string& arg)
93 {
94 _suite_name= frame_name;
95 _suite_name+= ".";
96 _suite_name+= arg;
97 }
98
99 Formatter::~Formatter()
100 {
101 std::for_each(_testcases.begin(), _testcases.end(), DeleteFromVector());
102 _testcases.clear();
103 }
104
105 TestCase* Formatter::current()
106 {
107 return _testcases.back();
108 }
109
110 void Formatter::skipped()
111 {
112 current()->result(TEST_SKIPPED);
113 Out << name() << "." << current()->name() << "\t\t\t\t\t" << "[ " << test_strerror(current()->result()) << " ]";
114
115 reset();
116 }
117
118 void Formatter::failed()
119 {
120 assert(current());
121 current()->result(TEST_FAILURE);
122
123 Out << name() << "." << current()->name() << "\t\t\t\t\t" << "[ " << test_strerror(current()->result()) << " ]";
124
125 reset();
126 }
127
128 void Formatter::success(const libtest::Timer& timer_)
129 {
130 assert(current());
131 current()->result(TEST_SUCCESS, timer_);
132
133 Out << name() << "."
134 << current()->name()
135 << "\t\t\t\t\t"
136 << current()->timer()
137 << " [ " << test_strerror(current()->result()) << " ]";
138
139 reset();
140 }
141
142 void Formatter::xml(libtest::Framework& framework_, std::ofstream& output)
143 {
144 output << "<testsuites name=\"" << framework_.name() << "\">" << std::endl;
145 for (Suites::iterator framework_iter= framework_.suites().begin();
146 framework_iter != framework_.suites().end();
147 ++framework_iter)
148 {
149 output << "\t<testsuite name=\"" << (*framework_iter)->name() << "\" classname=\"\" package=\"\">" << std::endl;
150
151 for (TestCases::iterator case_iter= (*framework_iter)->formatter()->testcases().begin();
152 case_iter != (*framework_iter)->formatter()->testcases().end();
153 ++case_iter)
154 {
155 output << "\t\t<testcase name=\""
156 << (*case_iter)->name()
157 << "\" time=\""
158 << (*case_iter)->timer().elapsed_milliseconds()
159 << "\">"
160 << std::endl;
161
162 switch ((*case_iter)->result())
163 {
164 case TEST_SKIPPED:
165 output << "\t\t <skipped/>" << std::endl;
166 break;
167
168 case TEST_FAILURE:
169 output << "\t\t <failure message=\"\" type=\"\"/>"<< std::endl;
170 break;
171
172 case TEST_SUCCESS:
173 break;
174 }
175 output << "\t\t</testcase>" << std::endl;
176 }
177 output << "\t</testsuite>" << std::endl;
178 }
179 output << "</testsuites>" << std::endl;
180 }
181
182 void Formatter::push_testcase(const std::string& arg)
183 {
184 assert(_suite_name.empty() == false);
185 TestCase* _current_testcase= new TestCase(arg);
186 _testcases.push_back(_current_testcase);
187 }
188
189 void Formatter::reset()
190 {
191 }
192 } // namespace libtest