collection.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <config.h>
  37. #include <libtest/common.h>
  38. static test_return_t runner_code(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. {
  80. fatal_assert(arg);
  81. }
  82. test_return_t Collection::exec()
  83. {
  84. Out << "Collection: " << _name;
  85. if (test_success(_frame->runner()->pre(_pre, _frame->creators_ptr())))
  86. {
  87. for (test_st *run= _tests; run->name; run++)
  88. {
  89. if (_frame->match(run->name))
  90. {
  91. continue;
  92. }
  93. _total++;
  94. test_return_t return_code;
  95. try
  96. {
  97. if (run->requires_flush)
  98. {
  99. if (test_failed(_frame->runner()->flush(_frame->creators_ptr())))
  100. {
  101. Error << "frame->runner()->flush(creators_ptr)";
  102. _skipped++;
  103. continue;
  104. }
  105. }
  106. return_code= runner_code(_frame, run, _timer);
  107. }
  108. catch (libtest::fatal &e)
  109. {
  110. Error << "Fatal exception was thrown: " << e.what();
  111. stream::cerr(__FILE__, __LINE__, __func__) << e.what();
  112. _failed++;
  113. throw;
  114. }
  115. switch (return_code)
  116. {
  117. case TEST_SUCCESS:
  118. Out << "\tTesting "
  119. << run->name
  120. << "\t\t\t\t\t"
  121. << _timer
  122. << " [ " << test_strerror(return_code) << " ]";
  123. _success++;
  124. break;
  125. case TEST_FAILURE:
  126. _failed++;
  127. Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
  128. break;
  129. case TEST_SKIPPED:
  130. _skipped++;
  131. Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
  132. break;
  133. default:
  134. fatal_message("invalid return code");
  135. }
  136. #if 0
  137. @TODO add code here to allow for a collection to define a method to reset to allow tests to continue.
  138. #endif
  139. }
  140. (void) _frame->runner()->post(_post, _frame->creators_ptr());
  141. }
  142. if (_failed == 0 and _skipped == 0 and _success)
  143. {
  144. return TEST_SUCCESS;
  145. }
  146. if (_failed)
  147. {
  148. return TEST_FAILURE;
  149. }
  150. fatal_assert(_skipped or _success == 0);
  151. return TEST_SKIPPED;
  152. }
  153. } // namespace libtest