Test mixing custom (de)serialization with generated code

This commit is contained in:
Martchus 2017-11-04 15:17:06 +01:00
parent 516f80b325
commit 95719ac8a4
2 changed files with 34 additions and 2 deletions

View File

@ -32,6 +32,7 @@ class JsonGeneratorTests : public TestFixture {
CPPUNIT_TEST(testNesting);
CPPUNIT_TEST(testSingleInheritence);
CPPUNIT_TEST(testMultipleInheritence);
CPPUNIT_TEST(testCustomSerialization);
CPPUNIT_TEST_SUITE_END();
public:
@ -42,6 +43,7 @@ public:
void testNesting();
void testSingleInheritence();
void testMultipleInheritence();
void testCustomSerialization();
private:
const vector<string> m_expectedCode;
@ -191,6 +193,23 @@ void JsonGeneratorTests::testMultipleInheritence()
CPPUNIT_ASSERT_EQUAL(test.arrayOfStrings, parsedTest.arrayOfStrings);
}
/*!
* \brief Like testIncludingGeneratedHeader() but also tests custom (de)serialization.
*/
void JsonGeneratorTests::testCustomSerialization()
{
const StructWithCustomTypes test;
const string str("{\"dt\":\"2017-04-02T15:31:21.165125\",\"ts\":\"03:15:19.125\"}");
// test serialization
CPPUNIT_ASSERT_EQUAL(str, string(test.toJson().GetString()));
// test deserialization
const StructWithCustomTypes parsedTest(StructWithCustomTypes::fromJson(str));
CPPUNIT_ASSERT_EQUAL(test.dt.toString(), parsedTest.dt.toString());
CPPUNIT_ASSERT_EQUAL(test.ts.toString(), parsedTest.ts.toString());
}
// include file required for reflection of TestStruct and other structs defined in structs.h
// NOTE: * generation of this header is triggered using the CMake function add_reflection_generator_invocation()
// * the include must happen in exactly one translation unit of the project at a point where the structs are defined

View File

@ -1,8 +1,12 @@
#ifndef REFLECTIVE_RAPIDJSON_TESTS_STRUCTS_H
#define REFLECTIVE_RAPIDJSON_TESTS_STRUCTS_H
#include "../../lib/json/reflector-chronoutilities.h"
#include "../../lib/json/serializable.h"
#include <c++utilities/chrono/datetime.h>
#include <c++utilities/chrono/timespan.h>
#include <deque>
#include <list>
#include <string>
@ -32,7 +36,7 @@ struct NestedTestStruct : public JsonSerializable<NestedTestStruct> {
/*!
* \brief The AnotherTestStruct struct inherits from JsonSerializable and should hence have functional fromJson()
* and toJson() methods. This is asserted in OverallTests::testInheritence();
* and toJson() methods. This is asserted in OverallTests::testSingleInheritence();
*/
struct AnotherTestStruct : public JsonSerializable<AnotherTestStruct> {
vector<string> arrayOfStrings{ "a", "b", "cd" };
@ -55,7 +59,7 @@ struct NonSerializable {
/*!
* \brief The MultipleDerivedTestStruct struct inherits from JsonSerializable and should hence have functional fromJson()
* and toJson() methods. This is asserted in OverallTests::testInheritence();
* and toJson() methods. This is asserted in OverallTests::testMultipleInheritence();
*/
struct MultipleDerivedTestStruct : public TestStruct,
public AnotherTestStruct,
@ -64,4 +68,13 @@ struct MultipleDerivedTestStruct : public TestStruct,
bool someBool = false;
};
/*!
* \brief The StructWithCustomTypes struct inherits from JsonSerializable and should hence have functional fromJson()
* and toJson() methods. This is asserted in OverallTests::testCustomSerialization();
*/
struct StructWithCustomTypes : public JsonSerializable<StructWithCustomTypes> {
ChronoUtilities::DateTime dt = ChronoUtilities::DateTime::fromDateAndTime(2017, 4, 2, 15, 31, 21, 165.125);
ChronoUtilities::TimeSpan ts = ChronoUtilities::TimeSpan::fromHours(3.25) + ChronoUtilities::TimeSpan::fromSeconds(19.125);
};
#endif // REFLECTIVE_RAPIDJSON_TESTS_STRUCTS_H