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>
47 std::string
& escape4XML(std::string
const& arg
, std::string
& escaped_string
)
49 escaped_string
.clear();
52 for (std::string::const_iterator x
= arg
.begin(), end
= arg
.end(); x
!= end
; ++x
)
57 escaped_string
+= "&";
61 escaped_string
+= ">";
65 escaped_string
+= "<";
69 escaped_string
+= "'"; break;
73 escaped_string
+= """;
85 char const* const hexdig
= "0123456789ABCDEF";
86 escaped_string
+= "&#x";
87 escaped_string
+= hexdig
[c
>> 4];
88 escaped_string
+= hexdig
[c
& 0xF];
94 return escaped_string
;
99 TestCase(const std::string
& arg
):
101 _result(TEST_FAILURE
)
105 const std::string
& name() const
110 test_return_t
result() const
115 void result(test_return_t arg
)
120 void result(test_return_t arg
, const libtest::Timer
& timer_
)
126 const libtest::Timer
& timer() const
131 void timer(libtest::Timer
& arg
)
138 test_return_t _result
;
139 libtest::Timer _timer
;
142 Formatter::Formatter(const std::string
& frame_name
, const std::string
& arg
)
144 _suite_name
= frame_name
;
149 Formatter::~Formatter()
151 std::for_each(_testcases
.begin(), _testcases
.end(), DeleteFromVector());
155 TestCase
* Formatter::current()
157 return _testcases
.back();
160 void Formatter::skipped()
162 current()->result(TEST_SKIPPED
);
166 << "[ " << test_strerror(current()->result()) << " ]";
171 void Formatter::failed()
174 current()->result(TEST_FAILURE
);
177 << "." << current()->name() << "\t\t\t\t\t"
178 << "[ " << test_strerror(current()->result()) << " ]";
183 void Formatter::success(const libtest::Timer
& timer_
)
186 current()->result(TEST_SUCCESS
, timer_
);
187 std::string escaped_string
;
192 << current()->timer()
193 << " [ " << test_strerror(current()->result()) << " ]";
198 void Formatter::xml(libtest::Framework
& framework_
, std::ofstream
& output
)
200 std::string escaped_string
;
202 output
<< "<testsuites name="
203 << escape4XML(framework_
.name(), escaped_string
) << ">" << std::endl
;
205 for (Suites::iterator framework_iter
= framework_
.suites().begin();
206 framework_iter
!= framework_
.suites().end();
209 output
<< "\t<testsuite name="
210 << escape4XML((*framework_iter
)->name(), escaped_string
)
212 << " classname=\"\" package=\"\""
216 for (TestCases::iterator case_iter
= (*framework_iter
)->formatter()->testcases().begin();
217 case_iter
!= (*framework_iter
)->formatter()->testcases().end();
220 output
<< "\t\t<testcase name="
221 << escape4XML((*case_iter
)->name(), escaped_string
)
223 << (*case_iter
)->timer().elapsed_milliseconds()
226 switch ((*case_iter
)->result())
229 output
<< ">" << std::endl
;
230 output
<< "\t\t <skipped/>" << std::endl
;
231 output
<< "\t\t</testcase>" << std::endl
;
235 output
<< ">" << std::endl
;
236 output
<< "\t\t <failure message=\"\" type=\"\"/>"<< std::endl
;
237 output
<< "\t\t</testcase>" << std::endl
;
241 output
<< "/>" << std::endl
;
245 output
<< "\t</testsuite>" << std::endl
;
247 output
<< "</testsuites>" << std::endl
;
250 void Formatter::push_testcase(const std::string
& arg
)
252 assert(_suite_name
.empty() == false);
253 TestCase
* _current_testcase
= new TestCase(arg
);
254 _testcases
.push_back(_current_testcase
);
257 void Formatter::reset()
260 } // namespace libtest