You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.1KB

  1. // ---
  2. //
  3. // $Id: textoutput.cpp,v 1.4 2008/07/15 20:33:31 hartwork Exp $
  4. //
  5. // CppTest - A C++ Unit Testing Framework
  6. // Copyright (c) 2003 Niklas Lundell
  7. //
  8. // ---
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Lesser General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this library; if not, write to the
  22. // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. // Boston, MA 02111-1307, USA.
  24. //
  25. // ---
  26. #include "cpptest-textoutput.h"
  27. #include "cpptest-time.h"
  28. #include "utils.h"
  29. namespace Test
  30. {
  31. namespace
  32. {
  33. // Outputs detailed assert source information. Used in verbose mode.
  34. //
  35. struct ShowSource
  36. {
  37. std::ostream& _stream;
  38. ShowSource(std::ostream& stream) : _stream(stream) {}
  39. void operator()(const Source& s)
  40. {
  41. _stream << "\tTest: " << s.test() << std::endl
  42. << "\tSuite: " << s.suite() << std::endl
  43. << "\tFile: " << s.file() << std::endl
  44. << "\tLine: " << s.line() << std::endl
  45. << "\tMessage: " << s.message() << std::endl << std::endl;
  46. }
  47. };
  48. } // anonymous namespace
  49. /// Constructs a text output handler.
  50. ///
  51. /// \param mode Output mode.
  52. /// \param stream Stream to output to.
  53. ///
  54. TextOutput::TextOutput(Mode mode, std::ostream& stream)
  55. : _mode(mode),
  56. _stream(stream),
  57. _total_errors(0)
  58. {}
  59. void
  60. TextOutput::finished(int tests, const Time& time)
  61. {
  62. _stream << "Total: " << tests << " tests, "
  63. << correct(tests, _total_errors) << "% correct"
  64. << " in " << time << " seconds" << std::endl;
  65. }
  66. void
  67. TextOutput::suite_start(int tests, const std::string& name)
  68. {
  69. if (tests > 0)
  70. {
  71. _suite_name = name;
  72. _suite_tests = _suite_errors = 0;
  73. _suite_total_tests = tests;
  74. _suite_error_list.clear();
  75. _stream << _suite_name << ": "
  76. << "0/" << _suite_total_tests
  77. << "\r" << std::flush;
  78. }
  79. }
  80. void
  81. TextOutput::suite_end(int tests, const std::string& name, const Time& time)
  82. {
  83. if (tests > 0)
  84. {
  85. _stream << name << ": " << tests << "/" << tests << ", "
  86. << correct(tests, _suite_errors) << "% correct"
  87. << " in " << time << " seconds" << std::endl;
  88. if (_mode == Verbose && _suite_errors)
  89. std::for_each(_suite_error_list.begin(), _suite_error_list.end(),
  90. ShowSource(_stream));
  91. _total_errors += _suite_errors;
  92. }
  93. }
  94. void
  95. TextOutput::test_end(const std::string&, bool ok, const Time&)
  96. {
  97. _stream << _suite_name << ": "
  98. << ++_suite_tests << "/" << _suite_total_tests
  99. << "\r" << std::flush;
  100. if (!ok)
  101. ++_suite_errors;
  102. }
  103. void
  104. TextOutput::assertment(const Source& s)
  105. {
  106. _suite_error_list.push_back(s);
  107. }
  108. } // namespace Test