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) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3366998..c236c45 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) @@ -10,5 +9,17 @@ 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) +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; +} 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") \