From 924cb28fbdb79729090bd9f3f8f47a425fc3f0ce Mon Sep 17 00:00:00 2001 From: mejrs Date: Sun, 1 May 2022 16:39:45 +0200 Subject: [PATCH] Add tests --- src/conversions/array.rs | 30 ++++++++++++++++++++++++++++-- tests/test_compile_error.rs | 1 + tests/ui/missing_intopy.rs | 8 ++++++++ tests/ui/missing_intopy.stderr | 29 +++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/ui/missing_intopy.rs create mode 100644 tests/ui/missing_intopy.stderr diff --git a/src/conversions/array.rs b/src/conversions/array.rs index cf385f365d7..c6e9e70c15c 100644 --- a/src/conversions/array.rs +++ b/src/conversions/array.rs @@ -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, PyResult, Python, ToPyObject}; #[test] fn test_extract_small_bytearray_to_array() { @@ -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); @@ -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::().unwrap(), 0.0); + assert_eq!(pylist[1].extract::().unwrap(), -16.0); + assert_eq!(pylist[2].extract::().unwrap(), 16.0); + assert_eq!(pylist[3].extract::().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: &crate::PyCell = list.get_item(4).unwrap().extract().unwrap(); + }); + } } diff --git a/tests/test_compile_error.rs b/tests/test_compile_error.rs index 713c50925c8..989928c2775 100644 --- a/tests/test_compile_error.rs +++ b/tests/test_compile_error.rs @@ -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)] diff --git a/tests/ui/missing_intopy.rs b/tests/ui/missing_intopy.rs new file mode 100644 index 00000000000..7a465b14221 --- /dev/null +++ b/tests/ui/missing_intopy.rs @@ -0,0 +1,8 @@ +struct Blah; + +#[pyo3::pyfunction] +fn blah() -> Blah{ + Blah +} + +fn main(){} \ No newline at end of file diff --git a/tests/ui/missing_intopy.stderr b/tests/ui/missing_intopy.stderr new file mode 100644 index 00000000000..b4cf47e1b29 --- /dev/null +++ b/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>` + | doesn't satisfy `Blah: IntoResult>` +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>` + which is required by `Blah: IntoResult>` + `&Blah: IntoPy>` + which is required by `&Blah: IntoResult>` + `&mut Blah: IntoPy>` + which is required by `&mut Blah: IntoResult>` +note: the following trait must be implemented + --> src/conversion.rs + | + | / pub trait IntoPy: 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)