Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mejrs committed May 1, 2022
1 parent 7af2326 commit e4645a5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/conversions/array.rs
Expand Up @@ -295,7 +295,7 @@ fn invalid_sequence_length(expected: usize, actual: usize) -> PyErr {

#[cfg(test)]
mod tests {
use crate::{types::PyList, PyResult, Python};
use crate::{types::PyList, IntoPy, PyCell, PyResult, Python, ToPyObject};

#[test]
fn test_extract_small_bytearray_to_array() {
Expand All @@ -310,7 +310,6 @@ mod tests {
}
#[test]
fn test_topyobject_array_conversion() {
use crate::ToPyObject;
Python::with_gil(|py| {
let array: [f32; 4] = [0.0, -16.0, 16.0, 42.0];
let pyobject = array.to_object(py);
Expand All @@ -335,4 +334,31 @@ mod tests {
);
})
}

#[test]
fn test_intopy_array_conversion() {
Python::with_gil(|py| {
let array: [f32; 4] = [0.0, -16.0, 16.0, 42.0];
let pyobject = array.into_py(py);
let pylist: &PyList = pyobject.extract(py).unwrap();
assert_eq!(pylist[0].extract::<f32>().unwrap(), 0.0);
assert_eq!(pylist[1].extract::<f32>().unwrap(), -16.0);
assert_eq!(pylist[2].extract::<f32>().unwrap(), 16.0);
assert_eq!(pylist[3].extract::<f32>().unwrap(), 42.0);
});
}

#[cfg(feature = "macros")]
#[test]
fn test_pyclass_intopy_array_conversion() {
#[crate::pyclass(crate = "crate")]
struct Foo;

Python::with_gil(|py| {
let array: [Foo; 8] = [Foo, Foo, Foo, Foo, Foo, Foo, Foo, Foo];
let pyobject = array.into_py(py);
let list: &PyList = pyobject.cast_as(py).unwrap();
let _cell: &PyCell<Foo> = list.get_item(4).unwrap().extract().unwrap();
});
}
}
1 change: 1 addition & 0 deletions tests/test_compile_error.rs
Expand Up @@ -96,6 +96,7 @@ fn _test_compile_errors() {
fn tests_rust_1_60(t: &trybuild::TestCases) {
t.compile_fail("tests/ui/invalid_immutable_pyclass_borrow.rs");
t.compile_fail("tests/ui/invalid_pymethod_receiver.rs");
t.compile_fail("tests/ui/missing_intopy.rs");
}

#[rustversion::before(1.60)]
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/missing_intopy.rs
@@ -0,0 +1,8 @@
struct Blah;

#[pyo3::pyfunction]
fn blah() -> Blah{
Blah
}

fn main(){}
29 changes: 29 additions & 0 deletions tests/ui/missing_intopy.stderr
@@ -0,0 +1,29 @@
error[E0599]: the method `into_result` exists for struct `Blah`, but its trait bounds were not satisfied
--> tests/ui/missing_intopy.rs:3:1
|
1 | struct Blah;
| ------------
| |
| method `into_result` not found for this
| doesn't satisfy `Blah: IntoPy<Py<PyAny>>`
| doesn't satisfy `Blah: IntoResult<Result<Blah, PyErr>>`
2 |
3 | #[pyo3::pyfunction]
| ^^^^^^^^^^^^^^^^^^^ method cannot be called on `Blah` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`Blah: IntoPy<Py<PyAny>>`
which is required by `Blah: IntoResult<Result<Blah, PyErr>>`
`&Blah: IntoPy<Py<PyAny>>`
which is required by `&Blah: IntoResult<Result<&Blah, PyErr>>`
`&mut Blah: IntoPy<Py<PyAny>>`
which is required by `&mut Blah: IntoResult<Result<&mut Blah, PyErr>>`
note: the following trait must be implemented
--> src/conversion.rs
|
| / pub trait IntoPy<T>: Sized {
| | /// Performs the conversion.
| | fn into_py(self, py: Python<'_>) -> T;
| | }
| |_^
= note: this error originates in the attribute macro `pyo3::pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit e4645a5

Please sign in to comment.