Skip to content

Commit bfb0c32

Browse files
committed
unittest: Refactor test to test on std::variant and boost::variant
1 parent 30e4a49 commit bfb0c32

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

unittest/CMakeLists.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,28 @@ add_lib_unit_test(std_vector)
4545
add_lib_unit_test(std_array)
4646
add_lib_unit_test(std_pair)
4747
add_lib_unit_test(user_struct)
48-
add_lib_unit_test(boost_variant)
4948

50-
function(config_bind_optional tagname opttype)
51-
set(MODNAME bind_optional_${tagname})
52-
set(OPTIONAL ${opttype})
53-
configure_file(bind_optional.cpp.in ${MODNAME}.cpp)
49+
function(config_test test tagname opttype)
50+
set(MODNAME ${test}_${tagname})
51+
set(TEST_TYPE ${opttype})
52+
configure_file(${test}.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/${MODNAME}.cpp)
5453

55-
set(py_file test_optional_${tagname}.py)
56-
configure_file(python/test_optional.py.in
54+
set(py_file test_${test}_${tagname}.py)
55+
configure_file(python/test_${test}.py.in
5756
${CMAKE_CURRENT_BINARY_DIR}/python/${py_file})
5857
add_lib_unit_test(${MODNAME})
59-
add_python_unit_test("py-optional-${tagname}" "unittest/python/${py_file}"
58+
add_python_unit_test("py-${test}-${tagname}" "unittest/python/${py_file}"
6059
"unittest")
6160
endfunction()
6261

63-
config_bind_optional(boost "boost::optional")
62+
config_test(variant boost "boost::variant")
6463
if(CMAKE_CXX_STANDARD GREATER 14 AND CMAKE_CXX_STANDARD LESS 98)
65-
config_bind_optional(std "std::optional")
64+
config_test(variant std "std::variant")
65+
endif()
66+
67+
config_test(bind_optional boost "boost::optional")
68+
if(CMAKE_CXX_STANDARD GREATER 14 AND CMAKE_CXX_STANDARD LESS 98)
69+
config_test(bind_optional std "std::optional")
6670
endif()
6771

6872
add_lib_unit_test(bind_virtual_factory)
@@ -133,10 +137,6 @@ add_python_unit_test("py-user-struct" "unittest/python/test_user_struct.py"
133137
"python;unittest")
134138
set_tests_properties("py-user-struct" PROPERTIES DEPENDS ${PYWRAP})
135139

136-
add_python_unit_test("py-boost-variant" "unittest/python/test_boost_variant.py"
137-
"python;unittest")
138-
set_tests_properties("py-boost-variant" PROPERTIES DEPENDS ${PYWRAP})
139-
140140
add_python_unit_test("py-bind-virtual" "unittest/python/test_bind_virtual.py"
141141
"python;unittest")
142142
set_tests_properties("py-bind-virtual" PROPERTIES DEPENDS ${PYWRAP})

unittest/bind_optional.cpp.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <optional>
99
#endif
1010

11-
#cmakedefine OPTIONAL @OPTIONAL@
11+
#cmakedefine TEST_TYPE @TEST_TYPE@
12+
#define OPTIONAL TEST_TYPE
1213

1314
typedef eigenpy::detail::nullopt_helper<OPTIONAL> none_helper;
1415
static auto OPT_NONE = none_helper::value();
@@ -74,6 +75,7 @@ BOOST_PYTHON_MODULE(@MODNAME@) {
7475
bp::make_setter(&mystruct::msg));
7576

7677
bp::def("none_if_zero", none_if_zero, bp::args("i"));
77-
bp::def("create_if_true", create_if_true, (bp::arg("flag"), bp::arg("b") = OPT_NONE));
78+
bp::def("create_if_true", create_if_true,
79+
(bp::arg("flag"), bp::arg("b") = OPT_NONE));
7880
bp::def("random_mat_if_true", random_mat_if_true, bp::args("flag"));
7981
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
from boost_variant import V1, V2, VariantHolder, make_variant
1+
import importlib
2+
3+
variant_module = importlib.import_module("@MODNAME@")
4+
V1 = variant_module.V1
5+
V2 = variant_module.V2
6+
VariantHolder = variant_module.VariantHolder
7+
make_variant = variant_module.make_variant
28

39
variant = make_variant()
410
assert isinstance(variant, V1)
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <eigenpy/eigenpy.hpp>
55
#include <eigenpy/variant.hpp>
66

7+
#cmakedefine TEST_TYPE @TEST_TYPE@
8+
#define VARIANT TEST_TYPE
9+
710
namespace bp = boost::python;
811

912
struct V1 {
@@ -12,15 +15,15 @@ struct V1 {
1215
struct V2 {
1316
char v;
1417
};
15-
typedef boost::variant<V1, V2> MyVariant;
18+
typedef VARIANT<V1, V2> MyVariant;
1619

1720
MyVariant make_variant() { return V1(); }
1821

1922
struct VariantHolder {
2023
MyVariant variant;
2124
};
2225

23-
BOOST_PYTHON_MODULE(boost_variant) {
26+
BOOST_PYTHON_MODULE(@MODNAME@) {
2427
using namespace eigenpy;
2528

2629
enableEigenPy();

0 commit comments

Comments
 (0)