1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
37 #include "libtest/yatlcon.h"
39 #include <libtest/common.h>
48 std::string
& escape4XML(std::string
const& arg
, std::string
& escaped_string
)
50 escaped_string
.clear();
53 for (std::string::const_iterator x
= arg
.begin(), end
= arg
.end(); x
!= end
; ++x
)
58 escaped_string
+= "&";
62 escaped_string
+= ">";
66 escaped_string
+= "<";
70 escaped_string
+= "'"; break;
74 escaped_string
+= """;
86 char const* const hexdig
= "0123456789ABCDEF";
87 escaped_string
+= "&#x";
88 escaped_string
+= hexdig
[c
>> 4];
89 escaped_string
+= hexdig
[c
& 0xF];
95 return escaped_string
;
100 TestCase(const std::string
& arg
):
102 _result(TEST_FAILURE
)
106 const std::string
& name() const
111 test_return_t
result() const
116 void result(test_return_t arg
)
121 void result(test_return_t arg
, const libtest::Timer
& timer_
)
127 const libtest::Timer
& timer() const
132 void timer(libtest::Timer
& arg
)
139 test_return_t _result
;
140 libtest::Timer _timer
;
143 Formatter::Formatter(const std::string
& frame_name
, const std::string
& arg
)
145 _suite_name
= frame_name
;
150 Formatter::~Formatter()
152 std::for_each(_testcases
.begin(), _testcases
.end(), DeleteFromVector());
156 TestCase
* Formatter::current()
158 return _testcases
.back();
161 void Formatter::skipped()
164 current()->result(TEST_SKIPPED
);
167 << "[ " << test_strerror(current()->result()) << " ]"
169 << name() << "." << current()->name()
175 void Formatter::failed()
178 current()->result(TEST_FAILURE
);
181 << "[ " << test_strerror(current()->result()) << " ]"
183 << name() << "." << current()->name()
189 void Formatter::success(const libtest::Timer
& timer_
)
192 current()->result(TEST_SUCCESS
, timer_
);
195 << "[ " << test_strerror(current()->result()) << " ]"
197 << current()->timer()
199 << name() << "." << current()->name()
205 void Formatter::xml(libtest::Framework
& framework_
, std::ofstream
& output
)
207 std::string escaped_string
;
209 output
<< "<testsuites name="
210 << escape4XML(framework_
.name(), escaped_string
) << ">" << std::endl
;
212 for (Suites::iterator framework_iter
= framework_
.suites().begin();
213 framework_iter
!= framework_
.suites().end();
216 output
<< "\t<testsuite name="
217 << escape4XML((*framework_iter
)->name(), escaped_string
)
219 << " classname=\"\" package=\"\""
223 for (TestCases::iterator case_iter
= (*framework_iter
)->formatter()->testcases().begin();
224 case_iter
!= (*framework_iter
)->formatter()->testcases().end();
227 output
<< "\t\t<testcase name="
228 << escape4XML((*case_iter
)->name(), escaped_string
)
230 << (*case_iter
)->timer().elapsed_milliseconds()
233 switch ((*case_iter
)->result())
236 output
<< ">" << std::endl
;
237 output
<< "\t\t <skipped/>" << std::endl
;
238 output
<< "\t\t</testcase>" << std::endl
;
242 output
<< ">" << std::endl
;
243 output
<< "\t\t <failure message=\"\" type=\"\"/>"<< std::endl
;
244 output
<< "\t\t</testcase>" << std::endl
;
248 output
<< "/>" << std::endl
;
252 output
<< "\t</testsuite>" << std::endl
;
254 output
<< "</testsuites>" << std::endl
;
257 void Formatter::push_testcase(const std::string
& arg
)
259 assert(_suite_name
.empty() == false);
260 TestCase
* _current_testcase
= new TestCase(arg
);
261 _testcases
.push_back(_current_testcase
);
267 << name() << "." << current()->name()
272 void Formatter::reset()
275 } // namespace libtest