From 70d1a546b50b304ce4cc2511a8be9547f2ccf7e8 Mon Sep 17 00:00:00 2001 From: Thomas Kittelmann Date: Tue, 1 Oct 2024 11:44:15 +0200 Subject: [PATCH] Common header-only include path for test modules --- tests/CMakeLists.txt | 11 +++- tests/cmake/mctools_testutils.cmake | 30 ++++++++-- tests/include/NCTestUtils/NCTestModUtils.hh | 63 +++++++++++++++++++++ tests/modules/lib_fpe/fpe.cc | 31 +--------- tests/modules/lib_misc/lib.cc | 35 +----------- tests/modules/lib_testfileutils/lib.cc | 37 +----------- 6 files changed, 102 insertions(+), 105 deletions(-) create mode 100644 tests/include/NCTestUtils/NCTestModUtils.hh diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1ee4172d..2f311267 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -88,14 +88,18 @@ else() "${CMAKE_CURRENT_LIST_DIR}/scripts" "${nctest_pyenvmod}" ) endif() +set( nctests_extra_inc_dirs PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include" ) mctools_testutils_add_test_libs( - "${CMAKE_CURRENT_LIST_DIR}/libs" "${nctests_ncrystallib_target}" + "${CMAKE_CURRENT_LIST_DIR}/libs" + "${nctests_ncrystallib_target}" + "${nctests_extra_inc_dirs}" ) mctools_testutils_add_test_modules( - "${CMAKE_CURRENT_LIST_DIR}/modules" "${nctests_ncrystallib_target}" + "${CMAKE_CURRENT_LIST_DIR}/modules" + "${nctests_ncrystallib_target}" + "${nctests_extra_inc_dirs}" ) - set( nctest_appenvmod "" ) if ( WIN32 ) if ( NCTESTS_STANDALONE ) @@ -134,6 +138,7 @@ list( mctools_testutils_add_tests_apps( "${CMAKE_CURRENT_LIST_DIR}/src" "${nctests_ncrystallib_target}" + "${nctests_extra_inc_dirs}" "${nctest_appenvmod}" ) #FIXME: TODO Need python unit test which launches compiled app! diff --git a/tests/cmake/mctools_testutils.cmake b/tests/cmake/mctools_testutils.cmake index 63dfc8f4..eccdd6da 100644 --- a/tests/cmake/mctools_testutils.cmake +++ b/tests/cmake/mctools_testutils.cmake @@ -143,15 +143,28 @@ function( mctools_testutils_internal_missingpydeps resvar pydeps ) set( "${resvar}" "${missing}" PARENT_SCOPE ) endfunction() -function( mctools_testutils_add_test_modules librootdir extra_link_libs ) - mctools_testutils_internal_add_test_libs( "${librootdir}" "${extra_link_libs}" "ON" ) +function( + mctools_testutils_add_test_modules + librootdir extra_link_libs extra_inc_dirs + ) + mctools_testutils_internal_add_test_libs( + "${librootdir}" "${extra_link_libs}" "${extra_inc_dirs}" "ON" + ) endfunction() -function( mctools_testutils_add_test_libs librootdir extra_link_libs ) - mctools_testutils_internal_add_test_libs( "${librootdir}" "${extra_link_libs}" "OFF" ) +function( + mctools_testutils_add_test_libs + librootdir extra_link_libs extra_inc_dirs + ) + mctools_testutils_internal_add_test_libs( + "${librootdir}" "${extra_link_libs}" "${extra_inc_dirs}" "OFF" + ) endfunction() -function( mctools_testutils_internal_add_test_libs librootdir extra_link_libs is_module ) +function( + mctools_testutils_internal_add_test_libs + librootdir extra_link_libs extra_inc_dirs is_module + ) file( GLOB libdirs LIST_DIRECTORIES true @@ -195,6 +208,7 @@ function( mctools_testutils_internal_add_test_libs librootdir extra_link_libs is endif() target_link_libraries( ${name} PRIVATE ${extra_link_libs} ) + target_include_directories( ${name} ${extra_inc_dirs} ) if ( EXISTS "${libdir}/include" ) if ( is_module ) @@ -222,7 +236,10 @@ function( mctools_testutils_internal_add_test_libs librootdir extra_link_libs is endforeach() endfunction() -function( mctools_testutils_add_tests_apps approotdir extra_link_libs envmod ) +function( + mctools_testutils_add_tests_apps + approotdir extra_link_libs extra_inc_dirs envmod + ) set( testsbindir "${PROJECT_BINARY_DIR}/mctools_tests_bin" ) file( MAKE_DIRECTORY "${testsbindir}" ) file( @@ -241,6 +258,7 @@ function( mctools_testutils_add_tests_apps approotdir extra_link_libs envmod ) target_link_libraries( ${bn} PRIVATE "TestLib_${dep}" ) endforeach() target_link_libraries( ${bn} PRIVATE ${extra_link_libs} ) + target_include_directories( ${bn} ${extra_inc_dirs} ) set( reflog "${appdir}/test.log" ) if ( EXISTS "${reflog}" ) diff --git a/tests/include/NCTestUtils/NCTestModUtils.hh b/tests/include/NCTestUtils/NCTestModUtils.hh new file mode 100644 index 00000000..10f15d76 --- /dev/null +++ b/tests/include/NCTestUtils/NCTestModUtils.hh @@ -0,0 +1,63 @@ +#ifndef NCrystal_TestModUtils_hh +#define NCrystal_TestModUtils_hh + +//////////////////////////////////////////////////////////////////////////////// +// // +// This file is part of NCrystal (see https://mctools.github.io/ncrystal/) // +// // +// Copyright 2015-2024 NCrystal developers // +// // +// Licensed under the Apache License, Version 2.0 (the "License"); // +// you may not use this file except in compliance with the License. // +// You may obtain a copy of the License at // +// // +// http://www.apache.org/licenses/LICENSE-2.0 // +// // +// Unless required by applicable law or agreed to in writing, software // +// distributed under the License is distributed on an "AS IS" BASIS, // +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // +// See the License for the specific language governing permissions and // +// limitations under the License. // +// // +//////////////////////////////////////////////////////////////////////////////// + +// Macros and inline functions for implementing compiled test modules (to be +// loaded with the Lib class from loadlib.py. + +#if defined (_WIN32) || defined (__CYGWIN__) || defined (WIN32) +# define NCTEST_API __declspec(dllexport) +#elif defined(__GNUC__) || defined(__clang__) +# define NCTEST_API __attribute__ ((visibility ("default"))) +#else +# define NCTEST_API +#endif +#define NCTEST_CTYPES extern "C" NCTEST_API + +#include +#include +#include + +namespace nctest { + namespace { + inline void printErrAndExit(const std::exception &e) noexcept(true) { + const std::runtime_error* stdrte = dynamic_cast(&e); + if (stdrte) + std::cout<<"NCTest ERROR (std::runtime_error): "<what()< -#include -#include - -namespace nctest { - inline void printErrAndExit(const std::exception &e) noexcept(true) { - const std::runtime_error* stdrte = dynamic_cast(&e); - if (stdrte) - std::cout<<"NCTest ERROR (std::runtime_error): "<what()< -#include -#include - -namespace nctest { - inline void printErrAndExit(const std::exception &e) noexcept(true) { - const std::runtime_error* stdrte = dynamic_cast(&e); - if (stdrte) - std::cout<<"NCTest ERROR (std::runtime_error): "<what()< -namespace NC = NCrystal; - -////////////////////////////////////////////////////// -//FIXME: To common test header: -#if defined (_WIN32) || defined (__CYGWIN__) || defined (WIN32) -# define NCTEST_API __declspec(dllexport) -#elif defined(__GNUC__) || defined(__clang__) -# define NCTEST_API __attribute__ ((visibility ("default"))) -#else -# define NCTEST_API -#endif -#define NCTEST_CTYPES extern "C" NCTEST_API - -#include -#include -#include - -namespace nctest { - inline void printErrAndExit(const std::exception &e) noexcept(true) { - const std::runtime_error* stdrte = dynamic_cast(&e); - if (stdrte) - std::cout<<"NCTest ERROR (std::runtime_error): "<what()<