From 79c125b54f760697481aca9761735175f18c042c Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Wed, 14 Sep 2022 18:15:47 +0200 Subject: [PATCH 01/13] Initialize data members for defaulted c'tors This patch addresses a static analysis issue reported by Cppcheck 2.9 where several classes in the toml/datetime.hpp header explicitly default all their special member functions, including the default constructor, but don't provide initializers for their data members. This might or might not have caused any observable surprising behavior but I agree with Cppcheck on this one in that an explicitly defaulted default constructor should be expected to initialize all data members. So let's do that. --- toml/datetime.hpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/toml/datetime.hpp b/toml/datetime.hpp index 36db1e10..83d04ba4 100644 --- a/toml/datetime.hpp +++ b/toml/datetime.hpp @@ -85,9 +85,9 @@ enum class month_t : std::uint8_t struct local_date { - std::int16_t year; // A.D. (like, 2018) - std::uint8_t month; // [0, 11] - std::uint8_t day; // [1, 31] + std::int16_t year{}; // A.D. (like, 2018) + std::uint8_t month{}; // [0, 11] + std::uint8_t day{}; // [1, 31] local_date(int y, month_t m, int d) : year (static_cast(y)), @@ -181,12 +181,12 @@ operator<<(std::basic_ostream& os, const local_date& date) struct local_time { - std::uint8_t hour; // [0, 23] - std::uint8_t minute; // [0, 59] - std::uint8_t second; // [0, 60] - std::uint16_t millisecond; // [0, 999] - std::uint16_t microsecond; // [0, 999] - std::uint16_t nanosecond; // [0, 999] + std::uint8_t hour{}; // [0, 23] + std::uint8_t minute{}; // [0, 59] + std::uint8_t second{}; // [0, 60] + std::uint16_t millisecond{}; // [0, 999] + std::uint16_t microsecond{}; // [0, 999] + std::uint16_t nanosecond{}; // [0, 999] local_time(int h, int m, int s, int ms = 0, int us = 0, int ns = 0) @@ -297,8 +297,8 @@ operator<<(std::basic_ostream& os, const local_time& time) struct time_offset { - std::int8_t hour; // [-12, 12] - std::int8_t minute; // [-59, 59] + std::int8_t hour{}; // [-12, 12] + std::int8_t minute{}; // [-59, 59] time_offset(int h, int m) : hour (static_cast(h)), @@ -364,8 +364,8 @@ operator<<(std::basic_ostream& os, const time_offset& offset) struct local_datetime { - local_date date; - local_time time; + local_date date{}; + local_time time{}; local_datetime(local_date d, local_time t): date(d), time(t) {} @@ -478,9 +478,9 @@ operator<<(std::basic_ostream& os, const local_datetime& dt) struct offset_datetime { - local_date date; - local_time time; - time_offset offset; + local_date date{}; + local_time time{}; + time_offset offset{}; offset_datetime(local_date d, local_time t, time_offset o) : date(d), time(t), offset(o) From 8bb2c63a0184496f0fa44d852396ef7dc954d4d9 Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Wed, 14 Sep 2022 18:55:57 +0200 Subject: [PATCH 02/13] Don't compare iterators from potentially different containers This patch addresses a static analysis issue reported by Cppcheck 2.9 where an assertion in the toml/region.hpp header would compare two container's (that are known to be of type std::vector) begin() and end() iterators in order to verify that they are the same. This assertion either passes or invokes undefined behavior. Which isn't technically wrong because calling code must always ensure that preconditions are met and assertions therefore pass anyway but it does make the value that is added by having the assertion in the first place marginal. Fortunately, the assertion was easy to rewrite: Just compare the container's address itself. This is well-defined regardless of whether the assertion will pass or fail. --- toml/region.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/toml/region.hpp b/toml/region.hpp index 2e01e51d..8c4f2f16 100644 --- a/toml/region.hpp +++ b/toml/region.hpp @@ -227,8 +227,7 @@ struct region final : public region_base region& operator+=(const region& other) { // different regions cannot be concatenated - assert(this->begin() == other.begin() && this->end() == other.end() && - this->last_ == other.first_); + assert(this->source_ == other.source_ && this->last_ == other.first_); this->last_ = other.last_; return *this; From cf8a977be2f8466a28141675fbbb3edc16cefe4a Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Wed, 14 Sep 2022 19:51:57 +0200 Subject: [PATCH 03/13] Don't deliberately dereference the null pointer This patch addresses a static analysis issue reported by Cppcheck 2.9 where several member functions of the toml::discard_comment class defined in the toml/comments.hpp header were implemented to deliberately dereference the null pointer returned unconditionally by the always-empty container's data() member function. This behavior wasn't technically wrong because those functions all have as precondition that the container is non-empty so they must never be called on an instance of toml::discard_comment but we can still be more helpful without adversely affecting code generation. Instead of dereferencing the null pointer, this patch has these functions call an inline private helper function which is defined to invoke __builtin_unreachable() if available "and then" throw an exception with a helpful error message. Even at the -O1 level, GCC will optimize the code under the assumption that the function will never be called (i.e. no assembly is emitted), making failure to ensure this undefined behavior exactly as if the null pointer had been dereferenced. However, static analysis will now understand the programmer's intent and remain silent. Furthermore, when using the -O0 or -Og levels, GCC won't optimize under this assumption so the exception will be thrown and might be helpful for debugging. Compilers that don't have __builtin_unreachable() won't get any help in determining that the function must not be called and will have to figure this out by analyzing the calling code -- which really shouldn't exist in the first place anyway as the whole point is that these functions must not be called. --- toml/comments.hpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/toml/comments.hpp b/toml/comments.hpp index ec250411..c0f66f39 100644 --- a/toml/comments.hpp +++ b/toml/comments.hpp @@ -425,14 +425,14 @@ struct discard_comments // empty, so accessing through operator[], front/back, data causes address // error. - reference operator[](const size_type) noexcept {return *data();} - const_reference operator[](const size_type) const noexcept {return *data();} + reference operator[](const size_type) noexcept {never_call("toml::discard_comment::operator[]");} + const_reference operator[](const size_type) const noexcept {never_call("toml::discard_comment::operator[]");} reference at(const size_type) {throw std::out_of_range("toml::discard_comment is always empty.");} const_reference at(const size_type) const {throw std::out_of_range("toml::discard_comment is always empty.");} - reference front() noexcept {return *data();} - const_reference front() const noexcept {return *data();} - reference back() noexcept {return *data();} - const_reference back() const noexcept {return *data();} + reference front() noexcept {never_call("toml::discard_comment::front");} + const_reference front() const noexcept {never_call("toml::discard_comment::front");} + reference back() noexcept {never_call("toml::discard_comment::back");} + const_reference back() const noexcept {never_call("toml::discard_comment::back");} pointer data() noexcept {return nullptr;} const_pointer data() const noexcept {return nullptr;} @@ -450,6 +450,18 @@ struct discard_comments const_reverse_iterator rend() const noexcept {return const_iterator{};} const_reverse_iterator crbegin() const noexcept {return const_iterator{};} const_reverse_iterator crend() const noexcept {return const_iterator{};} + + private: + + [[noreturn]] static void never_call(const char *const this_function) + { +#ifdef __has_builtin +# if __has_builtin(__builtin_unreachable) + __builtin_unreachable(); +# endif +#endif + throw std::logic_error{this_function}; + } }; inline bool operator==(const discard_comments&, const discard_comments&) noexcept {return true;} From 5312b8eb0e70060e181c464f2541dd87ca76760f Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Thu, 15 Sep 2022 19:54:27 +0200 Subject: [PATCH 04/13] Honor UNITTEST_FRAMEWORK_LIBRARY_EXIST in all unit test files Most unit test files checked UNITTEST_FRAMEWORK_LIBRARY_EXIST and adapted themselves accordingly to either use the header-only version or link with the library. Alas, eight files didn't so the project couldn't really be tested with the header-only version of that Boost library. This patch adds the missing pre-processor logic to the files that were missing it. The style of using no indentation after the '#' was followed from the existing unit test files. Some other files in this repository do indent their pre-processor logic, though. Since replicating the conditional in every file is kind of verbose, tedious and, apparently, easily forgotten, I'm wondering whether isolating that logic into a tiny little auxiliary header and then unconditionally including that one in each unit test file would be a good idea, though. --- tests/test_lex_boolean.cpp | 5 +++++ tests/test_lex_datetime.cpp | 5 +++++ tests/test_lex_floating.cpp | 5 +++++ tests/test_lex_integer.cpp | 5 +++++ tests/test_lex_key_comment.cpp | 5 +++++ tests/test_lex_string.cpp | 5 +++++ tests/test_result.cpp | 5 +++++ tests/test_string.cpp | 5 +++++ 8 files changed, 40 insertions(+) diff --git a/tests/test_lex_boolean.cpp b/tests/test_lex_boolean.cpp index c2e43a87..c872de3d 100644 --- a/tests/test_lex_boolean.cpp +++ b/tests/test_lex_boolean.cpp @@ -1,5 +1,10 @@ #define BOOST_TEST_MODULE "test_lex_boolean" +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include +#else +#define BOOST_TEST_NO_LIB +#include +#endif #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_datetime.cpp b/tests/test_lex_datetime.cpp index a790eebc..fa9b4c33 100644 --- a/tests/test_lex_datetime.cpp +++ b/tests/test_lex_datetime.cpp @@ -1,5 +1,10 @@ #define BOOST_TEST_MODULE "test_lex_datetime" +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include +#else +#define BOOST_TEST_NO_LIB +#include +#endif #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_floating.cpp b/tests/test_lex_floating.cpp index 9c5c8097..882cd81b 100644 --- a/tests/test_lex_floating.cpp +++ b/tests/test_lex_floating.cpp @@ -1,5 +1,10 @@ #define BOOST_TEST_MODULE "test_lex_floating" +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include +#else +#define BOOST_TEST_NO_LIB +#include +#endif #include #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_integer.cpp b/tests/test_lex_integer.cpp index bb6dbb7a..f16c81cb 100644 --- a/tests/test_lex_integer.cpp +++ b/tests/test_lex_integer.cpp @@ -1,5 +1,10 @@ #define BOOST_TEST_MODULE "test_lex_integer" +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include +#else +#define BOOST_TEST_NO_LIB +#include +#endif #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_key_comment.cpp b/tests/test_lex_key_comment.cpp index 0921bd6f..ba5c37c8 100644 --- a/tests/test_lex_key_comment.cpp +++ b/tests/test_lex_key_comment.cpp @@ -1,5 +1,10 @@ #define BOOST_TEST_MODULE "lex_key_comment_test" +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include +#else +#define BOOST_TEST_NO_LIB +#include +#endif #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_string.cpp b/tests/test_lex_string.cpp index 643ab31a..0671358b 100644 --- a/tests/test_lex_string.cpp +++ b/tests/test_lex_string.cpp @@ -1,5 +1,10 @@ #define BOOST_TEST_MODULE "test_lex_string" +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include +#else +#define BOOST_TEST_NO_LIB +#include +#endif #include #include "test_lex_aux.hpp" diff --git a/tests/test_result.cpp b/tests/test_result.cpp index d693bd69..36368a27 100644 --- a/tests/test_result.cpp +++ b/tests/test_result.cpp @@ -1,5 +1,10 @@ #define BOOST_TEST_MODULE "test_result" +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include +#else +#define BOOST_TEST_NO_LIB +#include +#endif #include #include diff --git a/tests/test_string.cpp b/tests/test_string.cpp index 444fc639..473c6119 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -1,5 +1,10 @@ #define BOOST_TEST_MODULE "test_string" +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include +#else +#define BOOST_TEST_NO_LIB +#include +#endif #include BOOST_AUTO_TEST_CASE(test_basic_string) From fe471bcbe92376d529cd081292e6174f108eb6bb Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Thu, 15 Sep 2022 21:23:16 +0200 Subject: [PATCH 05/13] Autodetect the best option to use Boost.Test The conditional inclusion of either the library or the header-only version of the Boost.Test header wasn't tremendously useful in practice because the tests/CMakeLists.txt file would unconditionally add compile definitions to basically fore dynamic linking. This patch adds feature detection to the tests/CMakeLists.txt file to determine whether to use dynamic linking, static linking or the header-only version (in that order of preference, for best performance). The automatic detection could be overridden if needed by defining the TOML11_WITH_BOOST_TEST_{HEADER,STATIC,DYNAMIC} variables on the CMake command line. While we are at it, instead of having a conditional #define BOOST_TEST_NO_LIB in each unit test file, handle this once in the CMakeLists.txt file. --- tests/CMakeLists.txt | 44 ++++++++++++++++++++++++++--- tests/test_comments.cpp | 1 - tests/test_datetime.cpp | 1 - tests/test_error_detection.cpp | 1 - tests/test_expect.cpp | 1 - tests/test_extended_conversions.cpp | 1 - tests/test_find.cpp | 1 - tests/test_find_or.cpp | 1 - tests/test_find_or_recursive.cpp | 1 - tests/test_format_error.cpp | 1 - tests/test_get.cpp | 1 - tests/test_get_or.cpp | 1 - tests/test_lex_boolean.cpp | 1 - tests/test_lex_datetime.cpp | 1 - tests/test_lex_floating.cpp | 1 - tests/test_lex_integer.cpp | 1 - tests/test_lex_key_comment.cpp | 1 - tests/test_lex_string.cpp | 1 - tests/test_literals.cpp | 1 - tests/test_parse_array.cpp | 1 - tests/test_parse_boolean.cpp | 1 - tests/test_parse_datetime.cpp | 1 - tests/test_parse_file.cpp | 1 - tests/test_parse_floating.cpp | 1 - tests/test_parse_inline_table.cpp | 1 - tests/test_parse_integer.cpp | 1 - tests/test_parse_key.cpp | 1 - tests/test_parse_string.cpp | 1 - tests/test_parse_table.cpp | 1 - tests/test_parse_table_key.cpp | 1 - tests/test_parse_unicode.cpp | 1 - tests/test_result.cpp | 1 - tests/test_serialize_file.cpp | 1 - tests/test_string.cpp | 1 - tests/test_traits.cpp | 1 - tests/test_utility.cpp | 1 - tests/test_value.cpp | 1 - 37 files changed, 40 insertions(+), 40 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a3c32885..eec938ae 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -152,13 +152,10 @@ if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4265") endif() -find_package(Boost COMPONENTS unit_test_framework REQUIRED) -add_definitions(-DBOOST_TEST_DYN_LINK) -add_definitions(-DUNITTEST_FRAMEWORK_LIBRARY_EXIST) +include(CheckCXXSourceCompiles) # check which standard library implementation is used. If libstdc++ is used, # it will fail to compile. It compiles if libc++ is used. -include(CheckCXXSourceCompiles) check_cxx_source_compiles(" #include #ifdef __GLIBCXX__ @@ -183,6 +180,45 @@ int main() { # the tests run. We can set this option and, I think, it is easier and better. option(TOML11_REQUIRE_FILESYSTEM_LIBRARY "need to link -lstdc++fs or -lc++fs" OFF) +find_package(Boost COMPONENTS unit_test_framework REQUIRED) + +list(APPEND CMAKE_REQUIRED_INCLUDES ${Boost_INCLUDE_DIRS}) +list(APPEND CMAKE_REQUIRED_LIBRARIES ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) + +check_cxx_source_compiles(" +#define BOOST_TEST_MODULE \"dummy\" +#undef BOOST_TEST_DYN_LINK +#define BOOST_TEST_NO_LIB +#include +BOOST_AUTO_TEST_CASE(proforma) { BOOST_TEST(true); } +" TOML11_WITH_BOOST_TEST_HEADER) + +check_cxx_source_compiles(" +#define BOOST_TEST_MODULE \"dummy\" +#undef BOOST_TEST_DYN_LINK +#undef BOOST_TEST_NO_LIB +#include +BOOST_AUTO_TEST_CASE(proforma) { BOOST_TEST(true); } +" TOML11_WITH_BOOST_TEST_STATIC) + +check_cxx_source_compiles(" +#define BOOST_TEST_MODULE \"dummy\" +#define BOOST_TEST_DYN_LINK +#undef BOOST_TEST_NO_LIB +#include +BOOST_AUTO_TEST_CASE(proforma) { BOOST_TEST(true); } +" TOML11_WITH_BOOST_TEST_DYNAMIC) + +if(TOML11_WITH_BOOST_TEST_DYNAMIC) + add_definitions(-DUNITTEST_FRAMEWORK_LIBRARY_EXIST -DBOOST_TEST_DYN_LINK) +elseif(TOML11_WITH_BOOST_TEST_STATIC) + add_definitions(-DUNITTEST_FRAMEWORK_LIBRARY_EXIST) +elseif(TOML11_WITH_BOOST_TEST_HEADER) + add_definitions(-DBOOST_TEST_NO_LIB) +else() + message(FATAL_ERROR "Neither the Boost.Test static or shared library nor the header-only version seem to be usable.") +endif() + foreach(TEST_NAME ${TEST_NAMES}) add_executable(${TEST_NAME} ${TEST_NAME}.cpp) target_link_libraries(${TEST_NAME} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} toml11::toml11) diff --git a/tests/test_comments.cpp b/tests/test_comments.cpp index 8cc65739..23d4a680 100644 --- a/tests/test_comments.cpp +++ b/tests/test_comments.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif diff --git a/tests/test_datetime.cpp b/tests/test_datetime.cpp index 395949e9..31bdcf7f 100644 --- a/tests/test_datetime.cpp +++ b/tests/test_datetime.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_error_detection.cpp b/tests/test_error_detection.cpp index 07c607f4..fbefcd4b 100644 --- a/tests/test_error_detection.cpp +++ b/tests/test_error_detection.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_expect.cpp b/tests/test_expect.cpp index 2971f0c0..d376ef1e 100644 --- a/tests/test_expect.cpp +++ b/tests/test_expect.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_extended_conversions.cpp b/tests/test_extended_conversions.cpp index 7e727ac8..e84f0133 100644 --- a/tests/test_extended_conversions.cpp +++ b/tests/test_extended_conversions.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_find.cpp b/tests/test_find.cpp index aa44b7b5..cf58017d 100644 --- a/tests/test_find.cpp +++ b/tests/test_find.cpp @@ -3,7 +3,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif diff --git a/tests/test_find_or.cpp b/tests/test_find_or.cpp index b0f786dc..ed36e3a8 100644 --- a/tests/test_find_or.cpp +++ b/tests/test_find_or.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_find_or_recursive.cpp b/tests/test_find_or_recursive.cpp index 364112ac..d31e8b6e 100644 --- a/tests/test_find_or_recursive.cpp +++ b/tests/test_find_or_recursive.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_format_error.cpp b/tests/test_format_error.cpp index df558da8..1ae8db65 100644 --- a/tests/test_format_error.cpp +++ b/tests/test_format_error.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_get.cpp b/tests/test_get.cpp index a0dd3157..090eb077 100644 --- a/tests/test_get.cpp +++ b/tests/test_get.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_get_or.cpp b/tests/test_get_or.cpp index 081b966a..7aad2733 100644 --- a/tests/test_get_or.cpp +++ b/tests/test_get_or.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_lex_boolean.cpp b/tests/test_lex_boolean.cpp index c872de3d..933ab9d7 100644 --- a/tests/test_lex_boolean.cpp +++ b/tests/test_lex_boolean.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_lex_datetime.cpp b/tests/test_lex_datetime.cpp index fa9b4c33..25e24bf9 100644 --- a/tests/test_lex_datetime.cpp +++ b/tests/test_lex_datetime.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_lex_floating.cpp b/tests/test_lex_floating.cpp index 882cd81b..e14bdb32 100644 --- a/tests/test_lex_floating.cpp +++ b/tests/test_lex_floating.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_lex_integer.cpp b/tests/test_lex_integer.cpp index f16c81cb..1e8a31bc 100644 --- a/tests/test_lex_integer.cpp +++ b/tests/test_lex_integer.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_lex_key_comment.cpp b/tests/test_lex_key_comment.cpp index ba5c37c8..1aad90b6 100644 --- a/tests/test_lex_key_comment.cpp +++ b/tests/test_lex_key_comment.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_lex_string.cpp b/tests/test_lex_string.cpp index 0671358b..880ee923 100644 --- a/tests/test_lex_string.cpp +++ b/tests/test_lex_string.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_literals.cpp b/tests/test_literals.cpp index 5aaf9283..9d8ebeaf 100644 --- a/tests/test_literals.cpp +++ b/tests/test_literals.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_array.cpp b/tests/test_parse_array.cpp index a75a6f19..ee6a9ae1 100644 --- a/tests/test_parse_array.cpp +++ b/tests/test_parse_array.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_boolean.cpp b/tests/test_parse_boolean.cpp index 068aaa4a..92c0042f 100644 --- a/tests/test_parse_boolean.cpp +++ b/tests/test_parse_boolean.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_datetime.cpp b/tests/test_parse_datetime.cpp index e7247b09..5e86108b 100644 --- a/tests/test_parse_datetime.cpp +++ b/tests/test_parse_datetime.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_file.cpp b/tests/test_parse_file.cpp index b525f984..c4aba749 100644 --- a/tests/test_parse_file.cpp +++ b/tests/test_parse_file.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_floating.cpp b/tests/test_parse_floating.cpp index 15f433d1..3fa2d03f 100644 --- a/tests/test_parse_floating.cpp +++ b/tests/test_parse_floating.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_inline_table.cpp b/tests/test_parse_inline_table.cpp index 5fa99c16..9b3460a5 100644 --- a/tests/test_parse_inline_table.cpp +++ b/tests/test_parse_inline_table.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_integer.cpp b/tests/test_parse_integer.cpp index 8e73686e..ea203afc 100644 --- a/tests/test_parse_integer.cpp +++ b/tests/test_parse_integer.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_key.cpp b/tests/test_parse_key.cpp index f05c26b9..f7448a61 100644 --- a/tests/test_parse_key.cpp +++ b/tests/test_parse_key.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_string.cpp b/tests/test_parse_string.cpp index 5367c005..48374600 100644 --- a/tests/test_parse_string.cpp +++ b/tests/test_parse_string.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_table.cpp b/tests/test_parse_table.cpp index cb5baeeb..b6c9dd03 100644 --- a/tests/test_parse_table.cpp +++ b/tests/test_parse_table.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_table_key.cpp b/tests/test_parse_table_key.cpp index 47df434a..1a7466bc 100644 --- a/tests/test_parse_table_key.cpp +++ b/tests/test_parse_table_key.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_parse_unicode.cpp b/tests/test_parse_unicode.cpp index e3797a60..3c54cb02 100644 --- a/tests/test_parse_unicode.cpp +++ b/tests/test_parse_unicode.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_result.cpp b/tests/test_result.cpp index 36368a27..9a700417 100644 --- a/tests/test_result.cpp +++ b/tests/test_result.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_serialize_file.cpp b/tests/test_serialize_file.cpp index 7e1d8e1a..75733637 100644 --- a/tests/test_serialize_file.cpp +++ b/tests/test_serialize_file.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_string.cpp b/tests/test_string.cpp index 473c6119..a9fec2a0 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_traits.cpp b/tests/test_traits.cpp index 63b8a142..f6f5bc1e 100644 --- a/tests/test_traits.cpp +++ b/tests/test_traits.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_utility.cpp b/tests/test_utility.cpp index 69193997..05981c25 100644 --- a/tests/test_utility.cpp +++ b/tests/test_utility.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include diff --git a/tests/test_value.cpp b/tests/test_value.cpp index 099d502f..b25731e2 100644 --- a/tests/test_value.cpp +++ b/tests/test_value.cpp @@ -2,7 +2,6 @@ #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else -#define BOOST_TEST_NO_LIB #include #endif #include From ac949437f8534014403651cec41fab54e148d619 Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Thu, 15 Sep 2022 22:01:35 +0200 Subject: [PATCH 06/13] Avoid false negative Boost.Test detection due to -Werror Depending on the CMake and Boost version, the -Werror flags that might get added to CMAKE_CXX_FLAGS could adversely affect the feature detection logic, leading to the wrong conclusion that Boost.Test isn't usable altogether. Performing these checks first and only afterwards altering CMAKE_CXX_FLAGS avoids this issue. --- tests/CMakeLists.txt | 143 +++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 67 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index eec938ae..c6ee6e23 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -62,6 +62,82 @@ CHECK_CXX_COMPILER_FLAG("-Wrange-loop-analysis" COMPILER_SUPPORTS_WRANGE_LOOP_AN CHECK_CXX_COMPILER_FLAG("-Wundef" COMPILER_SUPPORTS_WUNDEF) CHECK_CXX_COMPILER_FLAG("-Wshadow" COMPILER_SUPPORTS_WSHADOW) +include(CheckCXXSourceCompiles) + +# check which standard library implementation is used. If libstdc++ is used, +# it will fail to compile. It compiles if libc++ is used. +check_cxx_source_compiles(" +#include +#ifdef __GLIBCXX__ + static_assert(false); +#endif +int main() { + return 0; +}" TOML11_WITH_LIBCXX_LIBRARY) + +# LLVM 8 requires -lc++fs if compiled with libc++ to use . +# LLVM 9+ does not require any special library. +# GCC 8 requires -lstdc++fs. GCC 9+ does not require it. +# +# Yes, we can check the version of the compiler used in the current build +# directly in CMake. But, in most cases, clang build uses libstdc++ as the +# standard library implementation and it makes the condition complicated. +# In many environment, the default installed C++ compiler is GCC and libstdc++ +# is installed along with it. In most build on such an environment, even if we +# chose clang as the C++ compiler, still libstdc++ is used. Checking default +# gcc version makes the condition complicated. +# The purpose of this file is to compile tests. We know the environment on which +# the tests run. We can set this option and, I think, it is easier and better. +option(TOML11_REQUIRE_FILESYSTEM_LIBRARY "need to link -lstdc++fs or -lc++fs" OFF) + +find_package(Boost COMPONENTS unit_test_framework REQUIRED) + +set(PREVIOUSLY_REQUIRED_INCLUDES "${CMAKE_REQUIRED_INCLUDES}") +set(PREVIOUSLY_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}") + +list(APPEND CMAKE_REQUIRED_INCLUDES ${Boost_INCLUDE_DIRS}) +list(APPEND CMAKE_REQUIRED_LIBRARIES ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) + +check_cxx_source_compiles(" +#define BOOST_TEST_MODULE \"dummy\" +#undef BOOST_TEST_DYN_LINK +#define BOOST_TEST_NO_LIB +#include +BOOST_AUTO_TEST_CASE(proforma) { BOOST_TEST(true); } +" TOML11_WITH_BOOST_TEST_HEADER) + +check_cxx_source_compiles(" +#define BOOST_TEST_MODULE \"dummy\" +#undef BOOST_TEST_DYN_LINK +#undef BOOST_TEST_NO_LIB +#include +BOOST_AUTO_TEST_CASE(proforma) { BOOST_TEST(true); } +" TOML11_WITH_BOOST_TEST_STATIC) + +check_cxx_source_compiles(" +#define BOOST_TEST_MODULE \"dummy\" +#define BOOST_TEST_DYN_LINK +#undef BOOST_TEST_NO_LIB +#include +BOOST_AUTO_TEST_CASE(proforma) { BOOST_TEST(true); } +" TOML11_WITH_BOOST_TEST_DYNAMIC) + +set(CMAKE_REQUIRED_INCLUDES "${PREVIOUSLY_REQUIRED_INCLUDES}") +set(CMAKE_REQUIRED_LIBRARIES "${PREVIOUSLY_REQUIRED_LIBRARIES}") + +unset(PREVIOUSLY_REQUIRED_INCLUDES) +unset(PREVIOUSLY_REQUIRED_LIBRARIES) + +if(TOML11_WITH_BOOST_TEST_DYNAMIC) + add_definitions(-DUNITTEST_FRAMEWORK_LIBRARY_EXIST -DBOOST_TEST_DYN_LINK) +elseif(TOML11_WITH_BOOST_TEST_STATIC) + add_definitions(-DUNITTEST_FRAMEWORK_LIBRARY_EXIST) +elseif(TOML11_WITH_BOOST_TEST_HEADER) + add_definitions(-DBOOST_TEST_NO_LIB) +else() + message(FATAL_ERROR "Neither the Boost.Test static or shared library nor the header-only version seem to be usable.") +endif() + if(COMPILER_SUPPORTS_WALL) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") endif() @@ -152,73 +228,6 @@ if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4265") endif() -include(CheckCXXSourceCompiles) - -# check which standard library implementation is used. If libstdc++ is used, -# it will fail to compile. It compiles if libc++ is used. -check_cxx_source_compiles(" -#include -#ifdef __GLIBCXX__ - static_assert(false); -#endif -int main() { - return 0; -}" TOML11_WITH_LIBCXX_LIBRARY) - -# LLVM 8 requires -lc++fs if compiled with libc++ to use . -# LLVM 9+ does not require any special library. -# GCC 8 requires -lstdc++fs. GCC 9+ does not require it. -# -# Yes, we can check the version of the compiler used in the current build -# directly in CMake. But, in most cases, clang build uses libstdc++ as the -# standard library implementation and it makes the condition complicated. -# In many environment, the default installed C++ compiler is GCC and libstdc++ -# is installed along with it. In most build on such an environment, even if we -# chose clang as the C++ compiler, still libstdc++ is used. Checking default -# gcc version makes the condition complicated. -# The purpose of this file is to compile tests. We know the environment on which -# the tests run. We can set this option and, I think, it is easier and better. -option(TOML11_REQUIRE_FILESYSTEM_LIBRARY "need to link -lstdc++fs or -lc++fs" OFF) - -find_package(Boost COMPONENTS unit_test_framework REQUIRED) - -list(APPEND CMAKE_REQUIRED_INCLUDES ${Boost_INCLUDE_DIRS}) -list(APPEND CMAKE_REQUIRED_LIBRARIES ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) - -check_cxx_source_compiles(" -#define BOOST_TEST_MODULE \"dummy\" -#undef BOOST_TEST_DYN_LINK -#define BOOST_TEST_NO_LIB -#include -BOOST_AUTO_TEST_CASE(proforma) { BOOST_TEST(true); } -" TOML11_WITH_BOOST_TEST_HEADER) - -check_cxx_source_compiles(" -#define BOOST_TEST_MODULE \"dummy\" -#undef BOOST_TEST_DYN_LINK -#undef BOOST_TEST_NO_LIB -#include -BOOST_AUTO_TEST_CASE(proforma) { BOOST_TEST(true); } -" TOML11_WITH_BOOST_TEST_STATIC) - -check_cxx_source_compiles(" -#define BOOST_TEST_MODULE \"dummy\" -#define BOOST_TEST_DYN_LINK -#undef BOOST_TEST_NO_LIB -#include -BOOST_AUTO_TEST_CASE(proforma) { BOOST_TEST(true); } -" TOML11_WITH_BOOST_TEST_DYNAMIC) - -if(TOML11_WITH_BOOST_TEST_DYNAMIC) - add_definitions(-DUNITTEST_FRAMEWORK_LIBRARY_EXIST -DBOOST_TEST_DYN_LINK) -elseif(TOML11_WITH_BOOST_TEST_STATIC) - add_definitions(-DUNITTEST_FRAMEWORK_LIBRARY_EXIST) -elseif(TOML11_WITH_BOOST_TEST_HEADER) - add_definitions(-DBOOST_TEST_NO_LIB) -else() - message(FATAL_ERROR "Neither the Boost.Test static or shared library nor the header-only version seem to be usable.") -endif() - foreach(TEST_NAME ${TEST_NAMES}) add_executable(${TEST_NAME} ${TEST_NAME}.cpp) target_link_libraries(${TEST_NAME} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} toml11::toml11) From b10348c5762f4fcadf21a339e12c736eacb95c6e Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Fri, 16 Sep 2022 11:51:32 +0200 Subject: [PATCH 07/13] More flexible https://github.com/toml-lang/toml handling Instead of unconditionally attempting to clone from a fixed location (GitHub) during the build / test process, honor the following two configuration variables: TOML11_LANGSPEC_GIT_REPOSITORY Can be set to override the URL from which the repository is cloned. This allows using a local mirror, including file:// URLs for working offline or reducing network traffic. TOML11_LANGSPEC_SOURCE_DIR Can be set to configure the location at which the repository is expected. If it already exists no download will be attempted. This allows avoiding the additional git-clone(1) altogether and use an existing directory as-is. This offers two new possibilities: (1) The same checkout can be reused for building multiple configurations (e.g. Debug versus Release) saving a little bit of time and disk space. (2) Experimental changes can easily be applied to the local source tree without having them destroyed by the build process. In order for this flexible location to work, the unit tests which attempt to read files from the repository had to be adjusted. They now honor an environment variable TOMLDIR which can be set to point to an alternate root directory. All defaults are set such that the previous behavior is maintained. Instead of introducing the TOMLDIR environment variable, an alternative solution would have been to set the WORKING_DIRECTORY of the tests to the TOML11_LANGSPEC_SOURCE_DIR and leave the relative paths in these tests hard-coded. Alas, some tests also expect that they can /write/ into the current working directory which isn't desirable if it is potentially pointing outside the build tree. I personally prefer to mount the source directory read-only and build in a fast tempfs, so this would e a problem. To be perfectly honest, I don't quite understand why these tests need to write to the file system in the first place, though. It seems to me that refactoring them to serialize to a std::ostrstream instead of a std::ofstream would not only simplify but also speed up the unit tests and avoid file system problems. But there might have been a hidden reason why actually using the real file system was considered necessary for these tests, so I didn't went ahead with that change yet. --- tests/CMakeLists.txt | 33 +++++++++++++-------- tests/test_parse_file.cpp | 54 +++++++++++++++++++++-------------- tests/test_parse_unicode.cpp | 14 ++++++++- tests/test_serialize_file.cpp | 42 +++++++++++++++++---------- 4 files changed, 93 insertions(+), 50 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c6ee6e23..11e449e1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,11 +1,20 @@ include(ExternalProject) -ExternalProject_Add(toml - SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/toml - GIT_REPOSITORY https://github.com/toml-lang/toml - GIT_TAG v0.5.0 + +set(TOML11_LANGSPEC_GIT_REPOSITORY "https://github.com/toml-lang/toml" CACHE STRING + "URL of the TOML language specification repository") + +set(TOML11_LANGSPEC_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/toml" CACHE FILEPATH + "directory for the TOML language specification tree") + +if(NOT EXISTS "${TOML11_LANGSPEC_SOURCE_DIR}/toml.abnf") + ExternalProject_Add(toml + SOURCE_DIR "${TOML11_LANGSPEC_SOURCE_DIR}" + GIT_REPOSITORY "${TOML11_LANGSPEC_GIT_REPOSITORY}" + GIT_TAG "v0.5.0" CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "") +endif() set(TEST_NAMES test_datetime @@ -228,6 +237,13 @@ if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4265") endif() +set(TEST_ENVIRON "TOMLDIR=${TOML11_LANGSPEC_SOURCE_DIR}") +if(WIN32) + # Set the PATH to be able to find Boost DLL + STRING(REPLACE ";" "\\;" PATH_STRING "$ENV{PATH}") + list(APPEND TEST_ENVIRON "PATH=${PATH_STRING}\;${Boost_LIBRARY_DIRS}") +endif() + foreach(TEST_NAME ${TEST_NAMES}) add_executable(${TEST_NAME} ${TEST_NAME}.cpp) target_link_libraries(${TEST_NAME} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} toml11::toml11) @@ -257,14 +273,7 @@ foreach(TEST_NAME ${TEST_NAMES}) endif() add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) - - # Set the PATH to be able to find Boost DLL - if(WIN32) - STRING(REPLACE ";" "\\;" PATH_STRING "$ENV{PATH}") - set_tests_properties(${TEST_NAME} - PROPERTIES ENVIRONMENT "PATH=${PATH_STRING}\;${Boost_LIBRARY_DIRS}" - ) - endif() + set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT "${TEST_ENVIRON}") endforeach(TEST_NAME) diff --git a/tests/test_parse_file.cpp b/tests/test_parse_file.cpp index c4aba749..144168ba 100644 --- a/tests/test_parse_file.cpp +++ b/tests/test_parse_file.cpp @@ -9,10 +9,22 @@ #include #include #include +#include + +static auto testinput(const std::string& basename) -> std::string +{ + const auto this_or_that = [](const char *const s, const char *const t) { return s ? s : t; }; + std::string directory = this_or_that(std::getenv("TOMLDIR"), "toml"); + if (!directory.empty() && directory.back() != '/') + { + directory.push_back('/'); + } + return directory.append("tests/").append(basename); +} BOOST_AUTO_TEST_CASE(test_example) { - const auto data = toml::parse("toml/tests/example.toml"); + const auto data = toml::parse(testinput("example.toml")); BOOST_TEST(toml::find(data, "title") == "TOML Example"); const auto& owner = toml::find(data, "owner"); @@ -75,7 +87,7 @@ BOOST_AUTO_TEST_CASE(test_example) BOOST_AUTO_TEST_CASE(test_example_stream) { - std::ifstream ifs("toml/tests/example.toml", std::ios::binary); + std::ifstream ifs(testinput("example.toml"), std::ios::binary); const auto data = toml::parse(ifs); BOOST_TEST(toml::find(data, "title") == "TOML Example"); @@ -143,7 +155,7 @@ BOOST_AUTO_TEST_CASE(test_example_stream) BOOST_AUTO_TEST_CASE(test_example_file_pointer) { - FILE * file = fopen("toml/tests/example.toml", "rb"); + FILE * file = fopen(testinput("example.toml").c_str(), "rb"); const auto data = toml::parse(file, "toml/tests/example.toml"); fclose(file); @@ -212,7 +224,7 @@ BOOST_AUTO_TEST_CASE(test_example_file_pointer) BOOST_AUTO_TEST_CASE(test_fruit) { - const auto data = toml::parse("toml/tests/fruit.toml"); + const auto data = toml::parse(testinput("fruit.toml")); const auto blah = toml::find(toml::find(data, "fruit"), "blah"); BOOST_TEST(toml::find(blah.at(0), "name") == "apple"); BOOST_TEST(toml::find(blah.at(1), "name") == "banana"); @@ -230,7 +242,7 @@ BOOST_AUTO_TEST_CASE(test_fruit) BOOST_AUTO_TEST_CASE(test_hard_example) { - const auto data = toml::parse("toml/tests/hard_example.toml"); + const auto data = toml::parse(testinput("hard_example.toml")); const auto the = toml::find(data, "the"); BOOST_TEST(toml::find(the, "test_string") == "You'll hate me after this - #"); @@ -257,7 +269,7 @@ BOOST_AUTO_TEST_CASE(test_hard_example) } BOOST_AUTO_TEST_CASE(test_hard_example_comment) { - const auto data = toml::parse("toml/tests/hard_example.toml"); + const auto data = toml::parse(testinput("hard_example.toml")); const auto the = toml::find(data, "the"); BOOST_TEST(toml::find(the, "test_string") == "You'll hate me after this - #"); @@ -286,7 +298,7 @@ BOOST_AUTO_TEST_CASE(test_hard_example_comment) BOOST_AUTO_TEST_CASE(test_example_preserve_comment) { - const auto data = toml::parse("toml/tests/example.toml"); + const auto data = toml::parse(testinput("example.toml")); BOOST_TEST(toml::find(data, "title") == "TOML Example"); const auto& owner = toml::find(data, "owner"); @@ -368,8 +380,8 @@ BOOST_AUTO_TEST_CASE(test_example_preserve_comment) BOOST_AUTO_TEST_CASE(test_example_preserve_stdmap_stddeque) { - const auto data = toml::parse("toml/tests/example.toml"); + const auto data = toml::parse( + testinput("example.toml")); static_assert(std::is_same::type> @@ -992,38 +1004,36 @@ BOOST_AUTO_TEST_CASE(test_file_ends_without_lf) BOOST_AUTO_TEST_CASE(test_parse_function_compiles) { + const auto c = [](std::string& s) -> const std::string& { return s; }; + /*mutable*/ std::string example = testinput("example.toml"); + // toml::parse(""); - const auto string_literal = toml::parse("toml/tests/example.toml"); + using result_type = decltype(toml::parse("string literal")); BOOST_TEST_MESSAGE("string_literal"); - const char* fname_cstring = "toml/tests/example.toml"; // toml::parse(const char*); - const auto cstring = toml::parse(fname_cstring); + const result_type cstring = toml::parse(example.c_str()); BOOST_TEST_MESSAGE("const char*"); // toml::parse(char*); - std::array fname_char_ptr; - std::strncpy(fname_char_ptr.data(), fname_cstring, 24); - const auto char_ptr = toml::parse(fname_char_ptr.data()); + const result_type char_ptr = toml::parse(&example.front()); BOOST_TEST_MESSAGE("char*"); // toml::parse(const std::string&); - const std::string fname_string("toml/tests/example.toml"); - const auto string = toml::parse(fname_string); - std::string fname_string_mut("toml/tests/example.toml"); + const result_type string = toml::parse(c(example)); // toml::parse(std::string&); - const auto string_mutref = toml::parse(fname_string_mut); + const result_type string_mutref = toml::parse(example); // toml::parse(std::string&&); - const auto string_rref = toml::parse(std::move(fname_string_mut)); + const result_type string_rref = toml::parse(std::move(example)); BOOST_TEST_MESSAGE("strings"); #ifdef TOML11_HAS_STD_FILESYSTEM - const std::filesystem::path fname_path(fname_string.begin(), fname_string.end()); - const auto filesystem_path = toml::parse(fname_path); + const std::filesystem::path fname_path(example.begin(), example.end()); + const result_type filesystem_path = toml::parse(fname_path); BOOST_TEST_MESSAGE("path"); #endif } diff --git a/tests/test_parse_unicode.cpp b/tests/test_parse_unicode.cpp index 3c54cb02..7f07cb43 100644 --- a/tests/test_parse_unicode.cpp +++ b/tests/test_parse_unicode.cpp @@ -7,10 +7,22 @@ #include #include #include +#include + +static auto testinput(const std::string& basename) -> std::string +{ + const auto this_or_that = [](const char *const s, const char *const t) { return s ? s : t; }; + std::string directory = this_or_that(std::getenv("TOMLDIR"), "toml"); + if (!directory.empty() && directory.back() != '/') + { + directory.push_back('/'); + } + return directory.append("tests/").append(basename); +} BOOST_AUTO_TEST_CASE(test_hard_example_unicode) { - const auto data = toml::parse("toml/tests/hard_example_unicode.toml"); + const auto data = toml::parse(testinput("hard_example_unicode.toml")); const auto the = toml::find(data, "the"); BOOST_TEST(toml::get(the.at("test_string")) == diff --git a/tests/test_serialize_file.cpp b/tests/test_serialize_file.cpp index 75733637..305ca0d7 100644 --- a/tests/test_serialize_file.cpp +++ b/tests/test_serialize_file.cpp @@ -5,6 +5,7 @@ #include #endif #include +#include #include #include #include @@ -44,9 +45,20 @@ bool has_comment_inside(const toml::basic_value& v) return true; } +static auto testinput(const std::string& basename) -> std::string +{ + const auto this_or_that = [](const char *const s, const char *const t) { return s ? s : t; }; + std::string directory = this_or_that(std::getenv("TOMLDIR"), "toml"); + if (!directory.empty() && directory.back() != '/') + { + directory.push_back('/'); + } + return directory.append("tests/").append(basename); +} + BOOST_AUTO_TEST_CASE(test_example) { - const auto data = toml::parse("toml/tests/example.toml"); + const auto data = toml::parse(testinput("example.toml")); { std::ofstream ofs("tmp1.toml"); ofs << std::setw(80) << data; @@ -68,7 +80,7 @@ BOOST_AUTO_TEST_CASE(test_example) BOOST_AUTO_TEST_CASE(test_example_map_dq) { const auto data = toml::parse( - "toml/tests/example.toml"); + testinput("example.toml")); { std::ofstream ofs("tmp1_map_dq.toml"); ofs << std::setw(80) << data; @@ -90,7 +102,7 @@ BOOST_AUTO_TEST_CASE(test_example_map_dq) BOOST_AUTO_TEST_CASE(test_example_with_comment) { - const auto data = toml::parse("toml/tests/example.toml"); + const auto data = toml::parse(testinput("example.toml")); { std::ofstream ofs("tmp1_com.toml"); ofs << std::setw(80) << data; @@ -116,7 +128,7 @@ BOOST_AUTO_TEST_CASE(test_example_with_comment) BOOST_AUTO_TEST_CASE(test_example_with_comment_nocomment) { { - const auto data = toml::parse("toml/tests/example.toml"); + const auto data = toml::parse(testinput("example.toml")); { std::ofstream ofs("tmp1_com_nocomment.toml"); ofs << std::setw(80) << toml::nocomment << data; @@ -126,7 +138,7 @@ BOOST_AUTO_TEST_CASE(test_example_with_comment_nocomment) BOOST_TEST(!has_comment_inside(serialized)); } { - const auto data_nocomment = toml::parse("toml/tests/example.toml"); + const auto data_nocomment = toml::parse(testinput("example.toml")); auto serialized = toml::parse("tmp1_com_nocomment.toml"); { auto& owner = toml::find(serialized, "owner"); @@ -145,7 +157,7 @@ BOOST_AUTO_TEST_CASE(test_example_with_comment_nocomment) BOOST_AUTO_TEST_CASE(test_example_with_comment_map_dq) { const auto data = toml::parse( - "toml/tests/example.toml"); + testinput("example.toml")); { std::ofstream ofs("tmp1_com_map_dq.toml"); ofs << std::setw(80) << data; @@ -172,7 +184,7 @@ BOOST_AUTO_TEST_CASE(test_example_with_comment_map_dq) BOOST_AUTO_TEST_CASE(test_example_with_comment_map_dq_nocomment) { { - const auto data = toml::parse("toml/tests/example.toml"); + const auto data = toml::parse(testinput("example.toml")); { std::ofstream ofs("tmp1_com_map_dq_nocomment.toml"); ofs << std::setw(80) << toml::nocomment << data; @@ -181,7 +193,7 @@ BOOST_AUTO_TEST_CASE(test_example_with_comment_map_dq_nocomment) BOOST_TEST(!has_comment_inside(serialized)); } { - const auto data_nocomment = toml::parse("toml/tests/example.toml"); + const auto data_nocomment = toml::parse(testinput("example.toml")); auto serialized = toml::parse("tmp1_com_map_dq_nocomment.toml"); { auto& owner = toml::find(serialized, "owner"); @@ -198,7 +210,7 @@ BOOST_AUTO_TEST_CASE(test_example_with_comment_map_dq_nocomment) BOOST_AUTO_TEST_CASE(test_fruit) { - const auto data = toml::parse("toml/tests/fruit.toml"); + const auto data = toml::parse(testinput("fruit.toml")); { std::ofstream ofs("tmp2.toml"); ofs << std::setw(80) << data; @@ -210,7 +222,7 @@ BOOST_AUTO_TEST_CASE(test_fruit) BOOST_AUTO_TEST_CASE(test_fruit_map_dq) { const auto data = toml::parse( - "toml/tests/fruit.toml"); + testinput("fruit.toml")); { std::ofstream ofs("tmp2.toml"); ofs << std::setw(80) << data; @@ -222,7 +234,7 @@ BOOST_AUTO_TEST_CASE(test_fruit_map_dq) BOOST_AUTO_TEST_CASE(test_fruit_with_comments) { - const auto data = toml::parse("toml/tests/fruit.toml"); + const auto data = toml::parse(testinput("fruit.toml")); { std::ofstream ofs("tmp2_com.toml"); ofs << std::setw(80) << data; @@ -234,7 +246,7 @@ BOOST_AUTO_TEST_CASE(test_fruit_with_comments) BOOST_AUTO_TEST_CASE(test_fruit_with_comments_map_dq) { const auto data = toml::parse( - "toml/tests/fruit.toml"); + testinput("fruit.toml")); { std::ofstream ofs("tmp2_com.toml"); ofs << std::setw(80) << data; @@ -245,7 +257,7 @@ BOOST_AUTO_TEST_CASE(test_fruit_with_comments_map_dq) BOOST_AUTO_TEST_CASE(test_hard_example) { - const auto data = toml::parse("toml/tests/hard_example.toml"); + const auto data = toml::parse(testinput("hard_example.toml")); { std::ofstream ofs("tmp3.toml"); ofs << std::setw(80) << data; @@ -257,7 +269,7 @@ BOOST_AUTO_TEST_CASE(test_hard_example) BOOST_AUTO_TEST_CASE(test_hard_example_map_dq) { const auto data = toml::parse( - "toml/tests/hard_example.toml"); + testinput("hard_example.toml")); { std::ofstream ofs("tmp3.toml"); ofs << std::setw(80) << data; @@ -270,7 +282,7 @@ BOOST_AUTO_TEST_CASE(test_hard_example_map_dq) BOOST_AUTO_TEST_CASE(test_hard_example_with_comment) { const auto data = toml::parse( - "toml/tests/hard_example.toml"); + testinput("hard_example.toml")); { std::ofstream ofs("tmp3_com.toml"); ofs << std::setw(80) << data; From d7c04ed5eeeab8d34484548936133ec5d0e0f611 Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Fri, 16 Sep 2022 12:51:41 +0200 Subject: [PATCH 08/13] Factor redundant test boilerplate out into unit_test.hpp helper --- tests/test_comments.cpp | 6 +----- tests/test_datetime.cpp | 7 ++----- tests/test_error_detection.cpp | 7 ++----- tests/test_expect.cpp | 7 ++----- tests/test_extended_conversions.cpp | 7 ++----- tests/test_find.cpp | 7 +------ tests/test_find_or.cpp | 7 ++----- tests/test_find_or_recursive.cpp | 7 ++----- tests/test_format_error.cpp | 7 ++----- tests/test_get.cpp | 7 ++----- tests/test_get_or.cpp | 7 ++----- tests/test_lex_boolean.cpp | 7 ++----- tests/test_lex_datetime.cpp | 7 ++----- tests/test_lex_floating.cpp | 7 ++----- tests/test_lex_integer.cpp | 7 ++----- tests/test_lex_key_comment.cpp | 7 ++----- tests/test_lex_string.cpp | 7 ++----- tests/test_literals.cpp | 7 ++----- tests/test_parse_array.cpp | 7 ++----- tests/test_parse_boolean.cpp | 7 ++----- tests/test_parse_datetime.cpp | 7 ++----- tests/test_parse_file.cpp | 19 ++----------------- tests/test_parse_floating.cpp | 7 ++----- tests/test_parse_inline_table.cpp | 7 ++----- tests/test_parse_integer.cpp | 7 ++----- tests/test_parse_key.cpp | 7 ++----- tests/test_parse_string.cpp | 7 ++----- tests/test_parse_table.cpp | 7 ++----- tests/test_parse_table_key.cpp | 7 ++----- tests/test_parse_unicode.cpp | 19 ++----------------- tests/test_result.cpp | 7 ++----- tests/test_serialize_file.cpp | 19 ++----------------- tests/test_string.cpp | 7 ++----- tests/test_traits.cpp | 7 ++----- tests/test_utility.cpp | 7 ++----- tests/test_value.cpp | 7 ++----- tests/unit_test.hpp | 23 +++++++++++++++++++++++ 37 files changed, 93 insertions(+), 217 deletions(-) create mode 100644 tests/unit_test.hpp diff --git a/tests/test_comments.cpp b/tests/test_comments.cpp index 23d4a680..a5765dce 100644 --- a/tests/test_comments.cpp +++ b/tests/test_comments.cpp @@ -1,9 +1,5 @@ #define BOOST_TEST_MODULE "test_comments" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" #include diff --git a/tests/test_datetime.cpp b/tests/test_datetime.cpp index 31bdcf7f..1759c341 100644 --- a/tests/test_datetime.cpp +++ b/tests/test_datetime.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_datetime" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include BOOST_AUTO_TEST_CASE(test_local_date) diff --git a/tests/test_error_detection.cpp b/tests/test_error_detection.cpp index fbefcd4b..3364d16b 100644 --- a/tests/test_error_detection.cpp +++ b/tests/test_error_detection.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_error_detection" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include diff --git a/tests/test_expect.cpp b/tests/test_expect.cpp index d376ef1e..131c1cc8 100644 --- a/tests/test_expect.cpp +++ b/tests/test_expect.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_expect" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include diff --git a/tests/test_extended_conversions.cpp b/tests/test_extended_conversions.cpp index e84f0133..28974d72 100644 --- a/tests/test_extended_conversions.cpp +++ b/tests/test_extended_conversions.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_extended_conversions" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include diff --git a/tests/test_find.cpp b/tests/test_find.cpp index cf58017d..682ce87b 100644 --- a/tests/test_find.cpp +++ b/tests/test_find.cpp @@ -1,10 +1,5 @@ #define BOOST_TEST_MODULE "test_find" - -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" #include #include diff --git a/tests/test_find_or.cpp b/tests/test_find_or.cpp index ed36e3a8..8f1eb736 100644 --- a/tests/test_find_or.cpp +++ b/tests/test_find_or.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_find_or" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include diff --git a/tests/test_find_or_recursive.cpp b/tests/test_find_or_recursive.cpp index d31e8b6e..22fcfb58 100644 --- a/tests/test_find_or_recursive.cpp +++ b/tests/test_find_or_recursive.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_find_or_recursive" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include diff --git a/tests/test_format_error.cpp b/tests/test_format_error.cpp index 1ae8db65..d347d857 100644 --- a/tests/test_format_error.cpp +++ b/tests/test_format_error.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_format_error" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include diff --git a/tests/test_get.cpp b/tests/test_get.cpp index 090eb077..eaae50ee 100644 --- a/tests/test_get.cpp +++ b/tests/test_get.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_get" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include diff --git a/tests/test_get_or.cpp b/tests/test_get_or.cpp index 7aad2733..68c4884d 100644 --- a/tests/test_get_or.cpp +++ b/tests/test_get_or.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_get_or" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include diff --git a/tests/test_lex_boolean.cpp b/tests/test_lex_boolean.cpp index 933ab9d7..b9f830c9 100644 --- a/tests/test_lex_boolean.cpp +++ b/tests/test_lex_boolean.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_lex_boolean" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_datetime.cpp b/tests/test_lex_datetime.cpp index 25e24bf9..75e1c051 100644 --- a/tests/test_lex_datetime.cpp +++ b/tests/test_lex_datetime.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_lex_datetime" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_floating.cpp b/tests/test_lex_floating.cpp index e14bdb32..16548742 100644 --- a/tests/test_lex_floating.cpp +++ b/tests/test_lex_floating.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_lex_floating" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_integer.cpp b/tests/test_lex_integer.cpp index 1e8a31bc..c3171c70 100644 --- a/tests/test_lex_integer.cpp +++ b/tests/test_lex_integer.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_lex_integer" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_key_comment.cpp b/tests/test_lex_key_comment.cpp index 1aad90b6..2315d866 100644 --- a/tests/test_lex_key_comment.cpp +++ b/tests/test_lex_key_comment.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "lex_key_comment_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_lex_aux.hpp" diff --git a/tests/test_lex_string.cpp b/tests/test_lex_string.cpp index 880ee923..fb42ee37 100644 --- a/tests/test_lex_string.cpp +++ b/tests/test_lex_string.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_lex_string" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_lex_aux.hpp" diff --git a/tests/test_literals.cpp b/tests/test_literals.cpp index 9d8ebeaf..e5508458 100644 --- a/tests/test_literals.cpp +++ b/tests/test_literals.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_literals" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include diff --git a/tests/test_parse_array.cpp b/tests/test_parse_array.cpp index ee6a9ae1..9943a98b 100644 --- a/tests/test_parse_array.cpp +++ b/tests/test_parse_array.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "parse_array_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_boolean.cpp b/tests/test_parse_boolean.cpp index 92c0042f..f6cb26f2 100644 --- a/tests/test_parse_boolean.cpp +++ b/tests/test_parse_boolean.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_parse_boolean" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_datetime.cpp b/tests/test_parse_datetime.cpp index 5e86108b..e4b2d23a 100644 --- a/tests/test_parse_datetime.cpp +++ b/tests/test_parse_datetime.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "parse_datetime_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_file.cpp b/tests/test_parse_file.cpp index 144168ba..0bce6b71 100644 --- a/tests/test_parse_file.cpp +++ b/tests/test_parse_file.cpp @@ -1,26 +1,11 @@ #define BOOST_TEST_MODULE "test_parse_file" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include #include #include -#include - -static auto testinput(const std::string& basename) -> std::string -{ - const auto this_or_that = [](const char *const s, const char *const t) { return s ? s : t; }; - std::string directory = this_or_that(std::getenv("TOMLDIR"), "toml"); - if (!directory.empty() && directory.back() != '/') - { - directory.push_back('/'); - } - return directory.append("tests/").append(basename); -} BOOST_AUTO_TEST_CASE(test_example) { diff --git a/tests/test_parse_floating.cpp b/tests/test_parse_floating.cpp index 3fa2d03f..dbe012dc 100644 --- a/tests/test_parse_floating.cpp +++ b/tests/test_parse_floating.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "parse_floating_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_inline_table.cpp b/tests/test_parse_inline_table.cpp index 9b3460a5..dad32572 100644 --- a/tests/test_parse_inline_table.cpp +++ b/tests/test_parse_inline_table.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "parse_inline_table_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_integer.cpp b/tests/test_parse_integer.cpp index ea203afc..2d8e97f1 100644 --- a/tests/test_parse_integer.cpp +++ b/tests/test_parse_integer.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "parse_integer_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_key.cpp b/tests/test_parse_key.cpp index f7448a61..24e8f02b 100644 --- a/tests/test_parse_key.cpp +++ b/tests/test_parse_key.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "parse_key_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_string.cpp b/tests/test_parse_string.cpp index 48374600..63e734ec 100644 --- a/tests/test_parse_string.cpp +++ b/tests/test_parse_string.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "parse_string_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_table.cpp b/tests/test_parse_table.cpp index b6c9dd03..f5b88563 100644 --- a/tests/test_parse_table.cpp +++ b/tests/test_parse_table.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "parse_table_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_table_key.cpp b/tests/test_parse_table_key.cpp index 1a7466bc..18ba2ea9 100644 --- a/tests/test_parse_table_key.cpp +++ b/tests/test_parse_table_key.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "parse_table_key_test" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include "test_parse_aux.hpp" diff --git a/tests/test_parse_unicode.cpp b/tests/test_parse_unicode.cpp index 7f07cb43..0f891e97 100644 --- a/tests/test_parse_unicode.cpp +++ b/tests/test_parse_unicode.cpp @@ -1,24 +1,9 @@ #define BOOST_TEST_MODULE "test_parse_unicode" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include -#include - -static auto testinput(const std::string& basename) -> std::string -{ - const auto this_or_that = [](const char *const s, const char *const t) { return s ? s : t; }; - std::string directory = this_or_that(std::getenv("TOMLDIR"), "toml"); - if (!directory.empty() && directory.back() != '/') - { - directory.push_back('/'); - } - return directory.append("tests/").append(basename); -} BOOST_AUTO_TEST_CASE(test_hard_example_unicode) { diff --git a/tests/test_result.cpp b/tests/test_result.cpp index 9a700417..47332d2f 100644 --- a/tests/test_result.cpp +++ b/tests/test_result.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_result" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include diff --git a/tests/test_serialize_file.cpp b/tests/test_serialize_file.cpp index 305ca0d7..d26ce699 100644 --- a/tests/test_serialize_file.cpp +++ b/tests/test_serialize_file.cpp @@ -1,11 +1,7 @@ #define BOOST_TEST_MODULE "test_serialize_file" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include -#include #include #include #include @@ -45,17 +41,6 @@ bool has_comment_inside(const toml::basic_value& v) return true; } -static auto testinput(const std::string& basename) -> std::string -{ - const auto this_or_that = [](const char *const s, const char *const t) { return s ? s : t; }; - std::string directory = this_or_that(std::getenv("TOMLDIR"), "toml"); - if (!directory.empty() && directory.back() != '/') - { - directory.push_back('/'); - } - return directory.append("tests/").append(basename); -} - BOOST_AUTO_TEST_CASE(test_example) { const auto data = toml::parse(testinput("example.toml")); diff --git a/tests/test_string.cpp b/tests/test_string.cpp index a9fec2a0..2ed2c173 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_string" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include BOOST_AUTO_TEST_CASE(test_basic_string) diff --git a/tests/test_traits.cpp b/tests/test_traits.cpp index f6f5bc1e..ce73d3d0 100644 --- a/tests/test_traits.cpp +++ b/tests/test_traits.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_traits" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include diff --git a/tests/test_utility.cpp b/tests/test_utility.cpp index 05981c25..37302cb4 100644 --- a/tests/test_utility.cpp +++ b/tests/test_utility.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_acceptor" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include diff --git a/tests/test_value.cpp b/tests/test_value.cpp index b25731e2..29781ad1 100644 --- a/tests/test_value.cpp +++ b/tests/test_value.cpp @@ -1,9 +1,6 @@ #define BOOST_TEST_MODULE "test_value" -#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST -#include -#else -#include -#endif +#include "unit_test.hpp" + #include #include #include diff --git a/tests/unit_test.hpp b/tests/unit_test.hpp new file mode 100644 index 00000000..5999f57c --- /dev/null +++ b/tests/unit_test.hpp @@ -0,0 +1,23 @@ +#ifndef BOOST_TEST_MODULE +# error "Please #define BOOST_TEST_MODULE before you #include " +#endif + +#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST +# include +#else +# include +#endif + +#include +#include + +static inline auto testinput(const std::string& basename) -> std::string +{ + const auto this_or_that = [](const char *const s, const char *const t) { return s ? s : t; }; + std::string directory = this_or_that(std::getenv("TOMLDIR"), "toml"); + if (!directory.empty() && directory.back() != '/') + { + directory.push_back('/'); + } + return directory.append("tests/").append(basename); +} From 81c5ba9082df1b20169b193ef25774e4c2880031 Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Fri, 16 Sep 2022 13:04:29 +0200 Subject: [PATCH 09/13] Define BOOST_TEST_MODULE in CMake This removes one #define from each unit test file and ensures consistency between file and module names. This consistency, was not strictly maintained before. I hope that any discrepancies were unintentional and that a 1:1 mapping is actually what is desired. Since the definition is now done at one single place, it would be easy to apply transformations like removing the 'test_' prefix or replacing '_' with '-' if this should be desired. --- tests/CMakeLists.txt | 1 + tests/test_comments.cpp | 1 - tests/test_datetime.cpp | 1 - tests/test_error_detection.cpp | 1 - tests/test_expect.cpp | 1 - tests/test_extended_conversions.cpp | 1 - tests/test_find.cpp | 1 - tests/test_find_or.cpp | 1 - tests/test_find_or_recursive.cpp | 1 - tests/test_format_error.cpp | 1 - tests/test_get.cpp | 1 - tests/test_get_or.cpp | 1 - tests/test_lex_boolean.cpp | 1 - tests/test_lex_datetime.cpp | 1 - tests/test_lex_floating.cpp | 1 - tests/test_lex_integer.cpp | 1 - tests/test_lex_key_comment.cpp | 1 - tests/test_lex_string.cpp | 1 - tests/test_literals.cpp | 1 - tests/test_parse_array.cpp | 1 - tests/test_parse_boolean.cpp | 1 - tests/test_parse_datetime.cpp | 1 - tests/test_parse_file.cpp | 1 - tests/test_parse_floating.cpp | 1 - tests/test_parse_inline_table.cpp | 1 - tests/test_parse_integer.cpp | 1 - tests/test_parse_key.cpp | 1 - tests/test_parse_string.cpp | 1 - tests/test_parse_table.cpp | 1 - tests/test_parse_table_key.cpp | 1 - tests/test_parse_unicode.cpp | 1 - tests/test_result.cpp | 1 - tests/test_serialize_file.cpp | 1 - tests/test_string.cpp | 1 - tests/test_traits.cpp | 1 - tests/test_utility.cpp | 1 - tests/test_value.cpp | 1 - 37 files changed, 1 insertion(+), 36 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 11e449e1..332eaa80 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -248,6 +248,7 @@ foreach(TEST_NAME ${TEST_NAMES}) add_executable(${TEST_NAME} ${TEST_NAME}.cpp) target_link_libraries(${TEST_NAME} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} toml11::toml11) target_include_directories(${TEST_NAME} SYSTEM PRIVATE ${Boost_INCLUDE_DIRS}) + target_compile_definitions(${TEST_NAME} PRIVATE "BOOST_TEST_MODULE=\"${TEST_NAME}\"") # to compile tests with ... if(TOML11_REQUIRE_FILESYSTEM_LIBRARY) diff --git a/tests/test_comments.cpp b/tests/test_comments.cpp index a5765dce..751e2406 100644 --- a/tests/test_comments.cpp +++ b/tests/test_comments.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_comments" #include "unit_test.hpp" #include diff --git a/tests/test_datetime.cpp b/tests/test_datetime.cpp index 1759c341..df0ccb96 100644 --- a/tests/test_datetime.cpp +++ b/tests/test_datetime.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_datetime" #include "unit_test.hpp" #include diff --git a/tests/test_error_detection.cpp b/tests/test_error_detection.cpp index 3364d16b..9fba8a71 100644 --- a/tests/test_error_detection.cpp +++ b/tests/test_error_detection.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_error_detection" #include "unit_test.hpp" #include diff --git a/tests/test_expect.cpp b/tests/test_expect.cpp index 131c1cc8..bb2646b5 100644 --- a/tests/test_expect.cpp +++ b/tests/test_expect.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_expect" #include "unit_test.hpp" #include diff --git a/tests/test_extended_conversions.cpp b/tests/test_extended_conversions.cpp index 28974d72..3e2c94a2 100644 --- a/tests/test_extended_conversions.cpp +++ b/tests/test_extended_conversions.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_extended_conversions" #include "unit_test.hpp" #include diff --git a/tests/test_find.cpp b/tests/test_find.cpp index 682ce87b..54ab3c98 100644 --- a/tests/test_find.cpp +++ b/tests/test_find.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_find" #include "unit_test.hpp" #include diff --git a/tests/test_find_or.cpp b/tests/test_find_or.cpp index 8f1eb736..fc1a469f 100644 --- a/tests/test_find_or.cpp +++ b/tests/test_find_or.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_find_or" #include "unit_test.hpp" #include diff --git a/tests/test_find_or_recursive.cpp b/tests/test_find_or_recursive.cpp index 22fcfb58..20b595b2 100644 --- a/tests/test_find_or_recursive.cpp +++ b/tests/test_find_or_recursive.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_find_or_recursive" #include "unit_test.hpp" #include diff --git a/tests/test_format_error.cpp b/tests/test_format_error.cpp index d347d857..f539b470 100644 --- a/tests/test_format_error.cpp +++ b/tests/test_format_error.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_format_error" #include "unit_test.hpp" #include diff --git a/tests/test_get.cpp b/tests/test_get.cpp index eaae50ee..e2eb3ab2 100644 --- a/tests/test_get.cpp +++ b/tests/test_get.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_get" #include "unit_test.hpp" #include diff --git a/tests/test_get_or.cpp b/tests/test_get_or.cpp index 68c4884d..878ad1c9 100644 --- a/tests/test_get_or.cpp +++ b/tests/test_get_or.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_get_or" #include "unit_test.hpp" #include diff --git a/tests/test_lex_boolean.cpp b/tests/test_lex_boolean.cpp index b9f830c9..21631785 100644 --- a/tests/test_lex_boolean.cpp +++ b/tests/test_lex_boolean.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_lex_boolean" #include "unit_test.hpp" #include diff --git a/tests/test_lex_datetime.cpp b/tests/test_lex_datetime.cpp index 75e1c051..e40f1244 100644 --- a/tests/test_lex_datetime.cpp +++ b/tests/test_lex_datetime.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_lex_datetime" #include "unit_test.hpp" #include diff --git a/tests/test_lex_floating.cpp b/tests/test_lex_floating.cpp index 16548742..0208b221 100644 --- a/tests/test_lex_floating.cpp +++ b/tests/test_lex_floating.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_lex_floating" #include "unit_test.hpp" #include diff --git a/tests/test_lex_integer.cpp b/tests/test_lex_integer.cpp index c3171c70..7220a3e5 100644 --- a/tests/test_lex_integer.cpp +++ b/tests/test_lex_integer.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_lex_integer" #include "unit_test.hpp" #include diff --git a/tests/test_lex_key_comment.cpp b/tests/test_lex_key_comment.cpp index 2315d866..83c88b3b 100644 --- a/tests/test_lex_key_comment.cpp +++ b/tests/test_lex_key_comment.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "lex_key_comment_test" #include "unit_test.hpp" #include diff --git a/tests/test_lex_string.cpp b/tests/test_lex_string.cpp index fb42ee37..02138f21 100644 --- a/tests/test_lex_string.cpp +++ b/tests/test_lex_string.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_lex_string" #include "unit_test.hpp" #include diff --git a/tests/test_literals.cpp b/tests/test_literals.cpp index e5508458..61869932 100644 --- a/tests/test_literals.cpp +++ b/tests/test_literals.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_literals" #include "unit_test.hpp" #include diff --git a/tests/test_parse_array.cpp b/tests/test_parse_array.cpp index 9943a98b..a3e2886b 100644 --- a/tests/test_parse_array.cpp +++ b/tests/test_parse_array.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "parse_array_test" #include "unit_test.hpp" #include diff --git a/tests/test_parse_boolean.cpp b/tests/test_parse_boolean.cpp index f6cb26f2..f0b87b16 100644 --- a/tests/test_parse_boolean.cpp +++ b/tests/test_parse_boolean.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_parse_boolean" #include "unit_test.hpp" #include diff --git a/tests/test_parse_datetime.cpp b/tests/test_parse_datetime.cpp index e4b2d23a..dc7e44c7 100644 --- a/tests/test_parse_datetime.cpp +++ b/tests/test_parse_datetime.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "parse_datetime_test" #include "unit_test.hpp" #include diff --git a/tests/test_parse_file.cpp b/tests/test_parse_file.cpp index 0bce6b71..a3559de3 100644 --- a/tests/test_parse_file.cpp +++ b/tests/test_parse_file.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_parse_file" #include "unit_test.hpp" #include diff --git a/tests/test_parse_floating.cpp b/tests/test_parse_floating.cpp index dbe012dc..c3efb02d 100644 --- a/tests/test_parse_floating.cpp +++ b/tests/test_parse_floating.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "parse_floating_test" #include "unit_test.hpp" #include diff --git a/tests/test_parse_inline_table.cpp b/tests/test_parse_inline_table.cpp index dad32572..cde30be9 100644 --- a/tests/test_parse_inline_table.cpp +++ b/tests/test_parse_inline_table.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "parse_inline_table_test" #include "unit_test.hpp" #include diff --git a/tests/test_parse_integer.cpp b/tests/test_parse_integer.cpp index 2d8e97f1..bc3951b4 100644 --- a/tests/test_parse_integer.cpp +++ b/tests/test_parse_integer.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "parse_integer_test" #include "unit_test.hpp" #include diff --git a/tests/test_parse_key.cpp b/tests/test_parse_key.cpp index 24e8f02b..14f18472 100644 --- a/tests/test_parse_key.cpp +++ b/tests/test_parse_key.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "parse_key_test" #include "unit_test.hpp" #include diff --git a/tests/test_parse_string.cpp b/tests/test_parse_string.cpp index 63e734ec..0865b95d 100644 --- a/tests/test_parse_string.cpp +++ b/tests/test_parse_string.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "parse_string_test" #include "unit_test.hpp" #include diff --git a/tests/test_parse_table.cpp b/tests/test_parse_table.cpp index f5b88563..bec44911 100644 --- a/tests/test_parse_table.cpp +++ b/tests/test_parse_table.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "parse_table_test" #include "unit_test.hpp" #include diff --git a/tests/test_parse_table_key.cpp b/tests/test_parse_table_key.cpp index 18ba2ea9..8a526abc 100644 --- a/tests/test_parse_table_key.cpp +++ b/tests/test_parse_table_key.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "parse_table_key_test" #include "unit_test.hpp" #include diff --git a/tests/test_parse_unicode.cpp b/tests/test_parse_unicode.cpp index 0f891e97..e33713b7 100644 --- a/tests/test_parse_unicode.cpp +++ b/tests/test_parse_unicode.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_parse_unicode" #include "unit_test.hpp" #include diff --git a/tests/test_result.cpp b/tests/test_result.cpp index 47332d2f..2004c537 100644 --- a/tests/test_result.cpp +++ b/tests/test_result.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_result" #include "unit_test.hpp" #include diff --git a/tests/test_serialize_file.cpp b/tests/test_serialize_file.cpp index d26ce699..87d0cda0 100644 --- a/tests/test_serialize_file.cpp +++ b/tests/test_serialize_file.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_serialize_file" #include "unit_test.hpp" #include diff --git a/tests/test_string.cpp b/tests/test_string.cpp index 2ed2c173..efb1dd47 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_string" #include "unit_test.hpp" #include diff --git a/tests/test_traits.cpp b/tests/test_traits.cpp index ce73d3d0..149252d1 100644 --- a/tests/test_traits.cpp +++ b/tests/test_traits.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_traits" #include "unit_test.hpp" #include diff --git a/tests/test_utility.cpp b/tests/test_utility.cpp index 37302cb4..6e58430a 100644 --- a/tests/test_utility.cpp +++ b/tests/test_utility.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_acceptor" #include "unit_test.hpp" #include diff --git a/tests/test_value.cpp b/tests/test_value.cpp index 29781ad1..fca014e2 100644 --- a/tests/test_value.cpp +++ b/tests/test_value.cpp @@ -1,4 +1,3 @@ -#define BOOST_TEST_MODULE "test_value" #include "unit_test.hpp" #include From 10fd14f8b91b68153f677c58cddfbd6734afc50e Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Fri, 16 Sep 2022 13:16:01 +0200 Subject: [PATCH 10/13] Consistent unit test header inclusion order This patch consistently changes the inclusion order for unit test files to the following: 1. The header of the unit under test (using <> includes). 2. The unit_test.hpp header (using "" includes). 3. Any additional auxiliary test headers (using "" includes and sorted alphabetically). 4. Additional system headers needed for the test (using <> includes and sorted alphabetically). 5. Conditionally included system headers (using <> includes). Putting the unit under test's header at the very beginning has the advantage of also testing that the header is self-contained. It also makes it very quick to tell what unit is tested in this file. --- tests/check.cpp | 3 ++- tests/check_serialization.cpp | 5 +++-- tests/check_toml_test.cpp | 5 +++-- tests/test_comments.cpp | 4 ++-- tests/test_datetime.cpp | 4 ++-- tests/test_error_detection.cpp | 5 +++-- tests/test_expect.cpp | 9 +++++---- tests/test_extended_conversions.cpp | 3 ++- tests/test_find.cpp | 13 +++++++------ tests/test_find_or.cpp | 12 +++++++----- tests/test_find_or_recursive.cpp | 12 +++++++----- tests/test_format_error.cpp | 3 ++- tests/test_get.cpp | 12 +++++++----- tests/test_get_or.cpp | 12 +++++++----- tests/test_lex_boolean.cpp | 4 ++-- tests/test_lex_datetime.cpp | 4 ++-- tests/test_lex_floating.cpp | 5 +++-- tests/test_lex_integer.cpp | 4 ++-- tests/test_lex_key_comment.cpp | 4 ++-- tests/test_lex_string.cpp | 4 ++-- tests/test_literals.cpp | 3 ++- tests/test_parse_array.cpp | 4 ++-- tests/test_parse_boolean.cpp | 4 ++-- tests/test_parse_datetime.cpp | 4 ++-- tests/test_parse_file.cpp | 7 ++++--- tests/test_parse_floating.cpp | 5 +++-- tests/test_parse_inline_table.cpp | 4 ++-- tests/test_parse_integer.cpp | 4 ++-- tests/test_parse_key.cpp | 4 ++-- tests/test_parse_string.cpp | 4 ++-- tests/test_parse_table.cpp | 6 +++--- tests/test_parse_table_key.cpp | 4 ++-- tests/test_parse_unicode.cpp | 5 +++-- tests/test_result.cpp | 3 ++- tests/test_serialize_file.cpp | 7 ++++--- tests/test_string.cpp | 4 ++-- tests/test_traits.cpp | 12 ++++++------ tests/test_utility.cpp | 5 +++-- tests/test_value.cpp | 3 ++- 39 files changed, 122 insertions(+), 97 deletions(-) diff --git a/tests/check.cpp b/tests/check.cpp index a271f3f3..bf67775c 100644 --- a/tests/check.cpp +++ b/tests/check.cpp @@ -1,4 +1,5 @@ -#include "toml.hpp" +#include + #include #include diff --git a/tests/check_serialization.cpp b/tests/check_serialization.cpp index 6f601030..e97d51b1 100644 --- a/tests/check_serialization.cpp +++ b/tests/check_serialization.cpp @@ -1,6 +1,7 @@ -#include "toml.hpp" -#include +#include + #include +#include int main(int argc, char **argv) { diff --git a/tests/check_toml_test.cpp b/tests/check_toml_test.cpp index 9c57572a..51404c9e 100644 --- a/tests/check_toml_test.cpp +++ b/tests/check_toml_test.cpp @@ -1,6 +1,7 @@ -#include "toml.hpp" -#include +#include + #include +#include struct json_serializer { diff --git a/tests/test_comments.cpp b/tests/test_comments.cpp index 751e2406..2d2b6a67 100644 --- a/tests/test_comments.cpp +++ b/tests/test_comments.cpp @@ -1,7 +1,7 @@ -#include "unit_test.hpp" - #include +#include "unit_test.hpp" + BOOST_AUTO_TEST_CASE(test_comment_before) { { diff --git a/tests/test_datetime.cpp b/tests/test_datetime.cpp index df0ccb96..bbbcc428 100644 --- a/tests/test_datetime.cpp +++ b/tests/test_datetime.cpp @@ -1,7 +1,7 @@ -#include "unit_test.hpp" - #include +#include "unit_test.hpp" + BOOST_AUTO_TEST_CASE(test_local_date) { const toml::local_date date(2018, toml::month_t::Jan, 1); diff --git a/tests/test_error_detection.cpp b/tests/test_error_detection.cpp index 9fba8a71..5e39ef6c 100644 --- a/tests/test_error_detection.cpp +++ b/tests/test_error_detection.cpp @@ -1,8 +1,9 @@ +#include + #include "unit_test.hpp" -#include -#include #include +#include BOOST_AUTO_TEST_CASE(test_detect_empty_key) { diff --git a/tests/test_expect.cpp b/tests/test_expect.cpp index bb2646b5..308d4fba 100644 --- a/tests/test_expect.cpp +++ b/tests/test_expect.cpp @@ -1,11 +1,12 @@ +#include + #include "unit_test.hpp" -#include +#include +#include +#include #include #include -#include -#include -#include BOOST_AUTO_TEST_CASE(test_expect) { diff --git a/tests/test_extended_conversions.cpp b/tests/test_extended_conversions.cpp index 3e2c94a2..1b21264e 100644 --- a/tests/test_extended_conversions.cpp +++ b/tests/test_extended_conversions.cpp @@ -1,6 +1,7 @@ +#include + #include "unit_test.hpp" -#include #include #include diff --git a/tests/test_find.cpp b/tests/test_find.cpp index 54ab3c98..944b8e82 100644 --- a/tests/test_find.cpp +++ b/tests/test_find.cpp @@ -1,15 +1,17 @@ +#include + #include "unit_test.hpp" -#include +#include +#include +#include #include +#include #include -#include -#include -#include + #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L #include #endif -#include using test_value_types = std::tuple< toml::basic_value, @@ -823,4 +825,3 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_get_toml_offset_datetime, value_type, test_va BOOST_TEST(tm.tm_sec == 0); } } - diff --git a/tests/test_find_or.cpp b/tests/test_find_or.cpp index fc1a469f..8348f2f1 100644 --- a/tests/test_find_or.cpp +++ b/tests/test_find_or.cpp @@ -1,12 +1,14 @@ +#include + #include "unit_test.hpp" -#include -#include -#include -#include -#include #include +#include +#include +#include #include +#include + #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L #include #endif diff --git a/tests/test_find_or_recursive.cpp b/tests/test_find_or_recursive.cpp index 20b595b2..41e8a055 100644 --- a/tests/test_find_or_recursive.cpp +++ b/tests/test_find_or_recursive.cpp @@ -1,12 +1,14 @@ +#include + #include "unit_test.hpp" -#include -#include -#include -#include -#include #include +#include +#include +#include #include +#include + #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L #include #endif diff --git a/tests/test_format_error.cpp b/tests/test_format_error.cpp index f539b470..c00f8455 100644 --- a/tests/test_format_error.cpp +++ b/tests/test_format_error.cpp @@ -1,6 +1,7 @@ +#include + #include "unit_test.hpp" -#include #include // to check it successfully compiles. it does not check the formatted string. diff --git a/tests/test_get.cpp b/tests/test_get.cpp index e2eb3ab2..0db4abff 100644 --- a/tests/test_get.cpp +++ b/tests/test_get.cpp @@ -1,12 +1,14 @@ +#include + #include "unit_test.hpp" -#include -#include -#include -#include -#include #include +#include +#include +#include #include +#include + #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L #include #endif diff --git a/tests/test_get_or.cpp b/tests/test_get_or.cpp index 878ad1c9..9ecb36e9 100644 --- a/tests/test_get_or.cpp +++ b/tests/test_get_or.cpp @@ -1,12 +1,14 @@ +#include + #include "unit_test.hpp" -#include -#include -#include -#include -#include #include +#include +#include +#include #include +#include + #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L #include #endif diff --git a/tests/test_lex_boolean.cpp b/tests/test_lex_boolean.cpp index 21631785..deecffef 100644 --- a/tests/test_lex_boolean.cpp +++ b/tests/test_lex_boolean.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_lex_aux.hpp" using namespace toml; diff --git a/tests/test_lex_datetime.cpp b/tests/test_lex_datetime.cpp index e40f1244..8e33d384 100644 --- a/tests/test_lex_datetime.cpp +++ b/tests/test_lex_datetime.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_lex_aux.hpp" using namespace toml; diff --git a/tests/test_lex_floating.cpp b/tests/test_lex_floating.cpp index 0208b221..8c9cd200 100644 --- a/tests/test_lex_floating.cpp +++ b/tests/test_lex_floating.cpp @@ -1,8 +1,9 @@ +#include + #include "unit_test.hpp" +#include "test_lex_aux.hpp" -#include #include -#include "test_lex_aux.hpp" using namespace toml; using namespace detail; diff --git a/tests/test_lex_integer.cpp b/tests/test_lex_integer.cpp index 7220a3e5..50b9178f 100644 --- a/tests/test_lex_integer.cpp +++ b/tests/test_lex_integer.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_lex_aux.hpp" using namespace toml; diff --git a/tests/test_lex_key_comment.cpp b/tests/test_lex_key_comment.cpp index 83c88b3b..da123a42 100644 --- a/tests/test_lex_key_comment.cpp +++ b/tests/test_lex_key_comment.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_lex_aux.hpp" using namespace toml; diff --git a/tests/test_lex_string.cpp b/tests/test_lex_string.cpp index 02138f21..f8f24243 100644 --- a/tests/test_lex_string.cpp +++ b/tests/test_lex_string.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_lex_aux.hpp" using namespace toml; diff --git a/tests/test_literals.cpp b/tests/test_literals.cpp index 61869932..de81f39b 100644 --- a/tests/test_literals.cpp +++ b/tests/test_literals.cpp @@ -1,6 +1,7 @@ +#include + #include "unit_test.hpp" -#include #include BOOST_AUTO_TEST_CASE(test_file_as_literal) diff --git a/tests/test_parse_array.cpp b/tests/test_parse_array.cpp index a3e2886b..053d4c56 100644 --- a/tests/test_parse_array.cpp +++ b/tests/test_parse_array.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_parse_aux.hpp" using namespace toml; diff --git a/tests/test_parse_boolean.cpp b/tests/test_parse_boolean.cpp index f0b87b16..5ed128c2 100644 --- a/tests/test_parse_boolean.cpp +++ b/tests/test_parse_boolean.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_parse_aux.hpp" using namespace toml; diff --git a/tests/test_parse_datetime.cpp b/tests/test_parse_datetime.cpp index dc7e44c7..46bd78c9 100644 --- a/tests/test_parse_datetime.cpp +++ b/tests/test_parse_datetime.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_parse_aux.hpp" using namespace toml; diff --git a/tests/test_parse_file.cpp b/tests/test_parse_file.cpp index a3559de3..cea5387a 100644 --- a/tests/test_parse_file.cpp +++ b/tests/test_parse_file.cpp @@ -1,10 +1,11 @@ +#include + #include "unit_test.hpp" -#include -#include +#include #include +#include #include -#include BOOST_AUTO_TEST_CASE(test_example) { diff --git a/tests/test_parse_floating.cpp b/tests/test_parse_floating.cpp index c3efb02d..5c05baba 100644 --- a/tests/test_parse_floating.cpp +++ b/tests/test_parse_floating.cpp @@ -1,8 +1,9 @@ +#include + #include "unit_test.hpp" +#include "test_parse_aux.hpp" -#include #include -#include "test_parse_aux.hpp" using namespace toml; using namespace detail; diff --git a/tests/test_parse_inline_table.cpp b/tests/test_parse_inline_table.cpp index cde30be9..9217a230 100644 --- a/tests/test_parse_inline_table.cpp +++ b/tests/test_parse_inline_table.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_parse_aux.hpp" using namespace toml; diff --git a/tests/test_parse_integer.cpp b/tests/test_parse_integer.cpp index bc3951b4..423961fd 100644 --- a/tests/test_parse_integer.cpp +++ b/tests/test_parse_integer.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_parse_aux.hpp" using namespace toml; diff --git a/tests/test_parse_key.cpp b/tests/test_parse_key.cpp index 14f18472..3af0eaa3 100644 --- a/tests/test_parse_key.cpp +++ b/tests/test_parse_key.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_parse_aux.hpp" using namespace toml; diff --git a/tests/test_parse_string.cpp b/tests/test_parse_string.cpp index 0865b95d..bf381e35 100644 --- a/tests/test_parse_string.cpp +++ b/tests/test_parse_string.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_parse_aux.hpp" using namespace toml; diff --git a/tests/test_parse_table.cpp b/tests/test_parse_table.cpp index bec44911..0fd54c7f 100644 --- a/tests/test_parse_table.cpp +++ b/tests/test_parse_table.cpp @@ -1,7 +1,7 @@ -#include "unit_test.hpp" - -#include #include +#include + +#include "unit_test.hpp" #include "test_parse_aux.hpp" using namespace toml; diff --git a/tests/test_parse_table_key.cpp b/tests/test_parse_table_key.cpp index 8a526abc..a921c969 100644 --- a/tests/test_parse_table_key.cpp +++ b/tests/test_parse_table_key.cpp @@ -1,6 +1,6 @@ -#include "unit_test.hpp" - #include + +#include "unit_test.hpp" #include "test_parse_aux.hpp" using namespace toml; diff --git a/tests/test_parse_unicode.cpp b/tests/test_parse_unicode.cpp index e33713b7..8bc42539 100644 --- a/tests/test_parse_unicode.cpp +++ b/tests/test_parse_unicode.cpp @@ -1,8 +1,9 @@ +#include + #include "unit_test.hpp" -#include -#include #include +#include BOOST_AUTO_TEST_CASE(test_hard_example_unicode) { diff --git a/tests/test_result.cpp b/tests/test_result.cpp index 2004c537..ff43fe03 100644 --- a/tests/test_result.cpp +++ b/tests/test_result.cpp @@ -1,7 +1,8 @@ +#include + #include "unit_test.hpp" #include -#include BOOST_AUTO_TEST_CASE(test_construct) { diff --git a/tests/test_serialize_file.cpp b/tests/test_serialize_file.cpp index 87d0cda0..b6b8acb7 100644 --- a/tests/test_serialize_file.cpp +++ b/tests/test_serialize_file.cpp @@ -1,10 +1,11 @@ +#include + #include "unit_test.hpp" -#include #include -#include -#include #include +#include +#include #include template +#include "unit_test.hpp" + BOOST_AUTO_TEST_CASE(test_basic_string) { { diff --git a/tests/test_traits.cpp b/tests/test_traits.cpp index 149252d1..4d5e3e22 100644 --- a/tests/test_traits.cpp +++ b/tests/test_traits.cpp @@ -1,16 +1,16 @@ -#include "unit_test.hpp" - #include -#include -#include -#include +#include "unit_test.hpp" + #include +#include +#include +#include #include #include +#include #include #include -#include struct dummy_type{}; diff --git a/tests/test_utility.cpp b/tests/test_utility.cpp index 6e58430a..c67e3b84 100644 --- a/tests/test_utility.cpp +++ b/tests/test_utility.cpp @@ -1,8 +1,9 @@ +#include + #include "unit_test.hpp" -#include -#include #include +#include BOOST_AUTO_TEST_CASE(test_try_reserve) { diff --git a/tests/test_value.cpp b/tests/test_value.cpp index fca014e2..296d3439 100644 --- a/tests/test_value.cpp +++ b/tests/test_value.cpp @@ -1,6 +1,7 @@ +#include + #include "unit_test.hpp" -#include #include #include From e86d7c3cd35af4ba15a341e5fa663a1b6997475c Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Fri, 16 Sep 2022 13:16:10 +0200 Subject: [PATCH 11/13] Remove excess blank lines at end of file --- tests/test_find_or_recursive.cpp | 5 ----- tests/test_get.cpp | 1 - tests/test_result.cpp | 2 -- tests/test_string.cpp | 1 - 4 files changed, 9 deletions(-) diff --git a/tests/test_find_or_recursive.cpp b/tests/test_find_or_recursive.cpp index 41e8a055..1f2f4800 100644 --- a/tests/test_find_or_recursive.cpp +++ b/tests/test_find_or_recursive.cpp @@ -391,8 +391,3 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_or_move_only, value_type, test_value_typ BOOST_TEST(ref == toml::find_or(v, "key1", "key2", std::move(opt))); } } - - - - - diff --git a/tests/test_get.cpp b/tests/test_get.cpp index 0db4abff..4a16e348 100644 --- a/tests/test_get.cpp +++ b/tests/test_get.cpp @@ -503,4 +503,3 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_get_toml_offset_datetime, value_type, test_va BOOST_TEST(tm.tm_sec == 0); } } - diff --git a/tests/test_result.cpp b/tests/test_result.cpp index ff43fe03..ad5084ca 100644 --- a/tests/test_result.cpp +++ b/tests/test_result.cpp @@ -438,5 +438,3 @@ BOOST_AUTO_TEST_CASE(test_and_or_other) BOOST_TEST("foo" == r1_gen().and_other(r2_gen()).unwrap_err()); } } - - diff --git a/tests/test_string.cpp b/tests/test_string.cpp index 8370ddb1..e3f093a1 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -151,4 +151,3 @@ BOOST_AUTO_TEST_CASE(test_string_add_assign) } - From e064a5c371333d19a5e19aa26f91f99af8954be9 Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Thu, 15 Sep 2022 21:41:13 +0200 Subject: [PATCH 12/13] Avoid unnecessary copies of parser result The 'result' class has unwrap() and unwrap_err() member functions overloaded for const lvalue and rvalue *this to avoid an unnecessarily copying the to-be unwrapped object of its containing object is going to be discarded anyway. Alas, the parse() function toml/parser.hpp file stored the parse result in a local `const` variable so, although the unwrap call would have been the last use of the object in each case, the unnecessary copy would still be made. This patch removes the `const` and adds a std::move() to actually benefit from the already implemented optimization. --- toml/parser.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index 4d59f0ac..590cb2a3 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -2414,12 +2414,14 @@ parse(std::vector& letters, const std::string& fname) } } - const auto data = detail::parse_toml_file(loc); - if(!data) + if (auto data = detail::parse_toml_file(loc)) { - throw syntax_error(data.unwrap_err(), source_location(loc)); + return std::move(data).unwrap(); + } + else + { + throw syntax_error(std::move(data).unwrap_err(), source_location(loc)); } - return data.unwrap(); } } // detail From 3f197c3cab3de9a325100d618413e6e6fa0f5da9 Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Wed, 28 Sep 2022 19:51:32 +0200 Subject: [PATCH 13/13] Fix use-after-move in test_parse_function_compiles and refactor My recent patch had introduced a conditional use-after-move bug into the test_parse_function_compiles function. This patch fixes that by reworking the entire test case into a compile-time check. In my opinion, we're not loosing anything by not actually executing the code (the result wasn't looked at anyway) and the code becomes much clearer by omitting the argument-preparation fluff. --- tests/test_parse_file.cpp | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/tests/test_parse_file.cpp b/tests/test_parse_file.cpp index cea5387a..2c456708 100644 --- a/tests/test_parse_file.cpp +++ b/tests/test_parse_file.cpp @@ -989,38 +989,18 @@ BOOST_AUTO_TEST_CASE(test_file_ends_without_lf) BOOST_AUTO_TEST_CASE(test_parse_function_compiles) { - const auto c = [](std::string& s) -> const std::string& { return s; }; - /*mutable*/ std::string example = testinput("example.toml"); - - // toml::parse(""); using result_type = decltype(toml::parse("string literal")); - - BOOST_TEST_MESSAGE("string_literal"); - - // toml::parse(const char*); - const result_type cstring = toml::parse(example.c_str()); - - BOOST_TEST_MESSAGE("const char*"); - - // toml::parse(char*); - const result_type char_ptr = toml::parse(&example.front()); - - BOOST_TEST_MESSAGE("char*"); - - // toml::parse(const std::string&); - const result_type string = toml::parse(c(example)); - // toml::parse(std::string&); - const result_type string_mutref = toml::parse(example); - // toml::parse(std::string&&); - const result_type string_rref = toml::parse(std::move(example)); - - BOOST_TEST_MESSAGE("strings"); - + (void) [](const char* that) -> result_type { return toml::parse(that); }; + (void) [](char* that) -> result_type { return toml::parse(that); }; + (void) [](const std::string& that) -> result_type { return toml::parse(that); }; + (void) [](std::string& that) -> result_type { return toml::parse(that); }; + (void) [](std::string&& that) -> result_type { return toml::parse(that); }; #ifdef TOML11_HAS_STD_FILESYSTEM - const std::filesystem::path fname_path(example.begin(), example.end()); - const result_type filesystem_path = toml::parse(fname_path); - BOOST_TEST_MESSAGE("path"); + (void) [](const std::filesystem::path& that) -> result_type { return toml::parse(that); }; + (void) [](std::filesystem::path& that) -> result_type { return toml::parse(that); }; + (void) [](std::filesystem::path&& that) -> result_type { return toml::parse(that); }; #endif + (void) [](std::FILE* that) -> result_type { return toml::parse(that, "mandatory.toml"); }; } BOOST_AUTO_TEST_CASE(test_parse_nonexistent_file)