rapidjson.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * rapidjson.cpp - rapidjson (de)serialization tests
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <catch.hpp>
  19. #include <iostream>
  20. #include <boost/uuid/uuid_io.hpp>
  21. #include <core/types.h>
  22. #include <core/node/node.h>
  23. #include <core/node/property.h>
  24. #include <core/serialize/rapidjson.h>
  25. #include <core/serialize/node_writer.h>
  26. #include <core/serialize/node_reader.h>
  27. using namespace rainynite::core;
  28. namespace rainynite::core {
  29. namespace serialize {
  30. template class AutoValueToString<double>;
  31. }
  32. class Add : public Node<double> {
  33. public:
  34. Add() {
  35. init<double>(a, 0);
  36. init<double>(b, 0);
  37. }
  38. public:
  39. double get(shared_ptr<Context> context) const override {
  40. auto a = get_a()->get(context);
  41. auto b = get_b()->get(context);
  42. return a+b;
  43. }
  44. NODE_PROPERTY(a, double);
  45. NODE_PROPERTY(b, double);
  46. };
  47. REGISTER_NODE(Add);
  48. }
  49. const char t1[] = R"({
  50. "UID": "af0b9a7a-1ded-11e7-bc83-5fbbc1c89c41",
  51. "TYPE": "Value<Real>",
  52. "VALUE": "4.0"
  53. })";
  54. const char t2[] = R"({
  55. "UID": "69fc1c3e-1ded-11e7-a9b3-139a4f764786",
  56. "TYPE": "Add",
  57. "a": {
  58. "UID": "ae861666-1ded-11e7-a77f-f7e6f7639c1d",
  59. "TYPE": "Value<Real>",
  60. "VALUE": "3.0"
  61. },
  62. "b": "ae861666-1ded-11e7-a77f-f7e6f7639c1d"
  63. })";
  64. struct DummyReader {};
  65. AbstractReference parse_string(const char* s) {
  66. serialize::RJSONWrapper<serialize::NodeDeserializer<DummyReader>> node_reader;
  67. rapidjson::Reader json_reader;
  68. rapidjson::StringStream stream(s);
  69. json_reader.Parse(stream, node_reader);
  70. return node_reader.get_node_reader().get_root();
  71. }
  72. TEST_CASE("Rapidjson deserialize value", "[node]") {
  73. auto node = parse_string(t1);
  74. CHECK(node != nullptr);
  75. auto n = dynamic_cast<BaseValue<double>*>(node.get());
  76. CHECK(n->get({}) == 4.0);
  77. }
  78. TEST_CASE("Rapidjson deserialize add", "[node]") {
  79. auto node = parse_string(t2);
  80. CHECK(node != nullptr);
  81. auto n = dynamic_cast<BaseValue<double>*>(node.get());
  82. CHECK(n->get({}) == 6);
  83. }