diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-05-01 19:29:32 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-11-07 16:41:37 +0000 |
commit | 4a4a45e9a4a881a6c9a30722089bec7d3cc07e8c (patch) | |
tree | f10b6a00dfbcd061da160a63007fbe7c593f6501 | |
parent | Name, rather than number, shared_ptr tests (diff) | |
download | ilt-4a4a45e9a4a881a6c9a30722089bec7d3cc07e8c.tar.bz2 ilt-4a4a45e9a4a881a6c9a30722089bec7d3cc07e8c.tar.xz ilt-4a4a45e9a4a881a6c9a30722089bec7d3cc07e8c.zip |
Error on bad cast of referenced object
-rw-r--r-- | lib/persistence.h | 3 | ||||
-rw-r--r-- | test/fixtures/json/shared_ptr_wrong_type.json | 8 | ||||
-rw-r--r-- | test/test-persistence.cpp | 19 |
3 files changed, 30 insertions, 0 deletions
diff --git a/lib/persistence.h b/lib/persistence.h index 86b0dce..e8e3415 100644 --- a/lib/persistence.h +++ b/lib/persistence.h @@ -303,6 +303,9 @@ namespace Persistence { if (auto teo = std::dynamic_pointer_cast<T>(sharedObjects.at(id))) { this->v = std::move(teo); } + else { + throw std::runtime_error("Named type doesn't cast to target type"); + } } }; } diff --git a/test/fixtures/json/shared_ptr_wrong_type.json b/test/fixtures/json/shared_ptr_wrong_type.json new file mode 100644 index 0000000..68f7533 --- /dev/null +++ b/test/fixtures/json/shared_ptr_wrong_type.json @@ -0,0 +1,8 @@ +{ + "@typeid": "SharedTestObject", + "sptr": { + "@typeid": "SubObject2", + "@id": "someid" + }, + "ssptr": "someid" +} diff --git a/test/test-persistence.cpp b/test/test-persistence.cpp index 05a02bb..a195007 100644 --- a/test/test-persistence.cpp +++ b/test/test-persistence.cpp @@ -38,6 +38,19 @@ struct SubObject : public AbsObject { } }; +struct SubObject2 : public AbsObject { + bool + persist(Persistence::PersistenceStore & store) override + { + return AbsObject::persist(store) && STORE_TYPE; + } + + void + dummy() const override + { + } +}; + struct TestObject : public Persistence::Persistable { TestObject() = default; @@ -227,6 +240,12 @@ BOOST_FIXTURE_TEST_CASE(load_shared_object_diff_default, JPP) BOOST_CHECK_NE(to->sptr, to->ssptr); } +BOOST_FIXTURE_TEST_CASE(load_shared_object_wrong_type, JPP) +{ + BOOST_CHECK_THROW(load_json<std::unique_ptr<SharedTestObject>>(FIXTURESDIR "json/shared_ptr_wrong_type.json"), + std::runtime_error); +} + BOOST_FIXTURE_TEST_CASE(load_shared_object_null, JPP) { auto to = load_json<std::unique_ptr<SharedTestObject>>(FIXTURESDIR "json/shared_ptr_null.json"); |