Test --json-classes argument

This commit is contained in:
Martchus 2017-11-06 20:04:42 +01:00
parent e12e33a28f
commit c4b4429880
4 changed files with 37 additions and 4 deletions

View File

@ -35,7 +35,7 @@ set(TEST_SRC_FILES
)
# find c++utilities and link against the library
find_package(c++utilities 4.11.0 REQUIRED)
find_package(c++utilities 4.12.0 REQUIRED)
use_cpp_utilities()
# find Clang for LibTooling; adding clangTooling should be sufficient as it pulls all transitive dependencies

View File

@ -1,7 +1,7 @@
#ifndef SOME_STRUCTS_H
#define SOME_STRUCTS_H
//#include <string>
#include <string>
#include "../../lib/json/serializable.h"
namespace TestNamespace1 {
@ -30,6 +30,11 @@ struct SomeOtherNonReflectableClass : public NonReflectableClass
namespace TestNamespace2 {
struct ThirdPartyStruct {
std::string test1;
unsigned long long test2;
};
}
#endif // SOME_STRUCTS_H

View File

@ -27,5 +27,31 @@ template <> inline void pull<::TestNamespace1::Person>(::TestNamespace1::Person
}
}
// define code for (de)serializing TestNamespace2::ThirdPartyStruct objects
template <> inline void push<::TestNamespace2::ThirdPartyStruct>(const ::TestNamespace2::ThirdPartyStruct &reflectable, ::RAPIDJSON_NAMESPACE::Value::Object &value, ::RAPIDJSON_NAMESPACE::Document::AllocatorType &allocator)
{
// push base classes
// push members
push(reflectable.test1, "test1", value, allocator);
push(reflectable.test2, "test2", value, allocator);
}
template <> inline void pull<::TestNamespace2::ThirdPartyStruct>(::TestNamespace2::ThirdPartyStruct &reflectable, const ::RAPIDJSON_NAMESPACE::GenericValue<::RAPIDJSON_NAMESPACE::UTF8<char>>::ConstObject &value, JsonDeserializationErrors *errors)
{
// pull base classes
// set error context for current record
const char *previousRecord;
if (errors) {
previousRecord = errors->currentRecord;
errors->currentRecord = "TestNamespace2::ThirdPartyStruct";
}
// pull members
pull(reflectable.test1, "test1", value, errors);
pull(reflectable.test2, "test2", value, errors);
// restore error context for previous record
if (errors) {
errors->currentRecord = previousRecord;
}
}
} // namespace JsonReflector
} // namespace ReflectiveRapidJSON

View File

@ -52,7 +52,7 @@ private:
CPPUNIT_TEST_SUITE_REGISTRATION(JsonGeneratorTests);
JsonGeneratorTests::JsonGeneratorTests()
: m_expectedCode(toArrayOfLines(readFile(testFilePath("some_structs_json_serialization.h"), 2 * 1024)))
: m_expectedCode(toArrayOfLines(readFile(testFilePath("some_structs_json_serialization.h"), 3 * 1024)))
{
}
@ -67,6 +67,8 @@ void JsonGeneratorTests::testGeneratorItself()
stringstream buffer;
JsonSerializationCodeGenerator::Options jsonOptions;
jsonOptions.additionalClassesArg.occurrenceInfo().emplace_back(0);
jsonOptions.additionalClassesArg.occurrenceInfo().back().values.emplace_back("TestNamespace2::ThirdPartyStruct");
CodeFactory factory(TestApplication::appPath(), inputFiles, clangOptions, buffer);
factory.addGenerator<JsonSerializationCodeGenerator>(jsonOptions);
CPPUNIT_ASSERT(factory.run());
@ -84,7 +86,7 @@ void JsonGeneratorTests::testCLI()
string stdout, stderr;
const string inputFilePath(testFilePath("some_structs.h"));
const char *const args1[] = { PROJECT_NAME, "-i", inputFilePath.data(), nullptr };
const char *const args1[] = { PROJECT_NAME, "-i", inputFilePath.data(), "--json-classes", "TestNamespace2::ThirdPartyStruct", nullptr };
TESTUTILS_ASSERT_EXEC(args1);
assertEqualityLinewise(m_expectedCode, toArrayOfLines(stdout));
#endif