81 lines
1.7 KiB
Rust
81 lines
1.7 KiB
Rust
#![feature(proc_macro, specialization)]
|
|
|
|
extern crate pyo3;
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
use pyo3::py::class;
|
|
|
|
#[macro_use]
|
|
mod common;
|
|
|
|
#[class]
|
|
struct EmptyClass {}
|
|
|
|
#[test]
|
|
fn empty_class() {
|
|
let gil = Python::acquire_gil();
|
|
let py = gil.python();
|
|
let typeobj = py.get_type::<EmptyClass>();
|
|
// By default, don't allow creating instances from python.
|
|
assert!(typeobj.call(NoArgs, NoArgs).is_err());
|
|
|
|
py_assert!(py, typeobj, "typeobj.__name__ == 'EmptyClass'");
|
|
}
|
|
|
|
/// Line1
|
|
///Line2
|
|
/// Line3
|
|
// this is not doc string
|
|
#[class]
|
|
struct ClassWithDocs {}
|
|
|
|
#[test]
|
|
fn class_with_docstr() {
|
|
{
|
|
let gil = Python::acquire_gil();
|
|
let py = gil.python();
|
|
let typeobj = py.get_type::<ClassWithDocs>();
|
|
py_run!(
|
|
py,
|
|
typeobj,
|
|
"assert typeobj.__doc__ == 'Line1\\nLine2\\n Line3'"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[class(name=CustomName)]
|
|
struct EmptyClass2 {}
|
|
|
|
#[test]
|
|
fn custom_class_name() {
|
|
let gil = Python::acquire_gil();
|
|
let py = gil.python();
|
|
let typeobj = py.get_type::<EmptyClass2>();
|
|
py_assert!(py, typeobj, "typeobj.__name__ == 'CustomName'");
|
|
}
|
|
|
|
#[class]
|
|
struct EmptyClassInModule {}
|
|
|
|
#[test]
|
|
fn empty_class_in_module() {
|
|
let gil = Python::acquire_gil();
|
|
let py = gil.python();
|
|
let module = PyModule::new(py, "test_module.nested").unwrap();
|
|
module.add_class::<EmptyClassInModule>().unwrap();
|
|
|
|
let ty = module.getattr("EmptyClassInModule").unwrap();
|
|
assert_eq!(
|
|
ty.getattr("__name__").unwrap().extract::<String>().unwrap(),
|
|
"EmptyClassInModule"
|
|
);
|
|
assert_eq!(
|
|
ty.getattr("__module__")
|
|
.unwrap()
|
|
.extract::<String>()
|
|
.unwrap(),
|
|
"test_module.nested"
|
|
);
|
|
}
|