From dfc484c43b6c6c595873db83cbf2f7b3516ef40b Mon Sep 17 00:00:00 2001 From: R2D2 Date: Tue, 1 Jun 2021 00:23:54 +0200 Subject: [PATCH] Started adding more unit tests --- tests/test_frompyobject.rs | 61 +++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/tests/test_frompyobject.rs b/tests/test_frompyobject.rs index 0b41a785..6e1e2c95 100644 --- a/tests/test_frompyobject.rs +++ b/tests/test_frompyobject.rs @@ -94,6 +94,7 @@ pub struct E { } #[pyclass] +#[derive(Clone)] pub struct PyE { #[pyo3(get)] test: String, @@ -138,7 +139,7 @@ fn test_named_field_with_ext_fn() { assert_eq!(c.test, "foo"); } -#[derive(FromPyObject)] +#[derive(Debug, FromPyObject)] pub struct Tuple(String, usize); #[test] @@ -170,6 +171,64 @@ fn test_transparent_tuple_struct() { assert_eq!(tup.0, "test"); } +#[pyclass] +struct PyBaz { + #[pyo3(get)] + tup: (String, String), + #[pyo3(get)] + e: PyE, +} + +#[derive(Debug, FromPyObject)] +struct Baz { + e: E, + tup: Tuple, +} + +#[test] +fn test_struct_nested_type_errors() { + let gil = Python::acquire_gil(); + let py = gil.python(); + + let pybaz = PyBaz { + tup: ("test".into(), "test".into()), + e: PyE { + test: "foo".into(), + test2: 0, + }, + } + .into_py(py); + + let baz: PyResult> = FromPyObject::extract(pybaz.as_ref(py)); + assert!(baz.is_err()); + assert_eq!( + "TypeError: failed to extract field Baz.tup\n\nCaused by:\n TypeError: failed to extract field Tuple.1\n\nCaused by:\n TypeError: 'str' object cannot be interpreted as an integer\n\n", + baz.unwrap_err().to_string() + ); +} + +#[test] +fn test_struct_nested_type_errors_with_generics() { + let gil = Python::acquire_gil(); + let py = gil.python(); + + let pybaz = PyBaz { + tup: ("test".into(), "test".into()), + e: PyE { + test: "foo".into(), + test2: 0, + }, + } + .into_py(py); + + let baz: PyResult> = FromPyObject::extract(pybaz.as_ref(py)); + assert!(baz.is_err()); + assert_eq!( + "TypeError: failed to extract field Baz.e\n\nCaused by:\n TypeError: failed to extract field E.test\n\nCaused by:\n TypeError: \'str\' object cannot be interpreted as an integer\n\n", + baz.unwrap_err().to_string() + ); +} + #[derive(Debug, FromPyObject)] pub enum Foo<'a> { TupleVar(usize, String),