123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /*
- * serialize.cpp - serializing tests
- * Copyright (C) 2017 caryoscelus
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <catch.hpp>
- #include <iostream>
- #include <core/types.h>
- #include <core/node/node.h>
- #include <core/node/property.h>
- #include <core/serialize/json.h>
- #include <core/serialize/node_writer.h>
- #include <core/serialize/node_reader.h>
- using namespace rainynite;
- using namespace rainynite::core;
- namespace rainynite::core {
- namespace serialize {
- template class AutoValueToString<double>;
- }
- class Add : public Node<double> {
- public:
- Add() {
- init<double>(a, 0);
- init<double>(b, 0);
- }
- public:
- double get(shared_ptr<Context> context) const override {
- auto a = get_a()->get(context);
- auto b = get_b()->get(context);
- return a+b;
- }
- NODE_PROPERTY(a, double);
- NODE_PROPERTY(b, double);
- };
- REGISTER_NODE(Add);
- }
- TEST_CASE("Dumb json serialize", "[serialize,node]") {
- std::ostringstream stream;
- auto writer = serialize::DumbJsonWriter<serialize::NodeWriter, AbstractReference>(stream);
- AbstractReference three = make_value<double>(3);
- auto add = make_node<Add>();
- add->set_property("a", three);
- add->set_property("b", three);
- AbstractReference add_a = add;
- writer.object(add_a);
- std::cout << stream.str() << std::endl;
- stream.str("");
- auto list = make_shared<ListValue<double>>();
- list->new_id();
- push_value(list, 1.5);
- writer.object(list);
- std::cout << stream.str() << std::endl;
- }
- TEST_CASE("Read double", "[serialize]") {
- auto x = parse_primitive_type_to<double>("Real", "3.5");
- CHECK(x == 3.5);
- }
- class DummyReader {};
- TEST_CASE("Deserialize", "[serialize,node]") {
- auto uuid_gen = boost::uuids::random_generator();
- serialize::NodeDeserializer<DummyReader> s;
- s.object_start(uuid_gen());
- s.type("Add");
- s.key("a");
- s.object_start(uuid_gen());
- s.type("Value<Real>");
- s.object_value_start();
- s.value_string("1");
- s.object_value_end();
- s.object_end();
- s.key("b");
- s.object_start(uuid_gen());
- s.type("Value<Real>");
- s.object_value_start();
- s.value_string("2");
- s.object_value_end();
- s.object_end();
- s.object_end();
- auto add = s.get_root();
- CHECK(add->get_type() == typeid(double));
- CHECK(any_cast<double>(add->get_any({})) == 3);
- }
|