TestLogger.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "TestLogger.h"
  2. TestLogger::TestLogger()
  3. {
  4. }
  5. TestLogger::~TestLogger()
  6. {
  7. }
  8. void TestLogger::print(const std::string* message, const std::exception* ex,
  9. const std::string* function, const std::string* scriptname, const Script* sc, int line)
  10. {
  11. (void) function;
  12. (void) scriptname;
  13. (void) sc;
  14. (void) line;
  15. if(ex == nullptr)
  16. {
  17. if(message != nullptr)
  18. {
  19. int start = 0;
  20. while(true)
  21. {
  22. int newLine = message->find('\n', start);
  23. if(newLine == -1)
  24. {
  25. output.push_back(message->substr(start, message->size() - start));
  26. break;
  27. }
  28. output.push_back(message->substr(start, newLine - start));
  29. start = newLine + 1;
  30. }
  31. }
  32. }
  33. else
  34. {
  35. output.push_back(ex->what());
  36. }
  37. }
  38. void TestLogger::reset()
  39. {
  40. output.clear();
  41. }
  42. bool TestLogger::check(const std::string& name, std::ifstream& check)
  43. {
  44. std::vector<std::string> file;
  45. while(!check.eof())
  46. {
  47. std::string line;
  48. std::getline(check, line);
  49. if(!check.eof())
  50. {
  51. file.push_back(line);
  52. }
  53. }
  54. if(file.size() != output.size())
  55. {
  56. std::cout << file.size() << " " << output.size() << std::endl;
  57. printNoMatch(name, file, 0);
  58. return false;
  59. }
  60. for(size_t i = 0; i < file.size(); i++)
  61. {
  62. if(file[i] != output[i])
  63. {
  64. printNoMatch(name, file, i + 1);
  65. return false;
  66. }
  67. }
  68. return true;
  69. }
  70. void TestLogger::printNoMatch(const std::string& name, std::vector<std::string>& file, unsigned int line)
  71. {
  72. std::cout << "error checking " << name << ", error starting at " << line << "\n";
  73. std::cout << "Expected ---------------------------------------------\n";
  74. for(unsigned int i = 0; i < file.size(); i++)
  75. {
  76. std::cout << file[i] << "\n";
  77. }
  78. std::cout << "Actual -----------------------------------------------\n";
  79. for(unsigned int i = 0; i < output.size(); i++)
  80. {
  81. std::cout << output[i] << "\n";
  82. }
  83. std::cout << "------------------------------------------------------\n";
  84. }