From 7cdf9f1a68ce5318685a4709f100009de3316bc6 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Fri, 12 May 2017 13:11:08 -0400 Subject: [PATCH] Move reference_wrapper test from test_issues to test_python_types test_issues is deprecated, and the following commit adds other, related reference_wrapper tests. --- tests/test_issues.cpp | 13 ------------- tests/test_issues.py | 8 -------- tests/test_python_types.cpp | 19 +++++++++++++++++++ tests/test_python_types.py | 12 ++++++++++++ 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/tests/test_issues.cpp b/tests/test_issues.cpp index 7da37004..cab9ddf6 100644 --- a/tests/test_issues.cpp +++ b/tests/test_issues.cpp @@ -117,19 +117,6 @@ void init_issues(py::module &m) { .def(py::init()) .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; }); - // #171: Can't return reference wrappers (or STL datastructures containing them) - m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper p4) { - Placeholder *p1 = new Placeholder{1}; - Placeholder *p2 = new Placeholder{2}; - Placeholder *p3 = new Placeholder{3}; - std::vector> v; - v.push_back(std::ref(*p1)); - v.push_back(std::ref(*p2)); - v.push_back(std::ref(*p3)); - v.push_back(p4); - return v; - }); - // #181: iterator passthrough did not compile m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator { return py::make_iterator(std::begin(s), std::end(s)); diff --git a/tests/test_issues.py b/tests/test_issues.py index e60b5ca9..08678cfc 100644 --- a/tests/test_issues.py +++ b/tests/test_issues.py @@ -32,14 +32,6 @@ def test_dispatch_issue(msg): assert dispatch_issue_go(b) == "Yay.." -def test_reference_wrapper(): - """#171: Can't return reference wrappers (or STL data structures containing them)""" - from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper - - assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \ - "[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]" - - def test_iterator_passthrough(): """#181: iterator passthrough did not compile""" from pybind11_tests.issues import iterator_passthrough diff --git a/tests/test_python_types.cpp b/tests/test_python_types.cpp index f3375c67..c9f35f7b 100644 --- a/tests/test_python_types.cpp +++ b/tests/test_python_types.cpp @@ -560,6 +560,25 @@ test_initializer python_types([](py::module &m) { m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile m.def("cast_nullptr_t", []() { return std::nullptr_t{}; }); + + struct IntWrapper { int i; IntWrapper(int i) : i(i) { } }; + py::class_(m, "IntWrapper") + .def(py::init()) + .def("__repr__", [](const IntWrapper &p) { return "IntWrapper[" + std::to_string(p.i) + "]"; }); + + // #171: Can't return reference wrappers (or STL datastructures containing them) + m.def("return_vec_of_reference_wrapper", [](std::reference_wrapper p4) { + IntWrapper *p1 = new IntWrapper{1}; + IntWrapper *p2 = new IntWrapper{2}; + IntWrapper *p3 = new IntWrapper{3}; + std::vector> v; + v.push_back(std::ref(*p1)); + v.push_back(std::ref(*p2)); + v.push_back(std::ref(*p3)); + v.push_back(p4); + return v; + }); + }); #if defined(_MSC_VER) diff --git a/tests/test_python_types.py b/tests/test_python_types.py index f7c3d555..0e563ee1 100644 --- a/tests/test_python_types.py +++ b/tests/test_python_types.py @@ -606,3 +606,15 @@ def test_void_caster(): assert m.load_nullptr_t(None) is None assert m.cast_nullptr_t() is None + + +def test_reference_wrapper(): + """std::reference_wrapper tests. + + #171: Can't return reference wrappers (or STL data structures containing them) + """ + from pybind11_tests import IntWrapper, return_vec_of_reference_wrapper + + # 171: + assert str(return_vec_of_reference_wrapper(IntWrapper(4))) == \ + "[IntWrapper[1], IntWrapper[2], IntWrapper[3], IntWrapper[4]]"