Skip to content

Commit 7fb1eb2

Browse files
committed
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.
1 parent fc75967 commit 7fb1eb2

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020

2121
#include <pybind11/pybind11.h>
2222

23-
#include <SofaPython3/Sofa/Helper/Binding_Utils.h>
2423
#include <SofaPython3/PythonFactory.h>
24+
#include <SofaPython3/Sofa/Helper/Binding_Utils.h>
25+
26+
#include <iomanip>
27+
#include <sofa/version.h>
2528

2629
#include <sofa/helper/Utils.h>
2730

@@ -44,6 +47,20 @@ void moduleAddUtils(py::module &m) {
4447
Get the directory where is stored the sofa output data such as screenshots.
4548
)doc";
4649
utils.def_static("GetSofaDataDirectory", &sofa::helper::Utils::getSofaDataDirectory, GetSofaDataDirectoryDoc);
50+
51+
utils.def_static("GetVersion",
52+
[]()
53+
{
54+
std::stringstream version;
55+
constexpr auto major = SOFA_VERSION / 10000;
56+
constexpr auto minor = SOFA_VERSION / 100 % 100;
57+
version << 'v'
58+
<< std::setfill('0') << std::setw(2) << major
59+
<< "."
60+
<< std::setfill('0') << std::setw(2) << minor;
61+
return version.str();
62+
},
63+
"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.");
4764
}
4865

4966

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.Utils, "GetVersion"))
15+
16+
def test_version_format(self):
17+
version = Sofa.Helper.Utils.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)