testRST.cxx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 const& line, bool eol)
  13. {
  14. if (ret) {
  15. os << "\"" << line << "\" (" << (eol ? "with EOL" : "without EOL") << ")";
  16. } else {
  17. os << "EOF";
  18. }
  19. }
  20. int testRST(int argc, char* argv[])
  21. {
  22. if (argc != 2) {
  23. std::cerr << "Usage: testRST <dir>" << std::endl;
  24. return 1;
  25. }
  26. std::string dir = argv[1];
  27. if (dir.empty()) {
  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. std::cerr << "Could not open output " << a_name << std::endl;
  38. return 1;
  39. }
  40. cmRST r(fout, dir);
  41. if (!r.ProcessFile(fname)) {
  42. std::cerr << "Could not open input " << fname << std::endl;
  43. return 1;
  44. }
  45. }
  46. // Compare expected and actual outputs.
  47. std::ifstream e_fin(e_name.c_str());
  48. std::ifstream a_fin(a_name.c_str());
  49. if (!e_fin) {
  50. std::cerr << "Could not open input " << e_name << std::endl;
  51. return 1;
  52. }
  53. if (!a_fin) {
  54. std::cerr << "Could not open input " << a_name << std::endl;
  55. return 1;
  56. }
  57. int lineno = 0;
  58. bool e_ret;
  59. bool a_ret;
  60. do {
  61. std::string e_line;
  62. std::string a_line;
  63. bool e_eol;
  64. bool a_eol;
  65. e_ret = cmSystemTools::GetLineFromStream(e_fin, e_line, &e_eol);
  66. a_ret = cmSystemTools::GetLineFromStream(a_fin, a_line, &a_eol);
  67. ++lineno;
  68. if (e_ret != a_ret || e_line != a_line || e_eol != a_eol) {
  69. a_fin.seekg(0, std::ios::beg);
  70. std::cerr << "Actual output does not match that expected on line "
  71. << lineno << "." << std::endl
  72. << "Expected ";
  73. reportLine(std::cerr, e_ret, e_line, e_eol);
  74. std::cerr << " but got ";
  75. reportLine(std::cerr, a_ret, a_line, a_eol);
  76. std::cerr << "." << std::endl
  77. << "Actual output:" << std::endl
  78. << a_fin.rdbuf();
  79. return 1;
  80. }
  81. } while (e_ret && a_ret);
  82. return 0;
  83. }