diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Helper/Binding_Utils.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Helper/Binding_Utils.cpp index 6996b56c6..a8e68edaf 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Helper/Binding_Utils.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Helper/Binding_Utils.cpp @@ -20,8 +20,11 @@ #include -#include #include +#include + +#include +#include #include @@ -44,6 +47,20 @@ void moduleAddUtils(py::module &m) { Get the directory where is stored the sofa output data such as screenshots. )doc"; utils.def_static("GetSofaDataDirectory", &sofa::helper::Utils::getSofaDataDirectory, GetSofaDataDirectoryDoc); + + utils.def_static("GetVersion", + []() + { + std::stringstream version; + constexpr auto major = SOFA_VERSION / 10000; + constexpr auto minor = SOFA_VERSION / 100 % 100; + version << 'v' + << std::setfill('0') << std::setw(2) << major + << "." + << std::setfill('0') << std::setw(2) << minor; + return version.str(); + }, + "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."); } diff --git a/bindings/Sofa/tests/CMakeLists.txt b/bindings/Sofa/tests/CMakeLists.txt index 038c1cf0f..f4dde4270 100644 --- a/bindings/Sofa/tests/CMakeLists.txt +++ b/bindings/Sofa/tests/CMakeLists.txt @@ -22,6 +22,7 @@ set(PYTHON_FILES ${CMAKE_CURRENT_SOURCE_DIR}/Core/Mass.py ${CMAKE_CURRENT_SOURCE_DIR}/Core/MyRestShapeForceField.py ${CMAKE_CURRENT_SOURCE_DIR}/Helper/Message.py + ${CMAKE_CURRENT_SOURCE_DIR}/Helper/Version.py ${CMAKE_CURRENT_SOURCE_DIR}/Simulation/Node.py ${CMAKE_CURRENT_SOURCE_DIR}/Simulation/Simulation.py ${CMAKE_CURRENT_SOURCE_DIR}/Types/BoundingBox.py diff --git a/bindings/Sofa/tests/Helper/Version.py b/bindings/Sofa/tests/Helper/Version.py new file mode 100644 index 000000000..e6a1a347c --- /dev/null +++ b/bindings/Sofa/tests/Helper/Version.py @@ -0,0 +1,26 @@ +import Sofa +import Sofa.Helper +import unittest + +class GetVersionTest(unittest.TestCase): + """ + Contains unit tests for verifying the existence and format of the `GetVersion` method + in the `Sofa.Helper.Utils` module. + + Includes tests to assert that the version is correctly formatted and meets the expected + structure. + """ + def test_exist(self): + self.assertTrue(hasattr(Sofa.Helper.Utils, "GetVersion")) + + def test_version_format(self): + version = Sofa.Helper.Utils.GetVersion() + print(f"SOFA Version: {version}") + self.assertTrue(version.startswith('v')) + version = version.replace('v', '') + self.assertTrue(version.count('.') == 1) + major, minor = version.split('.') + self.assertTrue(major.isdigit()) + self.assertTrue(minor.isdigit()) + self.assertEqual(len(major), 2) + self.assertEqual(len(minor), 2)