Skip to content

Commit 2c3fabc

Browse files
authored
Extract testing features from the main library (#333)
* Extract testing features from the main library * folder * Fix letter case
1 parent 0dbf16a commit 2c3fabc

18 files changed

Lines changed: 164 additions & 47 deletions

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ add_subdirectory(Plugin)
175175
add_subdirectory(bindings)
176176
add_subdirectory(examples)
177177
add_subdirectory(docs)
178+
if (SP3_BUILD_TEST)
179+
add_subdirectory(Testing)
180+
endif()
178181

179182
SP3_add_python_package(
180183
SOURCE_DIRECTORY

Plugin/CMakeLists.txt

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,6 @@ set(SOURCE_FILES
2626
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Prefab.cpp
2727
)
2828

29-
if(SP3_BUILD_TEST)
30-
find_package(Sofa.Testing REQUIRED)
31-
if(Sofa.Testing_FOUND)
32-
list(APPEND HEADER_FILES
33-
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/PythonTest.h
34-
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/PythonTestExtractor.h
35-
)
36-
list(APPEND SOURCE_FILES
37-
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/PythonTest.cpp
38-
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/PythonTestExtractor.cpp
39-
)
40-
else()
41-
message(WARNING "Sofa.Testing has not been found, SofaPython3 tests cannot be built.")
42-
endif(Sofa.Testing_FOUND)
43-
endif(SP3_BUILD_TEST)
44-
4529
find_package(pybind11 CONFIG REQUIRED)
4630
sofa_find_package(Sofa.Simulation.Graph REQUIRED)
4731

@@ -53,10 +37,6 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE "-DSOFA_BUILD_SOFAPYTHON3")
5337
target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Simulation.Graph)
5438
target_link_libraries(${PROJECT_NAME} PUBLIC pybind11::module pybind11::embed)
5539

56-
if(SP3_BUILD_TEST AND Sofa.Testing_FOUND)
57-
target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Testing)
58-
endif()
59-
6040
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME SofaPython3)
6141

