Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Custom C++ namespace for library (#15) #16

Merged
merged 5 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")
leha-bot marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
15 changes: 13 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
#
# SPDX-License-Identifier: BSL-1.0 OR BlueOak-1.0.0

enable_testing()
find_package(doctest REQUIRED)

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)

20 changes: 20 additions & 0 deletions tests/print_ns.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>

#include <version/version.hpp>

#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;
}
7 changes: 6 additions & 1 deletion tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

#include <type_traits>

#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") \
Expand Down