testRST.cxx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2013 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmRST.h"
  11. #include "cmSystemTools.h"
  12. void reportLine(std::ostream& os, bool ret, std::string line, bool eol)
  13. {
  14. if(ret)
  15. {
  16. os << "\"" << line << "\" (" << (eol?"with EOL":"without EOL") << ")";
  17. }
  18. else
  19. {
  20. os << "EOF";
  21. }
  22. }
  23. int testRST(int, char*[])
  24. {
  25. std::string dir = cmSystemTools::GetFilenamePath(__FILE__);
  26. if(dir.empty())
  27. {
  28. dir = ".";
  29. }
  30. std::string a_name = "testRST.actual";
  31. std::string e_name = dir + "/testRST.expect";
  32. // Process the test RST file.
  33. {
  34. std::string fname = dir + "/testRST.rst";
  35. std::ofstream fout(a_name.c_str());
  36. if(!fout)
  37. {
  38. std::cerr << "Could not open output " << a_name << std::endl;
  39. return 1;
  40. }
  41. cmRST r(fout, dir);
  42. if(!r.ProcessFile(fname))
  43. {
  44. std::cerr << "Could not open input " << fname << std::endl;
  45. return 1;
  46. }
  47. }
  48. // Compare expected and actual outputs.
  49. std::ifstream e_fin(e_name.c_str());
  50. std::ifstream a_fin(a_name.c_str());
  51. if(!e_fin)
  52. {
  53. std::cerr << "Could not open input " << e_name << std::endl;
  54. return 1;
  55. }
  56. if(!a_fin)
  57. {
  58. std::cerr << "Could not open input " << a_name << std::endl;
  59. return 1;
  60. }
  61. int lineno = 0;
  62. bool e_ret;
  63. bool a_ret;
  64. do
  65. {
  66. std::string e_line;
  67. std::string a_line;
  68. bool e_eol;
  69. bool a_eol;
  70. e_ret = cmSystemTools::GetLineFromStream(e_fin, e_line, &e_eol);
  71. a_ret = cmSystemTools::GetLineFromStream(a_fin, a_line, &a_eol);
  72. ++lineno;
  73. if(e_ret != a_ret || e_line != a_line || e_eol != a_eol)
  74. {
  75. a_fin.seekg(0, std::ios::beg);
  76. std::cerr << "Actual output does not match that expected on line "
  77. << lineno << "." << std::endl << "Expected ";
  78. reportLine(std::cerr, e_ret, e_line, e_eol);
  79. std::cerr << " but got ";
  80. reportLine(std::cerr, a_ret, a_line, a_eol);
  81. std::cerr << "." << std::endl
  82. << "Actual output:" << std::endl
  83. << a_fin.rdbuf();
  84. return 1;
  85. }
  86. } while(e_ret && a_ret);
  87. return 0;
  88. }