6242
if (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")

Testing/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
project(SofaPython3Testing VERSION 1.0)
2+
3+
find_package(Sofa.Testing REQUIRED)
4+
5+
set(HEADER_FILES
6+
src/SofaPython3Testing/config.h.in
7+
src/SofaPython3Testing/PythonTest.h
8+
src/SofaPython3Testing/PythonTestExtractor.h
9+
)
10+
11+
set(SOURCE_FILES
12+
src/SofaPython3Testing/init.cpp
13+
src/SofaPython3Testing/PythonTest.cpp
14+
src/SofaPython3Testing/PythonTestExtractor.cpp
15+
)
16+
17+
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
18+
target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Testing SofaPython3::Plugin)
19+
target_compile_definitions(${PROJECT_NAME} PRIVATE "-DSOFA_BUILD_SOFAPYTHON3")
20+
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER Testing)
21+
22+
sofa_create_package_with_targets(
23+
PACKAGE_NAME ${PROJECT_NAME}
24+
PACKAGE_VERSION ${SofaPython3_VERSION}
25+
TARGETS ${PROJECT_NAME} AUTO_SET_TARGET_PROPERTIES
26+
INCLUDE_SOURCE_DIR "src"
27+
INCLUDE_INSTALL_DIR "${PROJECT_NAME}"
28+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# CMake package configuration file for @PROJECT_NAME@
2+
3+
@PACKAGE_GUARD@
4+
@PACKAGE_INIT@
5+
6+
set(SP3_BUILD_TEST @SP3_BUILD_TEST@)
7+
8+
find_package(Sofa.Testing QUIET REQUIRED)
9+
10+
# If we are importing this config file and the target is not yet there this is indicating that
11+
# target is an imported one. So we include it
12+
if(NOT TARGET @PROJECT_NAME@)
13+
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
14+
endif()
15+
16+
# Check that the component/target is there.
17+
check_required_components(@PROJECT_NAME@)

Plugin/src/SofaPython3/PythonTest.cpp renamed to Testing/src/SofaPython3Testing/PythonTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ namespace simpleapi=sofa::simpleapi;
3838
#include <sofa/helper/system/SetDirectory.h>
3939
using sofa::helper::system::SetDirectory;
4040

41-
#include <SofaPython3/PythonTestExtractor.h>
41+
#include <SofaPython3Testing/PythonTestExtractor.h>
4242
#include <numeric>
43-
#include "PythonEnvironment.h"
43+
#include <SofaPython3/PythonEnvironment.h>
4444
#include "PythonTest.h"
4545

4646
MSG_REGISTER_CLASS(sofapython3::PythonTest, "SofaPython3::PythonTest")
@@ -49,7 +49,7 @@ namespace sofapython3
4949
{
5050

5151
/// This function is used by gtest to print the content of the struct in a meaninfull way
52-
void SOFAPYTHON3_API PrintTo(const sofapython3::PythonTestData& d, ::std::ostream *os)
52+
void PrintTo(const sofapython3::PythonTestData& d, ::std::ostream *os)
5353
{
5454
(*os) << d.filepath ;
5555
(*os) << " with args {" ;

Plugin/src/SofaPython3/PythonTest.h renamed to Testing/src/SofaPython3Testing/PythonTest.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#pragma once
2222

2323
#include <string>
24-
#include <SofaPython3/config.h>
24+
#include <SofaPython3Testing/config.h>
2525
#include <sofa/testing/BaseTest.h>
2626

2727
#include <filesystem>
@@ -33,7 +33,7 @@ namespace sofapython3
3333
using sofa::testing::BaseTest;
3434

3535
/// a Python_test is defined by a python filepath and optional arguments
36-
struct SOFAPYTHON3_API PythonTestData
36+
struct SOFAPYTHON3_TESTING_API PythonTestData
3737
{
3838
PythonTestData (std::string filepath, std::string testgroup, std::vector<std::string> arguments)
3939
: filepath(std::move(filepath)), arguments(std::move(arguments)), testgroup{std::move(testgroup)} {}
@@ -48,10 +48,10 @@ struct SOFAPYTHON3_API PythonTestData
4848
/// test.all_tests/2, where GetParam() = /path/to/file.py with args {1,2,3}
4949
/// instead of the defautl googletest printer that output things like the following:
5050
/// test.all_tests/2, where GetParam() = 56-byte object <10-48 EC-37 18-56 00-00 67-00-00-00>
51-
void SOFAPYTHON3_API PrintTo(const PythonTestData& d, ::std::ostream* os);
51+
void SOFAPYTHON3_TESTING_API PrintTo(const PythonTestData& d, ::std::ostream* os);
5252

5353
/// A test written in python (but not as a sofa class to perform unitary testing on python functions)
54-
class SOFAPYTHON3_API PythonTest : public BaseTest,
54+
class SOFAPYTHON3_TESTING_API PythonTest : public BaseTest,
5555
public ::testing::WithParamInterface<PythonTestData>
5656
{
5757
public:
@@ -67,7 +67,7 @@ class SOFAPYTHON3_API PythonTest : public BaseTest,
6767

6868
/// This function is called by gtest to generate the test from the filename. This is nice
6969
/// As this allows to do mytest --gtest_filter=*MySomething*
70-
static std::string getTestName(const testing::TestParamInfo<PythonTestData>& p)
70+
static std::string getTestName(const ::testing::TestParamInfo<PythonTestData>& p)
7171
{
7272
if(p.param.arguments.size()==0)
7373
return std::to_string(p.index)+"_"+p.param.testgroup+path(p.param.filepath).stem().string();

Plugin/src/SofaPython3/PythonTestExtractor.cpp renamed to Testing/src/SofaPython3Testing/PythonTestExtractor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Contact information: contact@sofa-framework.org *
1919
******************************************************************************/
2020

21-
#include <SofaPython3/PythonTestExtractor.h>
21+
#include <SofaPython3Testing/PythonTestExtractor.h>
2222
#include <SofaPython3/PythonEnvironment.h>
2323

2424
#include <sofa/helper/logging/Messaging.h>
@@ -148,4 +148,4 @@ void PythonTestExtractor::addTestDirectory (const std::string & dir, const std::
148148
}
149149
}
150150

151-
} // namespace sofapython3
151+
} // namespace sofapython3

Plugin/src/SofaPython3/PythonTestExtractor.h renamed to Testing/src/SofaPython3Testing/PythonTestExtractor.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*******************************************************************************
1818
* Contact information: contact@sofa-framework.org *
1919
******************************************************************************/
20-
21-
#include <SofaPython3/config.h>
22-
#include <SofaPython3/PythonTest.h>
20+
#pragma once
21+
#include <SofaPython3Testing/config.h>
22+
#include <SofaPython3Testing/PythonTest.h>
2323

2424
#include <utility>
2525
#include <pybind11/pybind11.h>
@@ -32,7 +32,7 @@ namespace sofapython3
3232
/**
3333
* Utility class that loads up python files and extract the unittest.
3434
*/
35-
class SOFAPYTHON3_API PythonTestExtractor
35+
class SOFAPYTHON3_TESTING_API PythonTestExtractor
3636
{
3737
public:
3838
/**
@@ -72,4 +72,4 @@ class SOFAPYTHON3_API PythonTestExtractor
7272
std::vector<Entry> p_tests;
7373
};
7474

75-
} // namespace sofapython3
75+
} // namespace sofapython3
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify it *
6+
* under the terms of the GNU Lesser General Public License as published by *
7+
* the Free Software Foundation; either version 2.1 of the License, or (at *
8+
* your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, but WITHOUT *
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
13+
* for more details. *
14+
* *
15+
* You should have received a copy of the GNU Lesser General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
*******************************************************************************
18+
* Contact information: contact@sofa-framework.org *
19+
******************************************************************************/
20+
#pragma once
21+
#include <SofaPython3/config.h>
22+
23+
#ifdef SOFA_BUILD_SOFAPYTHON3
24+
# define SOFAPYTHON3_TESTING_API PYBIND11_EXPORT
25+
#else
26+
# define SOFAPYTHON3_TESTING_API SOFA_IMPORT_DYNAMIC_LIBRARY
27+
#endif
28+
29+
namespace sofapython3::testing
30+
{
31+
constexpr const char* MODULE_NAME = "@PROJECT_NAME@";
32+
constexpr const char* MODULE_VERSION = "@PROJECT_VERSION@";
33+
} // namespace sofapython3::testing
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/******************************************************************************
2+
* SOFA, Simulation Open-Framework Architecture *
3+
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify it *
6+
* under the terms of the GNU Lesser General Public License as published by *
7+
* the Free Software Foundation; either version 2.1 of the License, or (at *
8+
* your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, but WITHOUT *
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
13+
* for more details. *
14+
* *
15+
* You should have received a copy of the GNU Lesser General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
*******************************************************************************
18+
* Authors: The SOFA Team and external contributors (see Authors.txt) *
19+
* *
20+
* Contact information: contact@sofa-framework.org *
21+
******************************************************************************/
22+
#include <SofaPython3Testing/config.h>
23+
namespace sofapython3::testing
24+
{
25+
26+
extern "C" {
27+
SOFA_EXPORT_DYNAMIC_LIBRARY void initExternalModule();
28+
SOFA_EXPORT_DYNAMIC_LIBRARY const char* getModuleName();
29+
SOFA_EXPORT_DYNAMIC_LIBRARY const char* getModuleVersion();
30+
SOFA_EXPORT_DYNAMIC_LIBRARY const char* getModuleComponentList();
31+
}
32+
33+
void initExternalModule()
34+
{
35+
static bool first = true;
36+
if (first)
37+
{
38+
first = false;
39+
}
40+
}
41+
42+
const char* getModuleName()
43+
{
44+
return MODULE_NAME;
45+
}
46+
47+
const char* getModuleVersion()
48+
{
49+
return MODULE_VERSION;
50+
}
51+
52+
const char* getModuleComponentList()
53+
{
54+
return {};
55+
}
56+
}

0 commit comments

Comments
 (0)