From 06a11f9d46b22d362d3c8f75d9f601ac966c40f9 Mon Sep 17 00:00:00 2001 From: leha-bot Date: Tue, 13 Sep 2022 02:09:01 +0300 Subject: [PATCH 1/5] feat(cmake): pass-through the custom C++ namespace from CMake (#15) Via defining the LB_STD_VERSION_NS macro using the target_compile_definitions(). Its name a bit inconsistent though, but looks better (in my opinion) in C++, but doesn't look OK in a bunch of CMake configure parameters as it conflicts with another flags (`LBSTD_VERSION_xxx` vs `LB_STD_VERSION_xxx`). --- CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6db42e5..f6522c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,17 +5,22 @@ cmake_minimum_required(VERSION 3.20) project(lbstd_version VERSION 0.0.1 LANGUAGES CXX) -option(LBSTD_VERSION_NS_NAME "The enclosing namespace for all classes of std_version" "lbstd") +set(LBSTD_VERSION_NS "lbstd" CACHE STRING "The enclosing namespace for all classes of std_version") option(LBSTD_VERSION_ENABLE_FMTLIB "Enable fmtlib formatters integration" ON) option(LBSTD_VERSION_ENABLE_STDFORMAT "Enable std::format integration") option(LBSTD_VERSION_ENABLE_TESTS "Enable tests" ON) +if (LBSTD_VERSION_NS STREQUAL "") + message(FATAL_ERROR "The enclosing namespace name in variable LBSTD_VERSION_NS must be a non-empty string!") +endif() + if (LBSTD_VERSION_ENABLE_FMTLIB) find_package(fmt REQUIRED) endif() add_library(lbstd_version INTERFACE) target_include_directories(lbstd_version INTERFACE "${CMAKE_SOURCE_DIR}/include") +target_compile_definitions(lbstd_version INTERFACE "LB_STD_VERSION_NS=${LBSTD_VERSION_NS}") target_compile_features(lbstd_version INTERFACE cxx_std_20) if (LBSTD_VERSION_ENABLE_TESTS) From 682b648ee38a51045617b2649d837e01c92f711d Mon Sep 17 00:00:00 2001 From: leha-bot Date: Tue, 13 Sep 2022 02:13:24 +0300 Subject: [PATCH 2/5] chore(cmake/tests): drop ambiguous enable_testing() --- tests/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3366998..41964e3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,7 +2,6 @@ # # SPDX-License-Identifier: BSL-1.0 OR BlueOak-1.0.0 -enable_testing() find_package(doctest REQUIRED) include(doctest) From 5c03abf8ac8e65c6dba7256f2ad5e5d940be322c Mon Sep 17 00:00:00 2001 From: leha-bot Date: Tue, 13 Sep 2022 02:16:59 +0300 Subject: [PATCH 3/5] feat(tests): add macro for expanding C macros to strings For better test cases descriptions. --- tests/test.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test.cpp b/tests/test.cpp index 374d9fd..d6176a5 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -8,8 +8,13 @@ #include +#define MAKE_STRING(q) #q +#define UNPACK_MACRO(q) q +#define MAKE_STRING_FROM_MACRO(q) MAKE_STRING(q) + + #define GENERATE_COMPARISON_TEST_CASES(classname, val_less, val_eq, val_greater) \ -TEST_CASE("Comparison tests for " #classname) \ +TEST_CASE("Comparison tests for " MAKE_STRING_FROM_MACRO(classname)) \ {\ classname ver{(val_eq)}, ver_eq{(val_eq)}, ver_less{(val_less)}, ver_greater{(val_greater)}; \ SUBCASE("equal") \ From 6ebb94d5ce534bc6916e1cc4b26c024c213f363e Mon Sep 17 00:00:00 2001 From: leha-bot Date: Tue, 13 Sep 2022 02:17:53 +0300 Subject: [PATCH 4/5] feat(cmake/tests): ask doctest to auto-generate test labels For CTest statistics / grouping of similar tests. Doctest uses the TEST_SUITE() name for CTest label or the doctest::test_suite() decorator. --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 41964e3..79ff527 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,5 +9,5 @@ include(doctest) add_executable(lbstd_version_tests test.cpp) target_link_libraries(lbstd_version_tests doctest::doctest lbstd_version) -doctest_discover_tests(lbstd_version_tests) +doctest_discover_tests(lbstd_version_tests ADD_LABELS ON) From 7c3cb8b7243e37d45b8aefaa3113eadca4e39b74 Mon Sep 17 00:00:00 2001 From: leha-bot Date: Tue, 13 Sep 2022 02:20:07 +0300 Subject: [PATCH 5/5] feat(tests): add test for actual namespace passthrough --- tests/CMakeLists.txt | 12 ++++++++++++ tests/print_ns.cpp | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/print_ns.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 79ff527..c236c45 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,5 +9,17 @@ include(doctest) add_executable(lbstd_version_tests test.cpp) target_link_libraries(lbstd_version_tests doctest::doctest lbstd_version) +add_executable(lbstd_version_print_ns print_ns.cpp) +target_link_libraries(lbstd_version_print_ns lbstd_version) + +# The manual test for namespace passthrough. +set(LBSTD_VERSION_CMAKE_NS_PASSTHROUGH "CMake custom library namespace (\"${LBSTD_VERSION_NS}::\") passthrough in C++ code") +add_test(NAME "${LBSTD_VERSION_CMAKE_NS_PASSTHROUGH}" COMMAND lbstd_version_print_ns) +set_tests_properties("${LBSTD_VERSION_CMAKE_NS_PASSTHROUGH}" PROPERTIES + PASS_REGULAR_EXPRESSION "${LBSTD_VERSION_NS}" + LABELS "buildsystem" +) + +# Auto-discoverable doctest unit tests doctest_discover_tests(lbstd_version_tests ADD_LABELS ON) diff --git a/tests/print_ns.cpp b/tests/print_ns.cpp new file mode 100644 index 0000000..c2ffe3a --- /dev/null +++ b/tests/print_ns.cpp @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: 2022 leha-bot and contributors +// +// SPDX-License-Identifier: BSL-1.0 OR BlueOak-1.0.0 +// +// This executable is a smoke test for checking the proper namespace customizing via CMake's +// -DLBSTD_VERSION_NS="namespace_name" configure parameter. The main() function just prints the +// actual namespace which will be used in CMake's test check. +#include + +#include + +#define MAKE_STRING(q) #q +#define UNPACK_MACRO(q) q +#define MAKE_STRING_FROM_MACRO(q) MAKE_STRING(q) + +int main() +{ + std::cout << MAKE_STRING_FROM_MACRO(LB_STD_VERSION_NS); + return 0; +}