Split generator tests into multiple files

This commit is contained in:
Martchus 2017-10-25 18:25:34 +02:00
parent b725f59e78
commit c026dafff7
4 changed files with 110 additions and 86 deletions

View File

@ -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
)

41
generator/tests/helper.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef REFLECTIVE_RAPIDJSON_TESTS_HELPER_H
#define REFLECTIVE_RAPIDJSON_TESTS_HELPER_H
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/misc/traits.h>
#include <c++utilities/tests/testutils.h>
// ensure "operator<<" from TestUtilities is visible prior to the call site
using TestUtilities::operator<<;
#include <cppunit/extensions/HelperMacros.h>
/*!
* \brief Asserts equality of two iteratables printing the differing indices.
*/
template <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>>...>
inline void assertEqualityLinewise(const Iteratable &iteratable1, const Iteratable &iteratable2)
{
std::vector<std::string> 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

View File

@ -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 <c++utilities/conversion/stringbuilder.h>
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/io/misc.h>
#include <c++utilities/tests/testutils.h>
using TestUtilities::operator<<; // must be visible prior to the call site
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <iostream>
#include <string>
#include <vector>
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<TestStruct> {
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<AnotherTestStruct> {
vector<string> 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<DerivedTestStruct> {
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<MultipleDerivedTestStruct> {
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 <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>>...>
inline void assertEqualityLinewise(const Iteratable &iteratable1, const Iteratable &iteratable2)
{
std::vector<std::string> 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"

56
generator/tests/structs.h Normal file
View File

@ -0,0 +1,56 @@
#ifndef REFLECTIVE_RAPIDJSON_TESTS_STRUCTS_H
#define REFLECTIVE_RAPIDJSON_TESTS_STRUCTS_H
#include "../../lib/jsonserializable.h"
#include <string>
#include <vector>
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<TestStruct> {
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<AnotherTestStruct> {
vector<string> 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<DerivedTestStruct> {
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<MultipleDerivedTestStruct> {
bool someBool = false;
};
#endif // REFLECTIVE_RAPIDJSON_TESTS_STRUCTS_H