runner.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. namespace libtest {
  39. Runner::Runner() :
  40. _servers(NULL)
  41. {
  42. }
  43. test_return_t Runner::main(test_callback_fn* func, void *object)
  44. {
  45. test_return_t ret;
  46. try {
  47. ret= run(func, object);
  48. }
  49. catch (const libtest::__skipped& e)
  50. {
  51. ret= TEST_SKIPPED;
  52. }
  53. catch (const libtest::__failure& e)
  54. {
  55. libtest::stream::make_cerr(e.file(), e.line(), e.func()) << e.what();
  56. ret= TEST_FAILURE;
  57. }
  58. catch (const libtest::__success&)
  59. {
  60. ret= TEST_SUCCESS;
  61. }
  62. catch (const libtest::fatal&)
  63. {
  64. throw;
  65. }
  66. catch (const std::exception& e)
  67. {
  68. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << e.what();
  69. throw;
  70. }
  71. catch (...)
  72. {
  73. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << "Unknown exception thrown";
  74. throw;
  75. }
  76. return ret;
  77. }
  78. test_return_t Runner::setup(test_callback_fn* func, void *object)
  79. {
  80. test_return_t ret;
  81. try {
  82. ret= pre(func, object);
  83. }
  84. catch (const libtest::__skipped& e)
  85. {
  86. ret= TEST_SKIPPED;
  87. }
  88. catch (const libtest::__failure& e)
  89. {
  90. libtest::stream::make_cout(e.file(), e.line(), e.func()) << e.what();
  91. ret= TEST_FAILURE;
  92. }
  93. catch (const libtest::__success&)
  94. {
  95. ret= TEST_SUCCESS;
  96. }
  97. catch (const libtest::fatal& e)
  98. {
  99. throw;
  100. }
  101. catch (const std::exception& e)
  102. {
  103. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << e.what();
  104. throw;
  105. }
  106. catch (...)
  107. {
  108. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << "Unknown exception thrown";
  109. throw;
  110. }
  111. return ret;
  112. }
  113. test_return_t Runner::teardown(test_callback_fn* func, void *object)
  114. {
  115. test_return_t ret;
  116. try {
  117. ret= post(func, object);
  118. }
  119. catch (const libtest::__skipped& e)
  120. {
  121. ret= TEST_SKIPPED;
  122. }
  123. catch (const libtest::__failure& e)
  124. {
  125. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << e.what();
  126. ret= TEST_FAILURE;
  127. }
  128. catch (const libtest::__success&)
  129. {
  130. ret= TEST_SUCCESS;
  131. }
  132. catch (const libtest::fatal& e)
  133. {
  134. throw;
  135. }
  136. catch (const std::exception& e)
  137. {
  138. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << e.what();
  139. throw;
  140. }
  141. catch (...)
  142. {
  143. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << "Unknown exception thrown";
  144. throw;
  145. }
  146. return ret;
  147. }
  148. test_return_t Runner::flush(void*)
  149. {
  150. return TEST_SUCCESS;
  151. }
  152. test_return_t Runner::run(test_callback_fn* func, void *object)
  153. {
  154. if (func)
  155. {
  156. return func(object);
  157. }
  158. return TEST_SUCCESS;
  159. }
  160. test_return_t Runner::pre(test_callback_fn* func, void *object)
  161. {
  162. if (func)
  163. {
  164. return func(object);
  165. }
  166. return TEST_SUCCESS;
  167. }
  168. test_return_t Runner::post(test_callback_fn* func, void *object)
  169. {
  170. if (func)
  171. {
  172. return func(object);
  173. }
  174. return TEST_SUCCESS;
  175. }
  176. void Runner::set_servers(libtest::server_startup_st& arg)
  177. {
  178. _servers= &arg;
  179. }
  180. bool Runner::check()
  181. {
  182. return _servers ? _servers->check() : true;
  183. }
  184. } // namespace libtest