pyo3/tests/test_class_new.rs

82 lines
1.6 KiB
Rust
Raw Normal View History

use pyo3::prelude::*;
#[pyclass]
2018-09-02 21:33:45 +00:00
struct EmptyClassWithNew {}
#[pymethods]
impl EmptyClassWithNew {
2019-02-23 17:38:00 +00:00
#[new]
2019-12-14 14:16:39 +00:00
fn new() -> EmptyClassWithNew {
EmptyClassWithNew {}
}
}
#[test]
fn empty_class_with_new() {
let gil = Python::acquire_gil();
let py = gil.python();
let typeobj = py.get_type::<EmptyClassWithNew>();
assert!(typeobj
2019-02-23 17:01:22 +00:00
.call((), None)
.unwrap()
.cast_as::<PyCell<EmptyClassWithNew>>()
.is_ok());
}
#[pyclass]
2019-12-14 14:16:39 +00:00
#[derive(Debug)]
struct NewWithOneArg {
_data: i32,
}
#[pymethods]
impl NewWithOneArg {
#[new]
2019-12-14 14:16:39 +00:00
fn new(arg: i32) -> NewWithOneArg {
NewWithOneArg { _data: arg }
}
}
#[test]
fn new_with_one_arg() {
let gil = Python::acquire_gil();
let py = gil.python();
let typeobj = py.get_type::<NewWithOneArg>();
let wrp = typeobj.call((42,), None).unwrap();
let obj = wrp.cast_as::<PyCell<NewWithOneArg>>().unwrap();
let obj_ref = obj.borrow();
assert_eq!(obj_ref._data, 42);
}
#[pyclass]
struct NewWithTwoArgs {
_data1: i32,
_data2: i32,
}
#[pymethods]
impl NewWithTwoArgs {
#[new]
2019-12-14 14:16:39 +00:00
fn new(arg1: i32, arg2: i32) -> Self {
NewWithTwoArgs {
_data1: arg1,
_data2: arg2,
2019-12-14 14:16:39 +00:00
}
}
}
#[test]
fn new_with_two_args() {
let gil = Python::acquire_gil();
let py = gil.python();
let typeobj = py.get_type::<NewWithTwoArgs>();
let wrp = typeobj
.call((10, 20), None)
.map_err(|e| e.print(py))
.unwrap();
let obj = wrp.cast_as::<PyCell<NewWithTwoArgs>>().unwrap();
let obj_ref = obj.borrow();
assert_eq!(obj_ref._data1, 10);
assert_eq!(obj_ref._data2, 20);
}