node_tester.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* node_tester.cpp - load .rnite file and test it
  2. * Copyright (C) 2017-2018 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 <iostream>
  19. #include <sstream>
  20. #include <boost/uuid/uuid_io.hpp>
  21. #include <fmt/format.h>
  22. #include <core/std/string.h>
  23. #include <core/document.h>
  24. #include <core/fs/document_loader.h>
  25. #include <core/node/abstract_node.h>
  26. #include <core/node_info/node_info.h>
  27. #include <core/util/nullptr.h>
  28. #include "zero_context.h"
  29. using namespace fmt::literals;
  30. namespace rainynite::core {
  31. bool test_document(shared_ptr<AbstractDocument> document) {
  32. bool failed = false;
  33. if (auto test_list = abstract_node_cast(document)->get_property("_tests")) {
  34. int i = 0;
  35. for (auto nic : test_list->list_links(document->get_default_context())) {
  36. if (auto node = dynamic_cast<BaseValue<bool>*>(nic.node.get())) {
  37. if (node->value(nic.context) != true) {
  38. failed = true;
  39. std::cerr << "Test #{} failed:\n id = {}\n type = {}\n"_format(
  40. i,
  41. node->get_id(),
  42. node_name(*node)
  43. );
  44. }
  45. } else {
  46. std::cerr << "Test #{} failed:\n test node is not bool";
  47. failed = true;
  48. }
  49. ++i;
  50. }
  51. }
  52. return !failed;
  53. }
  54. bool load_and_test_file(string const& fname) {
  55. auto loader = DocumentLoader::instance();
  56. auto path = fs::Path(fname);
  57. auto document = loader->get_document(path);
  58. if (document == nullptr)
  59. throw std::runtime_error("Unknown parse failure");
  60. if (!test_document(document))
  61. return false;
  62. std::cerr << "All tests ok, ";
  63. std::stringstream saved_content;
  64. loader->write_document_to(path, saved_content);
  65. // TODO: avoid extra file read?..
  66. std::stringstream content;
  67. content << std::ifstream(fname).rdbuf();
  68. if (content.str() == saved_content.str()) {
  69. std::cerr << "saved content identical to source.\n";
  70. return true;
  71. }
  72. std::cerr << "but saved content differs from source\n";
  73. std::cerr << saved_content.str() << "\n\n";
  74. if (abstract_node_cast(document)->get_property_value<bool>("_test_save_verbatim", zero_context()).value_or(false)) {
  75. std::cerr << "This file is supposed to be saved verbatim => test failure.";
  76. return false;
  77. }
  78. std::cerr << "Will try to check if saved version is semantically identical... ";
  79. loader->unload_document(path);
  80. auto saved_document = no_null(loader->get_document_from(path, saved_content));
  81. if (test_document(saved_document)) {
  82. std::cerr << "All fine!\n";
  83. return true;
  84. } else {
  85. std::cerr << "Fail!\n";
  86. return false;
  87. }
  88. }
  89. } // namespace rainynite::core
  90. int main(int argc, char** argv) {
  91. if (argc < 2) {
  92. return 1;
  93. }
  94. return rainynite::core::load_and_test_file(argv[1]) ? 0 : 2;
  95. }