Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 1 | #pragma once |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 2 | #include <pybind11/pybind11.h> |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 3 | |
Jason Rhinelander | b57281b | 2017-07-03 19:12:09 -0400 | [diff] [blame] | 4 | #if defined(_MSC_VER) && _MSC_VER < 1910 |
| 5 | // We get some really long type names here which causes MSVC 2015 to emit warnings |
| 6 | # pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated |
| 7 | #endif |
| 8 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 9 | namespace py = pybind11; |
Dean Moldovan | c743e1b | 2016-08-29 03:05:42 +0200 | [diff] [blame] | 10 | using namespace pybind11::literals; |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 11 | |
| 12 | class test_initializer { |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 13 | using Initializer = void (*)(py::module &); |
| 14 | |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 15 | public: |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 16 | test_initializer(Initializer init); |
| 17 | test_initializer(const char *submodule_name, Initializer init); |
| 18 | }; |
| 19 | |
| 20 | #define TEST_SUBMODULE(name, variable) \ |
| 21 | void test_submodule_##name(py::module &); \ |
| 22 | test_initializer name(#name, test_submodule_##name); \ |
| 23 | void test_submodule_##name(py::module &variable) |
| 24 | |
| 25 | |
| 26 | /// Dummy type which is not exported anywhere -- something to trigger a conversion error |
| 27 | struct UnregisteredType { }; |
| 28 | |
| 29 | /// A user-defined type which is exported and can be used by any test |
| 30 | class UserType { |
| 31 | public: |
| 32 | UserType() = default; |
| 33 | UserType(int i) : i(i) { } |
| 34 | |
| 35 | int value() const { return i; } |
| 36 | |
| 37 | private: |
| 38 | int i = -1; |
| 39 | }; |
| 40 | |
| 41 | /// Like UserType, but increments `value` on copy for quick reference vs. copy tests |
| 42 | class IncType : public UserType { |
| 43 | public: |
| 44 | using UserType::UserType; |
| 45 | IncType() = default; |
| 46 | IncType(const IncType &other) : IncType(other.value() + 1) { } |
| 47 | IncType(IncType &&) = delete; |
| 48 | IncType &operator=(const IncType &) = delete; |
| 49 | IncType &operator=(IncType &&) = delete; |
Jason Rhinelander | 52f4be8 | 2016-09-03 14:54:22 -0400 | [diff] [blame] | 50 | }; |
Jason Rhinelander | b57281b | 2017-07-03 19:12:09 -0400 | [diff] [blame] | 51 | |
| 52 | /// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context. |
| 53 | /// Used to test recursive casters (e.g. std::tuple, stl containers). |
| 54 | struct RValueCaster {}; |
| 55 | NAMESPACE_BEGIN(pybind11) |
| 56 | NAMESPACE_BEGIN(detail) |
| 57 | template<> class type_caster<RValueCaster> { |
| 58 | public: |
| 59 | PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster")); |
| 60 | static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); } |
| 61 | static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); } |
| 62 | }; |
| 63 | NAMESPACE_END(detail) |
| 64 | NAMESPACE_END(pybind11) |