blob: 49f31ba1f0d7cfa6890b21dfcd9aca0549b80ccc [file] [log] [blame]
Dean Moldovana9a37b42016-08-13 00:57:24 +02001/*
2 tests/test_enums.cpp -- enumerations
3
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10#include "pybind11_tests.h"
11
Jason Rhinelander391c7542017-07-25 16:47:36 -040012TEST_SUBMODULE(enums, m) {
13 // test_unscoped_enum
14 enum UnscopedEnum {
15 EOne = 1,
16 ETwo
Dean Moldovana9a37b42016-08-13 00:57:24 +020017 };
Wenzel Jakob405f6d12016-11-17 23:24:47 +010018 py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic())
Dean Moldovana9a37b42016-08-13 00:57:24 +020019 .value("EOne", EOne)
20 .value("ETwo", ETwo)
21 .export_values();
22
Jason Rhinelander391c7542017-07-25 16:47:36 -040023 // test_scoped_enum
24 enum class ScopedEnum {
25 Two = 2,
26 Three
27 };
Wenzel Jakob405f6d12016-11-17 23:24:47 +010028 py::enum_<ScopedEnum>(m, "ScopedEnum", py::arithmetic())
Dean Moldovana9a37b42016-08-13 00:57:24 +020029 .value("Two", ScopedEnum::Two)
Wenzel Jakob405f6d12016-11-17 23:24:47 +010030 .value("Three", ScopedEnum::Three);
Dean Moldovana9a37b42016-08-13 00:57:24 +020031
Jason Rhinelander391c7542017-07-25 16:47:36 -040032 m.def("test_scoped_enum", [](ScopedEnum z) {
33 return "ScopedEnum::" + std::string(z == ScopedEnum::Two ? "Two" : "Three");
34 });
35
36 // test_binary_operators
37 enum Flags {
38 Read = 4,
39 Write = 2,
40 Execute = 1
41 };
Wenzel Jakob405f6d12016-11-17 23:24:47 +010042 py::enum_<Flags>(m, "Flags", py::arithmetic())
Pim Schellart90d27802016-11-16 11:28:11 -050043 .value("Read", Flags::Read)
44 .value("Write", Flags::Write)
45 .value("Execute", Flags::Execute)
46 .export_values();
Pim Schellart90d27802016-11-16 11:28:11 -050047
Jason Rhinelander391c7542017-07-25 16:47:36 -040048 // test_implicit_conversion
49 class ClassWithUnscopedEnum {
50 public:
51 enum EMode {
52 EFirstMode = 1,
53 ESecondMode
54 };
55
56 static EMode test_function(EMode mode) {
57 return mode;
58 }
59 };
Dean Moldovana9a37b42016-08-13 00:57:24 +020060 py::class_<ClassWithUnscopedEnum> exenum_class(m, "ClassWithUnscopedEnum");
61 exenum_class.def_static("test_function", &ClassWithUnscopedEnum::test_function);
62 py::enum_<ClassWithUnscopedEnum::EMode>(exenum_class, "EMode")
63 .value("EFirstMode", ClassWithUnscopedEnum::EFirstMode)
64 .value("ESecondMode", ClassWithUnscopedEnum::ESecondMode)
65 .export_values();
Wenzel Jakobe6fd2cd2017-04-28 14:46:52 +020066
Jason Rhinelander391c7542017-07-25 16:47:36 -040067 // test_enum_to_int
Wenzel Jakobe6fd2cd2017-04-28 14:46:52 +020068 m.def("test_enum_to_int", [](int) { });
69 m.def("test_enum_to_uint", [](uint32_t) { });
70 m.def("test_enum_to_long_long", [](long long) { });
Jason Rhinelander391c7542017-07-25 16:47:36 -040071}