Skip to content

Commit 835d78d

Browse files
authored
Add binding for GetVersion and associated unit tests (#583)
* Add binding for `GetVersion` and associated unit tests Introduce a static method `GetVersion` in `Sofa.Helper.Utils` to retrieve the SOFA version in `vMM.mm` format. Added unit tests in `Helper/Version.py` to verify existence and format adherence. Updated CMakeLists accordingly. * make the version string static * move GetVersion from Utils to Version * add GetVersion() to the Sofa package
1 parent 833bd69 commit 835d78d

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

bindings/Sofa/package/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ def unloadModules():
195195
for name in toremove:
196196
del(sys.modules[name]) # unload it
197197

198+
def GetVersion():
199+
"""Returns the version of SOFA as a string in the format 'vMM.mm', where MM is the major version and mm is the
200+
minor version.
201+
"""
202+
return Sofa.Helper.GetVersion()
198203

199204
def formatStackForSofa(o):
200205
""" format the stack trace provided as a parameter into a string like that:
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
21+
#include <pybind11/pybind11.h>
22+
23+
#include <SofaPython3/PythonFactory.h>
24+
#include <SofaPython3/Sofa/Helper/Binding_Version.h>
25+
26+
#include <iomanip>
27+
#include <sofa/version.h>
28+
29+
30+
/// Makes an alias for the pybind11 namespace to increase readability.
31+
namespace py { using namespace pybind11; }
32+
33+
namespace sofapython3
34+
{
35+
36+
void moduleAddVersion(py::module &m)
37+
{
38+
m.def("GetVersion",
39+
[]()
40+
{
41+
static const std::string sofaVersion = []() {
42+
std::stringstream version;
43+
constexpr auto major = SOFA_VERSION / 10000;
44+
constexpr auto minor = SOFA_VERSION / 100 % 100;
45+
version << 'v'
46+
<< std::setfill('0') << std::setw(2) << major
47+
<< "."
48+
<< std::setfill('0') << std::setw(2) << minor;
49+
return version.str();
50+
}();
51+
return sofaVersion;
52+
},
53+
"Returns the version of SOFA as a string in the format 'vMM.mm', where MM is the major version and mm is the minor version.");
54+
}
55+
56+
57+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
21+
#pragma once
22+
23+
#include <pybind11/pybind11.h>
24+
25+
namespace sofapython3
26+
{
27+
28+
void moduleAddVersion(pybind11::module &m);
29+
30+
}

bindings/Sofa/src/SofaPython3/Sofa/Helper/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ project(Bindings.Sofa.Helper)
33
set(HEADER_FILES
44
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Utils.h
55
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Vector.h
6+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Version.h
67
${CMAKE_CURRENT_SOURCE_DIR}/System/Submodule_System.h
78
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MessageHandler.h
89
${CMAKE_CURRENT_SOURCE_DIR}/System/Binding_FileRepository.h
@@ -13,6 +14,7 @@ set(SOURCE_FILES
1314
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MessageHandler.cpp
1415
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Utils.cpp
1516
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Vector.cpp
17+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Version.cpp
1618
${CMAKE_CURRENT_SOURCE_DIR}/System/Submodule_System.cpp
1719
${CMAKE_CURRENT_SOURCE_DIR}/System/Binding_FileRepository.cpp
1820
)

bindings/Sofa/src/SofaPython3/Sofa/Helper/Submodule_Helper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <SofaPython3/Sofa/Helper/Binding_MessageHandler.h>
2727
#include <SofaPython3/Sofa/Helper/Binding_Vector.h>
2828
#include <SofaPython3/Sofa/Helper/Binding_Utils.h>
29+
#include <SofaPython3/Sofa/Helper/Binding_Version.h>
2930

3031
/// Makes an alias for the pybind11 namespace to increase readability.
3132
namespace py { using namespace pybind11; }
@@ -153,6 +154,7 @@ PYBIND11_MODULE(Helper, helper)
153154
moduleAddVector(helper);
154155
moduleAddSystem(helper);
155156
moduleAddUtils(helper);
157+
moduleAddVersion(helper);
156158

157159
auto atexit = py::module_::import("atexit");
158160
atexit.attr("register")(py::cpp_function([]() {

bindings/Sofa/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(PYTHON_FILES
2222
${CMAKE_CURRENT_SOURCE_DIR}/Core/Mass.py
2323
${CMAKE_CURRENT_SOURCE_DIR}/Core/MyRestShapeForceField.py
2424
${CMAKE_CURRENT_SOURCE_DIR}/Helper/Message.py
25+
${CMAKE_CURRENT_SOURCE_DIR}/Helper/Version.py
2526
${CMAKE_CURRENT_SOURCE_DIR}/Simulation/Node.py
2627
${CMAKE_CURRENT_SOURCE_DIR}/Simulation/Simulation.py
2728
${CMAKE_CURRENT_SOURCE_DIR}/Types/BoundingBox.py
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Sofa
2+
import Sofa.Helper
3+
import unittest
4+
5+
class GetVersionTest(unittest.TestCase):
6+
"""
7+
Contains unit tests for verifying the existence and format of the `GetVersion` method
8+
in the `Sofa.Helper.Utils` module.
9+
10+
Includes tests to assert that the version is correctly formatted and meets the expected
11+
structure.
12+
"""
13+
def test_exist(self):
14+
self.assertTrue(hasattr(Sofa.Helper, "GetVersion"))
15+
16+
def test_version_format(self):
17+
version = Sofa.Helper.GetVersion()
18+
print(f"SOFA Version: {version}")
19+
self.assertTrue(version.startswith('v'))
20+
version = version.replace('v', '')
21+
self.assertTrue(version.count('.') == 1)
22+
major, minor = version.split('.')
23+
self.assertTrue(major.isdigit())
24+
self.assertTrue(minor.isdigit())
25+
self.assertEqual(len(major), 2)
26+
self.assertEqual(len(minor), 2)

0 commit comments

Comments
 (0)