Transfer test_mixed from test_class_sh_disowning to test_wip (preparation for debugging behavior difference).

This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-07-09 13:34:27 -07:00
parent c0f5078263
commit c5278145a0
2 changed files with 38 additions and 18 deletions

View File

@ -1,21 +1,32 @@
#include <pybind11/smart_holder.h>
#include "pybind11_tests.h" #include "pybind11_tests.h"
namespace pybind11_tests { namespace pybind11_tests {
namespace wip { namespace wip {
struct SomeType {}; template <int SerNo> // Using int as a trick to easily generate a series of types.
struct Atype {
int val = 0;
explicit Atype(int val_) : val{val_} {}
int get() const { return val * 10 + SerNo; }
};
int mixed(std::unique_ptr<Atype<1>> at1, std::unique_ptr<Atype<2>> at2) {
return at1->get() * 200 + at2->get() * 20;
}
} // namespace wip } // namespace wip
} // namespace pybind11_tests } // namespace pybind11_tests
TEST_SUBMODULE(wip, m) {
m.attr("__doc__") = "WIP";
using namespace pybind11_tests::wip; using namespace pybind11_tests::wip;
py::class_<SomeType>(m, "SomeType").def(py::init([]() { PYBIND11_SMART_HOLDER_TYPE_CASTERS(Atype<1>)
return std::unique_ptr<SomeType>(new SomeType); PYBIND11_SMART_HOLDER_TYPE_CASTERS(Atype<2>)
}));
m.def("make_some_type", []() { return std::unique_ptr<SomeType>(new SomeType); }); TEST_SUBMODULE(wip, m) {
py::classh<Atype<1>>(m, "Atype1").def(py::init<int>()).def("get", &Atype<1>::get);
py::classh<Atype<2>>(m, "Atype2").def(py::init<int>()).def("get", &Atype<2>::get);
m.def("mixed", mixed);
} }

View File

@ -1,17 +1,26 @@
from __future__ import annotations from __future__ import annotations
import pytest
from pybind11_tests import wip as m from pybind11_tests import wip as m
def test_doc(): def test_mixed():
assert m.__doc__ == "WIP" obj1a = m.Atype1(90)
obj2a = m.Atype2(25)
obj1b = m.Atype1(0)
obj2b = m.Atype2(0)
print("\nLOOOK A BEFORE m.mixed(obj1a, obj2a)", flush=True)
assert m.mixed(obj1a, obj2a) == (90 * 10 + 1) * 200 + (25 * 10 + 2) * 20
print("\nLOOOK A AFTER m.mixed(obj1a, obj2a)", flush=True)
def test_some_type_ctor(): print("\nLOOOK B BEFORE m.mixed(obj1b, obj2a)", flush=True)
obj = m.SomeType() with pytest.raises(ValueError):
assert isinstance(obj, m.SomeType) m.mixed(obj1b, obj2a)
print("\nLOOOK B AFTER m.mixed(obj1b, obj2a)", flush=True)
print("\nLOOOK C BEFORE m.mixed(obj1a, obj2b)", flush=True)
def test_make_some_type(): with pytest.raises(ValueError):
obj = m.make_some_type() m.mixed(obj1a, obj2b)
assert isinstance(obj, m.SomeType) print("\nLOOOK C AFTER m.mixed(obj1a, obj2b)", flush=True)