Add a test with class attrs returning `PyClass` instances

This commit is contained in:
scalexm 2020-05-07 20:13:10 +02:00
parent 8f22d10a14
commit d3d68eafb4
1 changed files with 39 additions and 1 deletions

View File

@ -3,7 +3,16 @@ use pyo3::prelude::*;
mod common;
#[pyclass]
struct Foo {}
struct Foo {
#[pyo3(get)]
x: i32,
}
#[pyclass]
struct Bar {
#[pyo3(get)]
x: i32,
}
#[pymethods]
impl Foo {
@ -17,6 +26,24 @@ impl Foo {
fn b() -> String {
"bar".to_string()
}
#[classattr]
fn foo() -> Foo {
Foo { x: 1 }
}
#[classattr]
fn bar() -> Bar {
Bar { x: 2 }
}
}
#[pymethods]
impl Bar {
#[classattr]
fn foo() -> Foo {
Foo { x: 3 }
}
}
#[test]
@ -35,3 +62,14 @@ fn class_attributes_are_immutable() {
let foo_obj = py.get_type::<Foo>();
py_expect_exception!(py, foo_obj, "foo_obj.a = 6", TypeError);
}
#[test]
fn recursive_class_attributes() {
let gil = Python::acquire_gil();
let py = gil.python();
let foo_obj = py.get_type::<Foo>();
let bar_obj = py.get_type::<Bar>();
py_assert!(py, foo_obj, "foo_obj.foo.x == 1");
py_assert!(py, foo_obj, "foo_obj.bar.x == 2");
py_assert!(py, bar_obj, "bar_obj.foo.x == 3");
}