From dbe09e0baba39856c32c33b237384182d1eb5260 Mon Sep 17 00:00:00 2001 From: Tristan Penman Date: Tue, 27 Aug 2019 12:19:27 +1000 Subject: [PATCH] Tidy up handling of optional dependencies --- CMakeLists.txt | 34 ++++----- tests/test_adapter_comparison.cpp | 109 ++++++++++++++++++--------- tests/test_json11_adapter.cpp | 5 -- tests/test_nlohmann_json_adapter.cpp | 5 -- tests/test_poco_json_adapter.cpp | 7 +- tests/test_validator.cpp | 19 ++--- 6 files changed, 98 insertions(+), 81 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db7a0f1..7a0ba36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) if(COMPILER_SUPPORTS_CXX11) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -DVALIJSON_BUILD_CXX11_ADAPTERS=1") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") else() message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") endif() @@ -61,7 +61,7 @@ include_directories(include SYSTEM if(valijson_BUILD_TESTS) if(NOT valijson_EXCLUDE_BOOST) - find_package(Boost 1.54.0) + find_package(Boost) endif() # Build local gtest @@ -77,22 +77,11 @@ if(valijson_BUILD_TESTS) tests/test_nlohmann_json_adapter.cpp tests/test_rapidjson_adapter.cpp tests/test_picojson_adapter.cpp - tests/test_poco_json_adapter.cpp tests/test_poly_constraint.cpp tests/test_validation_errors.cpp tests/test_validator.cpp ) - if(Qt5Core_FOUND) - include_directories(${Qt5Core_INCLUDE_DIRS}) - list(APPEND TEST_SOURCES tests/test_qtjson_adapter.cpp) - endif() - - if(Boost_FOUND) - include_directories(${Boost_INCLUDE_DIRS}) - list(APPEND TEST_SOURCES tests/test_property_tree_adapter.cpp) - endif() - # Unit tests executable add_executable(test_suite ${TEST_SOURCES}) @@ -102,20 +91,27 @@ if(valijson_BUILD_TESTS) set(TEST_LIBS gtest gtest_main jsoncpp json11) if(Boost_FOUND) + include_directories(${Boost_INCLUDE_DIRS}) + list(APPEND TEST_SOURCES tests/test_property_tree_adapter.cpp) add_definitions(-DBOOST_ALL_DYN_LINK) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) - endif() - - if(Qt5Core_FOUND) - list(APPEND TEST_LIBS Qt5::Core) - target_compile_definitions(test_suite PRIVATE "VALIJSON_BUILD_QT_ADAPTERS") + target_compile_definitions(test_suite PRIVATE "VALIJSON_BUILD_PROPERTY_TREE_ADAPTER") endif() if(Poco_FOUND) + include_directories(${Poco_INCLUDE_DIRS}) + list(APPEND TEST_SOURCES tests/test_poco_json_adapter.cpp) list(APPEND TEST_LIBS ${Poco_Foundation_LIBRARIES} ${Poco_JSON_LIBRARIES}) - target_compile_definitions(test_suite PRIVATE "VALIJSON_BUILD_POCO_ADAPTERS") + target_compile_definitions(test_suite PRIVATE "VALIJSON_BUILD_POCO_ADAPTER") + endif() + + if(Qt5Core_FOUND) + include_directories(${Qt5Core_INCLUDE_DIRS}) + list(APPEND TEST_SOURCES tests/test_qtjson_adapter.cpp) + list(APPEND TEST_LIBS Qt5::Core) + target_compile_definitions(test_suite PRIVATE "VALIJSON_BUILD_QT_ADAPTER") endif() target_link_libraries(test_suite ${TEST_LIBS} ${Boost_LIBRARIES}) diff --git a/tests/test_adapter_comparison.cpp b/tests/test_adapter_comparison.cpp index 7e0b251..e709a2f 100644 --- a/tests/test_adapter_comparison.cpp +++ b/tests/test_adapter_comparison.cpp @@ -2,35 +2,32 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef VALIJSON_BUILD_CXX11_ADAPTERS #include -#include - +#include #include +#include +#include + +#include +#include #include +#include +#include -#endif // VALIJSON_BUILD_CXX11_ADAPTERS +#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +#include +#include +#endif -#ifdef VALIJSON_BUILD_QT_ADAPTERS +#ifdef VALIJSON_BUILD_QT_ADAPTER #include #include +#endif -#endif // VALIJSON_BUILD_QT_ADAPTERS - -#ifdef VALIJSON_BUILD_POCO_ADAPTERS +#ifdef VALIJSON_BUILD_POCO_ADAPTER #include #include - -#endif // VALIJSON_BUILD_POCO_ADAPTERS +#endif #define TEST_DATA_DIR "../tests/data/documents/" @@ -136,6 +133,10 @@ protected: std::vector TestAdapterComparison::jsonFiles; +// +// JsonCppAdapter vs X +// ------------------------------------------------------------------------------------------------ + TEST_F(TestAdapterComparison, JsonCppVsJsonCpp) { testComparison< @@ -150,6 +151,8 @@ TEST_F(TestAdapterComparison, JsonCppVsPicoJson) valijson::adapters::PicoJsonAdapter>(); } +#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER + TEST_F(TestAdapterComparison, JsonCppVsPropertyTree) { testComparison< @@ -157,6 +160,8 @@ TEST_F(TestAdapterComparison, JsonCppVsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } +#endif + TEST_F(TestAdapterComparison, JsonCppVsRapidJson) { testComparison< @@ -173,6 +178,12 @@ TEST_F(TestAdapterComparison, JsonCppVsRapidJsonCrtAlloc) rapidjson::CrtAllocator> > >(); } +// +// PropertyTreeAdapter vs X +// ------------------------------------------------------------------------------------------------ + +#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER + TEST_F(TestAdapterComparison, PropertyTreeVsPicoJson) { testComparison< @@ -203,6 +214,12 @@ TEST_F(TestAdapterComparison, PropertyTreeVsRapidJsonCrtAlloc) rapidjson::CrtAllocator> > >(); } +#endif + +// +// RapidJson vs X +// ------------------------------------------------------------------------------------------------ + TEST_F(TestAdapterComparison, RapidJsonVsRapidJson) { testComparison< @@ -226,6 +243,10 @@ TEST_F(TestAdapterComparison, RapidJsonVsPicoJson) valijson::adapters::PicoJsonAdapter>(); } +// +// PicoJsonAdapter vs X +// ------------------------------------------------------------------------------------------------ + TEST_F(TestAdapterComparison, PicoJsonVsPicoJson) { testComparison< @@ -253,7 +274,9 @@ TEST_F(TestAdapterComparison, RapidJsonCrtAllocVsRapidJsonCrtAlloc) rapidjson::CrtAllocator> > >(); } -#ifdef VALIJSON_BUILD_CXX11_ADAPTERS +// +// Json11Adapter vs X +// ------------------------------------------------------------------------------------------------ TEST_F(TestAdapterComparison, Json11VsJson11) { @@ -293,6 +316,8 @@ TEST_F(TestAdapterComparison, Json11VsPicoJson) valijson::adapters::PicoJsonAdapter>(); } +#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER + TEST_F(TestAdapterComparison, Json11VsPropertyTree) { testComparison< @@ -300,6 +325,12 @@ TEST_F(TestAdapterComparison, Json11VsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } +#endif // VALIJSON_BUILD_PROPERTY_TREE_ADAPTER + +// +// NlohmannJsonAdapter vs X +// ------------------------------------------------------------------------------------------------ + TEST_F(TestAdapterComparison, NlohmannJsonVsNlohmannJson) { testComparison< valijson::adapters::NlohmannJsonAdapter, @@ -344,6 +375,8 @@ TEST_F(TestAdapterComparison, NlohmannJsonVsPicoJson) valijson::adapters::PicoJsonAdapter>(); } +#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER + TEST_F(TestAdapterComparison, NlohmannJsonVsPropertyTree) { testComparison< @@ -351,9 +384,13 @@ TEST_F(TestAdapterComparison, NlohmannJsonVsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } -#endif // VALIJSON_BUILD_CXX11_ADAPTERS +#endif // VALIJSON_BUILD_PROPERTY_TREE_ADAPTER -#ifdef VALIJSON_BUILD_QT_ADAPTERS +// +// QtJsonAdapter vs X +// ------------------------------------------------------------------------------------------------ + +#ifdef VALIJSON_BUILD_QT_ADAPTER TEST_F(TestAdapterComparison, QtJsonVsQtJson) { testComparison< @@ -391,6 +428,8 @@ TEST_F(TestAdapterComparison, QtJsonVsPicoJson) valijson::adapters::PicoJsonAdapter>(); } +#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER + TEST_F(TestAdapterComparison, QtJsonVsPropertyTree) { testComparison< @@ -398,9 +437,7 @@ TEST_F(TestAdapterComparison, QtJsonVsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } -#endif // VALIJSON_BUILD_QT_ADAPTERS - -#if defined(VALIJSON_BUILD_QT_ADAPTERS) && defined(VALIJSON_BUILD_CXX11_ADAPTERS) +#endif // VALIJSON_BUILD_PROPERTY_TREE_ADAPTER TEST_F(TestAdapterComparison, QtJsonVsJson11) { @@ -416,9 +453,13 @@ TEST_F(TestAdapterComparison, QtJsonVsNlohmannJson) valijson::adapters::NlohmannJsonAdapter>(); } -#endif +#endif // VALIJSON_BUILD_QT_ADAPTER -#ifdef VALIJSON_BUILD_POCO_ADAPTERS +// +// PocoJsonAdapter vs X +// ------------------------------------------------------------------------------------------------ + +#ifdef VALIJSON_BUILD_POCO_ADAPTER TEST_F(TestAdapterComparison, PocoJsonVsPocoJson) { @@ -457,6 +498,8 @@ TEST_F(TestAdapterComparison, PocoJsonVsPicoJson) valijson::adapters::PicoJsonAdapter>(); } +#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER + TEST_F(TestAdapterComparison, PocoJsonVsPropertyTree) { testComparison< @@ -464,9 +507,7 @@ TEST_F(TestAdapterComparison, PocoJsonVsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } -#endif - -#if defined(VALIJSON_BUILD_POCO_ADAPTERS) && defined(VALIJSON_BUILD_CXX11_ADAPTERS) +#endif // VALIJSON_BUILD_PROPERTY_TREE_ADAPTER TEST_F(TestAdapterComparison, PocoJsonVsJson11) { @@ -482,9 +523,7 @@ TEST_F(TestAdapterComparison, PocoJsonVsNlohmannJsonAdapter) valijson::adapters::NlohmannJsonAdapter>(); } -#endif - -#if defined(VALIJSON_BUILD_POCO_ADAPTERS) && defined(VALIJSON_BUILD_QT_ADAPTERS) +#ifdef VALIJSON_BUILD_QT_ADAPTER TEST_F(TestAdapterComparison, PocoJsonVsQtJson) { @@ -493,4 +532,6 @@ TEST_F(TestAdapterComparison, PocoJsonVsQtJson) valijson::adapters::QtJsonAdapter>(); } -#endif +#endif // VALIJSON_BUILD_QT_ADAPTER + +#endif // VALIJSON_BUILD_POCO_ADAPTER diff --git a/tests/test_json11_adapter.cpp b/tests/test_json11_adapter.cpp index 9250a03..a43ea74 100644 --- a/tests/test_json11_adapter.cpp +++ b/tests/test_json11_adapter.cpp @@ -1,5 +1,3 @@ -#ifdef VALIJSON_BUILD_CXX11_ADAPTERS - #include #include @@ -82,6 +80,3 @@ TEST_F(TestJson11Adapter, BasicObjectIteration) // Ensure that the correct number of elements were iterated over EXPECT_EQ( numElements, expectedValue ); } - -#endif // VALIJSON_BUILD_CXX11_ADAPTERS - diff --git a/tests/test_nlohmann_json_adapter.cpp b/tests/test_nlohmann_json_adapter.cpp index 075710e..e89a6a9 100644 --- a/tests/test_nlohmann_json_adapter.cpp +++ b/tests/test_nlohmann_json_adapter.cpp @@ -1,5 +1,3 @@ -#ifdef VALIJSON_BUILD_CXX11_ADAPTERS - #include #include @@ -79,6 +77,3 @@ TEST_F(TestNlohmannJsonAdapter, BasicObjectIteration) // Ensure that the correct number of elements were iterated over EXPECT_EQ( numElements, expectedValue ); } - -#endif // VALIJSON_BUILD_CXX11_ADAPTERS - diff --git a/tests/test_poco_json_adapter.cpp b/tests/test_poco_json_adapter.cpp index 9d2c17d..11249a6 100644 --- a/tests/test_poco_json_adapter.cpp +++ b/tests/test_poco_json_adapter.cpp @@ -1,5 +1,3 @@ -#ifdef VALIJSON_BUILD_POCO_ADAPTERS - #include #include @@ -15,7 +13,7 @@ TEST_F(TestPocoJsonAdapter, BasicArrayIteration) // Create a Json document that consists of an array of numbers Poco::JSON::Array::Ptr documentImpl = new Poco::JSON::Array(); - + for (unsigned int i = 0; i < numElements; i++) { documentImpl->set(i, static_cast(i)); } @@ -84,6 +82,3 @@ TEST_F(TestPocoJsonAdapter, BasicObjectIteration) // Ensure that the correct number of elements were iterated over EXPECT_EQ( numElements, expectedValue ); } - -#endif // VALIJSON_BUILD_POCO_ADAPTERS - diff --git a/tests/test_validator.cpp b/tests/test_validator.cpp index d417923..b2e15b7 100644 --- a/tests/test_validator.cpp +++ b/tests/test_validator.cpp @@ -4,26 +4,23 @@ #include +#include #include #include #include +#include #include -#include #include +#include #include #include #include #include -#ifdef VALIJSON_BUILD_CXX11_ADAPTERS -#include -#include -#endif // VALIJSON_BUILD_CXX11_ADAPTERS - -#ifdef VALIJSON_BUILD_POCO_ADAPTERS +#ifdef VALIJSON_BUILD_POCO_ADAPTER #include #include -#endif // VALIJSON_BUILD_POCO_ADAPTERS +#endif #define REMOTES_DIR "../thirdparty/JSON-Schema-Test-Suite/remotes/" @@ -172,13 +169,11 @@ protected: processTestFile(testFile, version); processTestFile(testFile, version); processTestFile(testFile, version); -#ifdef VALIJSON_BUILD_CXX11_ADAPTERS processTestFile(testFile, version); -#endif // VALIJSON_BUILD_CXX11_ADAPTERS -#ifdef VALIJSON_BUILD_POCO_ADAPTERS +#ifdef VALIJSON_BUILD_POCO_ADAPTER processTestFile(testFile, version); -#endif // VALIJSON_BUILD_POCO_ADAPTERS +#endif // VALIJSON_BUILD_POCO_ADAPTER } void processDraft3TestFile(const std::string &testFile)