ctkErrorLogModelTest3.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QCoreApplication>
  16. #include <QDebug>
  17. #include <QStringList>
  18. // CTK includes
  19. #include "ctkErrorLogModel.h"
  20. #include "ctkErrorLogQtMessageHandler.h"
  21. #include "ctkErrorLogStreamMessageHandler.h"
  22. #include "ctkModelTester.h"
  23. // STL includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. namespace
  27. {
  28. //-----------------------------------------------------------------------------
  29. // Utility function
  30. //-----------------------------------------------------------------------------
  31. QString checkRowCount(int line, int currentRowCount, int expectedRowCount)
  32. {
  33. if (currentRowCount != expectedRowCount)
  34. {
  35. QString errorMsg("Line %1 - Expected rowCount: %2 - Current rowCount: %3\n");
  36. return errorMsg.arg(line).arg(expectedRowCount).arg(currentRowCount);
  37. }
  38. return QString();
  39. }
  40. //-----------------------------------------------------------------------------
  41. QString checkTextMessages(int line, const ctkErrorLogModel& model, const QStringList& expectedMessages)
  42. {
  43. for(int i=0; i < expectedMessages.count(); ++i)
  44. {
  45. QModelIndex descriptionIndex = model.index(i, ctkErrorLogModel::DescriptionColumn);
  46. QString currentMessage = descriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
  47. if (currentMessage.compare(expectedMessages.value(i)) != 0)
  48. {
  49. QString errorMsg("Line %1 - Problem with row%2 !\n"
  50. "\tExpected message [%3]\n"
  51. "\tCurrent message [%4]\n");
  52. return errorMsg.arg(line).arg(i).arg(expectedMessages.value(i)).arg(currentMessage);
  53. }
  54. }
  55. return QString();
  56. }
  57. //-----------------------------------------------------------------------------
  58. void printTextMessages(const ctkErrorLogModel& model)
  59. {
  60. fprintf(stdout, "%s", "ErrorLogModel rows:\n");
  61. QString text("\trow %1 => %2\n");
  62. for (int i=0; i < model.rowCount(); ++i)
  63. {
  64. QString description =
  65. model.index(0, ctkErrorLogModel::DescriptionColumn).data().toString();
  66. fprintf(stdout, "%s", qPrintable(text.arg(i).arg(description)));
  67. }
  68. fflush(stdout);
  69. }
  70. //-----------------------------------------------------------------------------
  71. void printErrorMessage(const QString& errorMessage)
  72. {
  73. fprintf(stderr, "%s", qPrintable(errorMessage));
  74. fflush(stderr);
  75. }
  76. //-----------------------------------------------------------------------------
  77. QString checkBoolean(int line, const char* valueName, bool current, bool expected)
  78. {
  79. if (current != expected)
  80. {
  81. QString errorMsg("Line %1 - Expected %2: %3 - Current %4: %5\n");
  82. return errorMsg.arg(line).arg(valueName).
  83. arg(static_cast<int>(expected)).arg(valueName).arg(static_cast<int>(current));
  84. }
  85. return QString();
  86. }
  87. } // end namespace
  88. //-----------------------------------------------------------------------------
  89. int ctkErrorLogModelTest3(int argc, char * argv [])
  90. {
  91. QCoreApplication app(argc, argv);
  92. Q_UNUSED(app);
  93. QString errorMsg;
  94. ctkErrorLogModel model;
  95. bool currentTerminalOutputEnabled = model.terminalOutputEnabled();
  96. errorMsg = checkBoolean(__LINE__, "TerminalOutputEnabled", currentTerminalOutputEnabled, false);
  97. if (!errorMsg.isEmpty())
  98. {
  99. model.disableAllMsgHandler();
  100. printErrorMessage(errorMsg);
  101. printTextMessages(model);
  102. return EXIT_FAILURE;
  103. }
  104. model.setTerminalOutputEnabled(true);
  105. currentTerminalOutputEnabled = model.terminalOutputEnabled();
  106. errorMsg = checkBoolean(__LINE__, "TerminalOutputEnabled", currentTerminalOutputEnabled, true);
  107. if (!errorMsg.isEmpty())
  108. {
  109. model.disableAllMsgHandler();
  110. printErrorMessage(errorMsg);
  111. printTextMessages(model);
  112. return EXIT_FAILURE;
  113. }
  114. ctkModelTester modelTester;
  115. modelTester.setVerbose(false);
  116. try
  117. {
  118. modelTester.setModel(&model);
  119. // Monitor Qt messages
  120. model.registerMsgHandler(new ctkErrorLogQtMessageHandler);
  121. model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, true);
  122. // Monitor Stream messages
  123. model.registerMsgHandler(new ctkErrorLogStreamMessageHandler);
  124. model.setMsgHandlerEnabled(ctkErrorLogStreamMessageHandler::HandlerName, true);
  125. QString message0("This is a qDebug message");
  126. qDebug().nospace() << qPrintable(message0);
  127. QString message1("This is a std::cerr message");
  128. std::cerr << qPrintable(message1) << std::endl;
  129. QString message2("This is a qWarning message");
  130. qWarning().nospace() << qPrintable(message2);
  131. QString message3("This is a std::cout message");
  132. std::cout << qPrintable(message3) << std::endl;
  133. QString message4("This is a qCritical message");
  134. qCritical().nospace() << qPrintable(message4);
  135. QStringList expectedMessages;
  136. expectedMessages << message0 << message1
  137. << message2 << message3
  138. << message4;
  139. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedMessages.count());
  140. if (!errorMsg.isEmpty())
  141. {
  142. model.disableAllMsgHandler();
  143. printErrorMessage(errorMsg);
  144. printTextMessages(model);
  145. return EXIT_FAILURE;
  146. }
  147. errorMsg = checkTextMessages(__LINE__, model, expectedMessages);
  148. if (!errorMsg.isEmpty())
  149. {
  150. model.disableAllMsgHandler();
  151. printErrorMessage(errorMsg);
  152. return EXIT_FAILURE;
  153. }
  154. }
  155. catch (const char* error)
  156. {
  157. model.disableAllMsgHandler();
  158. std::cerr << error << std::endl;
  159. return EXIT_FAILURE;
  160. }
  161. return EXIT_SUCCESS;
  162. }