Add test to ensure support for from_py_with for enums

This commit is contained in:
Rico Hageman 2022-02-24 00:20:00 +01:00 committed by Rico
parent 10804b0d65
commit 84a763da14
1 changed files with 20 additions and 0 deletions

View File

@ -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);
});
}