pyo3/tests/test_enum.rs
b05902132 b7419b5278 Refactor #[pyclass] and now it supports enum.
There's no functionality since it does not generate __richcmp__.

Also it only works on enums with only variants, and does not support
C-like enums.
2021-11-21 20:05:53 +08:00

54 lines
1.2 KiB
Rust

use pyo3::prelude::*;
use pyo3::{py_run, wrap_pyfunction};
mod common;
#[pyclass]
#[derive(Debug, PartialEq, Clone)]
pub enum MyEnum {
Variant,
OtherVariant,
}
#[test]
fn test_enum_class_attr() {
let gil = Python::acquire_gil();
let py = gil.python();
let my_enum = py.get_type::<MyEnum>();
py_assert!(py, my_enum, "getattr(my_enum, 'Variant', None) is not None");
py_assert!(py, my_enum, "getattr(my_enum, 'foobar', None) is None");
py_run!(py, my_enum, "my_enum.Variant = None");
}
#[pyfunction]
fn return_enum() -> MyEnum {
MyEnum::Variant
}
#[test]
#[ignore] // need to implement __eq__
fn test_return_enum() {
let gil = Python::acquire_gil();
let py = gil.python();
let f = wrap_pyfunction!(return_enum)(py).unwrap();
let mynum = py.get_type::<MyEnum>();
py_run!(py, f mynum, "assert f() == mynum.Variant")
}
#[pyfunction]
fn enum_arg(e: MyEnum) {
assert_eq!(MyEnum::OtherVariant, e)
}
#[test]
#[ignore] // need to implement __eq__
fn test_enum_arg() {
let gil = Python::acquire_gil();
let py = gil.python();
let f = wrap_pyfunction!(enum_arg)(py).unwrap();
let mynum = py.get_type::<MyEnum>();
py_run!(py, f mynum, "f(mynum.Variant)")
}