summaryrefslogtreecommitdiff
path: root/test/test-persistence.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-05-07 20:50:48 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-11-07 16:41:37 +0000
commitce5c689a5f9e544d724b2a41fda2f752ccc4a4ae (patch)
tree33f2c53d2cb1b67f3c7024bbec2366be875b57d9 /test/test-persistence.cpp
parentFixup clang, cppcheck and iwyu warnings (diff)
downloadilt-ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae.tar.bz2
ilt-ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae.tar.xz
ilt-ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae.zip
Initial commit where writing objects back out to JSON
It's not perfect, writes explicit nulls, doesn't do shared @id
Diffstat (limited to 'test/test-persistence.cpp')
-rw-r--r--test/test-persistence.cpp53
1 files changed, 51 insertions, 2 deletions
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<unsigned long
std::string out;
BOOST_CHECK_THROW(json::jsonParser::appendEscape(cp, out), std::runtime_error);
}
+
+BOOST_AUTO_TEST_CASE(write_test_null)
+{
+ std::unique_ptr<TestObject> 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<TestObject>();
+ 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<std::unique_ptr<TestObject>>(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<std::unique_ptr<TestObject>>(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<std::unique_ptr<SharedTestObject>>(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"})");
+}