Skip to content

Commit 916d538

Browse files
committed
Adding test_minimal_real_caster. Keeping only one mock_caster.
1 parent 3131579 commit 916d538

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

tests/test_make_caster_adl.cpp

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,56 @@
11
#include "pybind11_tests.h"
22

3-
namespace adl_one {
4-
struct type_one {};
5-
struct type_caster_one {
3+
// adl = Argument Dependent Lookup
4+
5+
namespace adl_mock {
6+
struct type_mock {};
7+
struct mock_caster {
68
static int num() { return 101; }
79
};
8-
type_caster_one pybind11_select_caster(type_one *);
9-
} // namespace adl_one
10+
mock_caster pybind11_select_caster(type_mock *);
11+
} // namespace adl_mock
12+
13+
namespace adl_mrc { // minimal real caster
1014

11-
namespace adl_two {
12-
struct type_two {};
13-
struct type_caster_two {
14-
static int num() { return 202; }
15+
struct type_mrc {
16+
int value = -9999;
1517
};
16-
type_caster_two pybind11_select_caster(type_two *);
17-
} // namespace adl_two
18+
19+
struct minimal_real_caster {
20+
static constexpr auto name = py::detail::const_name<type_mrc>();
21+
22+
static py::handle
23+
cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {
24+
return py::int_(src.value + 1000).release();
25+
}
26+
27+
// Maximizing simplicity. This will go terribly wrong for other arg types.
28+
template <typename>
29+
using cast_op_type = const type_mrc &;
30+
31+
operator type_mrc const &() {
32+
static type_mrc obj;
33+
obj.value = 303;
34+
return obj;
35+
}
36+
37+
bool load(py::handle src, bool /*convert*/) {
38+
// Only accepts str, but the value is ignored.
39+
return py::isinstance<py::str>(src);
40+
}
41+
};
42+
43+
minimal_real_caster pybind11_select_caster(type_mrc *);
44+
45+
} // namespace adl_mrc
1846

1947
TEST_SUBMODULE(make_caster_adl, m) {
20-
m.def("num_one", []() { return py::detail::make_caster<adl_one::type_one>::num(); });
21-
m.def("num_two", []() { return py::detail::make_caster<adl_two::type_two>::num(); });
48+
m.def("num_mock", []() { return py::detail::make_caster<adl_mock::type_mock>::num(); });
49+
50+
m.def("obj_mrc_return", []() {
51+
adl_mrc::type_mrc obj;
52+
obj.value = 404;
53+
return obj;
54+
});
55+
m.def("obj_mrc_arg", [](adl_mrc::type_mrc const &obj) { return obj.value + 2000; });
2256
}

tests/test_make_caster_adl.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
# -*- coding: utf-8 -*-
2+
import pytest
23

34
from pybind11_tests import make_caster_adl as m
45

56

6-
def test_basic():
7-
assert m.num_one() == 101
8-
assert m.num_two() == 202
7+
def test_mock_caster():
8+
assert m.num_mock() == 101
9+
10+
11+
def test_minimal_real_caster():
12+
assert m.obj_mrc_return() == 1404
13+
assert m.obj_mrc_arg("ignored") == 2303
14+
with pytest.raises(TypeError) as excinfo:
15+
m.obj_mrc_arg(None)
16+
assert "(arg0: adl_mrc::type_mrc) -> int" in str(excinfo.value)

0 commit comments

Comments
 (0)