From ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 7 May 2021 20:50:48 +0100 Subject: Initial commit where writing objects back out to JSON It's not perfect, writes explicit nulls, doesn't do shared @id --- test/test-persistence.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test-persistence.cpp b/test/test-persistence.cpp index 72c8968..547396b 100644 --- a/test/test-persistence.cpp +++ b/test/test-persistence.cpp @@ -32,7 +32,7 @@ struct SubObject : public AbsObject { bool persist(Persistence::PersistenceStore & store) override { - return AbsObject::persist(store) && STORE_TYPE && STORE_MEMBER(sub); + return STORE_TYPE && AbsObject::persist(store) && STORE_MEMBER(sub); } void @@ -45,7 +45,7 @@ struct SubObject2 : public AbsObject { bool persist(Persistence::PersistenceStore & store) override { - return AbsObject::persist(store) && STORE_TYPE; + return STORE_TYPE && AbsObject::persist(store); } void @@ -297,3 +297,52 @@ BOOST_DATA_TEST_CASE(utf8_decode_bad, boost::unit_test::data::make to {}; + std::stringstream ss; + Persistence::JsonWritePersistence {ss}.saveState(to); + BOOST_CHECK_EQUAL(ss.str(), "null"); +} + +BOOST_AUTO_TEST_CASE(write_test_dfl) +{ + auto to = std::make_unique(); + std::stringstream ss; + Persistence::JsonWritePersistence {ss}.saveState(to); + // TODO We can omit writing explicit nulls + BOOST_CHECK_EQUAL(ss.str(), + R"({"@typeid":"TestObject","flt":0,"str":"","bl":false,"pos":[0,0,0],"flts":[],"poss":[],"nest":[],"ptr":null,"aptr":null,"vptr":[]})"); +} + +BOOST_FIXTURE_TEST_CASE(write_test_loaded, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/load_object.json"); + std::stringstream ss; + Persistence::JsonWritePersistence {ss}.saveState(to); + // TODO We can omit writing explicit nulls + BOOST_CHECK_EQUAL(ss.str(), + R"({"@typeid":"TestObject","flt":3.14,"str":"Lovely string","bl":true,"pos":[3.14,6.28,1.57],"flts":[3.14,6.28,1.57,0,-1,-3.14],"poss":[[3.14,6.28,1.57],[0,-1,-3.14]],"nest":[[["a","b"],["c","d","e"]],[["f"]],[]],"ptr":{"@typeid":"TestObject","flt":3.14,"str":"Lovely string","bl":false,"pos":[0,0,0],"flts":[],"poss":[],"nest":[],"ptr":null,"aptr":null,"vptr":[]},"aptr":null,"vptr":[]})"); +} + +BOOST_FIXTURE_TEST_CASE(write_test_loaded_abs, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/abs.json"); + std::stringstream ss; + Persistence::JsonWritePersistence {ss}.saveState(to); + // TODO We can omit writing explicit nulls + BOOST_CHECK_EQUAL(ss.str(), + R"({"@typeid":"TestObject","flt":0,"str":"","bl":false,"pos":[0,0,0],"flts":[],"poss":[],"nest":[],"ptr":null,"aptr":{"@typeid":"SubObject","base":"set base","sub":"set sub"},"vptr":[]})"); +} + +BOOST_FIXTURE_TEST_CASE(write_test_loaded_shared, JPP) +{ + auto to = load_json>(FIXTURESDIR "json/shared_ptr_same.json"); + std::stringstream ss; + Persistence::JsonWritePersistence {ss}.saveState(to); + // TODO We can omit writing explicit nulls + // TODO Also not really implemented, but it runs :) + // BOOST_CHECK_EQUAL(ss.str(), + // R"({"@typeid":"SharedTestObject","sptr":{"@typeid":"SubObject","@id":"someid"},"ssptr":"someid"})"); +} -- cgit v1.2.3