collection.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. static test_return_t runner_code(libtest::Framework* frame,
  39. test_st* run,
  40. libtest::Timer& _timer)
  41. { // Runner Code
  42. assert(frame->runner());
  43. assert(run->test_fn);
  44. test_return_t return_code;
  45. try
  46. {
  47. _timer.reset();
  48. return_code= frame->runner()->run(run->test_fn, frame->creators_ptr());
  49. }
  50. // Special case where check for the testing of the exception
  51. // system.
  52. catch (libtest::fatal &e)
  53. {
  54. if (libtest::fatal::is_disabled())
  55. {
  56. libtest::fatal::increment_disabled_counter();
  57. return_code= TEST_SUCCESS;
  58. }
  59. else
  60. {
  61. throw;
  62. }
  63. }
  64. _timer.sample();
  65. return return_code;
  66. }
  67. namespace libtest {
  68. Collection::Collection(Framework* frame_arg,
  69. collection_st* arg) :
  70. _name(arg->name),
  71. _pre(arg->pre),
  72. _post(arg->post),
  73. _tests(arg->tests),
  74. _frame(frame_arg),
  75. _success(0),
  76. _skipped(0),
  77. _failed(0),
  78. _total(0),
  79. _formatter(_name)
  80. {
  81. fatal_assert(arg);
  82. }
  83. test_return_t Collection::exec()
  84. {
  85. if (test_success(_frame->runner()->pre(_pre, _frame->creators_ptr())))
  86. {
  87. for (test_st *run= _tests; run->name; run++)
  88. {
  89. formatter()->push_testcase(run->name);
  90. if (_frame->match(run->name))
  91. {
  92. formatter()->skipped();
  93. continue;
  94. }
  95. _total++;
  96. test_return_t return_code;
  97. try
  98. {
  99. if (run->requires_flush)
  100. {
  101. if (test_failed(_frame->runner()->flush(_frame->creators_ptr())))
  102. {
  103. Error << "frame->runner()->flush(creators_ptr)";
  104. _skipped++;
  105. formatter()->skipped();
  106. continue;
  107. }
  108. }
  109. set_alarm();
  110. try
  111. {
  112. return_code= runner_code(_frame, run, _timer);
  113. }
  114. catch (...)
  115. {
  116. cancel_alarm();
  117. throw;
  118. }
  119. libtest::cancel_alarm();
  120. }
  121. catch (libtest::fatal &e)
  122. {
  123. stream::cerr(e.file(), e.line(), e.func()) << e.what();
  124. _failed++;
  125. formatter()->failed();
  126. throw;
  127. }
  128. switch (return_code)
  129. {
  130. case TEST_SUCCESS:
  131. _success++;
  132. formatter()->success(_timer);
  133. break;
  134. case TEST_FAILURE:
  135. _failed++;
  136. formatter()->failed();
  137. break;
  138. case TEST_SKIPPED:
  139. _skipped++;
  140. formatter()->skipped();
  141. break;
  142. default:
  143. fatal_message("invalid return code");
  144. }
  145. #if 0
  146. @TODO add code here to allow for a collection to define a method to reset to allow tests to continue.
  147. #endif
  148. }
  149. (void) _frame->runner()->post(_post, _frame->creators_ptr());
  150. }
  151. if (_failed == 0 and _skipped == 0 and _success)
  152. {
  153. return TEST_SUCCESS;
  154. }
  155. if (_failed)
  156. {
  157. return TEST_FAILURE;
  158. }
  159. fatal_assert(_skipped or _success == 0);
  160. return TEST_SKIPPED;
  161. }
  162. } // namespace libtest