node_tester.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* node_tester.cpp - load .rnite file and test it
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <fstream>
  18. #include <boost/uuid/uuid_io.hpp>
  19. #include <fmt/format.h>
  20. #include <core/std/string.h>
  21. #include <core/filters/yaml_reader.h>
  22. #include <core/filters/yaml_writer.h>
  23. #include <core/document.h>
  24. #include <core/node_info.h>
  25. #include "zero_context.h"
  26. using namespace fmt::literals;
  27. namespace rainynite::core {
  28. bool load_and_test_stream(shared_ptr<Document> document) {
  29. bool failed = false;
  30. if (auto test_list = document->get_property("_tests")) {
  31. int i = 0;
  32. for (auto nic : test_list->list_links(zero_context())) {
  33. if (auto node = dynamic_cast<BaseValue<bool>*>(nic.node.get())) {
  34. if (node->value(nic.context) != true) {
  35. failed = true;
  36. std::cerr << "Test #{} failed:\n id = {}\n type = {}\n"_format(
  37. i,
  38. to_string(node->get_id()),
  39. node_name(*node)
  40. );
  41. }
  42. } else {
  43. std::cerr << "Test #{} failed:\n test node is not bool";
  44. failed = true;
  45. }
  46. ++i;
  47. }
  48. }
  49. return !failed;
  50. }
  51. bool load_and_test_file(string const& fname) {
  52. std::stringstream content;
  53. {
  54. std::ifstream in(fname);
  55. content << in.rdbuf();
  56. }
  57. auto document = filters::YamlReader().read_document(content);
  58. if (document == nullptr)
  59. throw std::runtime_error("Unknown parse failure");
  60. if (!load_and_test_stream(document))
  61. return false;
  62. std::cerr << "All tests ok, ";
  63. std::stringstream saved_content;
  64. filters::YamlWriter().write_document(saved_content, document);
  65. if (content.str() == saved_content.str()) {
  66. std::cerr << "saved content identical to source.\n";
  67. return true;
  68. }
  69. std::cerr << "but saved content differs from source\n";
  70. std::cerr << saved_content.str() << "\n\n";
  71. if (document->get_property_value<bool>("_test_save_verbatim", zero_context()).value_or(false)) {
  72. std::cerr << "This file is supposed to be saved verbatim => test failure.";
  73. return false;
  74. }
  75. std::cerr << "Will try to check if saved version is semantically identical...\n";
  76. auto saved_document = filters::YamlReader().read_document(saved_content);
  77. return load_and_test_stream(saved_document);
  78. }
  79. } // namespace rainynite::core
  80. int main(int argc, char** argv) {
  81. if (argc < 2) {
  82. return 1;
  83. }
  84. return rainynite::core::load_and_test_file(argv[1]) ? 0 : 2;
  85. }