pybind11/tests/test_class_sh_factory_constructors.py
Ralf W. Grosse-Kunstleve 9be8d84b1c [smart_holder] Introduce PYBIND11_SMART_HOLDER_DISABLE option. (#5348)
* Step 1: Establish new `PYBIND11_SMART_HOLDER_ENABLED` macro, but only under include/pybind11/

At the stage `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent.

* Systematically replace all `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` with `PYBIND11_SMART_HOLDER_ENABLED` under tests/ and ubench/

As at the previous stage, `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent.

* Introduce `PYBIND11_SMART_HOLDER_DISABLE` option.

* `#ifdef` out entire `wrap()` function to avoid `unused-parameter` warning-as-error under macos-13

```
/Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:23: error: unused parameter 'm' [-Werror,-Wunused-parameter]
void wrap(py::module_ m, const char *py_class_name) {
                      ^
/Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:38: error: unused parameter 'py_class_name' [-Werror,-Wunused-parameter]
void wrap(py::module_ m, const char *py_class_name) {
                                     ^
2 errors generated.
```
2024-09-01 18:29:55 -07:00

57 lines
1.5 KiB
Python

from __future__ import annotations
import pytest
from pybind11_tests import class_sh_factory_constructors as m
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_atyp_factories():
assert m.atyp_valu().get_mtxt() == "Valu"
assert m.atyp_rref().get_mtxt() == "Rref"
# sert m.atyp_cref().get_mtxt() == "Cref"
# sert m.atyp_mref().get_mtxt() == "Mref"
# sert m.atyp_cptr().get_mtxt() == "Cptr"
assert m.atyp_mptr().get_mtxt() == "Mptr"
assert m.atyp_shmp().get_mtxt() == "Shmp"
# sert m.atyp_shcp().get_mtxt() == "Shcp"
assert m.atyp_uqmp().get_mtxt() == "Uqmp"
# sert m.atyp_uqcp().get_mtxt() == "Uqcp"
assert m.atyp_udmp().get_mtxt() == "Udmp"
# sert m.atyp_udcp().get_mtxt() == "Udcp"
@pytest.mark.parametrize(
("init_args", "expected"),
[
((3,), 300),
((5, 7), 570),
((9, 11, 13), 1023),
],
)
def test_with_alias_success(init_args, expected):
assert m.with_alias(*init_args).val == expected
@pytest.mark.parametrize(
("num_init_args", "smart_ptr"),
[
(4, "std::unique_ptr"),
(5, "std::shared_ptr"),
],
)
def test_with_alias_invalid(num_init_args, smart_ptr):
class PyDrvdWithAlias(m.with_alias):
pass
with pytest.raises(TypeError) as excinfo:
PyDrvdWithAlias(*((0,) * num_init_args))
assert (
str(excinfo.value)
== "pybind11::init(): construction failed: returned "
+ smart_ptr
+ " pointee is not an alias instance"
)