Module testing (#1255)

Address issue #1254

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
This commit is contained in:
Philip Top
2025-11-29 05:49:25 -08:00
committed by GitHub
parent 60c518cda8
commit 53608df1bd
17 changed files with 192 additions and 27 deletions

View File

@@ -357,6 +357,24 @@ if(CLI11_INSTALL_PACKAGE_TESTS)
set_property(TEST find-package-testsC PROPERTY LABELS Packaging)
set_property(TEST find-package-testsC PROPERTY DEPENDS find-package-testsB)
if(CLI11_MODULE_TESTS)
add_test(
find-package-module
${CMAKE_CTEST_COMMAND}
--build-and-test
"${CMAKE_CURRENT_SOURCE_DIR}/module_test"
"${CMAKE_CURRENT_BINARY_DIR}/module_test"
--build-generator
"Ninja"
--build-options
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
"-DCLI11_DIR=${CMAKE_INSTALL_PREFIX}"
${package_test_command})
set_property(TEST find-package-module PROPERTY LABELS Packaging)
endif()
if(NOT MSVC)
# Tests for other CMake projects using the package_config files
add_test(

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14...4.1)
cmake_minimum_required(VERSION 3.14...4.2)
project(CLI11-find-package-test)

View File

@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.14...4.2)
project(CLI11-module-test)
include(CTest)
if(CLI11_DIR)
set(CMAKE_PREFIX_PATH ${CLI11_DIR})
endif()
set(CMAKE_CXX_STANDARD 23)
# Test the CLI11 CMake package config
find_package(CLI11 2.5 REQUIRED)
# Test the target
add_executable(module-test module_test.cpp)
target_sources(module-test PUBLIC FILE_SET cmodule TYPE CXX_MODULES FILES cmodule.ixx)
target_link_libraries(module-test CLI11::CLI11)
target_compile_options(module-test PUBLIC -fmodules-ts)
add_test(NAME module-test1 COMMAND module-test one)
set_property(TEST module-test1 PROPERTY PASS_REGULAR_EXPRESSION "OK: export module")

21
tests/module_test/LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 scivision
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,20 @@
// Copyright (c) 2024 scivision
// Copyright (c) 2025 University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: MIT
// modified from https://github.com/iTrooz/CppModules/blob/cli11 for use in CLI11 tests
module;
#include <CLI/CLI.hpp>
export module cmodule;
export void foo(CLI::App *app) {}
export int add(int a, int b) { return a + b; }
export int subtract(int a, int b) { return a - b; }

View File

@@ -0,0 +1,30 @@
// Copyright (c) 2024 scivision
// Copyright (c) 2025 University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: MIT
// modified from https://github.com/iTrooz/CppModules/blob/cli11 for use in CLI11 tests
#include <cassert>
#include <cstdio>
#include <cstdlib>
import cmodule;
int main() {
int a = 1;
int b = 2;
int absum = add(a, b);
int abdif = subtract(a, b);
assert(a + b == absum);
assert(a - b == abdif);
// used this instead of <iostream> to work with older compilers that may choke on <iostream> implicit includes
printf("OK: export module\n");
return EXIT_SUCCESS;
}

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14...4.1)
cmake_minimum_required(VERSION 3.14...4.2)
project(CLI11-package-config-test)