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.

129 lines
3.2KB

  1. // ---
  2. //
  3. // $Id: compileroutput.cpp,v 1.3 2005/06/08 08:08:06 nilu 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-compileroutput.h"
  27. #include "cpptest-source.h"
  28. namespace Test
  29. {
  30. namespace
  31. {
  32. // Checks for output format modifiers.
  33. //
  34. bool
  35. check_format(const std::string& format,
  36. std::string::size_type& pos,
  37. const std::string& mod,
  38. int& mod_cnt)
  39. {
  40. if (format.compare(pos, mod.size(), mod) == 0)
  41. {
  42. if (++mod_cnt > 1)
  43. throw Test::CompilerOutput::InvalidFormat(format);
  44. pos += mod.size();
  45. return true;
  46. }
  47. return false;
  48. }
  49. } // anonymous namespace
  50. /// Constructs a compiler output handler.
  51. ///
  52. /// \param format Pre-defined compiler output format.
  53. /// \param stream Stream to output to.
  54. ///
  55. CompilerOutput::CompilerOutput(Format format, std::ostream& stream)
  56. : Output(),
  57. _stream(stream)
  58. {
  59. static const char* table[] =
  60. {
  61. "%file:%line: %text", // Generic
  62. "Error cpptest %file %line: %text", // BCC
  63. "%file:%line: %text", // GCC
  64. "%file(%line) : %text" // MSVC
  65. };
  66. _format = table[format];
  67. }
  68. /// Constructs a compiler output handler.
  69. ///
  70. /// \param format %Output format to use.
  71. /// \param stream Stream to output to.
  72. ///
  73. /// \exception InvalidFormat Invalid format specified.
  74. ///
  75. CompilerOutput::CompilerOutput(const std::string& format, std::ostream& stream)
  76. : Output(),
  77. _format(format),
  78. _stream(stream)
  79. {
  80. int expr(0), file(0), line(0);
  81. for (std::string::size_type pos = 0;
  82. (pos = _format.find_first_of('%', pos)) != std::string::npos; )
  83. {
  84. ++pos;
  85. if (check_format(_format, pos, "expr", expr)) ;
  86. else if (check_format(_format, pos, "file", file)) ;
  87. else if (check_format(_format, pos, "line", line)) ;
  88. else
  89. throw InvalidFormat(format);
  90. }
  91. if (!expr && !file && !line)
  92. throw InvalidFormat(format);
  93. }
  94. void
  95. CompilerOutput::assertment(const Source& s)
  96. {
  97. std::string fmt(_format);
  98. std::string::size_type pos;
  99. fmt.reserve(fmt.size() + 128);
  100. if ((pos = fmt.find("%file")) != std::string::npos)
  101. fmt.replace(pos, 5, s.file());
  102. if ((pos = fmt.find("%text")) != std::string::npos)
  103. fmt.replace(pos, 5, s.message());
  104. if ((pos = fmt.find("%line")) != std::string::npos)
  105. {
  106. std::ostringstream ss;
  107. ss << s.line();
  108. fmt.replace(pos, 5, ss.str());
  109. }
  110. _stream << fmt << std::endl;
  111. }
  112. } // namespace Test