From 84a763da14a5368fe09e23dcba487e4a6f7dc58e Mon Sep 17 00:00:00 2001 From: Rico Hageman Date: Thu, 24 Feb 2022 00:20:00 +0100 Subject: [PATCH] Add test to ensure support for from_py_with for enums --- tests/test_frompyobject.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_frompyobject.rs b/tests/test_frompyobject.rs index 57ecf1d6..152466cc 100644 --- a/tests/test_frompyobject.rs +++ b/tests/test_frompyobject.rs @@ -501,3 +501,23 @@ fn test_from_py_with_tuple_struct() { assert_eq!(zap.1, 3usize); }); } + +#[derive(Debug, FromPyObject, PartialEq)] +pub enum ZapEnum { + Zip(#[pyo3(from_py_with = "PyAny::len")] usize), + Zap(String, #[pyo3(from_py_with = "PyAny::len")] usize), +} + +#[test] +fn test_from_py_with_enum() { + Python::with_gil(|py| { + let py_zap = py + .eval(r#"("whatever", [1, 2, 3])"#, None, None) + .expect("failed to create dict"); + + let zap = ZapEnum::extract(py_zap).unwrap(); + let expected_zap = ZapEnum::Zap(String::from("whatever"), 3usize); + + assert_eq!(zap, expected_zap); + }); +}