formatter.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #include "libtest/yatlcon.h"
  37. #include <libtest/common.h>
  38. #include <algorithm>
  39. #include <fstream>
  40. #include <iostream>
  41. namespace {
  42. std::string& escape4XML(std::string const& arg, std::string& escaped_string)
  43. {
  44. escaped_string.clear();
  45. escaped_string+= '"';
  46. for (std::string::const_iterator x= arg.begin(), end= arg.end(); x != end; ++x)
  47. {
  48. unsigned char c= *x;
  49. if (c == '&')
  50. {
  51. escaped_string+= "&amp;";
  52. }
  53. else if (c == '>')
  54. {
  55. escaped_string+= "&gt;";
  56. }
  57. else if (c == '<')
  58. {
  59. escaped_string+= "&lt;";
  60. }
  61. else if (c == '\'')
  62. {
  63. escaped_string+= "&apos;"; break;
  64. }
  65. else if (c == '"')
  66. {
  67. escaped_string+= "&quot;";
  68. }
  69. else if (c == ' ')
  70. {
  71. escaped_string+= ' ';
  72. }
  73. else if (isalnum(c))
  74. {
  75. escaped_string+= c;
  76. }
  77. else
  78. {
  79. char const* const hexdig= "0123456789ABCDEF";
  80. escaped_string+= "&#x";
  81. escaped_string+= hexdig[c >> 4];
  82. escaped_string+= hexdig[c & 0xF];
  83. escaped_string+= ';';
  84. }
  85. }
  86. escaped_string+= '"';
  87. return escaped_string;
  88. }
  89. }
  90. namespace libtest {
  91. Formatter::Formatter(const Framework* frame_, std::ostream& output_):
  92. _frame(frame_),
  93. _output(output_)
  94. {
  95. }
  96. const std::string& Formatter::name() const
  97. {
  98. return _frame->name();
  99. }
  100. Legacy::Legacy(const Framework* frame_, std::ostream& output_):
  101. Formatter(frame_, output_),
  102. _collection(NULL)
  103. {
  104. }
  105. Legacy::~Legacy()
  106. {
  107. if (getenv("YATL_SUMMARY"))
  108. {
  109. Outn();
  110. Out << "Tests\t\t\t\t\t" << _frame->total();
  111. Out << "\tFailed\t\t\t\t\t" << _frame->failed();
  112. Out << "\tSkipped\t\t\t\t\t" << _frame->skipped();
  113. Out << "\tSucceeded\t\t\t\t" << _frame->success();
  114. }
  115. }
  116. void Legacy::plan(const Collection* collection)
  117. {
  118. _collection= collection;
  119. }
  120. void Legacy::report(const libtest::TestCase* test, size_t) const
  121. {
  122. switch (test->result())
  123. {
  124. case TEST_SUCCESS:
  125. Out << name() << "."
  126. << _collection->name() << "."
  127. << test->name()
  128. << "\t\t\t\t\t"
  129. << test->timer()
  130. << " [ " << test_strerror(test->result()) << " ]";
  131. break;
  132. case TEST_FAILURE:
  133. Out << name() << "."
  134. << _collection->name() << "."
  135. << test->name()
  136. << "\t\t\t\t\t" << "[ " << test_strerror(test->result()) << " ]";
  137. break;
  138. case TEST_SKIPPED:
  139. Out << name() << "."
  140. << _collection->name() << "."
  141. << test->name()
  142. << "\t\t\t\t\t" << "[ " << test_strerror(test->result()) << " ]";
  143. break;
  144. }
  145. }
  146. Junit::Junit(const Framework* frame_, std::ostream& output_):
  147. Formatter(frame_, output_)
  148. {
  149. std::string escaped_string;
  150. _output << "<testsuites name=" << escape4XML(name(), escaped_string) << ">" << std::endl;
  151. }
  152. Junit::~Junit()
  153. {
  154. _output << "</testsuites>" << std::endl;
  155. }
  156. void Junit::report(const libtest::TestCase* test, size_t) const
  157. {
  158. std::string escaped_string;
  159. _output << "\t\t<testcase name="
  160. << escape4XML(test->name(), escaped_string)
  161. << " time=\""
  162. << test->timer()
  163. << "\">"
  164. << std::endl;
  165. switch (test->result())
  166. {
  167. case TEST_SKIPPED:
  168. _output << "\t\t <skipped/>" << std::endl;
  169. break;
  170. case TEST_FAILURE:
  171. _output << "\t\t <failure message=\"\" type=\"\"/>"<< std::endl;
  172. break;
  173. case TEST_SUCCESS:
  174. break;
  175. }
  176. _output << "\t\t</testcase>" << std::endl;
  177. }
  178. void Junit::plan(const Collection* collection)
  179. {
  180. std::string escaped_string;
  181. _output << "\t<testsuite name="
  182. << escape4XML(collection->name(), escaped_string)
  183. << ">" << std::endl;
  184. #if 0
  185. << "\" classname=\"\" package=\"\">" << std::endl;
  186. #endif
  187. }
  188. void Junit::complete()
  189. {
  190. _output << "\t</testsuite>" << std::endl;
  191. }
  192. TAP::TAP(const Framework* frame_, std::ostream& output_):
  193. Formatter(frame_, output_)
  194. {
  195. }
  196. TAP::~TAP()
  197. {
  198. }
  199. void TAP::report(const libtest::TestCase* test, size_t position) const
  200. {
  201. assert(test);
  202. switch (test->result())
  203. {
  204. case TEST_SUCCESS:
  205. _output << "ok " << position << " - " << test->name() << " # ";
  206. _output << test->timer();
  207. break;
  208. case TEST_FAILURE:
  209. _output << "not ok " << position << " - " << test->name() << " # ";
  210. break;
  211. case TEST_SKIPPED:
  212. _output << "ok " << position << " - # SKIP ";
  213. break;
  214. }
  215. _output << std::endl;
  216. }
  217. void TAP::plan(const Collection* collection)
  218. {
  219. _output << "0.." << collection->total() << std::endl;
  220. }
  221. #if 0
  222. void Formatter::tap(libtest::Framework& framework_, std::ofstream& output)
  223. {
  224. for (Suites::iterator framework_iter= framework_.suites().begin();
  225. framework_iter != framework_.suites().end();
  226. ++framework_iter)
  227. {
  228. output << "1.." << (*framework_iter)->formatter()->testcases().size() << " # " << (*framework_iter)->name() << std::endl;
  229. size_t test_count= 1;
  230. for (TestCases::iterator case_iter= (*framework_iter)->formatter()->testcases().begin();
  231. case_iter != (*framework_iter)->formatter()->testcases().end();
  232. ++case_iter)
  233. {
  234. switch ((*case_iter)->result())
  235. {
  236. case TEST_SKIPPED:
  237. output << "ok " << test_count << " - # SKIP ";
  238. break;
  239. case TEST_FAILURE:
  240. output << "not ok " << test_count << " - " << (*case_iter)->name() << " # ";
  241. break;
  242. case TEST_SUCCESS:
  243. output << "ok " << test_count << " - " << (*case_iter)->name() << " # ";
  244. break;
  245. }
  246. output
  247. << (*case_iter)->timer().elapsed_milliseconds()
  248. << std::endl;
  249. }
  250. }
  251. }
  252. #endif
  253. #if 0
  254. void Formatter::xml(libtest::Framework& framework_, std::ofstream& output)
  255. {
  256. output << "<testsuites name=\"" << framework_.name() << "\">" << std::endl;
  257. for (Suites::iterator framework_iter= framework_.suites().begin();
  258. framework_iter != framework_.suites().end();
  259. ++framework_iter)
  260. {
  261. output << "\t<testsuite name=\"" << (*framework_iter)->name() << "\" classname=\"\" package=\"\">" << std::endl;
  262. for (TestCases::iterator case_iter= (*framework_iter)->formatter()->testcases().begin();
  263. case_iter != (*framework_iter)->formatter()->testcases().end();
  264. ++case_iter)
  265. {
  266. output << "\t\t<testcase name=\""
  267. << (*case_iter)->name()
  268. << "\" time=\""
  269. << (*case_iter)->timer().elapsed_milliseconds()
  270. << "\">"
  271. << std::endl;
  272. switch ((*case_iter)->result())
  273. {
  274. case TEST_SKIPPED:
  275. output << "\t\t <skipped/>" << std::endl;
  276. break;
  277. case TEST_FAILURE:
  278. output << "\t\t <failure message=\"\" type=\"\"/>"<< std::endl;
  279. break;
  280. case TEST_SUCCESS:
  281. break;
  282. }
  283. output << "\t\t</testcase>" << std::endl;
  284. }
  285. output << "\t</testsuite>" << std::endl;
  286. }
  287. output << "</testsuites>" << std::endl;
  288. }
  289. #endif
  290. } // namespace libtest