Skip to content

Commit

Permalink
Add test to ensure support for from_py_with for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ricohageman committed Feb 23, 2022
1 parent 10804b0 commit 84a763d
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/test_frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

0 comments on commit 84a763d

Please sign in to comment.