Add note about CMake standard version selection to unit tests

Document how CMake chooses which C++ standard version to use when
building tests.
This commit is contained in:
Florian Albrechtskirchinger 2022-04-22 17:57:34 +02:00
parent fe2c4d7777
commit 3572a9d556
No known key found for this signature in database
GPG Key ID: 19618CE9B2D4BE6D
5 changed files with 33 additions and 22 deletions

View File

@ -27,16 +27,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// cmake/test.cmake selects the C++ standard versions with which to build a
// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.
// When using macros that are only defined for particular versions of the standard
// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding
// version macro in a comment close by, like this:
// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
#define JSON_TESTS_PRIVATE #define JSON_TESTS_PRIVATE
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using nlohmann::json; using nlohmann::json;
// build this testcase in C++20-mode (CMake code detects macro use)
// JSON_HAS_CPP_20
#if JSON_HAS_THREE_WAY_COMPARISON #if JSON_HAS_THREE_WAY_COMPARISON
// this can be replaced with the doctest stl extension header in version 2.5 // this can be replaced with the doctest stl extension header in version 2.5
namespace doctest namespace doctest
@ -144,6 +147,7 @@ TEST_CASE("lexicographical comparison operators")
CAPTURE(j) CAPTURE(j)
// check precomputed values // check precomputed values
#if JSON_HAS_THREE_WAY_COMPARISON #if JSON_HAS_THREE_WAY_COMPARISON
// JSON_HAS_CPP_20 (do not remove; see note at top of file)
CHECK((j_types[i] < j_types[j]) == expected_lt[i][j]); CHECK((j_types[i] < j_types[j]) == expected_lt[i][j]);
#else #else
CHECK(operator<(j_types[i], j_types[j]) == expected_lt[i][j]); CHECK(operator<(j_types[i], j_types[j]) == expected_lt[i][j]);
@ -153,6 +157,7 @@ TEST_CASE("lexicographical comparison operators")
} }
} }
#if JSON_HAS_THREE_WAY_COMPARISON #if JSON_HAS_THREE_WAY_COMPARISON
// JSON_HAS_CPP_20 (do not remove; see note at top of file)
SECTION("comparison: 3-way") SECTION("comparison: 3-way")
{ {
std::vector<std::vector<std::partial_ordering>> expected = std::vector<std::vector<std::partial_ordering>> expected =
@ -508,6 +513,7 @@ TEST_CASE("lexicographical comparison operators")
} }
#if JSON_HAS_THREE_WAY_COMPARISON #if JSON_HAS_THREE_WAY_COMPARISON
// JSON_HAS_CPP_20 (do not remove; see note at top of file)
SECTION("comparison: 3-way") SECTION("comparison: 3-way")
{ {
std::vector<std::vector<std::partial_ordering>> expected = std::vector<std::vector<std::partial_ordering>> expected =

View File

@ -27,6 +27,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// cmake/test.cmake selects the C++ standard versions with which to build a
// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.
// When using macros that are only defined for particular versions of the standard
// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding
// version macro in a comment close by, like this:
// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
#define JSON_TESTS_PRIVATE #define JSON_TESTS_PRIVATE
@ -1582,12 +1589,4 @@ TEST_CASE("JSON to enum mapping")
} }
} }
#ifdef JSON_HAS_CPP_17
#undef JSON_HAS_CPP_17
#endif
#ifdef JSON_HAS_CPP_14
#undef JSON_HAS_CPP_14
#endif
DOCTEST_CLANG_SUPPRESS_WARNING_POP DOCTEST_CLANG_SUPPRESS_WARNING_POP

View File

@ -1448,13 +1448,5 @@ TEST_CASE("items()")
} }
} }
#ifdef JSON_HAS_CPP_17
#undef JSON_HAS_CPP_17
#endif
#ifdef JSON_HAS_CPP_14
#undef JSON_HAS_CPP_14
#endif
DOCTEST_GCC_SUPPRESS_WARNING_POP DOCTEST_GCC_SUPPRESS_WARNING_POP
DOCTEST_CLANG_SUPPRESS_WARNING_POP DOCTEST_CLANG_SUPPRESS_WARNING_POP

View File

@ -27,13 +27,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// cmake/test.cmake selects the C++ standard versions with which to build a
// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.
// When using macros that are only defined for particular versions of the standard
// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding
// version macro in a comment close by, like this:
// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using nlohmann::json; using nlohmann::json;
#if JSON_HAS_RANGES #if JSON_HAS_RANGES
// JSON_HAS_CPP_20 (magic keyword; do not remove)
#include <algorithm> #include <algorithm>
#include <ranges> #include <ranges>
#endif #endif
@ -890,6 +896,7 @@ TEST_CASE("iterators 2")
#if JSON_HAS_RANGES #if JSON_HAS_RANGES
// JSON_HAS_CPP_20 (do not remove; see note at top of file)
SECTION("ranges") SECTION("ranges")
{ {
SECTION("concepts") SECTION("concepts")

View File

@ -27,6 +27,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// cmake/test.cmake selects the C++ standard versions with which to build a
// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.
// When using macros that are only defined for particular versions of the standard
// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding
// version macro in a comment close by, like this:
// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)
#include "doctest_compatibility.h" #include "doctest_compatibility.h"
// for some reason including this after the json header leads to linker errors with VS 2017... // for some reason including this after the json header leads to linker errors with VS 2017...
@ -48,7 +55,6 @@ using ordered_json = nlohmann::ordered_json;
#endif #endif
#if JSON_HAS_EXPERIMENTAL_FILESYSTEM #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
// JSON_HAS_CPP_17 (magic keyword; do not remove)
#include <experimental/filesystem> #include <experimental/filesystem>
namespace nlohmann::detail namespace nlohmann::detail
{ {
@ -788,6 +794,7 @@ TEST_CASE("regression tests 2")
} }
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
// JSON_HAS_CPP_17 (do not remove; see note at top of file)
SECTION("issue #3070 - Version 3.10.3 breaks backward-compatibility with 3.10.2 ") SECTION("issue #3070 - Version 3.10.3 breaks backward-compatibility with 3.10.2 ")
{ {
nlohmann::detail::std_fs::path text_path("/tmp/text.txt"); nlohmann::detail::std_fs::path text_path("/tmp/text.txt");