testRST.cxx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmRST.h"
  4. #include "cmSystemTools.h"
  5. #include "cmsys/FStream.hxx"
  6. #include <iostream>
  7. #include <string>
  8. void reportLine(std::ostream& os, bool ret, std::string const& line, bool eol)
  9. {
  10. if (ret) {
  11. os << "\"" << line << "\" (" << (eol ? "with EOL" : "without EOL") << ")";
  12. } else {
  13. os << "EOF";
  14. }
  15. }
  16. int testRST(int argc, char* argv[])
  17. {
  18. if (argc != 2) {
  19. std::cerr << "Usage: testRST <dir>" << std::endl;
  20. return 1;
  21. }
  22. std::string dir = argv[1];
  23. if (dir.empty()) {
  24. dir = ".";
  25. }
  26. std::string a_name = "testRST.actual";
  27. std::string e_name = dir + "/testRST.expect";
  28. // Process the test RST file.
  29. {
  30. std::string fname = dir + "/testRST.rst";
  31. std::ofstream fout(a_name.c_str());
  32. if (!fout) {
  33. std::cerr << "Could not open output " << a_name << std::endl;
  34. return 1;
  35. }
  36. cmRST r(fout, dir);
  37. if (!r.ProcessFile(fname)) {
  38. std::cerr << "Could not open input " << fname << std::endl;
  39. return 1;
  40. }
  41. }
  42. // Compare expected and actual outputs.
  43. cmsys::ifstream e_fin(e_name.c_str());
  44. cmsys::ifstream a_fin(a_name.c_str());
  45. if (!e_fin) {
  46. std::cerr << "Could not open input " << e_name << std::endl;
  47. return 1;
  48. }
  49. if (!a_fin) {
  50. std::cerr << "Could not open input " << a_name << std::endl;
  51. return 1;
  52. }
  53. int lineno = 0;
  54. bool e_ret;
  55. bool a_ret;
  56. do {
  57. std::string e_line;
  58. std::string a_line;
  59. bool e_eol;
  60. bool a_eol;
  61. e_ret = cmSystemTools::GetLineFromStream(e_fin, e_line, &e_eol);
  62. a_ret = cmSystemTools::GetLineFromStream(a_fin, a_line, &a_eol);
  63. ++lineno;
  64. if (e_ret != a_ret || e_line != a_line || e_eol != a_eol) {
  65. a_fin.seekg(0, std::ios::beg);
  66. std::cerr << "Actual output does not match that expected on line "
  67. << lineno << "." << std::endl
  68. << "Expected ";
  69. reportLine(std::cerr, e_ret, e_line, e_eol);
  70. std::cerr << " but got ";
  71. reportLine(std::cerr, a_ret, a_line, a_eol);
  72. std::cerr << "." << std::endl
  73. << "Actual output:" << std::endl
  74. << a_fin.rdbuf();
  75. return 1;
  76. }
  77. } while (e_ret && a_ret);
  78. return 0;
  79. }