pybind11_tests.h 2.62 KB
Newer Older
1
#pragma once
2

3
#include <pybind11/eval.h>
4
#include <pybind11/pybind11.h>
Wenzel Jakob's avatar
Wenzel Jakob committed
5

6
namespace py = pybind11;
7
using namespace pybind11::literals;
8
9

class test_initializer {
10
    using Initializer = void (*)(py::module_ &);
11
12

public:
13
    explicit test_initializer(Initializer init);
14
15
16
    test_initializer(const char *submodule_name, Initializer init);
};

17
18
19
20
#define TEST_SUBMODULE(name, variable)                                                            \
    void test_submodule_##name(py::module_ &);                                                    \
    test_initializer name(#name, test_submodule_##name);                                          \
    void test_submodule_##name(py::module_ &(variable))
21
22

/// Dummy type which is not exported anywhere -- something to trigger a conversion error
23
struct UnregisteredType {};
24
25
26
27
28

/// A user-defined type which is exported and can be used by any test
class UserType {
public:
    UserType() = default;
29
    explicit UserType(int i) : i(i) {}
30
31

    int value() const { return i; }
32
    void set(int set) { i = set; }
33
34
35
36
37
38
39

private:
    int i = -1;
};

/// Like UserType, but increments `value` on copy for quick reference vs. copy tests
class IncType : public UserType {
40
public:
41
42
    using UserType::UserType;
    IncType() = default;
43
    IncType(const IncType &other) : IncType(other.value() + 1) {}
44
45
46
    IncType(IncType &&) = delete;
    IncType &operator=(const IncType &) = delete;
    IncType &operator=(IncType &&) = delete;
47
};
48

49
50
51
52
53
54
/// A simple union for basic testing
union IntFloat {
    int i;
    float f;
};

55
56
/// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast
/// context. Used to test recursive casters (e.g. std::tuple, stl containers).
57
struct RValueCaster {};
58
59
PYBIND11_NAMESPACE_BEGIN(pybind11)
PYBIND11_NAMESPACE_BEGIN(detail)
60
61
template <>
class type_caster<RValueCaster> {
62
public:
63
    PYBIND11_TYPE_CASTER(RValueCaster, const_name("RValueCaster"));
64
65
66
67
68
69
    static handle cast(RValueCaster &&, return_value_policy, handle) {
        return py::str("rvalue").release();
    }
    static handle cast(const RValueCaster &, return_value_policy, handle) {
        return py::str("lvalue").release();
    }
70
};
71
72
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(pybind11)
73
74
75
76
77
78
79
80
81
82

template <typename F>
void ignoreOldStyleInitWarnings(F &&body) {
    py::exec(R"(
    message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated"

    import warnings
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", message=message, category=FutureWarning)
        body()
83
84
    )",
             py::dict(py::arg("body") = py::cpp_function(body)));
85
}