From c026dafff70dd4ae525e1fc0c781d44671cfe397 Mon Sep 17 00:00:00 2001 From: Martchus Date: Wed, 25 Oct 2017 18:25:34 +0200 Subject: [PATCH] Split generator tests into multiple files --- generator/CMakeLists.txt | 6 +- generator/tests/helper.h | 41 ++++++++ .../tests/{overall.cpp => jsongenerator.cpp} | 93 ++----------------- generator/tests/structs.h | 56 +++++++++++ 4 files changed, 110 insertions(+), 86 deletions(-) create mode 100644 generator/tests/helper.h rename generator/tests/{overall.cpp => jsongenerator.cpp} (63%) create mode 100644 generator/tests/structs.h diff --git a/generator/CMakeLists.txt b/generator/CMakeLists.txt index 7305a41..6d0161e 100644 --- a/generator/CMakeLists.txt +++ b/generator/CMakeLists.txt @@ -24,10 +24,12 @@ set(SRC_FILES main.cpp ) set(TEST_HEADER_FILES + tests/structs.h + tests/helper.h ) set(TEST_SRC_FILES tests/cppunit.cpp - tests/overall.cpp + tests/jsongenerator.cpp ) # find c++utilities @@ -44,7 +46,7 @@ list(APPEND PRIVATE_LIBRARIES reflective_rapidjson) # trigger code generator for tests include(ReflectionGenerator) add_reflection_generator_invocation( - INPUT_FILES tests/overall.cpp tests/cppunit.cpp + INPUT_FILES tests/structs.h tests/cppunit.cpp GENERATORS json OUTPUT_LISTS TEST_HEADER_FILES ) diff --git a/generator/tests/helper.h b/generator/tests/helper.h new file mode 100644 index 0000000..f70e65a --- /dev/null +++ b/generator/tests/helper.h @@ -0,0 +1,41 @@ +#ifndef REFLECTIVE_RAPIDJSON_TESTS_HELPER_H +#define REFLECTIVE_RAPIDJSON_TESTS_HELPER_H + +#include +#include +#include + +// ensure "operator<<" from TestUtilities is visible prior to the call site +using TestUtilities::operator<<; + +#include + +/*! + * \brief Asserts equality of two iteratables printing the differing indices. + */ +template , Traits::Not>>...> +inline void assertEqualityLinewise(const Iteratable &iteratable1, const Iteratable &iteratable2) +{ + std::vector differentLines; + std::size_t currentLine = 0; + + for (auto i1 = iteratable1.cbegin(), i2 = iteratable2.cbegin(); i1 != iteratable1.cend() || i2 != iteratable2.cend(); ++currentLine) { + if (i1 != iteratable1.cend() && i2 != iteratable2.cend()) { + if (*i1 != *i2) { + differentLines.push_back(ConversionUtilities::numberToString(currentLine)); + } + ++i1, ++i2; + } else if (i1 != iteratable1.cend()) { + differentLines.push_back(ConversionUtilities::numberToString(currentLine)); + ++i1; + } else if (i2 != iteratable1.cend()) { + differentLines.push_back(ConversionUtilities::numberToString(currentLine)); + ++i2; + } + } + if (!differentLines.empty()) { + CPPUNIT_ASSERT_EQUAL_MESSAGE("the following lines differ: " + ConversionUtilities::joinStrings(differentLines, ", "), iteratable1, iteratable2); + } +} + +#endif // REFLECTIVE_RAPIDJSON_TESTS_HELPER_H diff --git a/generator/tests/overall.cpp b/generator/tests/jsongenerator.cpp similarity index 63% rename from generator/tests/overall.cpp rename to generator/tests/jsongenerator.cpp index 8d42fad..274a9f9 100644 --- a/generator/tests/overall.cpp +++ b/generator/tests/jsongenerator.cpp @@ -1,72 +1,23 @@ -#include "../codefactory.h" +#include "./structs.h" +#include "./helper.h" -#include "../../lib/jsonserializable.h" +#include "../codefactory.h" #include "resources/config.h" -#include #include #include #include -using TestUtilities::operator<<; // must be visible prior to the call site #include #include #include -#include -#include -using namespace std; using namespace CPPUNIT_NS; using namespace IoUtilities; using namespace TestUtilities; using namespace ConversionUtilities; -using namespace ReflectiveRapidJSON; - -/*! - * \brief The TestStruct struct inherits from JSONSerializable and should hence have functional fromJson() - * and toJson() methods. This is asserted in OverallTests::testIncludingGeneratedHeader(); - */ -struct TestStruct : public JSONSerializable { - int someInt = 0; - string someString = "foo"; - string yetAnotherString = "bar"; -}; - -/*! - * \brief The AnotherTestStruct struct inherits from JSONSerializable and should hence have functional fromJson() - * and toJson() methods. This is asserted in OverallTests::testInheritence(); - */ -struct AnotherTestStruct : public JSONSerializable { - vector arrayOfStrings{ "a", "b", "cd" }; -}; - -/*! - * \brief The DerivedTestStruct struct inherits from JSONSerializable and should hence have functional fromJson() - * and toJson() methods. This is asserted in OverallTests::testInheritence(); - */ -struct DerivedTestStruct : public TestStruct, public JSONSerializable { - bool someBool = true; -}; - -/*! - * \brief The NonSerializable struct should be ignored when used as base class because it isn't serializable. - */ -struct NonSerializable { - int ignored = 25; -}; - -/*! - * \brief The MultipleDerivedTestStruct struct inherits from JSONSerializable and should hence have functional fromJson() - * and toJson() methods. This is asserted in OverallTests::testInheritence(); - */ -struct MultipleDerivedTestStruct : public TestStruct, - public AnotherTestStruct, - public NonSerializable, - public JSONSerializable { - bool someBool = false; -}; /*! * \brief The OverallTests class tests the overall functionality of the code generator (CLI and generator itself). @@ -96,34 +47,6 @@ private: CPPUNIT_TEST_SUITE_REGISTRATION(OverallTests); -/*! - * \brief Asserts equality of two iteratables printing the differing indices. - */ -template , Traits::Not>>...> -inline void assertEqualityLinewise(const Iteratable &iteratable1, const Iteratable &iteratable2) -{ - std::vector differentLines; - std::size_t currentLine = 0; - - for (auto i1 = iteratable1.cbegin(), i2 = iteratable2.cbegin(); i1 != iteratable1.cend() || i2 != iteratable2.cend(); ++currentLine) { - if (i1 != iteratable1.cend() && i2 != iteratable2.cend()) { - if (*i1 != *i2) { - differentLines.push_back(numberToString(currentLine)); - } - ++i1, ++i2; - } else if (i1 != iteratable1.cend()) { - differentLines.push_back(numberToString(currentLine)); - ++i1; - } else if (i2 != iteratable1.cend()) { - differentLines.push_back(numberToString(currentLine)); - ++i2; - } - } - if (!differentLines.empty()) { - CPPUNIT_ASSERT_EQUAL_MESSAGE("the following lines differ: " + joinStrings(differentLines, ", "), iteratable1, iteratable2); - } -} - void OverallTests::setUp() { m_expectedCode = toArrayOfLines(readFile(testFilePath("some_structs_json_serialization.h"), 1024)); @@ -240,9 +163,11 @@ void OverallTests::testMultipleInheritence() CPPUNIT_ASSERT_EQUAL(test.arrayOfStrings, parsedTest.arrayOfStrings); } -// include file required for reflection of TestStruct and other structs; generation of this header is triggered using -// the CMake function add_reflection_generator_invocation() -#include "reflection/overall.h" +// 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 +#include "reflection/structs.h" -// this file should also be generated and hence includeable, but empty because it doesn't contain relevant classes +// this file should also be generated via add_reflection_generator_invocation() and hence includeable +// it is included to test the "empty" case when a unit doesn't contain relevant classes #include "reflection/cppunit.h" diff --git a/generator/tests/structs.h b/generator/tests/structs.h new file mode 100644 index 0000000..2a852c2 --- /dev/null +++ b/generator/tests/structs.h @@ -0,0 +1,56 @@ +#ifndef REFLECTIVE_RAPIDJSON_TESTS_STRUCTS_H +#define REFLECTIVE_RAPIDJSON_TESTS_STRUCTS_H + +#include "../../lib/jsonserializable.h" + +#include +#include + +using namespace std; +using namespace ReflectiveRapidJSON; + +/*! + * \brief The TestStruct struct inherits from JSONSerializable and should hence have functional fromJson() + * and toJson() methods. This is asserted in OverallTests::testIncludingGeneratedHeader(); + */ +struct TestStruct : public JSONSerializable { + int someInt = 0; + string someString = "foo"; + string yetAnotherString = "bar"; +}; + +/*! + * \brief The AnotherTestStruct struct inherits from JSONSerializable and should hence have functional fromJson() + * and toJson() methods. This is asserted in OverallTests::testInheritence(); + */ +struct AnotherTestStruct : public JSONSerializable { + vector arrayOfStrings{ "a", "b", "cd" }; +}; + +/*! + * \brief The DerivedTestStruct struct inherits from JSONSerializable and should hence have functional fromJson() + * and toJson() methods. This is asserted in OverallTests::testInheritence(); + */ +struct DerivedTestStruct : public TestStruct, public JSONSerializable { + bool someBool = true; +}; + +/*! + * \brief The NonSerializable struct should be ignored when used as base class because it isn't serializable. + */ +struct NonSerializable { + int ignored = 25; +}; + +/*! + * \brief The MultipleDerivedTestStruct struct inherits from JSONSerializable and should hence have functional fromJson() + * and toJson() methods. This is asserted in OverallTests::testInheritence(); + */ +struct MultipleDerivedTestStruct : public TestStruct, + public AnotherTestStruct, + public NonSerializable, + public JSONSerializable { + bool someBool = false; +}; + +#endif // REFLECTIVE_RAPIDJSON_TESTS_STRUCTS_H