blob: 90963a5dea7a2c56fa955ceaade53282708e2d85 [file] [log] [blame]
Jason Rhinelander52f4be82016-09-03 14:54:22 -04001#pragma once
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02002#include <pybind11/pybind11.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02003
Jason Rhinelanderb57281b2017-07-03 19:12:09 -04004#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 Jakob8f4eb002015-10-15 18:13:33 +02009namespace py = pybind11;
Dean Moldovanc743e1b2016-08-29 03:05:42 +020010using namespace pybind11::literals;
Jason Rhinelander52f4be82016-09-03 14:54:22 -040011
12class test_initializer {
Dean Moldovan83e328f2017-06-09 00:44:49 +020013 using Initializer = void (*)(py::module &);
14
Jason Rhinelander52f4be82016-09-03 14:54:22 -040015public:
Dean Moldovan83e328f2017-06-09 00:44:49 +020016 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
27struct UnregisteredType { };
28
29/// A user-defined type which is exported and can be used by any test
30class UserType {
31public:
32 UserType() = default;
33 UserType(int i) : i(i) { }
34
35 int value() const { return i; }
Jason Rhinelander391c7542017-07-25 16:47:36 -040036 void set(int set) { i = set; }
Dean Moldovan83e328f2017-06-09 00:44:49 +020037
38private:
39 int i = -1;
40};
41
42/// Like UserType, but increments `value` on copy for quick reference vs. copy tests
43class IncType : public UserType {
44public:
45 using UserType::UserType;
46 IncType() = default;
47 IncType(const IncType &other) : IncType(other.value() + 1) { }
48 IncType(IncType &&) = delete;
49 IncType &operator=(const IncType &) = delete;
50 IncType &operator=(IncType &&) = delete;
Jason Rhinelander52f4be82016-09-03 14:54:22 -040051};
Jason Rhinelanderb57281b2017-07-03 19:12:09 -040052
53/// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context.
54/// Used to test recursive casters (e.g. std::tuple, stl containers).
55struct RValueCaster {};
56NAMESPACE_BEGIN(pybind11)
57NAMESPACE_BEGIN(detail)
58template<> class type_caster<RValueCaster> {
59public:
60 PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster"));
61 static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); }
62 static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); }
63};
64NAMESPACE_END(detail)
65NAMESPACE_END(pybind11)